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