r13391: Only fall into password change when ACB_PWNOEXP is not set
[Samba.git] / source / nsswitch / pam_winbind.c
blob3848612c47d0a80f236b4d5a690020633d83eb77
1 /* pam_winbind module
3 Copyright Andrew Tridgell <tridge@samba.org> 2000
4 Copyright Tim Potter <tpot@samba.org> 2000
5 Copyright Andrew Bartlett <abartlet@samba.org> 2002
6 Copyright Guenther Deschner <gd@samba.org> 2005-2006
8 largely based on pam_userdb by Cristian Gafton <gafton@redhat.com>
9 also contains large slabs of code from pam_unix by Elliot Lee <sopwith@redhat.com>
10 (see copyright below for full details)
13 #include "includes.h"
14 #include "pam_winbind.h"
16 /* data tokens */
18 #define MAX_PASSWD_TRIES 3
20 /* some syslogging */
21 static void _pam_log(int err, const char *format, ...)
23 va_list args;
25 va_start(args, format);
26 openlog(MODULE_NAME, LOG_CONS|LOG_PID, LOG_AUTH);
27 vsyslog(err, format, args);
28 va_end(args);
29 closelog();
32 static void _pam_log_debug(int ctrl, int err, const char *format, ...)
34 va_list args;
36 if (!(ctrl & WINBIND_DEBUG_ARG)) {
37 return;
40 va_start(args, format);
41 openlog(MODULE_NAME, LOG_CONS|LOG_PID, LOG_AUTH);
42 vsyslog(err, format, args);
43 va_end(args);
44 closelog();
47 static int _pam_parse(int argc, const char **argv)
49 int ctrl = 0;
51 load_case_tables();
53 if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
54 return -1;
57 if (lp_parm_bool(-1, "pam_winbind", "cached_login", False)) {
58 ctrl |= WINBIND_CACHED_LOGIN;
60 if (lp_parm_bool(-1, "pam_winbind", "krb5_auth", False)) {
61 ctrl |= WINBIND_KRB5_AUTH;
63 if (lp_parm_const_string(-1, "pam_winbind", "krb5_ccache_type", NULL) != NULL) {
64 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
66 if ((lp_parm_const_string(-1, "pam_winbind", "require-membership-of", NULL) != NULL) ||
67 (lp_parm_const_string(-1, "pam_winbind", "require_membership_of", NULL) != NULL)) {
68 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
70 if (lp_parm_bool(-1, "pam_winbind", "create_homedir", False)) {
71 ctrl |= WINBIND_CREATE_HOMEDIR;
74 /* step through arguments */
75 for (; argc-- > 0; ++argv) {
77 /* generic options */
79 if (!StrCaseCmp(*argv, "debug"))
80 ctrl |= WINBIND_DEBUG_ARG;
81 else if (strequal(*argv, "use_authtok"))
82 ctrl |= WINBIND_USE_AUTHTOK_ARG;
83 else if (strequal(*argv, "use_first_pass"))
84 ctrl |= WINBIND_USE_FIRST_PASS_ARG;
85 else if (strequal(*argv, "try_first_pass"))
86 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
87 else if (strequal(*argv, "unknown_ok"))
88 ctrl |= WINBIND_UNKNOWN_OK_ARG;
89 else if (strnequal(*argv, "require_membership_of", strlen("require_membership_of")))
90 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
91 else if (strnequal(*argv, "require-membership-of", strlen("require-membership-of")))
92 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
93 else if (strequal(*argv, "krb5_auth"))
94 ctrl |= WINBIND_KRB5_AUTH;
95 else if (strnequal(*argv, "krb5_ccache_type", strlen("krb5_ccache_type")))
96 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
97 else if (strequal(*argv, "cached_login"))
98 ctrl |= WINBIND_CACHED_LOGIN;
99 else if (strequal(*argv, "create_homedir"))
100 ctrl |= WINBIND_CREATE_HOMEDIR;
101 else {
102 _pam_log(LOG_ERR, "pam_parse: unknown option; %s", *argv);
106 return ctrl;
109 static void _pam_winbind_cleanup_func(pam_handle_t *pamh, void *data, int error_status)
111 SAFE_FREE(data);
114 static const struct ntstatus_errors {
115 const char *ntstatus_string;
116 const char *error_string;
117 } ntstatus_errors[] = {
118 {"NT_STATUS_OK", "Success"},
119 {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
120 {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
121 {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
122 {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
123 {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
124 {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
125 {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
126 {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
127 {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
128 {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
129 {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
130 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
131 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
132 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
133 {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
134 {NULL, NULL}
137 const char *_get_ntstatus_error_string(const char *nt_status_string)
139 int i;
140 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
141 if (strequal(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
142 return ntstatus_errors[i].error_string;
145 return NULL;
148 /* --- authentication management functions --- */
150 /* Attempt a conversation */
152 static int converse(pam_handle_t *pamh, int nargs,
153 struct pam_message **message,
154 struct pam_response **response)
156 int retval;
157 struct pam_conv *conv;
159 retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv );
160 if (retval == PAM_SUCCESS) {
161 retval = conv->conv(nargs, (const struct pam_message **)message,
162 response, conv->appdata_ptr);
165 return retval; /* propagate error status */
169 static int _make_remark(pam_handle_t * pamh, int type, const char *text)
171 int retval = PAM_SUCCESS;
173 struct pam_message *pmsg[1], msg[1];
174 struct pam_response *resp;
176 pmsg[0] = &msg[0];
177 msg[0].msg = text;
178 msg[0].msg_style = type;
180 resp = NULL;
181 retval = converse(pamh, 1, pmsg, &resp);
183 if (resp) {
184 _pam_drop_reply(resp, 1);
186 return retval;
189 static int _make_remark_format(pam_handle_t * pamh, int type, const char *format, ...)
191 va_list args;
192 char *var;
194 va_start(args, format);
195 vasprintf(&var, format, args);
196 va_end(args);
198 return _make_remark(pamh, type, var);
201 static int pam_winbind_request(pam_handle_t * pamh, int ctrl,
202 enum winbindd_cmd req_type,
203 struct winbindd_request *request,
204 struct winbindd_response *response)
206 /* Fill in request and send down pipe */
207 init_request(request, req_type);
209 if (write_sock(request, sizeof(*request), 0) == -1) {
210 _pam_log(LOG_ERR, "write to socket failed!");
211 close_sock();
212 return PAM_SERVICE_ERR;
215 /* Wait for reply */
216 if (read_reply(response) == -1) {
217 _pam_log(LOG_ERR, "read from socket failed!");
218 close_sock();
219 return PAM_SERVICE_ERR;
222 /* We are done with the socket - close it and avoid mischeif */
223 close_sock();
225 /* Copy reply data from socket */
226 if (response->result != WINBINDD_OK) {
227 if (response->data.auth.pam_error != PAM_SUCCESS) {
228 _pam_log(LOG_ERR, "request failed: %s, PAM error was %d, NT error was %s",
229 response->data.auth.error_string,
230 response->data.auth.pam_error,
231 response->data.auth.nt_status_string);
232 return response->data.auth.pam_error;
233 } else {
234 _pam_log(LOG_ERR, "request failed, but PAM error 0!");
235 return PAM_SERVICE_ERR;
239 return PAM_SUCCESS;
242 static int pam_winbind_request_log(pam_handle_t * pamh,
243 int ctrl,
244 enum winbindd_cmd req_type,
245 struct winbindd_request *request,
246 struct winbindd_response *response,
247 const char *user)
249 int retval;
251 retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
253 switch (retval) {
254 case PAM_AUTH_ERR:
255 /* incorrect password */
256 _pam_log(LOG_WARNING, "user `%s' denied access (incorrect password or invalid membership)", user);
257 return retval;
258 case PAM_ACCT_EXPIRED:
259 /* account expired */
260 _pam_log(LOG_WARNING, "user `%s' account expired", user);
261 return retval;
262 case PAM_AUTHTOK_EXPIRED:
263 /* password expired */
264 _pam_log(LOG_WARNING, "user `%s' password expired", user);
265 return retval;
266 case PAM_NEW_AUTHTOK_REQD:
267 /* new password required */
268 _pam_log(LOG_WARNING, "user `%s' new password required", user);
269 return retval;
270 case PAM_USER_UNKNOWN:
271 /* the user does not exist */
272 _pam_log_debug(ctrl, LOG_NOTICE, "user `%s' not found",
273 user);
274 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
275 return PAM_IGNORE;
277 return retval;
278 case PAM_SUCCESS:
279 if (req_type == WINBINDD_PAM_AUTH) {
280 /* Otherwise, the authentication looked good */
281 _pam_log(LOG_NOTICE, "user '%s' granted access", user);
282 } else if (req_type == WINBINDD_PAM_CHAUTHTOK) {
283 /* Otherwise, the authentication looked good */
284 _pam_log(LOG_NOTICE, "user '%s' password changed", user);
285 } else {
286 /* Otherwise, the authentication looked good */
287 _pam_log(LOG_NOTICE, "user '%s' OK", user);
290 return retval;
291 default:
292 /* we don't know anything about this return value */
293 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s')",
294 retval, user);
295 return retval;
299 /* talk to winbindd */
300 static int winbind_auth_request(pam_handle_t * pamh,
301 int ctrl,
302 const char *user,
303 const char *pass,
304 const char *member,
305 const char *cctype,
306 int process_result)
308 struct winbindd_request request;
309 struct winbindd_response response;
310 int ret;
312 ZERO_STRUCT(request);
313 ZERO_STRUCT(response);
315 strncpy(request.data.auth.user, user,
316 sizeof(request.data.auth.user)-1);
318 strncpy(request.data.auth.pass, pass,
319 sizeof(request.data.auth.pass)-1);
321 request.data.auth.krb5_cc_type[0] = '\0';
322 request.data.auth.uid = -1;
324 request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_CONTACT_TRUSTDOM;
326 if (ctrl & WINBIND_KRB5_AUTH) {
328 struct passwd *pwd = NULL;
330 _pam_log_debug(ctrl, LOG_DEBUG, "enabling krb5 login flag\n");
332 request.flags |= WBFLAG_PAM_KRB5 | WBFLAG_PAM_FALLBACK_AFTER_KRB5;
334 pwd = getpwnam(user);
335 if (pwd == NULL) {
336 return PAM_USER_UNKNOWN;
338 request.data.auth.uid = pwd->pw_uid;
341 if (ctrl & WINBIND_CACHED_LOGIN) {
342 _pam_log_debug(ctrl, LOG_DEBUG, "enabling cached login flag\n");
343 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
346 if (cctype != NULL) {
347 strncpy(request.data.auth.krb5_cc_type, cctype,
348 sizeof(request.data.auth.krb5_cc_type) - 1);
349 _pam_log_debug(ctrl, LOG_DEBUG, "enabling request for a %s krb5 ccache\n", cctype);
352 request.data.auth.require_membership_of_sid[0] = '\0';
354 if (member != NULL) {
355 strncpy(request.data.auth.require_membership_of_sid, member,
356 sizeof(request.data.auth.require_membership_of_sid)-1);
359 /* lookup name? */
360 if ( (member != NULL) && (strncmp("S-", member, 2) != 0) ) {
362 struct winbindd_request sid_request;
363 struct winbindd_response sid_response;
365 ZERO_STRUCT(sid_request);
366 ZERO_STRUCT(sid_response);
368 _pam_log_debug(ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", member);
370 /* fortunatly winbindd can handle non-separated names */
371 fstrcpy(sid_request.data.name.name, member);
373 if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME, &sid_request, &sid_response, user)) {
374 _pam_log(LOG_INFO, "could not lookup name: %s\n", member);
375 return PAM_AUTH_ERR;
378 member = sid_response.data.sid.sid;
380 strncpy(request.data.auth.require_membership_of_sid, member,
381 sizeof(request.data.auth.require_membership_of_sid)-1);
384 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
386 if ((ctrl & WINBIND_KRB5_AUTH) &&
387 response.data.auth.krb5ccname[0] != '\0') {
389 char var[PATH_MAX];
391 _pam_log_debug(ctrl, LOG_DEBUG, "request returned KRB5CCNAME: %s",
392 response.data.auth.krb5ccname);
394 snprintf(var, sizeof(var), "KRB5CCNAME=%s", response.data.auth.krb5ccname);
396 ret = pam_putenv(pamh, var);
397 if (ret != PAM_SUCCESS) {
398 _pam_log(LOG_ERR, "failed to set KRB5CCNAME to %s", var);
399 return ret;
403 if (!process_result) {
404 return ret;
407 if (ret) {
408 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_EXPIRED");
409 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_MUST_CHANGE");
410 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_WORKSTATION");
411 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_LOGON_HOURS");
412 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_EXPIRED");
413 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_DISABLED");
414 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_LOCKED_OUT");
415 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
416 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
417 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
420 /* handle the case where the auth was ok, but the password must expire right now */
421 /* good catch from Ralf Haferkamp: an expiry of "never" is translated to -1 */
422 if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
423 (response.data.auth.policy.expire > 0) &&
424 (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire < time(NULL))) {
426 ret = PAM_AUTHTOK_EXPIRED;
428 _pam_log_debug(ctrl, LOG_DEBUG,"Password has expired (Password was last set: %d, "
429 "the policy says it should expire here %d (now it's: %d)\n",
430 response.data.auth.info3.pass_last_set_time,
431 response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire,
432 time(NULL));
434 PAM_WB_REMARK_DIRECT_RET(pamh, "NT_STATUS_PASSWORD_EXPIRED");
438 /* warn a user if the password is about to expire soon */
439 if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
440 (response.data.auth.policy.expire) &&
441 (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire > time(NULL) ) ) {
443 int days = response.data.auth.policy.expire / SECONDS_PER_DAY;
444 if (days <= DAYS_TO_WARN_BEFORE_PWD_EXPIRES) {
445 _make_remark_format(pamh, PAM_TEXT_INFO, "Your password will expire in %d days", days);
449 if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
450 _make_remark(pamh, PAM_ERROR_MSG, "Logging on using cached account. Network ressources can be unavailable");
453 /* save the CIFS homedir for pam_cifs / pam_mount */
454 if (response.data.auth.info3.home_dir[0] != '\0') {
455 char *buf;
457 if (!asprintf(&buf, "%s", response.data.auth.info3.home_dir)) {
458 return PAM_BUF_ERR;
461 pam_set_data( pamh, PAM_WINBIND_HOMEDIR, (void *)buf, _pam_winbind_cleanup_func);
464 return ret;
467 /* talk to winbindd */
468 static int winbind_chauthtok_request(pam_handle_t * pamh,
469 int ctrl,
470 const char *user,
471 const char *oldpass,
472 const char *newpass)
474 struct winbindd_request request;
475 struct winbindd_response response;
476 int ret;
478 ZERO_STRUCT(request);
479 ZERO_STRUCT(response);
481 if (request.data.chauthtok.user == NULL) return -2;
483 strncpy(request.data.chauthtok.user, user,
484 sizeof(request.data.chauthtok.user) - 1);
486 if (oldpass != NULL) {
487 strncpy(request.data.chauthtok.oldpass, oldpass,
488 sizeof(request.data.chauthtok.oldpass) - 1);
489 } else {
490 request.data.chauthtok.oldpass[0] = '\0';
493 if (newpass != NULL) {
494 strncpy(request.data.chauthtok.newpass, newpass,
495 sizeof(request.data.chauthtok.newpass) - 1);
496 } else {
497 request.data.chauthtok.newpass[0] = '\0';
500 if (ctrl & WINBIND_KRB5_AUTH) {
501 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
504 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
506 if (ret == PAM_SUCCESS) {
507 return ret;
510 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_BACKUP_CONTROLLER");
511 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCESS_DENIED");
513 /* TODO: tell the min pwd length ? */
514 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_SHORT");
516 /* TODO: tell the minage ? */
517 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_RECENT");
519 /* TODO: tell the history length ? */
520 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
522 if (strequal(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
524 /* FIXME: avoid to send multiple PAM messages after another */
525 switch (response.data.auth.reject_reason) {
526 case 0:
527 break;
528 case REJECT_REASON_TOO_SHORT:
529 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_SHORT");
530 break;
531 case REJECT_REASON_IN_HISTORY:
532 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_HISTORY_CONFLICT");
533 break;
534 case REJECT_REASON_NOT_COMPLEX:
535 _make_remark(pamh, PAM_ERROR_MSG, "Password does not meet complexity requirements");
536 break;
537 default:
538 _pam_log_debug(ctrl, LOG_DEBUG,
539 "unknown password change reject reason: %d",
540 response.data.auth.reject_reason);
541 break;
544 _make_remark_format(pamh, PAM_ERROR_MSG,
545 "Your password must be at least %d characters; "
546 "cannot repeat any of the your previous %d passwords"
547 "%s. "
548 "Please type a different password. "
549 "Type a password which meets these requirements in both text boxes.",
550 response.data.auth.policy.min_length_password,
551 response.data.auth.policy.password_history,
552 (response.data.auth.policy.password_properties & DOMAIN_PASSWORD_COMPLEX) ?
553 "; must contain capitals, numerals or punctuation; and cannot contain your account or full name" :
554 "");
558 return ret;
562 * Checks if a user has an account
564 * return values:
565 * 1 = User not found
566 * 0 = OK
567 * -1 = System error
569 static int valid_user(const char *user)
571 if (getpwnam(user)) return 0;
572 return 1;
575 static char *_pam_delete(register char *xx)
577 _pam_overwrite(xx);
578 _pam_drop(xx);
579 return NULL;
583 * obtain a password from the user
586 static int _winbind_read_password(pam_handle_t * pamh,
587 unsigned int ctrl,
588 const char *comment,
589 const char *prompt1,
590 const char *prompt2,
591 const char **pass)
593 int authtok_flag;
594 int retval;
595 const char *item;
596 char *token;
599 * make sure nothing inappropriate gets returned
602 *pass = token = NULL;
605 * which authentication token are we getting?
608 authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
611 * should we obtain the password from a PAM item ?
614 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
615 retval = pam_get_item(pamh, authtok_flag, (const void **) &item);
616 if (retval != PAM_SUCCESS) {
617 /* very strange. */
618 _pam_log(LOG_ALERT,
619 "pam_get_item returned error to unix-read-password"
621 return retval;
622 } else if (item != NULL) { /* we have a password! */
623 *pass = item;
624 item = NULL;
625 return PAM_SUCCESS;
626 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
627 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
628 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
629 && off(WINBIND__OLD_PASSWORD, ctrl)) {
630 return PAM_AUTHTOK_RECOVER_ERR;
634 * getting here implies we will have to get the password from the
635 * user directly.
639 struct pam_message msg[3], *pmsg[3];
640 struct pam_response *resp;
641 int i, replies;
643 /* prepare to converse */
645 if (comment != NULL) {
646 pmsg[0] = &msg[0];
647 msg[0].msg_style = PAM_TEXT_INFO;
648 msg[0].msg = comment;
649 i = 1;
650 } else {
651 i = 0;
654 pmsg[i] = &msg[i];
655 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
656 msg[i++].msg = prompt1;
657 replies = 1;
659 if (prompt2 != NULL) {
660 pmsg[i] = &msg[i];
661 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
662 msg[i++].msg = prompt2;
663 ++replies;
665 /* so call the conversation expecting i responses */
666 resp = NULL;
667 retval = converse(pamh, i, pmsg, &resp);
669 if (resp != NULL) {
671 /* interpret the response */
673 if (retval == PAM_SUCCESS) { /* a good conversation */
675 token = SMB_STRDUP(resp[i - replies].resp);
676 if (token != NULL) {
677 if (replies == 2) {
678 /* verify that password entered correctly */
679 if (!resp[i - 1].resp
680 || StrCaseCmp(token, resp[i - 1].resp)) {
681 _pam_delete(token); /* mistyped */
682 retval = PAM_AUTHTOK_RECOVER_ERR;
683 _make_remark(pamh, PAM_ERROR_MSG, MISTYPED_PASS);
686 } else {
687 _pam_log(LOG_NOTICE
688 ,"could not recover authentication token");
693 * tidy up the conversation (resp_retcode) is ignored
694 * -- what is it for anyway? AGM
697 _pam_drop_reply(resp, i);
699 } else {
700 retval = (retval == PAM_SUCCESS)
701 ? PAM_AUTHTOK_RECOVER_ERR : retval;
705 if (retval != PAM_SUCCESS) {
706 _pam_log_debug(ctrl, LOG_DEBUG,
707 "unable to obtain a password");
708 return retval;
710 /* 'token' is the entered password */
712 /* we store this password as an item */
714 retval = pam_set_item(pamh, authtok_flag, token);
715 _pam_delete(token); /* clean it up */
716 if (retval != PAM_SUCCESS ||
717 (retval = pam_get_item(pamh, authtok_flag, (const void **) &item)) != PAM_SUCCESS) {
719 _pam_log(LOG_CRIT, "error manipulating password");
720 return retval;
724 *pass = item;
725 item = NULL; /* break link to password */
727 return PAM_SUCCESS;
730 const char *get_conf_item_string(int argc,
731 const char **argv,
732 int ctrl,
733 const char *item,
734 int flag)
736 int i = 0;
737 char *parm = NULL;
738 const char *parm_opt = NULL;
740 if (!(ctrl & flag)) {
741 goto out;
744 /* let the pam opt take precedence over the smb.conf option */
745 parm_opt = lp_parm_const_string(-1, "pam_winbind", item, NULL);
747 for ( i=0; i<argc; i++ ) {
749 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
750 char *p;
752 parm = SMB_STRDUP(argv[i]);
754 if ( (p = strchr( parm, '=' )) == NULL) {
755 _pam_log(LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
756 goto out;
758 SAFE_FREE(parm);
759 _pam_log_debug(ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
760 return p + 1;
764 _pam_log_debug(ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
765 out:
766 SAFE_FREE(parm);
767 return parm_opt;
770 const char *get_krb5_cc_type_from_config(int argc, const char **argv, int ctrl)
772 return get_conf_item_string(argc, argv, ctrl, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
775 const char *get_member_from_config(int argc, const char **argv, int ctrl)
777 const char *ret = NULL;
778 ret = get_conf_item_string(argc, argv, ctrl, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
779 if (ret) {
780 return ret;
782 return get_conf_item_string(argc, argv, ctrl, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
785 PAM_EXTERN
786 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
787 int argc, const char **argv)
789 const char *username;
790 const char *password;
791 const char *member = NULL;
792 const char *cctype = NULL;
793 int retval = PAM_AUTH_ERR;
795 /* parse arguments */
796 int ctrl = _pam_parse(argc, argv);
797 if (ctrl == -1) {
798 return PAM_SYSTEM_ERR;
801 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_authenticate");
803 /* Get the username */
804 retval = pam_get_user(pamh, &username, NULL);
805 if ((retval != PAM_SUCCESS) || (!username)) {
806 _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
807 return PAM_SERVICE_ERR;
810 retval = _winbind_read_password(pamh, ctrl, NULL,
811 "Password: ", NULL,
812 &password);
814 if (retval != PAM_SUCCESS) {
815 _pam_log(LOG_ERR, "Could not retrieve user's password");
816 return PAM_AUTHTOK_ERR;
819 /* Let's not give too much away in the log file */
821 #ifdef DEBUG_PASSWORD
822 _pam_log_debug(ctrl, LOG_INFO, "Verify user `%s' with password `%s'",
823 username, password);
824 #else
825 _pam_log_debug(ctrl, LOG_INFO, "Verify user `%s'", username);
826 #endif
828 member = get_member_from_config(argc, argv, ctrl);
830 cctype = get_krb5_cc_type_from_config(argc, argv, ctrl);
832 /* Now use the username to look up password */
833 retval = winbind_auth_request(pamh, ctrl, username, password, member, cctype, True);
835 if (retval == PAM_NEW_AUTHTOK_REQD ||
836 retval == PAM_AUTHTOK_EXPIRED) {
838 char *buf;
840 if (!asprintf(&buf, "%d", retval)) {
841 return PAM_BUF_ERR;
844 pam_set_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (void *)buf, _pam_winbind_cleanup_func);
846 return PAM_SUCCESS;
849 return retval;
852 PAM_EXTERN
853 int pam_sm_setcred(pam_handle_t *pamh, int flags,
854 int argc, const char **argv)
856 /* parse arguments */
857 int ctrl = _pam_parse(argc, argv);
858 if (ctrl == -1) {
859 return PAM_SYSTEM_ERR;
862 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_setcred");
864 if (flags & PAM_DELETE_CRED) {
865 return pam_sm_close_session(pamh, flags, argc, argv);
868 return PAM_SUCCESS;
872 * Account management. We want to verify that the account exists
873 * before returning PAM_SUCCESS
875 PAM_EXTERN
876 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
877 int argc, const char **argv)
879 const char *username;
880 int retval = PAM_USER_UNKNOWN;
881 void *tmp = NULL;
883 /* parse arguments */
884 int ctrl = _pam_parse(argc, argv);
885 if (ctrl == -1) {
886 return PAM_SYSTEM_ERR;
889 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_acct_mgmt");
892 /* Get the username */
893 retval = pam_get_user(pamh, &username, NULL);
894 if ((retval != PAM_SUCCESS) || (!username)) {
895 _pam_log_debug(ctrl, LOG_DEBUG,"can not get the username");
896 return PAM_SERVICE_ERR;
899 /* Verify the username */
900 retval = valid_user(username);
901 switch (retval) {
902 case -1:
903 /* some sort of system error. The log was already printed */
904 return PAM_SERVICE_ERR;
905 case 1:
906 /* the user does not exist */
907 _pam_log_debug(ctrl, LOG_NOTICE, "user `%s' not found", username);
908 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
909 return PAM_IGNORE;
911 return PAM_USER_UNKNOWN;
912 case 0:
913 pam_get_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (const void **)&tmp);
914 if (tmp != NULL) {
915 retval = atoi(tmp);
916 switch (retval) {
917 case PAM_AUTHTOK_EXPIRED:
918 /* fall through, since new token is required in this case */
919 case PAM_NEW_AUTHTOK_REQD:
920 _pam_log(LOG_WARNING, "pam_sm_acct_mgmt success but %s is set",
921 PAM_WINBIND_NEW_AUTHTOK_REQD);
922 _pam_log(LOG_NOTICE, "user '%s' needs new password", username);
923 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
924 return PAM_NEW_AUTHTOK_REQD;
925 default:
926 _pam_log(LOG_WARNING, "pam_sm_acct_mgmt success");
927 _pam_log(LOG_NOTICE, "user '%s' granted access", username);
928 return PAM_SUCCESS;
932 /* Otherwise, the authentication looked good */
933 _pam_log(LOG_NOTICE, "user '%s' granted access", username);
934 return PAM_SUCCESS;
935 default:
936 /* we don't know anything about this return value */
937 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s')",
938 retval, username);
939 return PAM_SERVICE_ERR;
942 /* should not be reached */
943 return PAM_IGNORE;
946 PAM_EXTERN
947 int pam_sm_open_session(pam_handle_t *pamh, int flags,
948 int argc, const char **argv)
950 /* parse arguments */
951 int ctrl = _pam_parse(argc, argv);
952 if (ctrl == -1) {
953 return PAM_SYSTEM_ERR;
956 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_open_session handler");
959 if (ctrl & WINBIND_CREATE_HOMEDIR) {
961 struct passwd *pwd = NULL;
962 const char *username;
963 int ret;
964 fstring tok;
965 fstring create_dir;
966 SMB_STRUCT_STAT sbuf;
968 /* Get the username */
969 ret = pam_get_user(pamh, &username, NULL);
970 if ((ret != PAM_SUCCESS) || (!username)) {
971 _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
972 return PAM_SERVICE_ERR;
975 pwd = getpwnam(username);
976 if (pwd == NULL) {
977 _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
978 return PAM_SERVICE_ERR;
981 _pam_log_debug(ctrl, LOG_DEBUG, "homedir is: %s", pwd->pw_dir);
983 if (directory_exist(pwd->pw_dir, &sbuf)) {
984 return PAM_SUCCESS;
987 fstrcpy(create_dir, "/");
988 while (next_token((const char **)&pwd->pw_dir, tok, "/", sizeof(tok))) {
990 mode_t mode = 0755;
992 fstrcat(create_dir, tok);
993 fstrcat(create_dir, "/");
995 if (!directory_exist(create_dir, &sbuf)) {
996 if (mkdir(create_dir, mode) != 0) {
997 _pam_log(LOG_ERR, "could not create dir: %s (%s)",
998 create_dir, strerror(errno));
999 return PAM_SERVICE_ERR;
1004 if (sys_chown(create_dir, pwd->pw_uid, pwd->pw_gid) != 0) {
1005 _pam_log(LOG_ERR, "failed to chown user homedir: %s (%s)",
1006 create_dir, strerror(errno));
1007 return PAM_SERVICE_ERR;
1011 return PAM_SUCCESS;
1014 PAM_EXTERN
1015 int pam_sm_close_session(pam_handle_t *pamh, int flags,
1016 int argc, const char **argv)
1018 /* parse arguments */
1019 int ctrl = _pam_parse(argc, argv);
1020 if (ctrl == -1) {
1021 return PAM_SYSTEM_ERR;
1024 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_close_session handler");
1026 if (!(flags & PAM_DELETE_CRED)) {
1027 return PAM_SUCCESS;
1030 if (ctrl & WINBIND_KRB5_AUTH) {
1032 /* destroy the ccache here */
1033 struct winbindd_request request;
1034 struct winbindd_response response;
1035 const char *user;
1036 const char *ccname = NULL;
1037 struct passwd *pwd = NULL;
1039 int retval;
1041 ZERO_STRUCT(request);
1042 ZERO_STRUCT(response);
1044 retval = pam_get_user(pamh, &user, "Username: ");
1045 if (retval == PAM_SUCCESS) {
1046 if (user == NULL) {
1047 _pam_log(LOG_ERR, "username was NULL!");
1048 return PAM_USER_UNKNOWN;
1050 if (retval == PAM_SUCCESS) {
1051 _pam_log_debug(ctrl, LOG_DEBUG, "username [%s] obtained", user);
1053 } else {
1054 _pam_log_debug(ctrl, LOG_DEBUG, "could not identify user");
1055 return retval;
1058 ccname = pam_getenv(pamh, "KRB5CCNAME");
1059 if (ccname == NULL) {
1060 _pam_log_debug(ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1061 return PAM_BUF_ERR;
1064 fstrcpy(request.data.logoff.user, user);
1065 fstrcpy(request.data.logoff.krb5ccname, ccname);
1067 pwd = getpwnam(user);
1068 if (pwd == NULL) {
1069 return PAM_USER_UNKNOWN;
1071 request.data.logoff.uid = pwd->pw_uid;
1073 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1075 return pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1078 return PAM_SUCCESS;
1083 PAM_EXTERN
1084 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1085 int argc, const char **argv)
1087 unsigned int lctrl;
1088 int retval;
1089 unsigned int ctrl;
1091 /* <DO NOT free() THESE> */
1092 const char *user;
1093 char *pass_old, *pass_new;
1094 /* </DO NOT free() THESE> */
1096 fstring Announce;
1098 int retry = 0;
1100 ctrl = _pam_parse(argc, argv);
1101 if (ctrl == -1) {
1102 return PAM_SYSTEM_ERR;
1105 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_chauthtok");
1108 * First get the name of a user
1110 retval = pam_get_user(pamh, &user, "Username: ");
1111 if (retval == PAM_SUCCESS) {
1112 if (user == NULL) {
1113 _pam_log(LOG_ERR, "username was NULL!");
1114 return PAM_USER_UNKNOWN;
1116 if (retval == PAM_SUCCESS) {
1117 _pam_log_debug(ctrl, LOG_DEBUG, "username [%s] obtained",
1118 user);
1120 } else {
1121 _pam_log_debug(ctrl, LOG_DEBUG,
1122 "password - could not identify user");
1123 return retval;
1127 * obtain and verify the current password (OLDAUTHTOK) for
1128 * the user.
1131 if (flags & PAM_PRELIM_CHECK) {
1133 /* instruct user what is happening */
1134 #define greeting "Changing password for "
1135 fstrcpy(Announce, greeting);
1136 fstrcat(Announce, user);
1137 #undef greeting
1139 lctrl = ctrl | WINBIND__OLD_PASSWORD;
1140 retval = _winbind_read_password(pamh, lctrl,
1141 Announce,
1142 "(current) NT password: ",
1143 NULL,
1144 (const char **) &pass_old);
1145 if (retval != PAM_SUCCESS) {
1146 _pam_log(LOG_NOTICE, "password - (old) token not obtained");
1147 return retval;
1149 /* verify that this is the password for this user */
1151 retval = winbind_auth_request(pamh, ctrl, user, pass_old, NULL, NULL, False);
1153 if (retval != PAM_ACCT_EXPIRED
1154 && retval != PAM_AUTHTOK_EXPIRED
1155 && retval != PAM_NEW_AUTHTOK_REQD
1156 && retval != PAM_SUCCESS) {
1157 pass_old = NULL;
1158 return retval;
1161 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
1162 pass_old = NULL;
1163 if (retval != PAM_SUCCESS) {
1164 _pam_log(LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
1166 } else if (flags & PAM_UPDATE_AUTHTOK) {
1169 * obtain the proposed password
1173 * get the old token back.
1176 retval = pam_get_item(pamh, PAM_OLDAUTHTOK,
1177 (const void **) &pass_old);
1179 if (retval != PAM_SUCCESS) {
1180 _pam_log(LOG_NOTICE, "user not authenticated");
1181 return retval;
1184 lctrl = ctrl;
1186 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
1187 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
1189 retry = 0;
1190 retval = PAM_AUTHTOK_ERR;
1191 while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
1193 * use_authtok is to force the use of a previously entered
1194 * password -- needed for pluggable password strength checking
1197 retval = _winbind_read_password(pamh, lctrl,
1198 NULL,
1199 "Enter new NT password: ",
1200 "Retype new NT password: ",
1201 (const char **) &pass_new);
1203 if (retval != PAM_SUCCESS) {
1204 _pam_log_debug(ctrl, LOG_ALERT
1205 ,"password - new password not obtained");
1206 pass_old = NULL;/* tidy up */
1207 return retval;
1211 * At this point we know who the user is and what they
1212 * propose as their new password. Verify that the new
1213 * password is acceptable.
1216 if (pass_new[0] == '\0') {/* "\0" password = NULL */
1217 pass_new = NULL;
1222 * By reaching here we have approved the passwords and must now
1223 * rebuild the password database file.
1226 retval = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new);
1227 if (retval) {
1228 _pam_overwrite(pass_new);
1229 _pam_overwrite(pass_old);
1230 pass_old = pass_new = NULL;
1231 return retval;
1234 /* just in case we need krb5 creds after a password change over msrpc */
1236 if (ctrl & WINBIND_KRB5_AUTH) {
1238 const char *member = get_member_from_config(argc, argv, ctrl);
1239 const char *cctype = get_krb5_cc_type_from_config(argc, argv, ctrl);
1241 retval = winbind_auth_request(pamh, ctrl, user, pass_new, member, cctype, False);
1242 _pam_overwrite(pass_new);
1243 _pam_overwrite(pass_old);
1244 pass_old = pass_new = NULL;
1246 } else {
1247 retval = PAM_SERVICE_ERR;
1250 return retval;
1253 #ifdef PAM_STATIC
1255 /* static module data */
1257 struct pam_module _pam_winbind_modstruct = {
1258 MODULE_NAME,
1259 pam_sm_authenticate,
1260 pam_sm_setcred,
1261 pam_sm_acct_mgmt,
1262 pam_sm_open_session,
1263 pam_sm_close_session,
1264 pam_sm_chauthtok
1267 #endif
1270 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
1271 * Copyright (c) Tim Potter <tpot@samba.org> 2000
1272 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1273 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2006
1274 * Copyright (c) Jan Rêkorajski 1999.
1275 * Copyright (c) Andrew G. Morgan 1996-8.
1276 * Copyright (c) Alex O. Yuriev, 1996.
1277 * Copyright (c) Cristian Gafton 1996.
1278 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
1280 * Redistribution and use in source and binary forms, with or without
1281 * modification, are permitted provided that the following conditions
1282 * are met:
1283 * 1. Redistributions of source code must retain the above copyright
1284 * notice, and the entire permission notice in its entirety,
1285 * including the disclaimer of warranties.
1286 * 2. Redistributions in binary form must reproduce the above copyright
1287 * notice, this list of conditions and the following disclaimer in the
1288 * documentation and/or other materials provided with the distribution.
1289 * 3. The name of the author may not be used to endorse or promote
1290 * products derived from this software without specific prior
1291 * written permission.
1293 * ALTERNATIVELY, this product may be distributed under the terms of
1294 * the GNU Public License, in which case the provisions of the GPL are
1295 * required INSTEAD OF the above restrictions. (This clause is
1296 * necessary due to a potential bad interaction between the GPL and
1297 * the restrictions contained in a BSD-style copyright.)
1299 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
1300 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1301 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1302 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1303 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1304 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1305 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1306 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1307 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1308 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1309 * OF THE POSSIBILITY OF SUCH DAMAGE.