summaryrefslogtreecommitdiff
path: root/libpam/pam_modutil_getlogin.c
blob: 633dd6769cfe330c0c4ef5010b4280dbb1796993 (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
47
48
49
50
/*
 * $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;
    size_t curr_user_len;

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

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

    curr_user_len = strlen(logname)+1;
    curr_user = calloc(curr_user_len, 1);
    if (curr_user == NULL) {
      return NULL;
    }

    memcpy(curr_user, logname, curr_user_len);

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

    return curr_user;
}