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)
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
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.
24 #define _pam_overwrite(x) \
26 register char *__xx__; \
33 * Don't just free it, forget it too.
36 #define _pam_drop(X) \
44 #define _pam_drop_reply(/* struct pam_response * */ reply, /* int */ replies) \
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); \
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
, ... )
75 va_start( args
, format
);
76 openlog( "PAM_smbpass", LOG_CONS
| LOG_PID
, LOG_AUTH
);
77 vsyslog( err
, format
, args
);
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
)
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
));
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
;
118 msg
[0].msg_style
= type
;
121 return converse(pamh
, ctrl
, 1, pmsg
, &resp
);
127 /* set the control flags for the SMB module. */
129 int set_ctrl( int flags
, int argc
, const char **argv
)
132 static pstring servicesf
= CONFIGFILE
;
133 const char *service_file
= servicesf
;
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
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
)))
163 if (j
== SMB_CONF_FILE
) {
164 service_file
= argv
[i
] + 8;
169 /* Read some options from the Samba config. Can be overridden by
171 if(lp_load(service_file
,True
,False
,False
) == False
) {
172 _log_err( LOG_ERR
, "Error loading service file %s", service_file
);
177 if (lp_null_passwords()) {
178 set( SMB__NULLOK
, ctrl
);
181 /* now parse the rest of the arguments to this module */
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
)))
194 if (j
>= SMB_CTRLS_
) {
195 _log_err( LOG_ERR
, "unrecognized option [%s]", *argv
);
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 */
214 /* use this to free strings. ESPECIALLY password strings */
216 char * _pam_delete( register char *xx
)
218 _pam_overwrite( xx
);
223 void _cleanup( pam_handle_t
* pamh
, void *x
, int error_status
)
225 x
= _pam_delete( (char *) x
);
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
;
241 for (i
= 0; x
[i
]; ++i
); /* length of string */
242 if ((new = malloc(++i
)) == NULL
) {
244 _log_err( LOG_CRIT
, "out of memory in smbpXstrDup" );
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
)
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? */
270 #ifdef PAM_DATA_REPLACE
271 err
&= PAM_DATA_REPLACE
; /* are we just replacing data? */
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 */
280 if (!quiet
) { /* under advisement from Sun,may go away */
283 /* log the number of authentication failures */
284 if (failure
->count
!= 0) {
285 pam_get_item( pamh
, PAM_SERVICE
, (const void **) &service
);
287 , "%d authentication %s "
288 "from %s for service %s as %s(%d)"
290 , failure
->count
== 1 ? "failure" : "failures"
292 , service
== NULL
? "**unknown**" : service
293 , failure
->user
, failure
->id
);
294 if (failure
->count
> SMB_MAX_RETRIES
) {
296 , "service(%s) ignoring max retries; %d > %d"
297 , service
== NULL
? "**unknown**" : service
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
)
315 int retval
= PAM_AUTH_ERR
;
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 */
330 if (!pdb_get_lanman_passwd(sampass
))
332 _log_err( LOG_DEBUG
, "user %s has null SMB password"
335 if (off( SMB__NONULL
, ctrl
)
336 && (pdb_get_acct_ctrl(sampass
) & ACB_PWNOTREQ
))
337 { /* this means we've succeeded */
342 pam_get_item( pamh
, PAM_SERVICE
, (const void **)&service
);
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
) );
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
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 );
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
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
);
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
) );
411 /* any previous failures for this user ? */
412 pam_get_data(pamh
, data_name
, (const void **) &old
);
415 new->count
= old
->count
+ 1;
416 if (new->count
>= SMB_MAX_RETRIES
) {
417 retval
= PAM_MAXTRIES
;
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
) );
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
);
433 _log_err( LOG_CRIT
, "no memory for failure recorder" );
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
) );
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
);
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
)
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
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
)
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
)
497 struct pam_message msg
[3], *pmsg
[3];
498 struct pam_response
*resp
;
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
) {
517 , "pam_get_item returned error to smb_read_password" );
519 } else if (item
!= NULL
) { /* we have a password! */
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
537 /* prepare to converse */
538 if (comment
!= NULL
&& off(SMB__QUIET
, ctrl
)) {
540 msg
[0].msg_style
= PAM_TEXT_INFO
;
541 msg
[0].msg
= comment
;
548 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
549 msg
[i
++].msg
= prompt1
;
551 if (prompt2
!= NULL
) {
553 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
554 msg
[i
++].msg
= prompt2
;
561 retval
= converse( pamh
, ctrl
, i
, pmsg
, &resp
);
564 int j
= comment
? 1 : 0;
565 /* interpret the response */
567 if (retval
== PAM_SUCCESS
) { /* a good conversation */
569 token
= smbpXstrDup(resp
[j
++].resp
);
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
581 _log_err(LOG_NOTICE
, "could not recover authentication token");
586 _pam_drop_reply( resp
, expect
);
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" );
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" );
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
))
623 _log_err( LOG_CRIT
, "error manipulating password data [%s]"
624 , pam_strerror( pamh
, retval
));
625 _pam_delete( token
);
629 token
= NULL
; /* break link to password */
633 item
= NULL
; /* break link to password */
638 int _pam_smb_approve_pass(pam_handle_t
* pamh
,
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
)) {
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
;