forked from gplll/nss_securepass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pam_sp.c
426 lines (354 loc) · 11.6 KB
/
pam_sp.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
*
* File: pam_sp.c
* Author: gplll <gplll1818@gmail.com>, Aug 2015
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is derivative work of pam_radius.c code,
* taken from https://github.com/FreeRADIUS/pam_radius
*
* pam_radius.c License NOTICE:
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The original pam_radius.c code is copyright (c) Cristian Gafton, 1996,
* <gafton@redhat.com>
*
* Some challenge-response code is copyright (c) CRYPTOCard Inc, 1998.
* All rights reserved.
*/
#define PAM_SM_AUTH
#define PAM_SM_PASSWORD
#define PAM_SM_SESSION
#define PAM_SM_ACCOUNT
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <pthread.h>
#include <syslog.h>
#include <errno.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include "sp_api.h"
#include "pam_sp.h"
#define SP_INIT \
if (sp_config.status != SP_INITED) { \
if (sp_init () == -1) return PAM_SERVICE_ERR; \
}
#undef PAM_FAIL_CHECK
#define PAM_FAIL_CHECK if (retval != PAM_SUCCESS) { return retval; }
/* argument parsing */
/* returns PAM_SUCCESS if success, PAM_SERVICE_ERR if error */
static int _pam_parse(int argc, const char **argv, sp_conf_t *conf)
{
memset(conf, 0, sizeof(sp_conf_t)); /* ensure it's initialized */
/*
* If either is not there, then we can't parse anything.
*/
if ((argc == 0) || (argv == NULL)) {
return PAM_SUCCESS;
}
/* step through arguments */
for (; argc-- > 0; ++argv) {
if (!strcmp(*argv, "debug")) {
conf->debug = 1;
}
else if (!strcmp(*argv, "debug_stderr")) {
conf->debug_stderr = 1;
}
else {
error ("unrecognized option: %s", *argv);
return PAM_SERVICE_ERR;
}
}
return PAM_SUCCESS;
}
/* Callback function used to free the saved return value for pam_setcred. */
void _int_free(pam_handle_t * pamh, void *x, int error_status)
{
free(x);
}
static int sp_converse(pam_handle_t *pamh, int msg_style, char *message, char **password)
{
const struct pam_conv *conv;
struct pam_message resp_msg;
const struct pam_message *msg[1];
struct pam_response *resp = NULL;
int retval;
resp_msg.msg_style = msg_style;
resp_msg.msg = message;
msg[0] = &resp_msg;
/* grab the password */
retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
PAM_FAIL_CHECK;
retval = conv->conv(1, msg, &resp,conv->appdata_ptr);
PAM_FAIL_CHECK;
if (password) { /* assume msg.type needs a response */
/* I'm not sure if this next bit is necessary on Linux */
*password = resp->resp;
free(resp);
}
return PAM_SUCCESS;
}
#undef PAM_FAIL_CHECK
#define PAM_FAIL_CHECK if (retval != PAM_SUCCESS) { \
int *pret = malloc(sizeof(int)); \
*pret = retval; \
pam_set_data(pamh, "sp_setcred_return", (void *) pret, _int_free); \
return retval; }
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh,int flags,int argc,const char **argv)
{
const char *user;
char *password = NULL;
int retval;
sp_conf_t config;
retval = _pam_parse(argc, argv, &config);
PAM_FAIL_CHECK;
SP_INIT;
debug (2, "==> pam_sm_authenticate");
/* grab the user name */
retval = pam_get_user(pamh, &user, NULL);
PAM_FAIL_CHECK;
/* check that they've entered something */
if (user == NULL) {
retval = PAM_USER_UNKNOWN;
PAM_FAIL_CHECK;
}
debug (2, "Got user name %s", user);
#undef PAM_FAIL_CHECK
#define PAM_FAIL_CHECK if (retval != PAM_SUCCESS) {goto error; }
/* grab the password (if any) from the previous authentication layer */
retval = pam_get_item(pamh, PAM_AUTHTOK, (const void **) &password);
PAM_FAIL_CHECK;
if (password) {
password = strdup(password);
/* debug (2, "Got password from PAM: %s", password); */
debug (2, "Got password from PAM");
}
/* no previous password: get one from the user */
if (!password) {
retval = sp_converse(pamh, PAM_PROMPT_ECHO_OFF, "Password: ", &password);
PAM_FAIL_CHECK;
}
/* call securepass API */
if (sp_user_auth_p (user, password) != -1) {
retval = PAM_SUCCESS;
} else {
retval = PAM_AUTH_ERR; /* authentication failure */
error:
/* If there was a password pass it to the next layer */
if (password && *password) {
pam_set_item(pamh, PAM_AUTHTOK, password);
}
}
debug (2, "authentication for user %s %s", user, retval==PAM_SUCCESS ? "succeeded":"failed");
_pam_forget(password);
int *pret = malloc(sizeof(int));
*pret = retval;
pam_set_data(pamh, "sp_setcred_return", (void *) pret, _int_free);
return retval;
}
#undef PAM_FAIL_CHECK
#define PAM_FAIL_CHECK if (retval != PAM_SUCCESS) { return retval; }
PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh,int flags,int argc,const char **argv)
{
int retval, *pret;
sp_conf_t config;
retval = _pam_parse(argc, argv, &config);
PAM_FAIL_CHECK;
SP_INIT;
debug (2, "==> pam_sm_setcred, flags=0x%x argc=%d", flags, argc);
retval = PAM_SUCCESS;
pret = &retval;
pam_get_data(pamh, "sp_setcred_return", (const void **) &pret);
return (*pret==PAM_SUCCESS ? PAM_SUCCESS : PAM_CRED_ERR);
}
PAM_EXTERN int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
sp_conf_t config;
int retval = _pam_parse(argc, argv, &config);
PAM_FAIL_CHECK;
SP_INIT;
debug (2, "==> pam_sm_open_session() called...returning PAM_SUCCESS");
return PAM_SUCCESS;
}
PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
sp_conf_t config;
int retval = _pam_parse(argc, argv, &config);
PAM_FAIL_CHECK;
SP_INIT;
debug (2, "==> pam_sm_close_session() called...returning PAM_SUCCESS");
return PAM_SUCCESS;
}
PAM_EXTERN int pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
const char *user;
char *password = NULL;
char *new_password = NULL;
char *check_password = NULL;
int retval = PAM_AUTHTOK_ERR;
int attempts;
sp_conf_t config;
retval = _pam_parse(argc, argv, &config);
PAM_FAIL_CHECK;
SP_INIT;
debug (2, "==> pam_sm_chauthtok, flags=0x%x argc=%d", flags, argc);
/*
* check args, only accept debug, otherwise return error
*/
/* grab the user name */
retval = pam_get_user(pamh, &user, NULL);
PAM_FAIL_CHECK;
/* check that they've entered something */
if (user == NULL) {
return PAM_USER_UNKNOWN;
}
debug (2, "user=%s", user);
#undef PAM_FAIL_CHECK
#define PAM_FAIL_CHECK if (retval != PAM_SUCCESS) {goto error; }
/* grab the old password (if any) from the previous password layer */
retval = pam_get_item(pamh, PAM_OLDAUTHTOK, (const void **) &password);
PAM_FAIL_CHECK;
if (password) {
password = strdup(password);
/* debug (2, "old pwd= %s", password); */
debug (2, "got old pwd from previous layer");
}
/* grab the new password (if any) from the previous password layer */
retval = pam_get_item(pamh, PAM_AUTHTOK, (const void **) &new_password);
PAM_FAIL_CHECK;
if (new_password) {
new_password = strdup(new_password);
/* debug (2, "new pwd= %s", new_password); */
debug (2, "got new pwd from previous layer");
}
/* preliminary password change checks. */
if (flags & PAM_PRELIM_CHECK) {
if (!password) { /* no previous password: ask for one */
retval = sp_converse(pamh, PAM_PROMPT_ECHO_OFF, "Securepass password: ", &password);
PAM_FAIL_CHECK;
}
/*
* We now check the password to see if it's the right one.
* If it isn't, we let the user try again.
*/
/* call securepass API */
if (sp_user_auth_p (user, password) == -1) {
debug (3, "old password for user %s is wrong\n", user);
_pam_forget(password);
retval = PAM_PERM_DENIED;
goto error;
} else {
debug (3, "old password for user %s is correct\n", user);
}
/*
* We're now sure it's the right user.
* Ask for their new password, if appropriate
*/
if (!new_password) { /* not found yet: ask for it */
int new_attempts;
attempts = 0;
/* loop, trying to get matching new passwords */
while (attempts++ < 3) {
/* loop, trying to get a new password */
new_attempts = 0;
while (new_attempts++ < 3) {
retval = sp_converse (pamh, PAM_PROMPT_ECHO_OFF,
"New password: ", &new_password);
PAM_FAIL_CHECK;
if (strcmp(password, new_password) == 0) { /* are they the same? */
sp_converse(pamh, PAM_ERROR_MSG,
"You must choose a new password.", NULL);
_pam_forget(new_password);
continue;
}
break; /* the new password is OK */
}
if (new_attempts >= 3) { /* too many new password attempts: die */
retval = PAM_AUTHTOK_ERR;
goto error;
}
/* make sure of the password by asking for verification */
retval = sp_converse(pamh, PAM_PROMPT_ECHO_OFF,
"New password (again): ", &check_password);
PAM_FAIL_CHECK;
retval = strcmp(new_password, check_password);
_pam_forget(check_password);
/* if they don't match, don't pass them to the next module */
if (retval != 0) {
_pam_forget(new_password);
sp_converse(pamh, PAM_ERROR_MSG,
"You must enter the same password twice.", NULL);
retval = PAM_AUTHTOK_ERR;
goto error; /* ??? maybe this should be a 'continue' ??? */
}
break; /* everything's fine */
} /* loop, trying to get matching new passwords */
if (attempts >= 3) { /* too many new password attempts: die */
retval = PAM_AUTHTOK_ERR;
goto error;
}
} /* now we have a new password which passes all of our tests */
} else if (flags & PAM_UPDATE_AUTHTOK) {
if (!password || !new_password) { /* ensure we've got passwords */
retval = PAM_AUTHTOK_ERR;
goto error;
}
/* call SP API to change the passwd */
if (sp_user_password_change_p (user, new_password) == -1) {
debug (3, "can't set new password for user %s\n", user);
retval = PAM_AUTHTOK_ERR;
goto error;
} else {
debug (3, "new password for user %s has been set\n", user);
}
}
/*
* Send the passwords to the next stage if preliminary checks fail,
* or if the password change request fails.
*/
if ((flags & PAM_PRELIM_CHECK) || (retval != PAM_SUCCESS)) {
error:
/* If there was a password pass it to the next layer */
if (password && *password) {
pam_set_item(pamh, PAM_OLDAUTHTOK, password);
}
if (new_password && *new_password) {
pam_set_item(pamh, PAM_AUTHTOK, new_password);
}
}
debug (2, "password change %s", retval==PAM_SUCCESS ? "succeeded" : "failed");
_pam_forget(password);
_pam_forget(new_password);
return retval;
}
#undef PAM_FAIL_CHECK
#define PAM_FAIL_CHECK if (retval != PAM_SUCCESS) { return retval; }
PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh,int flags,int argc,const char **argv)
{
sp_conf_t config;
int retval = _pam_parse(argc, argv, &config);
PAM_FAIL_CHECK;
SP_INIT;
debug (2, "==> pam_sm_acct_mgmt() called...returning PAM_SUCCESS");
return PAM_SUCCESS;
}