/
pam.c
168 lines (140 loc) · 4.57 KB
/
pam.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <pwd.h>
#include <paths.h>
#include "pam.h"
#define SERVICE_NAME "display_manager"
#define err(name) \
do { \
fprintf(stderr, "%s: %s\n", name, \
pam_strerror(pam_handle, result)); \
end(result); \
return false; \
} while (1); \
static void init_env(struct passwd *pw);
static void set_env(char *name, char *value);
static int end(int last_result);
static int conv(int num_msg, const struct pam_message **msg,
struct pam_response **resp, void *appdata_ptr);
static pam_handle_t *pam_handle;
bool login(const char *username, const char *password, pid_t *child_pid) {
const char *data[2] = {username, password};
struct pam_conv pam_conv = {
conv, data
};
int result = pam_start(SERVICE_NAME, NULL, &pam_conv, &pam_handle);
if (result != PAM_SUCCESS) {
err("pam_start");
}
result = pam_set_item(pam_handle, PAM_USER, username);
if (result != PAM_SUCCESS) {
err("pam_set_item");
}
result = pam_authenticate(pam_handle, 0);
if (result != PAM_SUCCESS) {
err("pam_authenticate");
}
result = pam_acct_mgmt(pam_handle, 0);
if (result != PAM_SUCCESS) {
err("pam_acct_mgmt");
}
result = pam_setcred(pam_handle, PAM_ESTABLISH_CRED);
if (result != PAM_SUCCESS) {
err("pam_setcred");
}
result = pam_open_session(pam_handle, 0);
if (result != PAM_SUCCESS) {
pam_setcred(pam_handle, PAM_DELETE_CRED);
err("pam_open_session");
}
struct passwd *pw = getpwnam(username);
init_env(pw);
*child_pid = fork();
if (*child_pid == 0) {
chdir(pw->pw_dir);
// We don't use ~/.xinitrc because we should already be in the users home directory
char *cmd = "exec /bin/bash --login .xinitrc";
execl(pw->pw_shell, pw->pw_shell, "-c", cmd, NULL);
printf("Failed to start window manager");
exit(1);
}
return true;
}
bool logout(void) {
int result = pam_close_session(pam_handle, 0);
if (result != PAM_SUCCESS) {
pam_setcred(pam_handle, PAM_DELETE_CRED);
err("pam_close_session");
}
result = pam_setcred(pam_handle, PAM_DELETE_CRED);
if (result != PAM_SUCCESS) {
err("pam_setcred");
}
end(result);
return true;
}
static void init_env(struct passwd *pw) {
set_env("HOME", pw->pw_dir);
set_env("PWD", pw->pw_dir);
set_env("SHELL", pw->pw_shell);
set_env("USER", pw->pw_name);
set_env("LOGNAME", pw->pw_name);
set_env("PATH", "/usr/local/sbin:/usr/local/bin:/usr/bin");
//set_env("DISPLAY", DISPLAY);
set_env("MAIL", _PATH_MAILDIR);
char *xauthority = malloc(strlen(pw->pw_dir) + strlen("/.Xauthority") + 1);
strcpy(xauthority, pw->pw_dir);
strcat(xauthority, "/.Xauthority");
set_env("XAUTHORITY", xauthority);
free(xauthority);
}
static void set_env(char *name, char *value) {
char *name_value = malloc(strlen(name) + strlen(value) + 2);
strcpy(name_value, name);
strcat(name_value, "=");
strcat(name_value, value);
pam_putenv(pam_handle, name_value); // TODO: Handle errors
free(name_value);
}
static int end(int last_result) {
int result = pam_end(pam_handle, last_result);
pam_handle = 0;
return result;
}
static int conv(int num_msg, const struct pam_message **msg,
struct pam_response **resp, void *appdata_ptr) {
int i;
*resp = calloc(num_msg, sizeof(struct pam_response));
if (*resp == NULL) {
return PAM_BUF_ERR;
}
int result = PAM_SUCCESS;
for (i = 0; i < num_msg; i++) {
char *username, *password;
switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_ON:
username = ((char **) appdata_ptr)[0];
(*resp)[i].resp = strdup(username);
break;
case PAM_PROMPT_ECHO_OFF:
password = ((char **) appdata_ptr)[1];
(*resp)[i].resp = strdup(password);
break;
case PAM_ERROR_MSG:
fprintf(stderr, "%s\n", msg[i]->msg);
result = PAM_CONV_ERR;
break;
case PAM_TEXT_INFO:
printf("%s\n", msg[i]->msg);
break;
}
if (result != PAM_SUCCESS) {
break;
}
}
if (result != PAM_SUCCESS) {
free(*resp);
*resp = 0;
}
return result;
}