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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "../libcli/auth/libcli_auth.h"
25 #define _pam_overwrite(x) \
27 register char *__xx__; \
34 * Don't just free it, forget it too.
37 #define _pam_drop(X) \
45 #define _pam_drop_reply(/* struct pam_response * */ reply, /* int */ replies) \
49 for (reply_i=0; reply_i<replies; ++reply_i) { \
50 if (reply[reply_i].resp) { \
51 _pam_overwrite(reply[reply_i].resp); \
52 free(reply[reply_i].resp); \
60 int converse(pam_handle_t
*, int, int, struct pam_message
**,
61 struct pam_response
**);
62 int make_remark(pam_handle_t
*, unsigned int, int, const char *);
63 void _cleanup(pam_handle_t
*, void *, int);
64 char *_pam_delete(register char *);
66 /* syslogging function for errors and other information */
68 void _log_err( int err
, const char *format
, ... )
72 va_start( args
, format
);
73 openlog( "PAM_smbpass", LOG_CONS
| LOG_PID
, LOG_AUTH
);
74 vsyslog( err
, format
, args
);
79 /* this is a front-end for module-application conversations */
81 int converse( pam_handle_t
* pamh
, int ctrl
, int nargs
82 , struct pam_message
**message
83 , struct pam_response
**response
)
86 struct pam_conv
*conv
;
88 retval
= _pam_get_item(pamh
, PAM_CONV
, &conv
);
89 if (retval
== PAM_SUCCESS
) {
91 retval
= conv
->conv(nargs
, (const struct pam_message
**) message
92 ,response
, conv
->appdata_ptr
);
94 if (retval
!= PAM_SUCCESS
&& on(SMB_DEBUG
, ctrl
)) {
95 _log_err(LOG_DEBUG
, "conversation failure [%s]"
96 ,pam_strerror(pamh
, retval
));
99 _log_err(LOG_ERR
, "couldn't obtain coversation function [%s]"
100 ,pam_strerror(pamh
, retval
));
103 return retval
; /* propagate error status */
106 int make_remark( pam_handle_t
* pamh
, unsigned int ctrl
107 , int type
, const char *text
)
109 if (off(SMB__QUIET
, ctrl
)) {
110 struct pam_message
*pmsg
[1], msg
[1];
111 struct pam_response
*resp
;
114 msg
[0].msg
= CONST_DISCARD(char *, text
);
115 msg
[0].msg_style
= type
;
118 return converse(pamh
, ctrl
, 1, pmsg
, &resp
);
124 /* set the control flags for the SMB module. */
126 int set_ctrl( int flags
, int argc
, const char **argv
)
129 const char *service_file
= NULL
;
132 ctrl
= SMB_DEFAULTS
; /* the default selection of options */
134 /* set some flags manually */
136 /* A good, sane default (matches Samba's behavior). */
137 set( SMB__NONULL
, ctrl
);
139 /* initialize service file location */
140 service_file
=get_dyn_CONFIGFILE();
142 if (flags
& PAM_SILENT
) {
143 set( SMB__QUIET
, ctrl
);
146 /* Run through the arguments once, looking for an alternate smb config
151 for (j
= 0; j
< SMB_CTRLS_
; ++j
) {
152 if (smb_args
[j
].token
153 && !strncmp(argv
[i
], smb_args
[j
].token
, strlen(smb_args
[j
].token
)))
159 if (j
== SMB_CONF_FILE
) {
160 service_file
= argv
[i
] + 8;
165 /* Read some options from the Samba config. Can be overridden by
167 if(lp_load(service_file
,True
,False
,False
,True
) == False
) {
168 _log_err( LOG_ERR
, "Error loading service file %s", service_file
);
173 if (lp_null_passwords()) {
174 set( SMB__NULLOK
, ctrl
);
177 /* now parse the rest of the arguments to this module */
182 for (j
= 0; j
< SMB_CTRLS_
; ++j
) {
183 if (smb_args
[j
].token
184 && !strncmp(*argv
, smb_args
[j
].token
, strlen(smb_args
[j
].token
)))
190 if (j
>= SMB_CTRLS_
) {
191 _log_err( LOG_ERR
, "unrecognized option [%s]", *argv
);
193 ctrl
&= smb_args
[j
].mask
; /* for turning things off */
194 ctrl
|= smb_args
[j
].flag
; /* for turning things on */
197 ++argv
; /* step to next argument */
200 /* auditing is a more sensitive version of debug */
202 if (on( SMB_AUDIT
, ctrl
)) {
203 set( SMB_DEBUG
, ctrl
);
205 /* return the set of flags */
210 /* use this to free strings. ESPECIALLY password strings */
212 char * _pam_delete( register char *xx
)
214 _pam_overwrite( xx
);
219 void _cleanup( pam_handle_t
* pamh
, void *x
, int error_status
)
221 x
= _pam_delete( (char *) x
);
226 * Safe duplication of character strings. "Paranoid"; don't leave
227 * evidence of old token around for later stack analysis.
230 char * smbpXstrDup( const char *x
)
232 register char *newstr
= NULL
;
237 for (i
= 0; x
[i
]; ++i
); /* length of string */
238 if ((newstr
= SMB_MALLOC_ARRAY(char, ++i
)) == NULL
) {
240 _log_err( LOG_CRIT
, "out of memory in smbpXstrDup" );
248 return newstr
; /* return the duplicate or NULL on error */
251 /* ************************************************************** *
252 * Useful non-trivial functions *
253 * ************************************************************** */
255 void _cleanup_failures( pam_handle_t
* pamh
, void *fl
, int err
)
258 const char *service
= NULL
;
259 struct _pam_failed_auth
*failure
;
261 #ifdef PAM_DATA_SILENT
262 quiet
= err
& PAM_DATA_SILENT
; /* should we log something? */
266 #ifdef PAM_DATA_REPLACE
267 err
&= PAM_DATA_REPLACE
; /* are we just replacing data? */
269 failure
= (struct _pam_failed_auth
*) fl
;
271 if (failure
!= NULL
) {
273 #ifdef PAM_DATA_SILENT
274 if (!quiet
&& !err
) { /* under advisement from Sun,may go away */
276 if (!quiet
) { /* under advisement from Sun,may go away */
279 /* log the number of authentication failures */
280 if (failure
->count
!= 0) {
281 _pam_get_item( pamh
, PAM_SERVICE
, &service
);
283 , "%d authentication %s "
284 "from %s for service %s as %s(%d)"
286 , failure
->count
== 1 ? "failure" : "failures"
288 , service
== NULL
? "**unknown**" : service
289 , failure
->user
, failure
->id
);
290 if (failure
->count
> SMB_MAX_RETRIES
) {
292 , "service(%s) ignoring max retries; %d > %d"
293 , service
== NULL
? "**unknown**" : service
299 _pam_delete( failure
->agent
); /* tidy up */
300 _pam_delete( failure
->user
); /* tidy up */
301 SAFE_FREE( failure
);
305 int _smb_verify_password( pam_handle_t
* pamh
, struct samu
*sampass
,
306 const char *p
, unsigned int ctrl
)
310 int retval
= PAM_AUTH_ERR
;
317 name
= pdb_get_username(sampass
);
319 #ifdef HAVE_PAM_FAIL_DELAY
320 if (off( SMB_NODELAY
, ctrl
)) {
321 (void) pam_fail_delay( pamh
, 1000000 ); /* 1 sec delay for on failure */
325 if (!pdb_get_nt_passwd(sampass
))
327 _log_err( LOG_DEBUG
, "user %s has null SMB password"
330 if (off( SMB__NONULL
, ctrl
)
331 && (pdb_get_acct_ctrl(sampass
) & ACB_PWNOTREQ
))
332 { /* this means we've succeeded */
337 _pam_get_item( pamh
, PAM_SERVICE
, &service
);
338 _log_err( LOG_NOTICE
, "failed auth request by %s for service %s as %s",
339 uidtoname(getuid()), service
? service
: "**unknown**", name
);
344 data_name
= SMB_MALLOC_ARRAY(char, sizeof(FAIL_PREFIX
) + strlen( name
));
345 if (data_name
== NULL
) {
346 _log_err( LOG_CRIT
, "no memory for data-name" );
349 strncpy( data_name
, FAIL_PREFIX
, sizeof(FAIL_PREFIX
) );
350 strncpy( data_name
+ sizeof(FAIL_PREFIX
) - 1, name
, strlen( name
) + 1 );
353 * The password we were given wasn't an encrypted password, or it
354 * didn't match the one we have. We encrypt the password now and try
358 nt_lm_owf_gen(p
, nt_pw
, lm_pw
);
360 /* the moment of truth -- do we agree with the password? */
362 if (!memcmp( nt_pw
, pdb_get_nt_passwd(sampass
), 16 )) {
364 retval
= PAM_SUCCESS
;
365 if (data_name
) { /* reset failures */
366 pam_set_data(pamh
, data_name
, NULL
, _cleanup_failures
);
372 _pam_get_item( pamh
, PAM_SERVICE
, &service
);
374 if (data_name
!= NULL
) {
375 struct _pam_failed_auth
*newauth
= NULL
;
376 const struct _pam_failed_auth
*old
= NULL
;
378 /* get a failure recorder */
380 newauth
= SMB_MALLOC_P( struct _pam_failed_auth
);
382 if (newauth
!= NULL
) {
384 /* any previous failures for this user ? */
385 _pam_get_data(pamh
, data_name
, &old
);
388 newauth
->count
= old
->count
+ 1;
389 if (newauth
->count
>= SMB_MAX_RETRIES
) {
390 retval
= PAM_MAXTRIES
;
394 "failed auth request by %s for service %s as %s",
396 service
? service
: "**unknown**", name
);
399 if (!sid_to_uid(pdb_get_user_sid(sampass
), &(newauth
->id
))) {
401 "failed auth request by %s for service %s as %s",
403 service
? service
: "**unknown**", name
);
405 newauth
->user
= smbpXstrDup( name
);
406 newauth
->agent
= smbpXstrDup( uidtoname( getuid() ) );
407 pam_set_data( pamh
, data_name
, newauth
, _cleanup_failures
);
410 _log_err( LOG_CRIT
, "no memory for failure recorder" );
412 "failed auth request by %s for service %s as %s(%d)",
414 service
? service
: "**unknown**", name
);
418 "failed auth request by %s for service %s as %s(%d)",
420 service
? service
: "**unknown**", name
);
421 retval
= PAM_AUTH_ERR
;
424 _pam_delete( data_name
);
431 * _smb_blankpasswd() is a quick check for a blank password
433 * returns TRUE if user does not have a password
434 * - to avoid prompting for one in such cases (CG)
437 int _smb_blankpasswd( unsigned int ctrl
, struct samu
*sampass
)
442 * This function does not have to be too smart if something goes
443 * wrong, return FALSE and let this case to be treated somewhere
447 if (on( SMB__NONULL
, ctrl
))
448 return 0; /* will fail but don't let on yet */
450 if (!(pdb_get_acct_ctrl(sampass
) & ACB_PWNOTREQ
))
453 if (pdb_get_nt_passwd(sampass
) == NULL
)
462 * obtain a password from the user
465 int _smb_read_password( pam_handle_t
* pamh
, unsigned int ctrl
,
466 const char *comment
, const char *prompt1
,
467 const char *prompt2
, const char *data_name
, char **pass
)
474 struct pam_message msg
[3], *pmsg
[3];
475 struct pam_response
*resp
;
479 /* make sure nothing inappropriate gets returned */
481 *pass
= token
= NULL
;
483 /* which authentication token are we getting? */
485 authtok_flag
= on(SMB__OLD_PASSWD
, ctrl
) ? PAM_OLDAUTHTOK
: PAM_AUTHTOK
;
487 /* should we obtain the password from a PAM item ? */
489 if (on(SMB_TRY_FIRST_PASS
, ctrl
) || on(SMB_USE_FIRST_PASS
, ctrl
)) {
490 retval
= _pam_get_item( pamh
, authtok_flag
, &item
);
491 if (retval
!= PAM_SUCCESS
) {
494 , "pam_get_item returned error to smb_read_password" );
496 } else if (item
!= NULL
) { /* we have a password! */
500 } else if (on( SMB_USE_FIRST_PASS
, ctrl
)) {
501 return PAM_AUTHTOK_RECOVER_ERR
; /* didn't work */
502 } else if (on( SMB_USE_AUTHTOK
, ctrl
)
503 && off( SMB__OLD_PASSWD
, ctrl
))
505 return PAM_AUTHTOK_RECOVER_ERR
;
510 * getting here implies we will have to get the password from the
514 /* prepare to converse */
515 if (comment
!= NULL
&& off(SMB__QUIET
, ctrl
)) {
517 msg
[0].msg_style
= PAM_TEXT_INFO
;
518 msg
[0].msg
= CONST_DISCARD(char *, comment
);
525 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
526 msg
[i
++].msg
= CONST_DISCARD(char *, prompt1
);
528 if (prompt2
!= NULL
) {
530 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
531 msg
[i
++].msg
= CONST_DISCARD(char *, prompt2
);
538 retval
= converse( pamh
, ctrl
, i
, pmsg
, &resp
);
541 int j
= comment
? 1 : 0;
542 /* interpret the response */
544 if (retval
== PAM_SUCCESS
) { /* a good conversation */
546 token
= smbpXstrDup(resp
[j
++].resp
);
549 /* verify that password entered correctly */
550 if (!resp
[j
].resp
|| strcmp( token
, resp
[j
].resp
)) {
551 _pam_delete( token
);
552 retval
= PAM_AUTHTOK_RECOVER_ERR
;
553 make_remark( pamh
, ctrl
, PAM_ERROR_MSG
558 _log_err(LOG_NOTICE
, "could not recover authentication token");
563 _pam_drop_reply( resp
, expect
);
566 retval
= (retval
== PAM_SUCCESS
) ? PAM_AUTHTOK_RECOVER_ERR
: retval
;
569 if (retval
!= PAM_SUCCESS
) {
570 if (on( SMB_DEBUG
, ctrl
))
571 _log_err( LOG_DEBUG
, "unable to obtain a password" );
574 /* 'token' is the entered password */
576 if (off( SMB_NOT_SET_PASS
, ctrl
)) {
578 /* we store this password as an item */
580 retval
= pam_set_item( pamh
, authtok_flag
, (const void *)token
);
581 _pam_delete( token
); /* clean it up */
582 if (retval
!= PAM_SUCCESS
583 || (retval
= _pam_get_item( pamh
, authtok_flag
584 ,&item
)) != PAM_SUCCESS
)
586 _log_err( LOG_CRIT
, "error manipulating password" );
591 * then store it as data specific to this module. pam_end()
592 * will arrange to clean it up.
595 retval
= pam_set_data( pamh
, data_name
, (void *) token
, _cleanup
);
596 if (retval
!= PAM_SUCCESS
597 || (retval
= _pam_get_data( pamh
, data_name
, &item
))
600 _log_err( LOG_CRIT
, "error manipulating password data [%s]"
601 , pam_strerror( pamh
, retval
));
602 _pam_delete( token
);
606 token
= NULL
; /* break link to password */
610 item
= NULL
; /* break link to password */
615 int _pam_smb_approve_pass(pam_handle_t
* pamh
,
617 const char *pass_old
,
618 const char *pass_new
)
621 /* Further checks should be handled through module stacking. -SRL */
622 if (pass_new
== NULL
|| (pass_old
&& !strcmp( pass_old
, pass_new
)))
624 if (on(SMB_DEBUG
, ctrl
)) {
626 "passwd: bad authentication token (null or unchanged)" );
628 make_remark( pamh
, ctrl
, PAM_ERROR_MSG
, pass_new
== NULL
?
629 "No password supplied" : "Password unchanged" );
630 return PAM_AUTHTOK_ERR
;
637 * Work around the pam API that has functions with void ** as parameters
638 * These lead to strict aliasing warnings with gcc.
640 int _pam_get_item(const pam_handle_t
*pamh
,
644 const void **item
= (const void **)_item
;
645 return pam_get_item(pamh
, item_type
, item
);
648 int _pam_get_data(const pam_handle_t
*pamh
,
649 const char *module_data_name
,
652 const void **data
= (const void **)_data
;
653 return pam_get_data(pamh
, module_data_name
, data
);