summaryrefslogtreecommitdiff
path: root/libpam/pam_modutil_getlogin.c
blob: 2e7a0116778e6670e731028866553f5c7377fe06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
 * $Id$
 *
 * A central point for invoking getlogin(). Hopefully, this is a
 * little harder to spoof than all the other versions that are out
 * there.
 */

#include "pam_modutil_private.h"

#include <stdlib.h>
#include <unistd.h>

#define _PAMMODUTIL_GETLOGIN "_pammodutil_getlogin"

const char *
pam_modutil_getlogin(pam_handle_t *pamh)
{
    int status;
    const void *logname;
    char *curr_user;

    status = pam_get_data(pamh, _PAMMODUTIL_GETLOGIN, &logname);
    if (status == PAM_SUCCESS) {
	return logname;
    }

    logname = getlogin();
    if (logname == NULL) {
      return NULL;
    }

    curr_user = strdup(logname);
    if (curr_user == NULL) {
      return NULL;
    }

    status = pam_set_data(pamh, _PAMMODUTIL_GETLOGIN, curr_user,
			  pam_modutil_cleanup);
    if (status != PAM_SUCCESS) {
      free(curr_user);
      return NULL;
    }

    return curr_user;
}