sync'ing up for 3.0alpha20 release
[Samba.git] / source / nsswitch / pam_winbind.c
blobf95caefb4cd409fd63ba20666ff5f7ca06939f64
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
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"
14 /* data tokens */
16 #define MAX_PASSWD_TRIES 3
18 /* some syslogging */
19 static void _pam_log(int err, const char *format, ...)
21 va_list args;
23 va_start(args, format);
24 openlog(MODULE_NAME, LOG_CONS|LOG_PID, LOG_AUTH);
25 vsyslog(err, format, args);
26 va_end(args);
27 closelog();
30 static int _pam_parse(int argc, const char **argv)
32 int ctrl;
33 /* step through arguments */
34 for (ctrl = 0; argc-- > 0; ++argv) {
36 /* generic options */
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;
48 else {
49 _pam_log(LOG_ERR, "pam_parse: unknown option; %s", *argv);
53 return ctrl;
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)
64 int retval;
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;
84 pmsg[0] = &msg[0];
85 msg[0].msg = text;
86 msg[0].msg_style = type;
88 resp = NULL;
89 retval = converse(pamh, 1, pmsg, &resp);
91 if (resp) {
92 _pam_drop_reply(resp, 1);
94 return retval;
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!");
107 close_sock();
108 return PAM_SERVICE_ERR;
111 /* Wait for reply */
112 if (read_reply(response) == -1) {
113 _pam_log(LOG_ERR, "read from socket failed!");
114 close_sock();
115 return PAM_SERVICE_ERR;
118 /* We are done with the socket - close it and avoid mischeif */
119 close_sock();
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;
128 } else {
129 _pam_log(LOG_ERR, "request failed, but PAM error 0!");
130 return PAM_SERVICE_ERR;
134 return PAM_SUCCESS;
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;
142 int retval;
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);
154 switch (retval) {
155 case PAM_AUTH_ERR:
156 /* incorrect password */
157 _pam_log(LOG_WARNING, "user `%s' denied access (incorrect password)", user);
158 return retval;
159 case PAM_ACCT_EXPIRED:
160 /* account expired */
161 _pam_log(LOG_WARNING, "user `%s' account expired", user);
162 return retval;
163 case PAM_AUTHTOK_EXPIRED:
164 /* password expired */
165 _pam_log(LOG_WARNING, "user `%s' password expired", user);
166 return retval;
167 case PAM_NEW_AUTHTOK_REQD:
168 /* password expired */
169 _pam_log(LOG_WARNING, "user `%s' new password required", user);
170 return retval;
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",
175 user);
176 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
177 return PAM_IGNORE;
179 return retval;
180 case PAM_SUCCESS:
181 /* Otherwise, the authentication looked good */
182 _pam_log(LOG_NOTICE, "user '%s' granted acces", user);
183 return retval;
184 default:
185 /* we don't know anything about this return value */
186 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s'",
187 retval, user);
188 return retval;
190 /* should not be reached */
193 /* talk to winbindd */
194 static int winbind_chauthtok_request(const char *user, const char *oldpass,
195 const char *newpass)
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);
210 } else {
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);
217 } else {
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
227 * return values:
228 * 1 = User not found
229 * 0 = OK
230 * -1 = System error
232 static int valid_user(const char *user)
234 if (getpwnam(user)) return 0;
235 return 1;
238 static char *_pam_delete(register char *xx)
240 _pam_overwrite(xx);
241 _pam_drop(xx);
242 return NULL;
246 * obtain a password from the user
249 static int _winbind_read_password(pam_handle_t * pamh
250 ,unsigned int ctrl
251 ,const char *comment
252 ,const char *prompt1
253 ,const char *prompt2
254 ,const char **pass)
256 int authtok_flag;
257 int retval;
258 const char *item;
259 char *token;
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) {
280 /* very strange. */
281 _pam_log(LOG_ALERT,
282 "pam_get_item returned error to unix-read-password"
284 return retval;
285 } else if (item != NULL) { /* we have a password! */
286 *pass = item;
287 item = NULL;
288 return PAM_SUCCESS;
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
298 * user directly.
302 struct pam_message msg[3], *pmsg[3];
303 struct pam_response *resp;
304 int i, replies;
306 /* prepare to converse */
308 if (comment != NULL) {
309 pmsg[0] = &msg[0];
310 msg[0].msg_style = PAM_TEXT_INFO;
311 msg[0].msg = comment;
312 i = 1;
313 } else {
314 i = 0;
317 pmsg[i] = &msg[i];
318 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
319 msg[i++].msg = prompt1;
320 replies = 1;
322 if (prompt2 != NULL) {
323 pmsg[i] = &msg[i];
324 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
325 msg[i++].msg = prompt2;
326 ++replies;
328 /* so call the conversation expecting i responses */
329 resp = NULL;
330 retval = converse(pamh, i, pmsg, &resp);
332 if (resp != NULL) {
334 /* interpret the response */
336 if (retval == PAM_SUCCESS) { /* a good conversation */
338 token = x_strdup(resp[i - replies].resp);
339 if (token != NULL) {
340 if (replies == 2) {
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);
350 } else {
351 _pam_log(LOG_NOTICE
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);
363 } else {
364 retval = (retval == PAM_SUCCESS)
365 ? PAM_AUTHTOK_RECOVER_ERR : retval;
369 if (retval != PAM_SUCCESS) {
370 if (on(WINBIND_DEBUG_ARG, ctrl))
371 _pam_log(LOG_DEBUG,
372 "unable to obtain a password");
373 return retval;
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))
384 != PAM_SUCCESS) {
386 _pam_log(LOG_CRIT, "error manipulating password");
387 return retval;
391 *pass = item;
392 item = NULL; /* break link to password */
394 return PAM_SUCCESS;
397 PAM_EXTERN
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,
417 "Password: ", NULL,
418 &password);
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'",
431 username, password);
432 #else
433 _pam_log(LOG_INFO, "Verify user `%s'", username);
434 #endif
437 /* Now use the username to look up password */
438 return winbind_auth_request(username, password, ctrl);
441 PAM_EXTERN
442 int pam_sm_setcred(pam_handle_t *pamh, int flags,
443 int argc, const char **argv)
445 return PAM_SUCCESS;
449 * Account management. We want to verify that the account exists
450 * before returning PAM_SUCCESS
452 PAM_EXTERN
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);
472 switch (retval) {
473 case -1:
474 /* some sort of system error. The log was already printed */
475 return PAM_SERVICE_ERR;
476 case 1:
477 /* the user does not exist */
478 if (ctrl & WINBIND_DEBUG_ARG)
479 _pam_log(LOG_NOTICE, "user `%s' not found",
480 username);
481 if (ctrl & WINBIND_UNKNOWN_OK_ARG)
482 return PAM_IGNORE;
483 return PAM_USER_UNKNOWN;
484 case 0:
485 /* Otherwise, the authentication looked good */
486 _pam_log(LOG_NOTICE, "user '%s' granted acces", username);
487 return PAM_SUCCESS;
488 default:
489 /* we don't know anything about this return value */
490 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s'",
491 retval, username);
492 return PAM_SERVICE_ERR;
495 /* should not be reached */
496 return PAM_IGNORE;
498 PAM_EXTERN
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");
506 return PAM_SUCCESS;
508 PAM_EXTERN
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");
516 return PAM_SUCCESS;
521 PAM_EXTERN int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
522 int argc, const char **argv)
524 unsigned int lctrl;
525 int retval;
526 unsigned int ctrl = _pam_parse(argc, argv);
528 /* <DO NOT free() THESE> */
529 const char *user;
530 char *pass_old, *pass_new;
531 /* </DO NOT free() THESE> */
533 char *Announce;
535 int retry = 0;
538 * First get the name of a user
540 retval = pam_get_user(pamh, &user, "Username: ");
541 if (retval == PAM_SUCCESS) {
542 if (user == NULL) {
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",
548 user);
549 } else {
550 if (on(WINBIND_DEBUG_ARG, ctrl))
551 _pam_log(LOG_DEBUG,
552 "password - could not identify user");
553 return retval;
557 * obtain and verify the current password (OLDAUTHTOK) for
558 * the user.
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) {
567 _pam_log(LOG_CRIT,
568 "password - out of memory");
569 return PAM_BUF_ERR;
571 (void) strcpy(Announce, greeting);
572 (void) strcpy(Announce + sizeof(greeting) - 1, user);
573 #undef greeting
575 lctrl = ctrl | WINBIND__OLD_PASSWORD;
576 retval = _winbind_read_password(pamh, lctrl
577 ,Announce
578 ,"(current) NT password: "
579 ,NULL
580 ,(const char **) &pass_old);
581 free(Announce);
583 if (retval != PAM_SUCCESS) {
584 _pam_log(LOG_NOTICE
585 ,"password - (old) token not obtained");
586 return retval;
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) {
596 pass_old = NULL;
597 return retval;
600 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
601 pass_old = NULL;
602 if (retval != PAM_SUCCESS) {
603 _pam_log(LOG_CRIT,
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");
621 return retval;
624 lctrl = ctrl;
626 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
627 ctrl = WINBIND_USE_FIRST_PASS_ARG | lctrl;
629 retry = 0;
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
638 ,NULL
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)) {
645 _pam_log(LOG_ALERT
646 ,"password - new password not obtained");
648 pass_old = NULL;/* tidy up */
649 return retval;
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 */
659 pass_new = 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;
672 } else {
673 retval = PAM_SERVICE_ERR;
676 return retval;
679 #ifdef PAM_STATIC
681 /* static module data */
683 struct pam_module _pam_winbind_modstruct = {
684 MODULE_NAME,
685 pam_sm_authenticate,
686 pam_sm_setcred,
687 pam_sm_acct_mgmt,
688 pam_sm_open_session,
689 pam_sm_close_session,
690 pam_sm_chauthtok
693 #endif
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
707 * are met:
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.