create pidfile after forking
[Samba.git] / source / pam_smbpass / support.c
blob332b54f23b79c49ee18d96b4577dae3747f29d63
1 /* Unix NT password database implementation, version 0.6.
3 * This program is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 675
15 * Mass Ave, Cambridge, MA 02139, USA.
18 #include "includes.h"
19 #include "general.h"
21 #include "support.h"
24 #define _pam_overwrite(x) \
25 do { \
26 register char *__xx__; \
27 if ((__xx__=(x))) \
28 while (*__xx__) \
29 *__xx__++ = '\0'; \
30 } while (0)
33 * Don't just free it, forget it too.
36 #define _pam_drop(X) \
37 do { \
38 if (X) { \
39 free(X); \
40 X=NULL; \
41 } \
42 } while (0)
44 #define _pam_drop_reply(/* struct pam_response * */ reply, /* int */ replies) \
45 do { \
46 int reply_i; \
48 for (reply_i=0; reply_i<replies; ++reply_i) { \
49 if (reply[reply_i].resp) { \
50 _pam_overwrite(reply[reply_i].resp); \
51 free(reply[reply_i].resp); \
52 } \
53 } \
54 if (reply) \
55 free(reply); \
56 } while (0)
59 int converse(pam_handle_t *, int, int, struct pam_message **,
60 struct pam_response **);
61 int make_remark(pam_handle_t *, unsigned int, int, const char *);
62 void _cleanup(pam_handle_t *, void *, int);
63 char *_pam_delete(register char *);
65 /* default configuration file location */
67 pstring servicesf = CONFIGFILE;
69 /* syslogging function for errors and other information */
71 void _log_err( int err, const char *format, ... )
73 va_list args;
75 va_start( args, format );
76 openlog( "PAM_smbpass", LOG_CONS | LOG_PID, LOG_AUTH );
77 vsyslog( err, format, args );
78 va_end( args );
79 closelog();
82 /* this is a front-end for module-application conversations */
84 int converse( pam_handle_t * pamh, int ctrl, int nargs
85 , struct pam_message **message
86 , struct pam_response **response )
88 int retval;
89 struct pam_conv *conv;
91 retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
92 if (retval == PAM_SUCCESS) {
94 retval = conv->conv(nargs, (const struct pam_message **) message
95 ,response, conv->appdata_ptr);
97 if (retval != PAM_SUCCESS && on(SMB_DEBUG, ctrl)) {
98 _log_err(LOG_DEBUG, "conversation failure [%s]"
99 ,pam_strerror(pamh, retval));
101 } else {
102 _log_err(LOG_ERR, "couldn't obtain coversation function [%s]"
103 ,pam_strerror(pamh, retval));
106 return retval; /* propagate error status */
109 int make_remark( pam_handle_t * pamh, unsigned int ctrl
110 , int type, const char *text )
112 if (off(SMB__QUIET, ctrl)) {
113 struct pam_message *pmsg[1], msg[1];
114 struct pam_response *resp;
116 pmsg[0] = &msg[0];
117 msg[0].msg = text;
118 msg[0].msg_style = type;
119 resp = NULL;
121 return converse(pamh, ctrl, 1, pmsg, &resp);
123 return PAM_SUCCESS;
127 /* set the control flags for the SMB module. */
129 int set_ctrl( int flags, int argc, const char **argv )
131 int i = 0;
132 static pstring servicesf = CONFIGFILE;
133 const char *service_file = servicesf;
134 unsigned int ctrl;
136 ctrl = SMB_DEFAULTS; /* the default selection of options */
138 /* set some flags manually */
140 /* A good, sane default (matches Samba's behavior). */
141 set( SMB__NONULL, ctrl );
143 /* initialize service file location */
144 service_file=servicesf;
146 if (flags & PAM_SILENT) {
147 set( SMB__QUIET, ctrl );
150 /* Run through the arguments once, looking for an alternate smb config
151 file location */
152 while (i < argc) {
153 int j;
155 for (j = 0; j < SMB_CTRLS_; ++j) {
156 if (smb_args[j].token
157 && !strncmp(argv[i], smb_args[j].token, strlen(smb_args[j].token)))
159 break;
163 if (j == SMB_CONF_FILE) {
164 service_file = argv[i] + 8;
166 i++;
169 /* Read some options from the Samba config. Can be overridden by
170 the PAM config. */
171 if(lp_load(service_file,True,False,False) == False) {
172 _log_err( LOG_ERR, "Error loading service file %s", service_file );
175 secrets_init();
177 if (lp_null_passwords()) {
178 set( SMB__NULLOK, ctrl );
181 /* now parse the rest of the arguments to this module */
183 while (argc-- > 0) {
184 int j;
186 for (j = 0; j < SMB_CTRLS_; ++j) {
187 if (smb_args[j].token
188 && !strncmp(*argv, smb_args[j].token, strlen(smb_args[j].token)))
190 break;
194 if (j >= SMB_CTRLS_) {
195 _log_err( LOG_ERR, "unrecognized option [%s]", *argv );
196 } else {
197 ctrl &= smb_args[j].mask; /* for turning things off */
198 ctrl |= smb_args[j].flag; /* for turning things on */
201 ++argv; /* step to next argument */
204 /* auditing is a more sensitive version of debug */
206 if (on( SMB_AUDIT, ctrl )) {
207 set( SMB_DEBUG, ctrl );
209 /* return the set of flags */
211 return ctrl;
214 /* use this to free strings. ESPECIALLY password strings */
216 char * _pam_delete( register char *xx )
218 _pam_overwrite( xx );
219 _pam_drop( xx );
220 return NULL;
223 void _cleanup( pam_handle_t * pamh, void *x, int error_status )
225 x = _pam_delete( (char *) x );
228 /* JHT
230 * Safe duplication of character strings. "Paranoid"; don't leave
231 * evidence of old token around for later stack analysis.
234 char * smbpXstrDup( const char *x )
236 register char *new = NULL;
238 if (x != NULL) {
239 register int i;
241 for (i = 0; x[i]; ++i); /* length of string */
242 if ((new = malloc(++i)) == NULL) {
243 i = 0;
244 _log_err( LOG_CRIT, "out of memory in smbpXstrDup" );
245 } else {
246 while (i-- > 0) {
247 new[i] = x[i];
250 x = NULL;
252 return new; /* return the duplicate or NULL on error */
255 /* ************************************************************** *
256 * Useful non-trivial functions *
257 * ************************************************************** */
259 void _cleanup_failures( pam_handle_t * pamh, void *fl, int err )
261 int quiet;
262 const char *service = NULL;
263 struct _pam_failed_auth *failure;
265 #ifdef PAM_DATA_SILENT
266 quiet = err & PAM_DATA_SILENT; /* should we log something? */
267 #else
268 quiet = 0;
269 #endif
270 #ifdef PAM_DATA_REPLACE
271 err &= PAM_DATA_REPLACE; /* are we just replacing data? */
272 #endif
273 failure = (struct _pam_failed_auth *) fl;
275 if (failure != NULL) {
277 #ifdef PAM_DATA_SILENT
278 if (!quiet && !err) { /* under advisement from Sun,may go away */
279 #else
280 if (!quiet) { /* under advisement from Sun,may go away */
281 #endif
283 /* log the number of authentication failures */
284 if (failure->count != 0) {
285 pam_get_item( pamh, PAM_SERVICE, (const void **) &service );
286 _log_err( LOG_NOTICE
287 , "%d authentication %s "
288 "from %s for service %s as %s(%d)"
289 , failure->count
290 , failure->count == 1 ? "failure" : "failures"
291 , failure->agent
292 , service == NULL ? "**unknown**" : service
293 , failure->user, failure->id );
294 if (failure->count > SMB_MAX_RETRIES) {
295 _log_err( LOG_ALERT
296 , "service(%s) ignoring max retries; %d > %d"
297 , service == NULL ? "**unknown**" : service
298 , failure->count
299 , SMB_MAX_RETRIES );
303 _pam_delete( failure->agent ); /* tidy up */
304 _pam_delete( failure->user ); /* tidy up */
305 SAFE_FREE( failure );
309 int _smb_verify_password( pam_handle_t * pamh, SAM_ACCOUNT *sampass,
310 const char *p, unsigned int ctrl )
312 uchar hash_pass[16];
313 uchar lm_pw[16];
314 uchar nt_pw[16];
315 int retval = PAM_AUTH_ERR;
316 char *data_name;
317 const char *name;
319 if (!sampass)
320 return PAM_ABORT;
322 name = pdb_get_username(sampass);
324 #ifdef HAVE_PAM_FAIL_DELAY
325 if (off( SMB_NODELAY, ctrl )) {
326 (void) pam_fail_delay( pamh, 1000000 ); /* 1 sec delay for on failure */
328 #endif
330 if (!pdb_get_lanman_passwd(sampass))
332 _log_err( LOG_DEBUG, "user %s has null SMB password"
333 , name );
335 if (off( SMB__NONULL, ctrl )
336 && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ))
337 { /* this means we've succeeded */
338 return PAM_SUCCESS;
339 } else {
340 const char *service;
342 pam_get_item( pamh, PAM_SERVICE, (const void **)&service );
343 _log_err( LOG_NOTICE
344 , "failed auth request by %s for service %s as %s(%d)"
345 , uidtoname( getuid() )
346 , service ? service : "**unknown**", name
347 , pdb_get_uid(sampass) );
348 return PAM_AUTH_ERR;
352 data_name = (char *) malloc( sizeof(FAIL_PREFIX) + strlen( name ));
353 if (data_name == NULL) {
354 _log_err( LOG_CRIT, "no memory for data-name" );
356 strncpy( data_name, FAIL_PREFIX, sizeof(FAIL_PREFIX) );
357 strncpy( data_name + sizeof(FAIL_PREFIX) - 1, name, strlen( name ) + 1 );
359 /* First we check whether we've been given the password in already
360 encrypted form. */
361 if (strlen( p ) == 16 || (strlen( p ) == 32
362 && pdb_gethexpwd( p, (char *) hash_pass ))) {
364 if (!memcmp( hash_pass, pdb_get_lanman_passwd(sampass), 16 )
365 || (pdb_get_nt_passwd(sampass)
366 && !memcmp( hash_pass, pdb_get_nt_passwd(sampass), 16 )))
368 retval = PAM_SUCCESS;
369 if (data_name) { /* reset failures */
370 pam_set_data( pamh, data_name, NULL, _cleanup_failures );
372 _pam_delete( data_name );
373 memset( hash_pass, '\0', 16 );
374 return retval;
379 * The password we were given wasn't an encrypted password, or it
380 * didn't match the one we have. We encrypt the password now and try
381 * again.
384 nt_lm_owf_gen(p, nt_pw, lm_pw);
386 /* the moment of truth -- do we agree with the password? */
388 if (!memcmp( nt_pw, pdb_get_nt_passwd(sampass), 16 )) {
390 retval = PAM_SUCCESS;
391 if (data_name) { /* reset failures */
392 pam_set_data(pamh, data_name, NULL, _cleanup_failures);
394 } else {
396 const char *service;
398 pam_get_item( pamh, PAM_SERVICE, (const void **)&service );
400 if (data_name != NULL) {
401 struct _pam_failed_auth *new = NULL;
402 const struct _pam_failed_auth *old = NULL;
404 /* get a failure recorder */
406 new = (struct _pam_failed_auth *)
407 malloc( sizeof(struct _pam_failed_auth) );
409 if (new != NULL) {
411 /* any previous failures for this user ? */
412 pam_get_data(pamh, data_name, (const void **) &old);
414 if (old != NULL) {
415 new->count = old->count + 1;
416 if (new->count >= SMB_MAX_RETRIES) {
417 retval = PAM_MAXTRIES;
419 } else {
420 _log_err( LOG_NOTICE
421 , "failed auth request by %s for service %s as %s(%d)"
422 , uidtoname( getuid() )
423 , service ? service : "**unknown**", name
424 , pdb_get_uid(sampass) );
425 new->count = 1;
427 new->user = smbpXstrDup( name );
428 new->id = pdb_get_uid(sampass);
429 new->agent = smbpXstrDup( uidtoname( getuid() ) );
430 pam_set_data( pamh, data_name, new, _cleanup_failures );
432 } else {
433 _log_err( LOG_CRIT, "no memory for failure recorder" );
434 _log_err( LOG_NOTICE
435 , "failed auth request by %s for service %s as %s(%d)"
436 , uidtoname( getuid() )
437 , service ? service : "**unknown**", name
438 , pdb_get_uid(sampass) );
440 } else {
441 _log_err( LOG_NOTICE
442 , "failed auth request by %s for service %s as %s(%d)"
443 , uidtoname( getuid() )
444 , service ? service : "**unknown**", name
445 , pdb_get_uid(sampass) );
446 retval = PAM_AUTH_ERR;
450 _pam_delete( data_name );
452 return retval;
457 * _smb_blankpasswd() is a quick check for a blank password
459 * returns TRUE if user does not have a password
460 * - to avoid prompting for one in such cases (CG)
463 int _smb_blankpasswd( unsigned int ctrl, SAM_ACCOUNT *sampass )
465 int retval;
468 * This function does not have to be too smart if something goes
469 * wrong, return FALSE and let this case to be treated somewhere
470 * else (CG)
473 if (on( SMB__NONULL, ctrl ))
474 return 0; /* will fail but don't let on yet */
476 if (pdb_get_lanman_passwd(sampass) == NULL)
477 retval = 1;
478 else
479 retval = 0;
481 return retval;
485 * obtain a password from the user
488 int _smb_read_password( pam_handle_t * pamh, unsigned int ctrl,
489 char *comment, char *prompt1,
490 char *prompt2, char *data_name, char **pass )
492 int authtok_flag;
493 int retval;
494 char *item = NULL;
495 char *token;
497 struct pam_message msg[3], *pmsg[3];
498 struct pam_response *resp;
499 int i, expect;
502 /* make sure nothing inappropriate gets returned */
504 *pass = token = NULL;
506 /* which authentication token are we getting? */
508 authtok_flag = on(SMB__OLD_PASSWD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
510 /* should we obtain the password from a PAM item ? */
512 if (on(SMB_TRY_FIRST_PASS, ctrl) || on(SMB_USE_FIRST_PASS, ctrl)) {
513 retval = pam_get_item( pamh, authtok_flag, (const void **) &item );
514 if (retval != PAM_SUCCESS) {
515 /* very strange. */
516 _log_err( LOG_ALERT
517 , "pam_get_item returned error to smb_read_password" );
518 return retval;
519 } else if (item != NULL) { /* we have a password! */
520 *pass = item;
521 item = NULL;
522 return PAM_SUCCESS;
523 } else if (on( SMB_USE_FIRST_PASS, ctrl )) {
524 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
525 } else if (on( SMB_USE_AUTHTOK, ctrl )
526 && off( SMB__OLD_PASSWD, ctrl ))
528 return PAM_AUTHTOK_RECOVER_ERR;
533 * getting here implies we will have to get the password from the
534 * user directly.
537 /* prepare to converse */
538 if (comment != NULL && off(SMB__QUIET, ctrl)) {
539 pmsg[0] = &msg[0];
540 msg[0].msg_style = PAM_TEXT_INFO;
541 msg[0].msg = comment;
542 i = 1;
543 } else {
544 i = 0;
547 pmsg[i] = &msg[i];
548 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
549 msg[i++].msg = prompt1;
551 if (prompt2 != NULL) {
552 pmsg[i] = &msg[i];
553 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
554 msg[i++].msg = prompt2;
555 expect = 2;
556 } else
557 expect = 1;
559 resp = NULL;
561 retval = converse( pamh, ctrl, i, pmsg, &resp );
563 if (resp != NULL) {
564 int j = comment ? 1 : 0;
565 /* interpret the response */
567 if (retval == PAM_SUCCESS) { /* a good conversation */
569 token = smbpXstrDup(resp[j++].resp);
570 if (token != NULL) {
571 if (expect == 2) {
572 /* verify that password entered correctly */
573 if (!resp[j].resp || strcmp( token, resp[j].resp )) {
574 _pam_delete( token );
575 retval = PAM_AUTHTOK_RECOVER_ERR;
576 make_remark( pamh, ctrl, PAM_ERROR_MSG
577 , MISTYPED_PASS );
580 } else {
581 _log_err(LOG_NOTICE, "could not recover authentication token");
585 /* tidy up */
586 _pam_drop_reply( resp, expect );
588 } else {
589 retval = (retval == PAM_SUCCESS) ? PAM_AUTHTOK_RECOVER_ERR : retval;
592 if (retval != PAM_SUCCESS) {
593 if (on( SMB_DEBUG, ctrl ))
594 _log_err( LOG_DEBUG, "unable to obtain a password" );
595 return retval;
597 /* 'token' is the entered password */
599 if (off( SMB_NOT_SET_PASS, ctrl )) {
601 /* we store this password as an item */
603 retval = pam_set_item( pamh, authtok_flag, (const void *)token );
604 _pam_delete( token ); /* clean it up */
605 if (retval != PAM_SUCCESS
606 || (retval = pam_get_item( pamh, authtok_flag
607 ,(const void **)&item )) != PAM_SUCCESS)
609 _log_err( LOG_CRIT, "error manipulating password" );
610 return retval;
612 } else {
614 * then store it as data specific to this module. pam_end()
615 * will arrange to clean it up.
618 retval = pam_set_data( pamh, data_name, (void *) token, _cleanup );
619 if (retval != PAM_SUCCESS
620 || (retval = pam_get_data( pamh, data_name, (const void **)&item ))
621 != PAM_SUCCESS)
623 _log_err( LOG_CRIT, "error manipulating password data [%s]"
624 , pam_strerror( pamh, retval ));
625 _pam_delete( token );
626 item = NULL;
627 return retval;
629 token = NULL; /* break link to password */
632 *pass = item;
633 item = NULL; /* break link to password */
635 return PAM_SUCCESS;
638 int _pam_smb_approve_pass(pam_handle_t * pamh,
639 unsigned int ctrl,
640 const char *pass_old,
641 const char *pass_new )
644 /* Further checks should be handled through module stacking. -SRL */
645 if (pass_new == NULL || (pass_old && !strcmp( pass_old, pass_new )))
647 if (on(SMB_DEBUG, ctrl)) {
648 _log_err( LOG_DEBUG,
649 "passwd: bad authentication token (null or unchanged)" );
651 make_remark( pamh, ctrl, PAM_ERROR_MSG, pass_new == NULL ?
652 "No password supplied" : "Password unchanged" );
653 return PAM_AUTHTOK_ERR;
656 return PAM_SUCCESS;