r21143: Fix wrong check for pam error codes for getpwnam and lookup winbind
[kai/samba.git] / source3 / nsswitch / pam_winbind.c
1 /* pam_winbind module
2
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-2007
7
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)
11 */
12
13 #include "pam_winbind.h"
14
15 #define _PAM_LOG_FUNCTION_ENTER(function, pamh, ctrl, flags) \
16         do { \
17                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] ENTER: " function " (flags: 0x%04x)", (uint32) pamh, flags); \
18                 _pam_log_state(pamh, ctrl); \
19         } while (0)
20
21 #define _PAM_LOG_FUNCTION_LEAVE(function, pamh, ctrl, retval) \
22         do { \
23                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] LEAVE: " function " returning %d", (uint32) pamh, retval); \
24                 _pam_log_state(pamh, ctrl); \
25         } while (0)
26
27 /* data tokens */
28
29 #define MAX_PASSWD_TRIES        3
30
31 /*
32  * Work around the pam API that has functions with void ** as parameters.
33  * These lead to strict aliasing warnings with gcc.
34  */
35 static int _pam_get_item(const pam_handle_t *pamh, int item_type,
36                          const void *_item)
37 {
38         const void **item = (const void **)_item;
39         return pam_get_item(pamh, item_type, item);
40 }
41 static int _pam_get_data(const pam_handle_t *pamh,
42                          const char *module_data_name, const void *_data)
43 {
44         const void **data = (const void **)_data;
45         return pam_get_data(pamh, module_data_name, data);
46 }
47
48 /* some syslogging */
49
50 #ifdef HAVE_PAM_VSYSLOG
51 static void _pam_log_int(const pam_handle_t *pamh, int err, const char *format, va_list args)
52 {
53         pam_vsyslog(pamh, err, format, args);
54 }
55 #else
56 static void _pam_log_int(const pam_handle_t *pamh, int err, const char *format, va_list args)
57 {
58         char *format2 = NULL;
59         const char *service;
60
61         _pam_get_item(pamh, PAM_SERVICE, &service);
62
63         format2 = malloc(strlen(MODULE_NAME)+strlen(format)+strlen(service)+5);
64         if (format2 == NULL) {
65                 /* what else todo ? */
66                 vsyslog(err, format, args);
67                 return;
68         }
69
70         sprintf(format2, "%s(%s): %s", MODULE_NAME, service, format);
71         vsyslog(err, format2, args);
72         SAFE_FREE(format2);
73 }
74 #endif /* HAVE_PAM_VSYSLOG */
75
76 static BOOL _pam_log_is_silent(int ctrl)
77 {
78         return on(ctrl, WINBIND_SILENT);
79 }
80
81 static void _pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
82 {
83         va_list args;
84
85         if (_pam_log_is_silent(ctrl)) {
86                 return;
87         }
88
89         va_start(args, format);
90         _pam_log_int(pamh, err, format, args);
91         va_end(args);
92 }
93
94 static BOOL _pam_log_is_debug_enabled(int ctrl)
95 {
96         if (ctrl == -1) {
97                 return False;
98         }
99
100         if (_pam_log_is_silent(ctrl)) {
101                 return False;
102         }
103
104         if (!(ctrl & WINBIND_DEBUG_ARG)) {
105                 return False;
106         }
107
108         return True;
109 }
110
111 static BOOL _pam_log_is_debug_state_enabled(int ctrl)
112 {
113         if (!(ctrl & WINBIND_DEBUG_STATE)) {
114                 return False;
115         }
116
117         return _pam_log_is_debug_enabled(ctrl);
118 }
119
120 static void _pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
121 {
122         va_list args;
123
124         if (!_pam_log_is_debug_enabled(ctrl)) {
125                 return;
126         }
127
128         va_start(args, format);
129         _pam_log_int(pamh, err, format, args);
130         va_end(args);
131 }
132
133 static void _pam_log_state_datum(const pam_handle_t *pamh, int ctrl, int item_type, const char *key, int is_string)
134 {
135         const void *data = NULL;
136         if (item_type != 0) {
137                 pam_get_item(pamh, item_type, &data);
138         } else {
139                 pam_get_data(pamh, key, &data);
140         }
141         if (data != NULL) {
142                 const char *type = (item_type != 0) ? "ITEM" : "DATA";
143                 if (is_string != 0) {
144                         _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] STATE: %s(%s) = \"%s\" (0x%08x)", (uint32) pamh, type, key, (const char *) data, (uint32) data);
145                 } else {
146                         _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] STATE: %s(%s) = 0x%08x", (uint32) pamh, type, key, (uint32) data);
147                 }
148         }
149 }
150
151 #define _PAM_LOG_STATE_DATA_POINTER(pamh, ctrl, module_data_name) \
152         _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 0)
153
154 #define _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, module_data_name) \
155         _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 1)
156
157 #define _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, item_type) \
158         _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 0)
159
160 #define _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, item_type) \
161         _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 1)
162
163 #ifdef DEBUG_PASSWORD
164 #define _LOG_PASSWORD_AS_STRING 1
165 #else
166 #define _LOG_PASSWORD_AS_STRING 0
167 #endif
168
169 #define _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, item_type) \
170         _pam_log_state_datum(pamh, ctrl, item_type, #item_type, _LOG_PASSWORD_AS_STRING)
171
172 static void _pam_log_state(const pam_handle_t *pamh, int ctrl)
173 {
174         if (!_pam_log_is_debug_state_enabled(ctrl)) {
175                 return;
176         }
177
178         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_SERVICE);
179         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_USER);
180         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_TTY);
181         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_RHOST);
182         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_RUSER);
183         _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, PAM_OLDAUTHTOK);
184         _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, PAM_AUTHTOK);
185         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_USER_PROMPT);
186         _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_CONV);
187 #ifdef PAM_FAIL_DELAY
188         _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_FAIL_DELAY);
189 #endif
190 #ifdef PAM_REPOSITORY
191         _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_REPOSITORY);
192 #endif
193
194         _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_HOMEDIR);
195         _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_LOGONSCRIPT);
196         _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_PROFILEPATH);
197         _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_NEW_AUTHTOK_REQD); /* Use atoi to get PAM result code */
198         _PAM_LOG_STATE_DATA_POINTER(pamh, ctrl, PAM_WINBIND_PWD_LAST_SET);
199 }
200
201 static int _pam_parse(const pam_handle_t *pamh, int flags, int argc, const char **argv, dictionary **result_d)
202 {
203         int ctrl = 0;
204         const char *config_file = NULL;
205         int i;
206         const char **v;
207         dictionary *d = NULL;
208
209         if (flags & PAM_SILENT) {
210                 ctrl |= WINBIND_SILENT;
211         }
212
213         for (i=argc,v=argv; i-- > 0; ++v) {
214                 if (!strncasecmp(*v, "config", strlen("config"))) {
215                         ctrl |= WINBIND_CONFIG_FILE;
216                         config_file = v[i];
217                         break;
218                 }
219         }
220
221         if (config_file == NULL) {
222                 config_file = PAM_WINBIND_CONFIG_FILE;
223         }
224
225         d = iniparser_load(config_file);
226         if (d == NULL) {
227                 goto config_from_pam;
228         }
229
230         if (iniparser_getboolean(d, "global:debug", False)) {
231                 ctrl |= WINBIND_DEBUG_ARG;
232         }
233
234         if (iniparser_getboolean(d, "global:debug_state", False)) {
235                 ctrl |= WINBIND_DEBUG_STATE;
236         }
237
238         if (iniparser_getboolean(d, "global:cached_login", False)) {
239                 ctrl |= WINBIND_CACHED_LOGIN;
240         }
241
242         if (iniparser_getboolean(d, "global:krb5_auth", False)) {
243                 ctrl |= WINBIND_KRB5_AUTH;
244         }
245
246         if (iniparser_getboolean(d, "global:silent", False)) {
247                 ctrl |= WINBIND_SILENT;
248         }
249
250         if (iniparser_getstr(d, "global:krb5_ccache_type") != NULL) {
251                 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
252         }
253
254         if ((iniparser_getstr(d, "global:require-membership-of") != NULL) ||
255             (iniparser_getstr(d, "global:require_membership_of") != NULL)) {
256                 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
257         }
258
259         if (iniparser_getboolean(d, "global:try_first_pass", False)) {
260                 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
261         }
262
263 config_from_pam:
264         /* step through arguments */
265         for (i=argc,v=argv; i-- > 0; ++v) {
266
267                 /* generic options */
268                 if (!strcmp(*v,"debug"))
269                         ctrl |= WINBIND_DEBUG_ARG;
270                 else if (!strcasecmp(*v, "debug_state"))
271                         ctrl |= WINBIND_DEBUG_STATE;
272                 else if (!strcasecmp(*v, "use_authtok"))
273                         ctrl |= WINBIND_USE_AUTHTOK_ARG;
274                 else if (!strcasecmp(*v, "use_first_pass"))
275                         ctrl |= WINBIND_USE_FIRST_PASS_ARG;
276                 else if (!strcasecmp(*v, "try_first_pass"))
277                         ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
278                 else if (!strcasecmp(*v, "unknown_ok"))
279                         ctrl |= WINBIND_UNKNOWN_OK_ARG;
280                 else if (!strncasecmp(*v, "require_membership_of", strlen("require_membership_of")))
281                         ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
282                 else if (!strncasecmp(*v, "require-membership-of", strlen("require-membership-of")))
283                         ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
284                 else if (!strcasecmp(*v, "krb5_auth"))
285                         ctrl |= WINBIND_KRB5_AUTH;
286                 else if (!strncasecmp(*v, "krb5_ccache_type", strlen("krb5_ccache_type")))
287                         ctrl |= WINBIND_KRB5_CCACHE_TYPE;
288                 else if (!strcasecmp(*v, "cached_login"))
289                         ctrl |= WINBIND_CACHED_LOGIN;
290                 else {
291                         _pam_log(pamh, ctrl, LOG_ERR, "pam_parse: unknown option: %s", *v);
292                         return -1;
293                 }
294
295         }
296
297         if (result_d) {
298                 *result_d = d;
299         } else {
300                 if (d) {
301                         iniparser_freedict(d);
302                 }
303         }
304
305         return ctrl;
306 };
307
308 static void _pam_winbind_cleanup_func(pam_handle_t *pamh, void *data, int error_status)
309 {
310         int ctrl = _pam_parse(pamh, 0, 0, NULL, NULL);
311         if (_pam_log_is_debug_state_enabled(ctrl)) {
312                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] CLEAN: cleaning up PAM data 0x%08x (error_status = %d)", (uint32) pamh, (uint32) data, error_status);
313         }
314         SAFE_FREE(data);
315 }
316
317
318 static const struct ntstatus_errors {
319         const char *ntstatus_string;
320         const char *error_string;
321 } ntstatus_errors[] = {
322         {"NT_STATUS_OK", "Success"},
323         {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
324         {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND", "No domain controllers found"},
325         {"NT_STATUS_NO_LOGON_SERVERS", "No logon servers"},
326         {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
327         {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
328         {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
329         {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
330         {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
331         {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
332         {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
333         {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
334         {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
335         {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
336         {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
337         {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
338         {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
339         {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
340         {NULL, NULL}
341 };
342
343 const char *_get_ntstatus_error_string(const char *nt_status_string) 
344 {
345         int i;
346         for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
347                 if (!strcasecmp(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
348                         return ntstatus_errors[i].error_string;
349                 }
350         }
351         return NULL;
352 }
353
354 /* --- authentication management functions --- */
355
356 /* Attempt a conversation */
357
358 static int converse(pam_handle_t *pamh, int nargs,
359                     struct pam_message **message,
360                     struct pam_response **response)
361 {
362         int retval;
363         struct pam_conv *conv;
364
365         retval = _pam_get_item(pamh, PAM_CONV, &conv );
366         if (retval == PAM_SUCCESS) {
367                 retval = conv->conv(nargs, (const struct pam_message **)message,
368                                     response, conv->appdata_ptr);
369         }
370         
371         return retval; /* propagate error status */
372 }
373
374
375 static int _make_remark(pam_handle_t * pamh, int type, const char *text)
376 {
377         int retval = PAM_SUCCESS;
378
379         struct pam_message *pmsg[1], msg[1];
380         struct pam_response *resp;
381         
382         pmsg[0] = &msg[0];
383         msg[0].msg = CONST_DISCARD(char *, text);
384         msg[0].msg_style = type;
385         
386         resp = NULL;
387         retval = converse(pamh, 1, pmsg, &resp);
388         
389         if (resp) {
390                 _pam_drop_reply(resp, 1);
391         }
392         return retval;
393 }
394
395 static int _make_remark_v(pam_handle_t * pamh, int type, const char *format, va_list args)
396 {
397         char *var;
398         int ret;
399
400         ret = vasprintf(&var, format, args);
401         if (ret < 0) {
402                 _pam_log(pamh, 0, LOG_ERR, "memory allocation failure");
403                 return ret;
404         }
405
406         ret = _make_remark(pamh, type, var);
407         SAFE_FREE(var);
408         return ret;
409 }
410
411 static int _make_remark_format(pam_handle_t * pamh, int type, const char *format, ...)
412 {
413         int ret;
414         va_list args;
415
416         va_start(args, format);
417         ret = _make_remark_v(pamh, type, format, args);
418         va_end(args);
419         return ret;
420 }
421
422 static int pam_winbind_request(pam_handle_t * pamh, int ctrl,
423                                enum winbindd_cmd req_type,
424                                struct winbindd_request *request,
425                                struct winbindd_response *response)
426 {
427         /* Fill in request and send down pipe */
428         init_request(request, req_type);
429         
430         if (write_sock(request, sizeof(*request), 0) == -1) {
431                 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: write to socket failed!");
432                 close_sock();
433                 return PAM_SERVICE_ERR;
434         }
435         
436         /* Wait for reply */
437         if (read_reply(response) == -1) {
438                 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: read from socket failed!");
439                 close_sock();
440                 return PAM_SERVICE_ERR;
441         }
442
443         /* We are done with the socket - close it and avoid mischeif */
444         close_sock();
445
446         /* Copy reply data from socket */
447         if (response->result == WINBINDD_OK) {
448                 return PAM_SUCCESS;
449         }
450
451         /* no need to check for pam_error codes for getpwnam() */
452         switch (req_type) {
453
454                 case WINBINDD_GETPWNAM:
455                 case WINBINDD_LOOKUPNAME:
456                         _pam_log(pamh, ctrl, LOG_ERR, "request failed: %s, NT error was %s", 
457                                 response->data.auth.nt_status_string);
458                         return PAM_USER_UNKNOWN;
459                 default:
460                         break;
461         }
462
463         if (response->data.auth.pam_error != PAM_SUCCESS) {
464                 _pam_log(pamh, ctrl, LOG_ERR, "request failed: %s, PAM error was %s (%d), NT error was %s", 
465                          response->data.auth.error_string,
466                          pam_strerror(pamh, response->data.auth.pam_error),
467                          response->data.auth.pam_error,
468                          response->data.auth.nt_status_string);
469                 return response->data.auth.pam_error;
470         } 
471         
472         _pam_log(pamh, ctrl, LOG_ERR, "request failed, but PAM error 0!");
473
474         return PAM_SERVICE_ERR;
475 }
476
477 static int pam_winbind_request_log(pam_handle_t * pamh,
478                                    int ctrl,
479                                    enum winbindd_cmd req_type,
480                                    struct winbindd_request *request,
481                                    struct winbindd_response *response,
482                                    const char *user)
483 {
484         int retval;
485
486         retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
487
488         switch (retval) {
489         case PAM_AUTH_ERR:
490                 /* incorrect password */
491                 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' denied access (incorrect password or invalid membership)", user);
492                 return retval;
493         case PAM_ACCT_EXPIRED:
494                 /* account expired */
495                 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' account expired", user);
496                 return retval;
497         case PAM_AUTHTOK_EXPIRED:
498                 /* password expired */
499                 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' password expired", user);
500                 return retval;
501         case PAM_NEW_AUTHTOK_REQD:
502                 /* new password required */
503                 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' new password required", user);
504                 return retval;
505         case PAM_USER_UNKNOWN:
506                 /* the user does not exist */
507                 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", user);
508                 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
509                         return PAM_IGNORE;
510                 }        
511                 return retval;
512         case PAM_SUCCESS:
513                 if (req_type == WINBINDD_PAM_AUTH) {
514                         /* Otherwise, the authentication looked good */
515                         _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", user);
516                 } else if (req_type == WINBINDD_PAM_CHAUTHTOK) {
517                         /* Otherwise, the authentication looked good */
518                         _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' password changed", user);
519                 } else { 
520                         /* Otherwise, the authentication looked good */
521                         _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' OK", user);
522                 }
523         
524                 return retval;
525         default:
526                 /* we don't know anything about this return value */
527                 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (retval = %d, user = '%s')",
528                          retval, user);
529                 return retval;
530         }
531 }
532
533 static BOOL _pam_send_password_expiry_message(pam_handle_t *pamh, time_t next_change, time_t now) 
534 {
535         int days = 0;
536         struct tm tm_now, tm_next_change;
537
538         if ((next_change < 0) ||
539             (next_change < now) ||
540             (next_change > now + DAYS_TO_WARN_BEFORE_PWD_EXPIRES * SECONDS_PER_DAY)) {
541                 return False;
542         }
543
544         if ((localtime_r(&now, &tm_now) == NULL) || 
545             (localtime_r(&next_change, &tm_next_change) == NULL)) {
546                 return False;
547         }
548
549         days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) - (tm_now.tm_yday+tm_now.tm_year*365);
550
551         if (days == 0) {
552                 _make_remark(pamh, PAM_TEXT_INFO, "Your password expires today");
553                 return True;
554         } 
555         
556         if (days > 0 && days < DAYS_TO_WARN_BEFORE_PWD_EXPIRES) {
557                 _make_remark_format(pamh, PAM_TEXT_INFO, "Your password will expire in %d %s", 
558                         days, (days > 1) ? "days":"day");
559                 return True;
560         }
561
562         return False;
563 }
564
565 static void _pam_warn_password_expires_in_future(pam_handle_t *pamh, struct winbindd_response *response)
566 {
567         time_t now = time(NULL);
568         time_t next_change = 0;
569
570         /* accounts with ACB_PWNOEXP set never receive a warning */
571         if (response->data.auth.info3.acct_flags & ACB_PWNOEXP) {
572                 return;
573         }
574
575         /* no point in sending a warning if this is a grace logon */
576         if (PAM_WB_GRACE_LOGON(response->data.auth.info3.user_flgs)) {
577                 return;
578         }
579
580         /* check if the info3 must change timestamp has been set */
581         next_change = response->data.auth.info3.pass_must_change_time;
582
583         if (_pam_send_password_expiry_message(pamh, next_change, now)) {
584                 return;
585         }
586
587         /* now check for the global password policy */
588         if (response->data.auth.policy.expire <= 0) {
589                 return;
590         }
591
592         next_change = response->data.auth.info3.pass_last_set_time + 
593                       response->data.auth.policy.expire;
594
595         if (_pam_send_password_expiry_message(pamh, next_change, now)) {
596                 return;
597         }
598
599         /* no warning sent */
600 }
601
602 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
603
604 int safe_append_string(char *dest,
605                         const char *src,
606                         int dest_buffer_size)
607 /**
608  * Append a string, making sure not to overflow and to always return a NULL-terminated
609  * string.
610  *
611  * @param dest Destination string buffer (must already be NULL-terminated).
612  * @param src Source string buffer.
613  * @param dest_buffer_size Size of dest buffer in bytes.
614  *
615  * @return 0 if dest buffer is not big enough (no bytes copied), non-zero on success.
616  */
617 {
618         int dest_length = strlen(dest);
619         int src_length = strlen(src);
620
621         if ( dest_length + src_length + 1 > dest_buffer_size ) {
622                 return 0;
623         }
624
625         memcpy(dest + dest_length, src, src_length + 1);
626         return 1;
627 }
628
629 static int winbind_name_to_sid_string(pam_handle_t *pamh,
630                                 int ctrl,
631                                 const char *user,
632                                 const char *name,
633                                 char *sid_list_buffer,
634                                 int sid_list_buffer_size)
635 /**
636  * Convert a names into a SID string, appending it to a buffer.
637  *
638  * @param pamh PAM handle
639  * @param ctrl PAM winbind options.
640  * @param user User in PAM request.
641  * @param name Name to convert.
642  * @param sid_list_buffer Where to append the string sid.
643  * @param sid_list_buffer Size of sid_list_buffer (in bytes).
644  *
645  * @return 0 on failure, non-zero on success.
646  */
647 {
648         const char* sid_string;
649         struct winbindd_response sid_response;
650
651         /* lookup name? */ 
652         if (IS_SID_STRING(name)) {
653                 sid_string = name;
654         } else {
655                 struct winbindd_request sid_request;
656
657                 ZERO_STRUCT(sid_request);
658                 ZERO_STRUCT(sid_response);
659
660                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", name);
661
662                 /* fortunatly winbindd can handle non-separated names */
663                 strncpy(sid_request.data.name.name, name,
664                         sizeof(sid_request.data.name.name) - 1);
665
666                 if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME, &sid_request, &sid_response, user)) {
667                         _pam_log(pamh, ctrl, LOG_INFO, "could not lookup name: %s\n", name); 
668                         return 0;
669                 }
670
671                 sid_string = sid_response.data.sid.sid;
672         }
673
674         if (!safe_append_string(sid_list_buffer, sid_string, sid_list_buffer_size)) {
675                 return 0;
676         }
677
678         return 1;
679 }
680
681 static int winbind_name_list_to_sid_string_list(pam_handle_t *pamh,
682                                 int ctrl,
683                                 const char *user,
684                                 const char *name_list,
685                                 char *sid_list_buffer,
686                                 int sid_list_buffer_size)
687 /**
688  * Convert a list of names into a list of sids.
689  *
690  * @param pamh PAM handle
691  * @param ctrl PAM winbind options.
692  * @param user User in PAM request.
693  * @param name_list List of names or string sids, separated by commas.
694  * @param sid_list_buffer Where to put the list of string sids.
695  * @param sid_list_buffer Size of sid_list_buffer (in bytes).
696  *
697  * @return 0 on failure, non-zero on success.
698  */
699 {
700         int result = 0;
701         char *current_name = NULL;
702         const char *search_location;
703         const char *comma;
704
705         if ( sid_list_buffer_size > 0 ) {
706                 sid_list_buffer[0] = 0;
707         }
708
709         search_location = name_list;
710         while ( (comma = strstr(search_location, ",")) != NULL ) {
711                 current_name = strndup(search_location, comma - search_location);
712                 if (NULL == current_name) {
713                         goto out;
714                 }
715
716                 if (!winbind_name_to_sid_string(pamh, ctrl, user, current_name, sid_list_buffer, sid_list_buffer_size)) {
717                         goto out;
718                 }
719
720                 SAFE_FREE(current_name);
721
722                 if (!safe_append_string(sid_list_buffer, ",", sid_list_buffer_size)) {
723                         goto out;
724                 }
725
726                 search_location = comma + 1;
727         }
728
729         if (!winbind_name_to_sid_string(pamh, ctrl, user, search_location, sid_list_buffer, sid_list_buffer_size)) {
730                 goto out;
731         }
732
733         result = 1;
734
735 out:
736         SAFE_FREE(current_name);
737         return result;
738 }
739
740 /* talk to winbindd */
741 static int winbind_auth_request(pam_handle_t * pamh,
742                                 int ctrl, 
743                                 const char *user, 
744                                 const char *pass, 
745                                 const char *member, 
746                                 const char *cctype,
747                                 struct winbindd_response *p_response,
748                                 time_t *pwd_last_set,
749                                 char **user_ret)
750 {
751         struct winbindd_request request;
752         struct winbindd_response response;
753         int ret;
754
755         ZERO_STRUCT(request);
756         ZERO_STRUCT(response);
757
758         if (pwd_last_set) {
759                 *pwd_last_set = 0;
760         }
761
762         strncpy(request.data.auth.user, user, 
763                 sizeof(request.data.auth.user)-1);
764
765         strncpy(request.data.auth.pass, pass, 
766                 sizeof(request.data.auth.pass)-1);
767
768         request.data.auth.krb5_cc_type[0] = '\0';
769         request.data.auth.uid = -1;
770         
771         request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_CONTACT_TRUSTDOM;
772
773         if (ctrl & WINBIND_KRB5_AUTH) {
774
775                 struct passwd *pwd = NULL;
776
777                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling krb5 login flag\n"); 
778
779                 request.flags |= WBFLAG_PAM_KRB5 | WBFLAG_PAM_FALLBACK_AFTER_KRB5;
780
781                 pwd = getpwnam(user);
782                 if (pwd == NULL) {
783                         return PAM_USER_UNKNOWN;
784                 }
785                 request.data.auth.uid = pwd->pw_uid;
786         }
787
788         if (ctrl & WINBIND_CACHED_LOGIN) {
789                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling cached login flag\n"); 
790                 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
791         }
792
793         if (user_ret) {
794                 *user_ret = NULL;
795                 request.flags |= WBFLAG_PAM_UNIX_NAME;
796         }
797
798         if (cctype != NULL) {
799                 strncpy(request.data.auth.krb5_cc_type, cctype, 
800                         sizeof(request.data.auth.krb5_cc_type) - 1);
801                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling request for a %s krb5 ccache\n", cctype); 
802         }
803
804         request.data.auth.require_membership_of_sid[0] = '\0';
805
806         if (member != NULL) {
807
808                 if (!winbind_name_list_to_sid_string_list(pamh, ctrl, user, member,
809                         request.data.auth.require_membership_of_sid,
810                         sizeof(request.data.auth.require_membership_of_sid))) {
811
812                         _pam_log_debug(pamh, ctrl, LOG_ERR, "failed to serialize membership of sid \"%s\"\n", member);
813                         return PAM_AUTH_ERR;
814                 }
815         }
816
817         ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
818
819         if (pwd_last_set) {
820                 *pwd_last_set = response.data.auth.info3.pass_last_set_time;
821         }
822
823         if ((ctrl & WINBIND_KRB5_AUTH) && 
824             response.data.auth.krb5ccname[0] != '\0') {
825
826                 char var[PATH_MAX];
827
828                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "request returned KRB5CCNAME: %s", 
829                                response.data.auth.krb5ccname);
830         
831                 snprintf(var, sizeof(var), "KRB5CCNAME=%s", response.data.auth.krb5ccname);
832         
833                 ret = pam_putenv(pamh, var);
834                 if (ret != PAM_SUCCESS) {
835                         _pam_log(pamh, ctrl, LOG_ERR, "failed to set KRB5CCNAME to %s", var);
836                         return ret;
837                 }
838         }
839
840         if (p_response) {
841                 /* We want to process the response in the caller. */
842                 *p_response = response;
843                 return ret;
844         }
845
846         if (ret) {
847                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_EXPIRED");
848                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_MUST_CHANGE");
849                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_WORKSTATION");
850                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_LOGON_HOURS");
851                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_EXPIRED");
852                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_DISABLED");
853                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_LOCKED_OUT");
854                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
855                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
856                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
857                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
858                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NO_LOGON_SERVERS");
859         }
860
861         /* handle the case where the auth was ok, but the password must expire right now */
862         /* good catch from Ralf Haferkamp: an expiry of "never" is translated to -1 */
863         if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
864              ! (PAM_WB_GRACE_LOGON(response.data.auth.info3.user_flgs)) &&
865             (response.data.auth.policy.expire > 0) && 
866             (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire < time(NULL))) {
867
868                 ret = PAM_AUTHTOK_EXPIRED;
869
870                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"Password has expired (Password was last set: %d, "
871                                "the policy says it should expire here %d (now it's: %d)\n",
872                                response.data.auth.info3.pass_last_set_time,
873                                response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire,
874                                time(NULL));
875
876                 PAM_WB_REMARK_DIRECT_RET(pamh, "NT_STATUS_PASSWORD_EXPIRED");
877
878         }
879
880         /* warn a user if the password is about to expire soon */
881         _pam_warn_password_expires_in_future(pamh, &response);
882
883         /* inform about logon type */
884         if (PAM_WB_GRACE_LOGON(response.data.auth.info3.user_flgs)) {
885
886                 _make_remark(pamh, PAM_ERROR_MSG, 
887                         "Grace login. Please change your password as soon you're online again");
888                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
889                         "User %s logged on using grace logon\n", user);
890
891         } else if (PAM_WB_CACHED_LOGON(response.data.auth.info3.user_flgs)) {
892
893                 _make_remark(pamh, PAM_ERROR_MSG, 
894                         "Logging on using cached account. Network resources can be unavailable");
895                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
896                         "User %s logged on using cached account\n", user);
897         }
898
899         /* save the CIFS homedir for pam_cifs / pam_mount */
900         if (response.data.auth.info3.home_dir[0] != '\0') {
901
902                 int ret2 = pam_set_data(pamh, PAM_WINBIND_HOMEDIR,
903                                         (void *) strdup(response.data.auth.info3.home_dir),
904                                         _pam_winbind_cleanup_func);
905                 if (ret2) {
906                         _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s", 
907                                        pam_strerror(pamh, ret2));
908                 }
909
910         }
911
912         /* save the logon script path for other PAM modules */
913         if (response.data.auth.info3.logon_script[0] != '\0') {
914
915                 int ret2 = pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT, 
916                                         (void *) strdup(response.data.auth.info3.logon_script), 
917                                         _pam_winbind_cleanup_func);
918                 if (ret2) {
919                         _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s", 
920                                        pam_strerror(pamh, ret2));
921                 }
922         }
923
924         /* save the profile path for other PAM modules */
925         if (response.data.auth.info3.profile_path[0] != '\0') {
926
927                 int ret2 = pam_set_data(pamh, PAM_WINBIND_PROFILEPATH, 
928                                         (void *) strdup(response.data.auth.info3.profile_path), 
929                                         _pam_winbind_cleanup_func);
930                 if (ret2) {
931                         _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s", 
932                                        pam_strerror(pamh, ret2));
933                 }
934         }
935
936         /* If winbindd returned a username, return the pointer to it here. */
937         if (user_ret && response.extra_data.data) {
938                 /* We have to trust it's a null terminated string. */
939                 *user_ret = (char *)response.extra_data.data;
940         }
941
942         return ret;
943 }
944
945 /* talk to winbindd */
946 static int winbind_chauthtok_request(pam_handle_t * pamh,
947                                      int ctrl,
948                                      const char *user, 
949                                      const char *oldpass,
950                                      const char *newpass,
951                                      time_t pwd_last_set) 
952 {
953         struct winbindd_request request;
954         struct winbindd_response response;
955         int ret;
956
957         ZERO_STRUCT(request);
958         ZERO_STRUCT(response);
959
960         if (request.data.chauthtok.user == NULL) return -2;
961
962         strncpy(request.data.chauthtok.user, user, 
963                 sizeof(request.data.chauthtok.user) - 1);
964
965         if (oldpass != NULL) {
966                 strncpy(request.data.chauthtok.oldpass, oldpass, 
967                         sizeof(request.data.chauthtok.oldpass) - 1);
968         } else {
969                 request.data.chauthtok.oldpass[0] = '\0';
970         }
971         
972         if (newpass != NULL) {
973                 strncpy(request.data.chauthtok.newpass, newpass, 
974                         sizeof(request.data.chauthtok.newpass) - 1);
975         } else {
976                 request.data.chauthtok.newpass[0] = '\0';
977         }
978
979         if (ctrl & WINBIND_KRB5_AUTH) {
980                 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
981         }
982
983         ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
984
985         if (ret == PAM_SUCCESS) {
986                 return ret;
987         }
988
989         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_BACKUP_CONTROLLER");
990         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
991         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NO_LOGON_SERVERS");
992         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCESS_DENIED");
993
994         /* TODO: tell the min pwd length ? */
995         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_SHORT");
996
997         /* TODO: tell the minage ? */
998         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_RECENT");
999
1000         /* TODO: tell the history length ? */
1001         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
1002
1003         if (!strcasecmp(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
1004
1005                 /* FIXME: avoid to send multiple PAM messages after another */
1006                 switch (response.data.auth.reject_reason) {
1007                         case -1:
1008                                 break;
1009                         case REJECT_REASON_OTHER:
1010                                 if ((response.data.auth.policy.min_passwordage > 0) &&
1011                                     (pwd_last_set + response.data.auth.policy.min_passwordage > time(NULL))) {
1012                                         PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_RECENT");
1013                                 }
1014                                 break;
1015                         case REJECT_REASON_TOO_SHORT:
1016                                 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_SHORT");
1017                                 break;
1018                         case REJECT_REASON_IN_HISTORY:
1019                                 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_HISTORY_CONFLICT");
1020                                 break;
1021                         case REJECT_REASON_NOT_COMPLEX:
1022                                 _make_remark(pamh, PAM_ERROR_MSG, "Password does not meet complexity requirements");
1023                                 break;
1024                         default:
1025                                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1026                                                "unknown password change reject reason: %d", 
1027                                                response.data.auth.reject_reason);
1028                                 break;
1029                 }
1030
1031                 _make_remark_format(pamh, PAM_ERROR_MSG,  
1032                         "Your password must be at least %d characters; "
1033                         "cannot repeat any of the your previous %d passwords"
1034                         "%s. "
1035                         "Please type a different password. "
1036                         "Type a password which meets these requirements in both text boxes.",
1037                         response.data.auth.policy.min_length_password,
1038                         response.data.auth.policy.password_history,
1039                         (response.data.auth.policy.password_properties & DOMAIN_PASSWORD_COMPLEX) ? 
1040                                 "; must contain capitals, numerals or punctuation; and cannot contain your account or full name" : 
1041                                 "");
1042
1043         }
1044
1045         return ret;
1046 }
1047
1048 /*
1049  * Checks if a user has an account
1050  *
1051  * return values:
1052  *       1  = User not found
1053  *       0  = OK
1054  *      -1  = System error
1055  */
1056 static int valid_user(pam_handle_t *pamh, int ctrl, const char *user)
1057 {
1058         /* check not only if the user is available over NSS calls, also make
1059          * sure it's really a winbind user, this is important when stacking PAM
1060          * modules in the 'account' or 'password' facility. */
1061
1062         struct passwd *pwd = NULL;
1063         struct winbindd_request request;
1064         struct winbindd_response response;
1065         int ret;
1066
1067         ZERO_STRUCT(request);
1068         ZERO_STRUCT(response);
1069
1070         pwd = getpwnam(user);
1071         if (pwd == NULL) {
1072                 return 1;
1073         }
1074
1075         strncpy(request.data.username, user,
1076                 sizeof(request.data.username) - 1);
1077
1078         ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_GETPWNAM, &request, &response, user);
1079
1080         switch (ret) {
1081                 case PAM_USER_UNKNOWN:
1082                         return 1;
1083                 case PAM_SUCCESS:
1084                         return 0;
1085                 default:
1086                         break;
1087         }
1088         return -1;
1089 }
1090
1091 static char *_pam_delete(register char *xx)
1092 {
1093         _pam_overwrite(xx);
1094         _pam_drop(xx);
1095         return NULL;
1096 }
1097
1098 /*
1099  * obtain a password from the user
1100  */
1101
1102 static int _winbind_read_password(pam_handle_t * pamh,
1103                                   unsigned int ctrl,
1104                                   const char *comment,
1105                                   const char *prompt1,
1106                                   const char *prompt2,
1107                                   const char **pass)
1108 {
1109         int authtok_flag;
1110         int retval;
1111         const char *item;
1112         char *token;
1113
1114         _pam_log(pamh, ctrl, LOG_DEBUG, "getting password (0x%08x)", ctrl);
1115
1116         /*
1117          * make sure nothing inappropriate gets returned
1118          */
1119
1120         *pass = token = NULL;
1121
1122         /*
1123          * which authentication token are we getting?
1124          */
1125
1126         authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
1127
1128         /*
1129          * should we obtain the password from a PAM item ?
1130          */
1131
1132         if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1133                 retval = _pam_get_item(pamh, authtok_flag, &item);
1134                 if (retval != PAM_SUCCESS) {
1135                         /* very strange. */
1136                         _pam_log(pamh, ctrl, LOG_ALERT, 
1137                                  "pam_get_item returned error to unix-read-password"
1138                             );
1139                         return retval;
1140                 } else if (item != NULL) {      /* we have a password! */
1141                         *pass = item;
1142                         item = NULL;
1143                         _pam_log(pamh, ctrl, LOG_DEBUG, 
1144                                  "pam_get_item returned a password");
1145                         return PAM_SUCCESS;
1146                 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1147                         return PAM_AUTHTOK_RECOVER_ERR;         /* didn't work */
1148                 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
1149                            && off(WINBIND__OLD_PASSWORD, ctrl)) {
1150                         return PAM_AUTHTOK_RECOVER_ERR;
1151                 }
1152         }
1153         /*
1154          * getting here implies we will have to get the password from the
1155          * user directly.
1156          */
1157
1158         {
1159                 struct pam_message msg[3], *pmsg[3];
1160                 struct pam_response *resp;
1161                 int i, replies;
1162
1163                 /* prepare to converse */
1164
1165                 if (comment != NULL) {
1166                         pmsg[0] = &msg[0];
1167                         msg[0].msg_style = PAM_TEXT_INFO;
1168                         msg[0].msg = CONST_DISCARD(char *, comment);
1169                         i = 1;
1170                 } else {
1171                         i = 0;
1172                 }
1173
1174                 pmsg[i] = &msg[i];
1175                 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1176                 msg[i++].msg = CONST_DISCARD(char *, prompt1);
1177                 replies = 1;
1178
1179                 if (prompt2 != NULL) {
1180                         pmsg[i] = &msg[i];
1181                         msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1182                         msg[i++].msg = CONST_DISCARD(char *, prompt2);
1183                         ++replies;
1184                 }
1185                 /* so call the conversation expecting i responses */
1186                 resp = NULL;
1187                 retval = converse(pamh, i, pmsg, &resp);
1188
1189                 if (resp != NULL) {
1190
1191                         /* interpret the response */
1192
1193                         if (retval == PAM_SUCCESS) {    /* a good conversation */
1194
1195                                 token = x_strdup(resp[i - replies].resp);
1196                                 if (token != NULL) {
1197                                         if (replies == 2) {
1198                                                 /* verify that password entered correctly */
1199                                                 if (!resp[i - 1].resp
1200                                                     || strcmp(token, resp[i - 1].resp)) {
1201                                                         _pam_delete(token);     /* mistyped */
1202                                                         retval = PAM_AUTHTOK_RECOVER_ERR;
1203                                                         _make_remark(pamh, PAM_ERROR_MSG, MISTYPED_PASS);
1204                                                 }
1205                                         }
1206                                 } else {
1207                                         _pam_log(pamh, ctrl, LOG_NOTICE, "could not recover authentication token");
1208                                         retval = PAM_AUTHTOK_RECOVER_ERR;
1209                                 }
1210
1211                         }
1212                         /*
1213                          * tidy up the conversation (resp_retcode) is ignored
1214                          * -- what is it for anyway? AGM
1215                          */
1216
1217                         _pam_drop_reply(resp, i);
1218
1219                 } else {
1220                         retval = (retval == PAM_SUCCESS)
1221                             ? PAM_AUTHTOK_RECOVER_ERR : retval;
1222                 }
1223         }
1224
1225         if (retval != PAM_SUCCESS) {
1226                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1227                                  "unable to obtain a password");
1228                 return retval;
1229         }
1230         /* 'token' is the entered password */
1231
1232         /* we store this password as an item */
1233         
1234         retval = pam_set_item(pamh, authtok_flag, token);
1235         _pam_delete(token);     /* clean it up */
1236         if (retval != PAM_SUCCESS || 
1237             (retval = _pam_get_item(pamh, authtok_flag, &item)) != PAM_SUCCESS) {
1238                 
1239                 _pam_log(pamh, ctrl, LOG_CRIT, "error manipulating password");
1240                 return retval;
1241                 
1242         }
1243
1244         *pass = item;
1245         item = NULL;            /* break link to password */
1246
1247         return PAM_SUCCESS;
1248 }
1249
1250 const char *get_conf_item_string(const pam_handle_t *pamh,
1251                                  int argc, 
1252                                  const char **argv, 
1253                                  int ctrl,
1254                                  dictionary *d,
1255                                  const char *item, 
1256                                  int config_flag)
1257 {
1258         int i = 0;
1259         const char *parm_opt = NULL;
1260         char *key = NULL;
1261
1262         if (!(ctrl & config_flag)) {
1263                 goto out;
1264         }
1265
1266         /* let the pam opt take precedence over the pam_winbind.conf option */
1267
1268         if (d != NULL) {
1269
1270                 if (!asprintf(&key, "global:%s", item)) {
1271                         goto out;
1272                 }
1273
1274                 parm_opt = iniparser_getstr(d, key);
1275                 SAFE_FREE(key);
1276         }
1277
1278         for ( i=0; i<argc; i++ ) {
1279
1280                 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
1281                         char *p;
1282
1283                         if ( (p = strchr( argv[i], '=' )) == NULL) {
1284                                 _pam_log(pamh, ctrl, LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
1285                                 goto out;
1286                         }
1287                         _pam_log_debug(pamh, ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
1288                         return p + 1;
1289                 }
1290         }
1291
1292         if (d != NULL) {
1293                 _pam_log_debug(pamh, ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
1294         }
1295 out:
1296         return parm_opt;
1297 }
1298
1299 const char *get_krb5_cc_type_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1300 {
1301         return get_conf_item_string(pamh, argc, argv, ctrl, d, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
1302 }
1303
1304 const char *get_member_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1305 {
1306         const char *ret = NULL;
1307         ret = get_conf_item_string(pamh, argc, argv, ctrl, d, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
1308         if (ret) {
1309                 return ret;
1310         }
1311         return get_conf_item_string(pamh, argc, argv, ctrl, d, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
1312 }
1313
1314 PAM_EXTERN
1315 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
1316                         int argc, const char **argv)
1317 {
1318         const char *username;
1319         const char *password;
1320         const char *member = NULL;
1321         const char *cctype = NULL;
1322         int retval = PAM_AUTH_ERR;
1323         dictionary *d = NULL;
1324         char *username_ret = NULL;
1325         char *new_authtok_required = NULL;
1326
1327         /* parse arguments */
1328         int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1329         if (ctrl == -1) {
1330                 retval = PAM_SYSTEM_ERR;
1331                 goto out;
1332         }
1333
1334         _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", pamh, ctrl, flags);
1335
1336         /* Get the username */
1337         retval = pam_get_user(pamh, &username, NULL);
1338         if ((retval != PAM_SUCCESS) || (!username)) {
1339                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "can not get the username");
1340                 retval = PAM_SERVICE_ERR;
1341                 goto out;
1342         }
1343
1344         retval = _winbind_read_password(pamh, ctrl, NULL, 
1345                                         "Password: ", NULL,
1346                                         &password);
1347
1348         if (retval != PAM_SUCCESS) {
1349                 _pam_log(pamh, ctrl, LOG_ERR, "Could not retrieve user's password");
1350                 retval = PAM_AUTHTOK_ERR;
1351                 goto out;
1352         }
1353
1354         /* Let's not give too much away in the log file */
1355
1356 #ifdef DEBUG_PASSWORD
1357         _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s' with password '%s'", 
1358                        username, password);
1359 #else
1360         _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s'", username);
1361 #endif
1362
1363         member = get_member_from_config(pamh, argc, argv, ctrl, d);
1364
1365         cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1366
1367         /* Now use the username to look up password */
1368         retval = winbind_auth_request(pamh, ctrl, username, password, member,
1369                                       cctype, NULL, NULL, &username_ret);
1370
1371         if (retval == PAM_NEW_AUTHTOK_REQD ||
1372             retval == PAM_AUTHTOK_EXPIRED) {
1373
1374                 if (!asprintf(&new_authtok_required, "%d", retval)) {
1375                         retval = PAM_BUF_ERR;
1376                         goto out;
1377                 }
1378
1379                 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, new_authtok_required, _pam_winbind_cleanup_func);
1380
1381                 retval = PAM_SUCCESS;
1382                 goto out;
1383         }
1384
1385 out:
1386         if (username_ret) {
1387                 pam_set_item (pamh, PAM_USER, username_ret);
1388                 _pam_log_debug(pamh, ctrl, LOG_INFO, "Returned user was '%s'", username_ret);
1389                 free(username_ret);
1390         }
1391
1392         if (d) {
1393                 iniparser_freedict(d);
1394         }
1395
1396         if (!new_authtok_required) {
1397                 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
1398         }
1399
1400         _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", pamh, ctrl, retval);
1401
1402         return retval;
1403 }
1404
1405 PAM_EXTERN
1406 int pam_sm_setcred(pam_handle_t *pamh, int flags,
1407                    int argc, const char **argv)
1408 {
1409         int ret = PAM_SYSTEM_ERR;
1410         dictionary *d = NULL;
1411
1412         /* parse arguments */
1413         int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1414         if (ctrl == -1) {
1415                 ret = PAM_SYSTEM_ERR;
1416                 goto out;
1417         }
1418
1419         _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", pamh, ctrl, flags);
1420
1421         switch (flags & ~PAM_SILENT) {
1422
1423                 case PAM_DELETE_CRED:
1424                         ret = pam_sm_close_session(pamh, flags, argc, argv);
1425                         break;
1426                 case PAM_REFRESH_CRED:
1427                         _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_REFRESH_CRED not implemented");
1428                         ret = PAM_SUCCESS;
1429                         break;
1430                 case PAM_REINITIALIZE_CRED:
1431                         _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_REINITIALIZE_CRED not implemented");
1432                         ret = PAM_SUCCESS;
1433                         break;
1434                 case PAM_ESTABLISH_CRED:
1435                         _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_ESTABLISH_CRED not implemented");
1436                         ret = PAM_SUCCESS;
1437                         break;
1438                 default:
1439                         ret = PAM_SYSTEM_ERR;
1440                         break;
1441         }
1442
1443  out:
1444         if (d) {
1445                 iniparser_freedict(d);
1446         }
1447
1448         _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", pamh, ctrl, ret);
1449         
1450         return ret;
1451 }
1452
1453 /*
1454  * Account management. We want to verify that the account exists 
1455  * before returning PAM_SUCCESS
1456  */
1457 PAM_EXTERN
1458 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
1459                    int argc, const char **argv)
1460 {
1461         const char *username;
1462         int ret = PAM_USER_UNKNOWN;
1463         void *tmp = NULL;
1464         dictionary *d = NULL;
1465
1466         /* parse arguments */
1467         int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1468         if (ctrl == -1) {
1469                 return PAM_SYSTEM_ERR;
1470         }
1471
1472         _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", pamh, ctrl, flags);
1473
1474
1475         /* Get the username */
1476         ret = pam_get_user(pamh, &username, NULL);
1477         if ((ret != PAM_SUCCESS) || (!username)) {
1478                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"can not get the username");
1479                 ret = PAM_SERVICE_ERR;
1480                 goto out;
1481         }
1482
1483         /* Verify the username */
1484         ret = valid_user(pamh, ctrl, username);
1485         switch (ret) {
1486         case -1:
1487                 /* some sort of system error. The log was already printed */
1488                 ret = PAM_SERVICE_ERR;
1489                 goto out;
1490         case 1:
1491                 /* the user does not exist */
1492                 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", username);
1493                 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
1494                         ret = PAM_IGNORE;
1495                         goto out;
1496                 }
1497                 ret = PAM_USER_UNKNOWN;
1498                 goto out;
1499         case 0:
1500                 pam_get_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (const void **)&tmp);
1501                 if (tmp != NULL) {
1502                         ret = atoi((const char *)tmp);
1503                         switch (ret) {
1504                         case PAM_AUTHTOK_EXPIRED:
1505                                 /* fall through, since new token is required in this case */
1506                         case PAM_NEW_AUTHTOK_REQD:
1507                                 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success but %s is set", 
1508                                          PAM_WINBIND_NEW_AUTHTOK_REQD);
1509                                 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' needs new password", username);
1510                                 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
1511                                 ret = PAM_NEW_AUTHTOK_REQD;
1512                                 goto out;
1513                         default:
1514                                 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success");
1515                                 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1516                                 ret = PAM_SUCCESS;
1517                                 goto out;
1518                         }
1519                 }
1520
1521                 /* Otherwise, the authentication looked good */
1522                 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1523                 ret = PAM_SUCCESS;
1524                 goto out;
1525         default:
1526                 /* we don't know anything about this return value */
1527                 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (ret = %d, user = '%s')", 
1528                          ret, username);
1529                 ret = PAM_SERVICE_ERR;
1530                 goto out;
1531         }
1532
1533         /* should not be reached */
1534         ret = PAM_IGNORE;
1535
1536  out:
1537
1538         if (d) {
1539                 iniparser_freedict(d);
1540         }
1541
1542         _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", pamh, ctrl, ret);
1543         
1544         return ret;
1545 }
1546
1547 PAM_EXTERN
1548 int pam_sm_open_session(pam_handle_t *pamh, int flags,
1549                         int argc, const char **argv)
1550 {
1551         int ret = PAM_SYSTEM_ERR;
1552         dictionary *d = NULL;
1553
1554         /* parse arguments */
1555         int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1556         if (ctrl == -1) {
1557                 ret = PAM_SYSTEM_ERR;
1558                 goto out;
1559         }
1560
1561         _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", pamh, ctrl, flags);
1562
1563         ret = PAM_SUCCESS;
1564
1565  out:
1566         if (d) {
1567                 iniparser_freedict(d);
1568         }
1569
1570         _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", pamh, ctrl, ret);
1571         
1572         return ret;
1573 }
1574
1575 PAM_EXTERN
1576 int pam_sm_close_session(pam_handle_t *pamh, int flags,
1577                          int argc, const char **argv)
1578 {
1579         dictionary *d = NULL;
1580         int retval = PAM_SUCCESS;
1581
1582         /* parse arguments */
1583         int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1584         if (ctrl == -1) {
1585                 retval = PAM_SYSTEM_ERR;
1586                 goto out;
1587         }
1588
1589         _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", pamh, ctrl, flags);
1590
1591         if (!(flags & PAM_DELETE_CRED)) {
1592                 retval = PAM_SUCCESS;
1593                 goto out;
1594         }
1595
1596         if (ctrl & WINBIND_KRB5_AUTH) {
1597
1598                 /* destroy the ccache here */
1599                 struct winbindd_request request;
1600                 struct winbindd_response response;
1601                 const char *user;
1602                 const char *ccname = NULL;
1603                 struct passwd *pwd = NULL;
1604
1605                 ZERO_STRUCT(request);
1606                 ZERO_STRUCT(response);
1607
1608                 retval = pam_get_user(pamh, &user, "Username: ");
1609                 if (retval) {
1610                         _pam_log(pamh, ctrl, LOG_ERR, "could not identify user");
1611                         goto out;
1612                 }
1613
1614                 if (user == NULL) {
1615                         _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1616                         retval = PAM_USER_UNKNOWN;
1617                         goto out;
1618                 }
1619
1620                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
1621
1622                 ccname = pam_getenv(pamh, "KRB5CCNAME");
1623                 if (ccname == NULL) {
1624                         _pam_log_debug(pamh, ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1625                 }
1626
1627                 strncpy(request.data.logoff.user, user,
1628                         sizeof(request.data.logoff.user) - 1);
1629
1630                 if (ccname) {
1631                         strncpy(request.data.logoff.krb5ccname, ccname,
1632                                 sizeof(request.data.logoff.krb5ccname) - 1);
1633                 }
1634
1635                 pwd = getpwnam(user);
1636                 if (pwd == NULL) {
1637                         retval = PAM_USER_UNKNOWN;
1638                         goto out;
1639                 }
1640                 request.data.logoff.uid = pwd->pw_uid;
1641
1642                 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1643
1644                 retval = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1645         }
1646
1647 out:
1648         if (d) {
1649                 iniparser_freedict(d);
1650         }
1651
1652         _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", pamh, ctrl, retval);
1653         
1654         return retval;
1655 }
1656
1657
1658
1659 PAM_EXTERN 
1660 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1661                      int argc, const char **argv)
1662 {
1663         unsigned int lctrl;
1664         int ret;
1665         unsigned int ctrl;
1666
1667         /* <DO NOT free() THESE> */
1668         const char *user;
1669         char *pass_old, *pass_new;
1670         /* </DO NOT free() THESE> */
1671
1672         char *Announce;
1673         
1674         int retry = 0;
1675         dictionary *d = NULL;
1676
1677         ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1678         if (ctrl == -1) {
1679                 ret = PAM_SYSTEM_ERR;
1680                 goto out;
1681         }
1682
1683         _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", pamh, ctrl, flags);
1684
1685         /* clearing offline bit for the auth in the password change */
1686         ctrl &= ~WINBIND_CACHED_LOGIN;
1687
1688         /*
1689          * First get the name of a user
1690          */
1691         ret = pam_get_user(pamh, &user, "Username: ");
1692         if (ret) {
1693                 _pam_log(pamh, ctrl, LOG_ERR,
1694                          "password - could not identify user");
1695                 goto out;
1696         }
1697
1698         if (user == NULL) {
1699                 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1700                 ret = PAM_USER_UNKNOWN;
1701                 goto out;
1702         }
1703
1704         _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
1705
1706         /* check if this is really a user in winbindd, not only in NSS */
1707         ret = valid_user(pamh, ctrl, user);
1708         switch (ret) {
1709                 case 1:
1710                         ret = PAM_USER_UNKNOWN;
1711                         goto out;
1712                 case -1:
1713                         ret = PAM_SYSTEM_ERR;
1714                         goto out;
1715                 default:
1716                         break;
1717         }
1718                 
1719         /*
1720          * obtain and verify the current password (OLDAUTHTOK) for
1721          * the user.
1722          */
1723
1724         if (flags & PAM_PRELIM_CHECK) {
1725                 struct winbindd_response response;
1726                 time_t pwdlastset_prelim = 0;
1727                 
1728                 /* instruct user what is happening */
1729 #define greeting "Changing password for "
1730                 Announce = (char *) malloc(sizeof(greeting) + strlen(user));
1731                 if (Announce == NULL) {
1732                         _pam_log(pamh, ctrl, LOG_CRIT, "password - out of memory");
1733                         ret = PAM_BUF_ERR;
1734                         goto out;
1735                 }
1736                 (void) strcpy(Announce, greeting);
1737                 (void) strcpy(Announce + sizeof(greeting) - 1, user);
1738 #undef greeting
1739                 
1740                 lctrl = ctrl | WINBIND__OLD_PASSWORD;
1741                 ret = _winbind_read_password(pamh, lctrl,
1742                                                 Announce,
1743                                                 "(current) NT password: ",
1744                                                 NULL,
1745                                                 (const char **) &pass_old);
1746                 if (ret != PAM_SUCCESS) {
1747                         _pam_log(pamh, ctrl, LOG_NOTICE, "password - (old) token not obtained");
1748                         goto out;
1749                 }
1750
1751                 /* We don't need krb5 env set for password change test. */
1752                 ctrl &= ~WINBIND_KRB5_AUTH;
1753
1754                 /* verify that this is the password for this user */
1755                 
1756                 ret = winbind_auth_request(pamh, ctrl, user, pass_old,
1757                                         NULL, NULL, &response, &pwdlastset_prelim, NULL);
1758
1759                 if (ret != PAM_ACCT_EXPIRED && 
1760                     ret != PAM_AUTHTOK_EXPIRED &&
1761                     ret != PAM_NEW_AUTHTOK_REQD &&
1762                     ret != PAM_SUCCESS) {
1763                         pass_old = NULL;
1764                         if (d) {
1765                                 iniparser_freedict(d);
1766                         }
1767                         /* Deal with offline errors. */
1768                         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1769                                                 response,
1770                                                 "NT_STATUS_NO_LOGON_SERVERS");
1771                         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1772                                                 response,
1773                                                 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1774                         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1775                                                 response,
1776                                                 "NT_STATUS_ACCESS_DENIED");
1777                         return ret;
1778                 }
1779                 
1780                 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET, (void *)pwdlastset_prelim, NULL);
1781
1782                 ret = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
1783                 pass_old = NULL;
1784                 if (ret != PAM_SUCCESS) {
1785                         _pam_log(pamh, ctrl, LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
1786                 }
1787         } else if (flags & PAM_UPDATE_AUTHTOK) {
1788         
1789                 time_t pwdlastset_update = 0;
1790                 
1791                 /*
1792                  * obtain the proposed password
1793                  */
1794                 
1795                 /*
1796                  * get the old token back. 
1797                  */
1798                 
1799                 ret = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
1800                 
1801                 if (ret != PAM_SUCCESS) {
1802                         _pam_log(pamh, ctrl, LOG_NOTICE, "user not authenticated");
1803                         goto out;
1804                 }
1805                 
1806                 lctrl = ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
1807                 
1808                 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
1809                         lctrl |= WINBIND_USE_FIRST_PASS_ARG;
1810                 }
1811                 retry = 0;
1812                 ret = PAM_AUTHTOK_ERR;
1813                 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
1814                         /*
1815                          * use_authtok is to force the use of a previously entered
1816                          * password -- needed for pluggable password strength checking
1817                          */
1818                         
1819                         ret = _winbind_read_password(pamh, lctrl,
1820                                                         NULL,
1821                                                         "Enter new NT password: ",
1822                                                         "Retype new NT password: ",
1823                                                         (const char **) &pass_new);
1824                         
1825                         if (ret != PAM_SUCCESS) {
1826                                 _pam_log_debug(pamh, ctrl, LOG_ALERT
1827                                          ,"password - new password not obtained");
1828                                 pass_old = NULL;/* tidy up */
1829                                 goto out;
1830                         }
1831
1832                         /*
1833                          * At this point we know who the user is and what they
1834                          * propose as their new password. Verify that the new
1835                          * password is acceptable.
1836                          */
1837                         
1838                         if (pass_new[0] == '\0') {/* "\0" password = NULL */
1839                                 pass_new = NULL;
1840                         }
1841                 }
1842                 
1843                 /*
1844                  * By reaching here we have approved the passwords and must now
1845                  * rebuild the password database file.
1846                  */
1847                 _pam_get_data( pamh, PAM_WINBIND_PWD_LAST_SET,
1848                                &pwdlastset_update);
1849
1850                 ret = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new, pwdlastset_update);
1851                 if (ret) {
1852                         _pam_overwrite(pass_new);
1853                         _pam_overwrite(pass_old);
1854                         pass_old = pass_new = NULL;
1855                         goto out;
1856                 }
1857
1858                 /* just in case we need krb5 creds after a password change over msrpc */
1859
1860                 if (ctrl & WINBIND_KRB5_AUTH) {
1861                         struct winbindd_response response;
1862
1863                         const char *member = get_member_from_config(pamh, argc, argv, ctrl, d);
1864                         const char *cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1865
1866                         ret = winbind_auth_request(pamh, ctrl, user, pass_new,
1867                                                         member, cctype, &response, NULL, NULL);
1868                         _pam_overwrite(pass_new);
1869                         _pam_overwrite(pass_old);
1870                         pass_old = pass_new = NULL;
1871                         if (d) {
1872                                 iniparser_freedict(d);
1873                         }
1874                         /* Deal with offline errors. */
1875                         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1876                                                 response,
1877                                                 "NT_STATUS_NO_LOGON_SERVERS");
1878                         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1879                                                 response,
1880                                                 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1881                         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1882                                                 response,
1883                                                 "NT_STATUS_ACCESS_DENIED");
1884                         return ret;
1885                 }
1886         } else {
1887                 ret = PAM_SERVICE_ERR;
1888         }
1889
1890 out:
1891         if (d) {
1892                 iniparser_freedict(d);
1893         }
1894
1895         _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", pamh, ctrl, ret);
1896         
1897         return ret;
1898 }
1899
1900 #ifdef PAM_STATIC
1901
1902 /* static module data */
1903
1904 struct pam_module _pam_winbind_modstruct = {
1905         MODULE_NAME,
1906         pam_sm_authenticate,
1907         pam_sm_setcred,
1908         pam_sm_acct_mgmt,
1909         pam_sm_open_session,
1910         pam_sm_close_session,
1911         pam_sm_chauthtok
1912 };
1913
1914 #endif
1915
1916 /*
1917  * Copyright (c) Andrew Tridgell  <tridge@samba.org>   2000
1918  * Copyright (c) Tim Potter       <tpot@samba.org>     2000
1919  * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1920  * Copyright (c) Guenther Deschner <gd@samba.org>      2005-2007
1921  * Copyright (c) Jan Rêkorajski 1999.
1922  * Copyright (c) Andrew G. Morgan 1996-8.
1923  * Copyright (c) Alex O. Yuriev, 1996.
1924  * Copyright (c) Cristian Gafton 1996.
1925  * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software. 
1926  *
1927  * Redistribution and use in source and binary forms, with or without
1928  * modification, are permitted provided that the following conditions
1929  * are met:
1930  * 1. Redistributions of source code must retain the above copyright
1931  *    notice, and the entire permission notice in its entirety,
1932  *    including the disclaimer of warranties.
1933  * 2. Redistributions in binary form must reproduce the above copyright
1934  *    notice, this list of conditions and the following disclaimer in the
1935  *    documentation and/or other materials provided with the distribution.
1936  * 3. The name of the author may not be used to endorse or promote
1937  *    products derived from this software without specific prior
1938  *    written permission.
1939  *
1940  * ALTERNATIVELY, this product may be distributed under the terms of
1941  * the GNU Public License, in which case the provisions of the GPL are
1942  * required INSTEAD OF the above restrictions.  (This clause is
1943  * necessary due to a potential bad interaction between the GPL and
1944  * the restrictions contained in a BSD-style copyright.)
1945  *
1946  * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
1947  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1948  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1949  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1950  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1951  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1952  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1953  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1954  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1955  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1956  * OF THE POSSIBILITY OF SUCH DAMAGE.
1957  */