3 Copyright Andrew Tridgell <tridge@samba.org> 2000
4 Copyright Tim Potter <tpot@samba.org> 2000
5 Copyright Andrew Bartlett <abartlet@samba.org> 2002
7 largely based on pam_userdb by Christian Gafton <gafton@redhat.com>
8 also contains large slabs of code from pam_unix by Elliot Lee <sopwith@redhat.com>
9 (see copyright below for full details)
12 #include "pam_winbind.h"
16 #define MAX_PASSWD_TRIES 3
19 static void _pam_log(int err, const char *format, ...)
23 va_start(args, format);
24 openlog(MODULE_NAME, LOG_CONS|LOG_PID, LOG_AUTH);
25 vsyslog(err, format, args);
30 static int _pam_parse(int argc, const char **argv)
33 /* step through arguments */
34 for (ctrl = 0; argc-- > 0; ++argv) {
38 if (!strcmp(*argv,"debug"))
39 ctrl |= WINBIND_DEBUG_ARG;
40 else if (!strcasecmp(*argv, "use_authtok"))
41 ctrl |= WINBIND_USE_AUTHTOK_ARG;
42 else if (!strcasecmp(*argv, "use_first_pass"))
43 ctrl |= WINBIND_USE_FIRST_PASS_ARG;
44 else if (!strcasecmp(*argv, "try_first_pass"))
45 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
46 else if (!strcasecmp(*argv, "unknown_ok"))
47 ctrl |= WINBIND_UNKNOWN_OK_ARG;
49 _pam_log(LOG_ERR, "pam_parse: unknown option; %s", *argv);
56 /* --- authentication management functions --- */
58 /* Attempt a conversation */
60 static int converse(pam_handle_t *pamh, int nargs,
61 struct pam_message **message,
62 struct pam_response **response)
65 struct pam_conv *conv;
67 retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv ) ;
68 if (retval == PAM_SUCCESS) {
69 retval = conv->conv(nargs, (const struct pam_message **)message,
70 response, conv->appdata_ptr);
73 return retval; /* propagate error status */
77 static int _make_remark(pam_handle_t * pamh, int type, const char *text)
79 int retval = PAM_SUCCESS;
81 struct pam_message *pmsg[1], msg[1];
82 struct pam_response *resp;
86 msg[0].msg_style = type;
89 retval = converse(pamh, 1, pmsg, &resp);
92 _pam_drop_reply(resp, 1);
97 static int pam_winbind_request(enum winbindd_cmd req_type,
98 struct winbindd_request *request,
99 struct winbindd_response *response)
102 /* Fill in request and send down pipe */
103 init_request(request, req_type);
105 if (write_sock(request, sizeof(*request)) == -1) {
106 _pam_log(LOG_ERR, "write to socket failed!");
108 return PAM_SERVICE_ERR;
112 if (read_reply(response) == -1) {
113 _pam_log(LOG_ERR, "read from socket failed!");
115 return PAM_SERVICE_ERR;
118 /* We are done with the socket - close it and avoid mischeif */
121 /* Copy reply data from socket */
122 if (response->result != WINBINDD_OK) {
123 if (response->data.auth.pam_error != PAM_SUCCESS) {
124 _pam_log(LOG_ERR, "request failed, PAM error was %d, NT error was %s",
125 response->data.auth.pam_error,
126 response->data.auth.nt_status_string);
127 return response->data.auth.pam_error;
129 _pam_log(LOG_ERR, "request failed, but PAM error 0!");
130 return PAM_SERVICE_ERR;
137 /* talk to winbindd */
138 static int winbind_auth_request(const char *user, const char *pass, int ctrl)
140 struct winbindd_request request;
141 struct winbindd_response response;
144 ZERO_STRUCT(request);
146 strncpy(request.data.auth.user, user,
147 sizeof(request.data.auth.user)-1);
149 strncpy(request.data.auth.pass, pass,
150 sizeof(request.data.auth.pass)-1);
152 retval = pam_winbind_request(WINBINDD_PAM_AUTH, &request, &response);
156 /* incorrect password */
157 _pam_log(LOG_WARNING, "user `%s' denied access (incorrect password)", user);
159 case PAM_ACCT_EXPIRED:
160 /* account expired */
161 _pam_log(LOG_WARNING, "user `%s' account expired", user);
163 case PAM_AUTHTOK_EXPIRED:
164 /* password expired */
165 _pam_log(LOG_WARNING, "user `%s' password expired", user);
167 case PAM_NEW_AUTHTOK_REQD:
168 /* password expired */
169 _pam_log(LOG_WARNING, "user `%s' new password required", user);
171 case PAM_USER_UNKNOWN:
172 /* the user does not exist */
173 if (ctrl & WINBIND_DEBUG_ARG)
174 _pam_log(LOG_NOTICE, "user `%s' not found",
176 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
181 /* Otherwise, the authentication looked good */
182 _pam_log(LOG_NOTICE, "user '%s' granted acces", user);
185 /* we don't know anything about this return value */
186 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s'",
190 /* should not be reached */
193 /* talk to winbindd */
194 static int winbind_chauthtok_request(const char *user, const char *oldpass,
197 struct winbindd_request request;
198 struct winbindd_response response;
200 ZERO_STRUCT(request);
202 if (request.data.chauthtok.user == NULL) return -2;
204 strncpy(request.data.chauthtok.user, user,
205 sizeof(request.data.chauthtok.user) - 1);
207 if (oldpass != NULL) {
208 strncpy(request.data.chauthtok.oldpass, oldpass,
209 sizeof(request.data.chauthtok.oldpass) - 1);
211 request.data.chauthtok.oldpass[0] = '\0';
214 if (newpass != NULL) {
215 strncpy(request.data.chauthtok.newpass, newpass,
216 sizeof(request.data.chauthtok.newpass) - 1);
218 request.data.chauthtok.newpass[0] = '\0';
221 return pam_winbind_request(WINBINDD_PAM_CHAUTHTOK, &request, &response);
225 * Checks if a user has an account
232 static int valid_user(const char *user)
234 if (getpwnam(user)) return 0;
238 static char *_pam_delete(register char *xx)
246 * obtain a password from the user
249 static int _winbind_read_password(pam_handle_t * pamh
262 * make sure nothing inappropriate gets returned
265 *pass = token = NULL;
268 * which authentication token are we getting?
271 authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
274 * should we obtain the password from a PAM item ?
277 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
278 retval = pam_get_item(pamh, authtok_flag, (const void **) &item);
279 if (retval != PAM_SUCCESS) {
282 "pam_get_item returned error to unix-read-password"
285 } else if (item != NULL) { /* we have a password! */
289 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
290 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
291 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
292 && off(WINBIND__OLD_PASSWORD, ctrl)) {
293 return PAM_AUTHTOK_RECOVER_ERR;
297 * getting here implies we will have to get the password from the
302 struct pam_message msg[3], *pmsg[3];
303 struct pam_response *resp;
306 /* prepare to converse */
308 if (comment != NULL) {
310 msg[0].msg_style = PAM_TEXT_INFO;
311 msg[0].msg = comment;
318 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
319 msg[i++].msg = prompt1;
322 if (prompt2 != NULL) {
324 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
325 msg[i++].msg = prompt2;
328 /* so call the conversation expecting i responses */
330 retval = converse(pamh, i, pmsg, &resp);
334 /* interpret the response */
336 if (retval == PAM_SUCCESS) { /* a good conversation */
338 token = x_strdup(resp[i - replies].resp);
342 /* verify that password entered correctly */
343 if (!resp[i - 1].resp
344 || strcmp(token, resp[i - 1].resp)) {
345 _pam_delete(token); /* mistyped */
346 retval = PAM_AUTHTOK_RECOVER_ERR;
347 _make_remark(pamh ,PAM_ERROR_MSG, MISTYPED_PASS);
352 ,"could not recover authentication token");
357 * tidy up the conversation (resp_retcode) is ignored
358 * -- what is it for anyway? AGM
361 _pam_drop_reply(resp, i);
364 retval = (retval == PAM_SUCCESS)
365 ? PAM_AUTHTOK_RECOVER_ERR : retval;
369 if (retval != PAM_SUCCESS) {
370 if (on(WINBIND_DEBUG_ARG, ctrl))
372 "unable to obtain a password");
375 /* 'token' is the entered password */
377 /* we store this password as an item */
379 retval = pam_set_item(pamh, authtok_flag, token);
380 _pam_delete(token); /* clean it up */
381 if (retval != PAM_SUCCESS
382 || (retval = pam_get_item(pamh, authtok_flag
383 ,(const void **) &item))
386 _pam_log(LOG_CRIT, "error manipulating password");
392 item = NULL; /* break link to password */
398 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
399 int argc, const char **argv)
401 const char *username;
402 const char *password;
403 int retval = PAM_AUTH_ERR;
405 /* parse arguments */
406 int ctrl = _pam_parse(argc, argv);
408 /* Get the username */
409 retval = pam_get_user(pamh, &username, NULL);
410 if ((retval != PAM_SUCCESS) || (!username)) {
411 if (ctrl & WINBIND_DEBUG_ARG)
412 _pam_log(LOG_DEBUG,"can not get the username");
413 return PAM_SERVICE_ERR;
416 retval = _winbind_read_password(pamh, ctrl, NULL,
420 if (retval != PAM_SUCCESS) {
421 _pam_log(LOG_ERR, "Could not retrieve user's password");
422 return PAM_AUTHTOK_ERR;
425 if (ctrl & WINBIND_DEBUG_ARG) {
427 /* Let's not give too much away in the log file */
429 #ifdef DEBUG_PASSWORD
430 _pam_log(LOG_INFO, "Verify user `%s' with password `%s'",
433 _pam_log(LOG_INFO, "Verify user `%s'", username);
437 /* Now use the username to look up password */
438 return winbind_auth_request(username, password, ctrl);
442 int pam_sm_setcred(pam_handle_t *pamh, int flags,
443 int argc, const char **argv)
449 * Account management. We want to verify that the account exists
450 * before returning PAM_SUCCESS
453 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
454 int argc, const char **argv)
456 const char *username;
457 int retval = PAM_USER_UNKNOWN;
459 /* parse arguments */
460 int ctrl = _pam_parse(argc, argv);
462 /* Get the username */
463 retval = pam_get_user(pamh, &username, NULL);
464 if ((retval != PAM_SUCCESS) || (!username)) {
465 if (ctrl & WINBIND_DEBUG_ARG)
466 _pam_log(LOG_DEBUG,"can not get the username");
467 return PAM_SERVICE_ERR;
470 /* Verify the username */
471 retval = valid_user(username);
474 /* some sort of system error. The log was already printed */
475 return PAM_SERVICE_ERR;
477 /* the user does not exist */
478 if (ctrl & WINBIND_DEBUG_ARG)
479 _pam_log(LOG_NOTICE, "user `%s' not found",
481 if (ctrl & WINBIND_UNKNOWN_OK_ARG)
483 return PAM_USER_UNKNOWN;
485 /* Otherwise, the authentication looked good */
486 _pam_log(LOG_NOTICE, "user '%s' granted acces", username);
489 /* we don't know anything about this return value */
490 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s'",
492 return PAM_SERVICE_ERR;
495 /* should not be reached */
499 int pam_sm_open_session(pam_handle_t *pamh, int flags,
500 int argc, const char **argv)
502 /* parse arguments */
503 int ctrl = _pam_parse(argc, argv);
504 if (ctrl & WINBIND_DEBUG_ARG)
505 _pam_log(LOG_DEBUG,"libpam_winbind:pam_sm_open_session handler");
509 int pam_sm_close_session(pam_handle_t *pamh, int flags,
510 int argc, const char **argv)
512 /* parse arguments */
513 int ctrl = _pam_parse(argc, argv);
514 if (ctrl & WINBIND_DEBUG_ARG)
515 _pam_log(LOG_DEBUG,"libpam_winbind:pam_sm_close_session handler");
521 PAM_EXTERN int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
522 int argc, const char **argv)
526 unsigned int ctrl = _pam_parse(argc, argv);
528 /* <DO NOT free() THESE> */
530 char *pass_old, *pass_new;
531 /* </DO NOT free() THESE> */
538 * First get the name of a user
540 retval = pam_get_user(pamh, &user, "Username: ");
541 if (retval == PAM_SUCCESS) {
543 _pam_log(LOG_ERR, "username was NULL!");
544 return PAM_USER_UNKNOWN;
546 if (retval == PAM_SUCCESS && on(WINBIND_DEBUG_ARG, ctrl))
547 _pam_log(LOG_DEBUG, "username [%s] obtained",
550 if (on(WINBIND_DEBUG_ARG, ctrl))
552 "password - could not identify user");
557 * obtain and verify the current password (OLDAUTHTOK) for
561 if (flags & PAM_PRELIM_CHECK) {
563 /* instruct user what is happening */
564 #define greeting "Changing password for "
565 Announce = (char *) malloc(sizeof(greeting) + strlen(user));
566 if (Announce == NULL) {
568 "password - out of memory");
571 (void) strcpy(Announce, greeting);
572 (void) strcpy(Announce + sizeof(greeting) - 1, user);
575 lctrl = ctrl | WINBIND__OLD_PASSWORD;
576 retval = _winbind_read_password(pamh, lctrl
578 ,"(current) NT password: "
580 ,(const char **) &pass_old);
583 if (retval != PAM_SUCCESS) {
585 ,"password - (old) token not obtained");
588 /* verify that this is the password for this user */
590 retval = winbind_auth_request(user, pass_old, ctrl);
592 if (retval != PAM_ACCT_EXPIRED
593 && retval != PAM_AUTHTOK_EXPIRED
594 && retval != PAM_NEW_AUTHTOK_REQD
595 && retval != PAM_SUCCESS) {
600 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
602 if (retval != PAM_SUCCESS) {
604 "failed to set PAM_OLDAUTHTOK");
606 } else if (flags & PAM_UPDATE_AUTHTOK) {
609 * obtain the proposed password
613 * get the old token back.
616 retval = pam_get_item(pamh, PAM_OLDAUTHTOK
617 ,(const void **) &pass_old);
619 if (retval != PAM_SUCCESS) {
620 _pam_log(LOG_NOTICE, "user not authenticated");
626 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
627 ctrl = WINBIND_USE_FIRST_PASS_ARG | lctrl;
630 retval = PAM_AUTHTOK_ERR;
631 while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
633 * use_authtok is to force the use of a previously entered
634 * password -- needed for pluggable password strength checking
637 retval = _winbind_read_password(pamh, lctrl
639 ,"Enter new NT password: "
640 ,"Retype new NT password: "
641 ,(const char **) &pass_new);
643 if (retval != PAM_SUCCESS) {
644 if (on(WINBIND_DEBUG_ARG, ctrl)) {
646 ,"password - new password not obtained");
648 pass_old = NULL;/* tidy up */
653 * At this point we know who the user is and what they
654 * propose as their new password. Verify that the new
655 * password is acceptable.
658 if (pass_new[0] == '\0') {/* "\0" password = NULL */
664 * By reaching here we have approved the passwords and must now
665 * rebuild the password database file.
668 retval = winbind_chauthtok_request(user, pass_old, pass_new);
669 _pam_overwrite(pass_new);
670 _pam_overwrite(pass_old);
671 pass_old = pass_new = NULL;
673 retval = PAM_SERVICE_ERR;
681 /* static module data */
683 struct pam_module _pam_winbind_modstruct = {
689 pam_sm_close_session,
696 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
697 * Copyright (c) Tim Potter <tpot@samba.org> 2000
698 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
699 * Copyright (c) Jan Rêkorajski 1999.
700 * Copyright (c) Andrew G. Morgan 1996-8.
701 * Copyright (c) Alex O. Yuriev, 1996.
702 * Copyright (c) Cristian Gafton 1996.
703 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
705 * Redistribution and use in source and binary forms, with or without
706 * modification, are permitted provided that the following conditions
708 * 1. Redistributions of source code must retain the above copyright
709 * notice, and the entire permission notice in its entirety,
710 * including the disclaimer of warranties.
711 * 2. Redistributions in binary form must reproduce the above copyright
712 * notice, this list of conditions and the following disclaimer in the
713 * documentation and/or other materials provided with the distribution.
714 * 3. The name of the author may not be used to endorse or promote
715 * products derived from this software without specific prior
716 * written permission.
718 * ALTERNATIVELY, this product may be distributed under the terms of
719 * the GNU Public License, in which case the provisions of the GPL are
720 * required INSTEAD OF the above restrictions. (This clause is
721 * necessary due to a potential bad interaction between the GPL and
722 * the restrictions contained in a BSD-style copyright.)
724 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
725 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
726 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
727 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
728 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
729 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
730 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
731 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
732 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
733 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
734 * OF THE POSSIBILITY OF SUCH DAMAGE.