Add _pam_winbind_init/free_context.
[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> also
9    contains large slabs of code from pam_unix by Elliot Lee
10    <sopwith@redhat.com> (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: %p] ENTER: " \
18                                function " (flags: 0x%04x)", pamh, flags); \
19                 _pam_log_state(pamh, ctrl); \
20         } while (0)
21
22 #define _PAM_LOG_FUNCTION_LEAVE(function, pamh, ctrl, retval) \
23         do { \
24                 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: %p] LEAVE: " \
25                                function " returning %d", pamh, retval); \
26                 _pam_log_state(pamh, ctrl); \
27         } while (0)
28
29 /* data tokens */
30
31 #define MAX_PASSWD_TRIES        3
32
33 /*
34  * Work around the pam API that has functions with void ** as parameters
35  * These lead to strict aliasing warnings with gcc.
36  */
37 static int _pam_get_item(const pam_handle_t *pamh,
38                          int item_type,
39                          const void *_item)
40 {
41         const void **item = (const void **)_item;
42         return pam_get_item(pamh, item_type, item);
43 }
44 static int _pam_get_data(const pam_handle_t *pamh,
45                          const char *module_data_name,
46                          const void *_data)
47 {
48         const void **data = (const void **)_data;
49         return pam_get_data(pamh, module_data_name, data);
50 }
51
52 /* some syslogging */
53
54 #ifdef HAVE_PAM_VSYSLOG
55 static void _pam_log_int(const pam_handle_t *pamh,
56                          int err,
57                          const char *format,
58                          va_list args)
59 {
60         pam_vsyslog(pamh, err, format, args);
61 }
62 #else
63 static void _pam_log_int(const pam_handle_t *pamh,
64                          int err,
65                          const char *format,
66                          va_list args)
67 {
68         char *format2 = NULL;
69         const char *service;
70
71         _pam_get_item(pamh, PAM_SERVICE, &service);
72
73         format2 = (char *)malloc(strlen(MODULE_NAME)+strlen(format)+strlen(service)+5);
74         if (format2 == NULL) {
75                 /* what else todo ? */
76                 vsyslog(err, format, args);
77                 return;
78         }
79
80         sprintf(format2, "%s(%s): %s", MODULE_NAME, service, format);
81         vsyslog(err, format2, args);
82         SAFE_FREE(format2);
83 }
84 #endif /* HAVE_PAM_VSYSLOG */
85
86 static bool _pam_log_is_silent(int ctrl)
87 {
88         return on(ctrl, WINBIND_SILENT);
89 }
90
91 static void _pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
92 static void _pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
93 {
94         va_list args;
95
96         if (_pam_log_is_silent(ctrl)) {
97                 return;
98         }
99
100         va_start(args, format);
101         _pam_log_int(pamh, err, format, args);
102         va_end(args);
103 }
104
105 static bool _pam_log_is_debug_enabled(int ctrl)
106 {
107         if (ctrl == -1) {
108                 return false;
109         }
110
111         if (_pam_log_is_silent(ctrl)) {
112                 return false;
113         }
114
115         if (!(ctrl & WINBIND_DEBUG_ARG)) {
116                 return false;
117         }
118
119         return true;
120 }
121
122 static bool _pam_log_is_debug_state_enabled(int ctrl)
123 {
124         if (!(ctrl & WINBIND_DEBUG_STATE)) {
125                 return false;
126         }
127
128         return _pam_log_is_debug_enabled(ctrl);
129 }
130
131 static void _pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
132 static void _pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
133 {
134         va_list args;
135
136         if (!_pam_log_is_debug_enabled(ctrl)) {
137                 return;
138         }
139
140         va_start(args, format);
141         _pam_log_int(pamh, err, format, args);
142         va_end(args);
143 }
144
145 static void _pam_log_state_datum(const pam_handle_t *pamh,
146                                  int ctrl,
147                                  int item_type,
148                                  const char *key,
149                                  int is_string)
150 {
151         const void *data = NULL;
152         if (item_type != 0) {
153                 pam_get_item(pamh, item_type, &data);
154         } else {
155                 pam_get_data(pamh, key, &data);
156         }
157         if (data != NULL) {
158                 const char *type = (item_type != 0) ? "ITEM" : "DATA";
159                 if (is_string != 0) {
160                         _pam_log_debug(pamh, ctrl, LOG_DEBUG,
161                                        "[pamh: %p] STATE: %s(%s) = \"%s\" (%p)",
162                                        pamh, type, key, (const char *)data,
163                                        data);
164                 } else {
165                         _pam_log_debug(pamh, ctrl, LOG_DEBUG,
166                                        "[pamh: %p] STATE: %s(%s) = %p",
167                                        pamh, type, key, data);
168                 }
169         }
170 }
171
172 #define _PAM_LOG_STATE_DATA_POINTER(pamh, ctrl, module_data_name) \
173         _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 0)
174
175 #define _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, module_data_name) \
176         _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 1)
177
178 #define _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, item_type) \
179         _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 0)
180
181 #define _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, item_type) \
182         _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 1)
183
184 #ifdef DEBUG_PASSWORD
185 #define _LOG_PASSWORD_AS_STRING 1
186 #else
187 #define _LOG_PASSWORD_AS_STRING 0
188 #endif
189
190 #define _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, item_type) \
191         _pam_log_state_datum(pamh, ctrl, item_type, #item_type, \
192                              _LOG_PASSWORD_AS_STRING)
193
194 static void _pam_log_state(const pam_handle_t *pamh, int ctrl)
195 {
196         if (!_pam_log_is_debug_state_enabled(ctrl)) {
197                 return;
198         }
199
200         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_SERVICE);
201         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_USER);
202         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_TTY);
203         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_RHOST);
204         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_RUSER);
205         _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, PAM_OLDAUTHTOK);
206         _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, PAM_AUTHTOK);
207         _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_USER_PROMPT);
208         _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_CONV);
209 #ifdef PAM_FAIL_DELAY
210         _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_FAIL_DELAY);
211 #endif
212 #ifdef PAM_REPOSITORY
213         _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_REPOSITORY);
214 #endif
215
216         _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_HOMEDIR);
217         _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_LOGONSCRIPT);
218         _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_LOGONSERVER);
219         _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_PROFILEPATH);
220         _PAM_LOG_STATE_DATA_STRING(pamh, ctrl,
221                                    PAM_WINBIND_NEW_AUTHTOK_REQD);
222                                    /* Use atoi to get PAM result code */
223         _PAM_LOG_STATE_DATA_STRING(pamh, ctrl,
224                                    PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH);
225         _PAM_LOG_STATE_DATA_POINTER(pamh, ctrl, PAM_WINBIND_PWD_LAST_SET);
226 }
227
228 static int _pam_parse(const pam_handle_t *pamh,
229                       int flags,
230                       int argc,
231                       const char **argv,
232                       dictionary **result_d)
233 {
234         int ctrl = 0;
235         const char *config_file = NULL;
236         int i;
237         const char **v;
238         dictionary *d = NULL;
239
240         if (flags & PAM_SILENT) {
241                 ctrl |= WINBIND_SILENT;
242         }
243
244         for (i=argc,v=argv; i-- > 0; ++v) {
245                 if (!strncasecmp(*v, "config", strlen("config"))) {
246                         ctrl |= WINBIND_CONFIG_FILE;
247                         config_file = v[i];
248                         break;
249                 }
250         }
251
252         if (config_file == NULL) {
253                 config_file = PAM_WINBIND_CONFIG_FILE;
254         }
255
256         d = iniparser_load(config_file);
257         if (d == NULL) {
258                 goto config_from_pam;
259         }
260
261         if (iniparser_getboolean(d, "global:debug", false)) {
262                 ctrl |= WINBIND_DEBUG_ARG;
263         }
264
265         if (iniparser_getboolean(d, "global:debug_state", false)) {
266                 ctrl |= WINBIND_DEBUG_STATE;
267         }
268
269         if (iniparser_getboolean(d, "global:cached_login", false)) {
270                 ctrl |= WINBIND_CACHED_LOGIN;
271         }
272
273         if (iniparser_getboolean(d, "global:krb5_auth", false)) {
274                 ctrl |= WINBIND_KRB5_AUTH;
275         }
276
277         if (iniparser_getboolean(d, "global:silent", false)) {
278                 ctrl |= WINBIND_SILENT;
279         }
280
281         if (iniparser_getstr(d, "global:krb5_ccache_type") != NULL) {
282                 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
283         }
284
285         if ((iniparser_getstr(d, "global:require-membership-of") != NULL) ||
286             (iniparser_getstr(d, "global:require_membership_of") != NULL)) {
287                 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
288         }
289
290         if (iniparser_getboolean(d, "global:try_first_pass", false)) {
291                 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
292         }
293
294         if (iniparser_getint(d, "global:warn_pwd_expire", 0)) {
295                 ctrl |= WINBIND_WARN_PWD_EXPIRE;
296         }
297
298 config_from_pam:
299         /* step through arguments */
300         for (i=argc,v=argv; i-- > 0; ++v) {
301
302                 /* generic options */
303                 if (!strcmp(*v,"debug"))
304                         ctrl |= WINBIND_DEBUG_ARG;
305                 else if (!strcasecmp(*v, "debug_state"))
306                         ctrl |= WINBIND_DEBUG_STATE;
307                 else if (!strcasecmp(*v, "silent"))
308                         ctrl |= WINBIND_SILENT;
309                 else if (!strcasecmp(*v, "use_authtok"))
310                         ctrl |= WINBIND_USE_AUTHTOK_ARG;
311                 else if (!strcasecmp(*v, "use_first_pass"))
312                         ctrl |= WINBIND_USE_FIRST_PASS_ARG;
313                 else if (!strcasecmp(*v, "try_first_pass"))
314                         ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
315                 else if (!strcasecmp(*v, "unknown_ok"))
316                         ctrl |= WINBIND_UNKNOWN_OK_ARG;
317                 else if (!strncasecmp(*v, "require_membership_of",
318                                       strlen("require_membership_of")))
319                         ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
320                 else if (!strncasecmp(*v, "require-membership-of",
321                                       strlen("require-membership-of")))
322                         ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
323                 else if (!strcasecmp(*v, "krb5_auth"))
324                         ctrl |= WINBIND_KRB5_AUTH;
325                 else if (!strncasecmp(*v, "krb5_ccache_type",
326                                       strlen("krb5_ccache_type")))
327                         ctrl |= WINBIND_KRB5_CCACHE_TYPE;
328                 else if (!strcasecmp(*v, "cached_login"))
329                         ctrl |= WINBIND_CACHED_LOGIN;
330                 else {
331                         _pam_log(pamh, ctrl, LOG_ERR,
332                                  "pam_parse: unknown option: %s", *v);
333                         return -1;
334                 }
335
336         }
337
338         if (result_d) {
339                 *result_d = d;
340         } else {
341                 if (d) {
342                         iniparser_freedict(d);
343                 }
344         }
345
346         return ctrl;
347 };
348
349 static void _pam_winbind_free_context(struct pwb_context *ctx)
350 {
351         if (ctx->dict) {
352                 iniparser_freedict(ctx->dict);
353         }
354
355         SAFE_FREE(ctx);
356 }
357
358 static int _pam_winbind_init_context(const pam_handle_t *pamh,
359                                      int flags,
360                                      int argc,
361                                      const char **argv,
362                                      struct pwb_context **ctx_p)
363 {
364         struct pwb_context *r = NULL;
365
366         r = (struct pwb_context *)malloc(sizeof(struct pwb_context));
367         if (!r) {
368                 return PAM_BUF_ERR;
369         }
370
371         ZERO_STRUCTP(r);
372
373         r->pamh = pamh;
374         r->flags = flags;
375         r->argc = argc;
376         r->argv = argv;
377         r->ctrl = _pam_parse(pamh, flags, argc, argv, &r->dict);
378         if (r->ctrl == -1) {
379                 _pam_winbind_free_context(r);
380                 return PAM_SYSTEM_ERR;
381         }
382
383         *ctx_p = r;
384
385         return PAM_SUCCESS;
386 }
387
388 static void _pam_winbind_cleanup_func(pam_handle_t *pamh,
389                                       void *data,
390                                       int error_status)
391 {
392         int ctrl = _pam_parse(pamh, 0, 0, NULL, NULL);
393         if (_pam_log_is_debug_state_enabled(ctrl)) {
394                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
395                                "[pamh: %p] CLEAN: cleaning up PAM data %p "
396                                "(error_status = %d)", pamh, data,
397                                error_status);
398         }
399         SAFE_FREE(data);
400 }
401
402
403 static const struct ntstatus_errors {
404         const char *ntstatus_string;
405         const char *error_string;
406 } ntstatus_errors[] = {
407         {"NT_STATUS_OK",
408                 "Success"},
409         {"NT_STATUS_BACKUP_CONTROLLER",
410                 "No primary Domain Controler available"},
411         {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
412                 "No domain controllers found"},
413         {"NT_STATUS_NO_LOGON_SERVERS",
414                 "No logon servers"},
415         {"NT_STATUS_PWD_TOO_SHORT",
416                 "Password too short"},
417         {"NT_STATUS_PWD_TOO_RECENT",
418                 "The password of this user is too recent to change"},
419         {"NT_STATUS_PWD_HISTORY_CONFLICT",
420                 "Password is already in password history"},
421         {"NT_STATUS_PASSWORD_EXPIRED",
422                 "Your password has expired"},
423         {"NT_STATUS_PASSWORD_MUST_CHANGE",
424                 "You need to change your password now"},
425         {"NT_STATUS_INVALID_WORKSTATION",
426                 "You are not allowed to logon from this workstation"},
427         {"NT_STATUS_INVALID_LOGON_HOURS",
428                 "You are not allowed to logon at this time"},
429         {"NT_STATUS_ACCOUNT_EXPIRED",
430                 "Your account has expired. "
431                 "Please contact your System administrator"}, /* SCNR */
432         {"NT_STATUS_ACCOUNT_DISABLED",
433                 "Your account is disabled. "
434                 "Please contact your System administrator"}, /* SCNR */
435         {"NT_STATUS_ACCOUNT_LOCKED_OUT",
436                 "Your account has been locked. "
437                 "Please contact your System administrator"}, /* SCNR */
438         {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
439                 "Invalid Trust Account"},
440         {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
441                 "Invalid Trust Account"},
442         {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
443                 "Invalid Trust Account"},
444         {"NT_STATUS_ACCESS_DENIED",
445                 "Access is denied"},
446         {NULL, NULL}
447 };
448
449 static const char *_get_ntstatus_error_string(const char *nt_status_string)
450 {
451         int i;
452         for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
453                 if (!strcasecmp(ntstatus_errors[i].ntstatus_string,
454                                 nt_status_string)) {
455                         return ntstatus_errors[i].error_string;
456                 }
457         }
458         return NULL;
459 }
460
461 /* --- authentication management functions --- */
462
463 /* Attempt a conversation */
464
465 static int converse(pam_handle_t *pamh,
466                     int nargs,
467                     struct pam_message **message,
468                     struct pam_response **response)
469 {
470         int retval;
471         struct pam_conv *conv;
472
473         retval = _pam_get_item(pamh, PAM_CONV, &conv);
474         if (retval == PAM_SUCCESS) {
475                 retval = conv->conv(nargs,
476                                     (const struct pam_message **)message,
477                                     response, conv->appdata_ptr);
478         }
479
480         return retval; /* propagate error status */
481 }
482
483
484 static int _make_remark(pam_handle_t * pamh,
485                         int flags,
486                         int type,
487                         const char *text)
488 {
489         int retval = PAM_SUCCESS;
490
491         struct pam_message *pmsg[1], msg[1];
492         struct pam_response *resp;
493
494         if (flags & WINBIND_SILENT) {
495                 return PAM_SUCCESS;
496         }
497
498         pmsg[0] = &msg[0];
499         msg[0].msg = discard_const_p(char, text);
500         msg[0].msg_style = type;
501
502         resp = NULL;
503         retval = converse(pamh, 1, pmsg, &resp);
504
505         if (resp) {
506                 _pam_drop_reply(resp, 1);
507         }
508         return retval;
509 }
510
511 static int _make_remark_v(pam_handle_t *pamh,
512                           int flags,
513                           int type,
514                           const char *format,
515                           va_list args)
516 {
517         char *var;
518         int ret;
519
520         ret = vasprintf(&var, format, args);
521         if (ret < 0) {
522                 _pam_log(pamh, 0, LOG_ERR, "memory allocation failure");
523                 return ret;
524         }
525
526         ret = _make_remark(pamh, flags, type, var);
527         SAFE_FREE(var);
528         return ret;
529 }
530
531 static int _make_remark_format(pam_handle_t * pamh, int flags, int type, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
532 static int _make_remark_format(pam_handle_t * pamh, int flags, int type, const char *format, ...)
533 {
534         int ret;
535         va_list args;
536
537         va_start(args, format);
538         ret = _make_remark_v(pamh, flags, type, format, args);
539         va_end(args);
540         return ret;
541 }
542
543 static int pam_winbind_request(pam_handle_t *pamh,
544                                int ctrl,
545                                enum winbindd_cmd req_type,
546                                struct winbindd_request *request,
547                                struct winbindd_response *response)
548 {
549         /* Fill in request and send down pipe */
550         winbindd_init_request(request, req_type);
551
552         if (winbind_write_sock(request, sizeof(*request), 0, 0) == -1) {
553                 _pam_log(pamh, ctrl, LOG_ERR,
554                          "pam_winbind_request: write to socket failed!");
555                 winbind_close_sock();
556                 return PAM_SERVICE_ERR;
557         }
558
559         /* Wait for reply */
560         if (winbindd_read_reply(response) == -1) {
561                 _pam_log(pamh, ctrl, LOG_ERR,
562                          "pam_winbind_request: read from socket failed!");
563                 winbind_close_sock();
564                 return PAM_SERVICE_ERR;
565         }
566
567         /* We are done with the socket - close it and avoid mischeif */
568         winbind_close_sock();
569
570         /* Copy reply data from socket */
571         if (response->result == WINBINDD_OK) {
572                 return PAM_SUCCESS;
573         }
574
575         /* no need to check for pam_error codes for getpwnam() */
576         switch (req_type) {
577
578                 case WINBINDD_GETPWNAM:
579                 case WINBINDD_LOOKUPNAME:
580                         if (strlen(response->data.auth.nt_status_string) > 0) {
581                                 _pam_log(pamh, ctrl, LOG_ERR,
582                                          "request failed, NT error was %s",
583                                          response->data.auth.nt_status_string);
584                         } else {
585                                 _pam_log(pamh, ctrl, LOG_ERR, "request failed");
586                         }
587                         return PAM_USER_UNKNOWN;
588                 default:
589                         break;
590         }
591
592         if (response->data.auth.pam_error != PAM_SUCCESS) {
593                 _pam_log(pamh, ctrl, LOG_ERR,
594                          "request failed: %s, "
595                          "PAM error was %s (%d), NT error was %s",
596                          response->data.auth.error_string,
597                          pam_strerror(pamh, response->data.auth.pam_error),
598                          response->data.auth.pam_error,
599                          response->data.auth.nt_status_string);
600                 return response->data.auth.pam_error;
601         }
602
603         _pam_log(pamh, ctrl, LOG_ERR, "request failed, but PAM error 0!");
604
605         return PAM_SERVICE_ERR;
606 }
607
608 static int pam_winbind_request_log(pam_handle_t *pamh,
609                                    int ctrl,
610                                    enum winbindd_cmd req_type,
611                                    struct winbindd_request *request,
612                                    struct winbindd_response *response,
613                                    const char *user)
614 {
615         int retval;
616
617         retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
618
619         switch (retval) {
620         case PAM_AUTH_ERR:
621                 /* incorrect password */
622                 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' denied access "
623                          "(incorrect password or invalid membership)", user);
624                 return retval;
625         case PAM_ACCT_EXPIRED:
626                 /* account expired */
627                 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' account expired",
628                          user);
629                 return retval;
630         case PAM_AUTHTOK_EXPIRED:
631                 /* password expired */
632                 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' password expired",
633                          user);
634                 return retval;
635         case PAM_NEW_AUTHTOK_REQD:
636                 /* new password required */
637                 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' new password "
638                          "required", user);
639                 return retval;
640         case PAM_USER_UNKNOWN:
641                 /* the user does not exist */
642                 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found",
643                                user);
644                 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
645                         return PAM_IGNORE;
646                 }
647                 return retval;
648         case PAM_SUCCESS:
649                 /* Otherwise, the authentication looked good */
650                 switch (req_type) {
651                         case WINBINDD_INFO:
652                                 break;
653                         case WINBINDD_PAM_AUTH:
654                                 _pam_log(pamh, ctrl, LOG_NOTICE,
655                                          "user '%s' granted access", user);
656                                 break;
657                         case WINBINDD_PAM_CHAUTHTOK:
658                                 _pam_log(pamh, ctrl, LOG_NOTICE,
659                                          "user '%s' password changed", user);
660                                 break;
661                         default:
662                                 _pam_log(pamh, ctrl, LOG_NOTICE,
663                                          "user '%s' OK", user);
664                                 break;
665                 }
666
667                 return retval;
668         default:
669                 /* we don't know anything about this return value */
670                 _pam_log(pamh, ctrl, LOG_ERR,
671                          "internal module error (retval = %d, user = '%s')",
672                          retval, user);
673                 return retval;
674         }
675 }
676
677 /**
678  * send a password expiry message if required
679  *
680  * @param pamh PAM handle
681  * @param ctrl PAM winbind options.
682  * @param next_change expected (calculated) next expiry date.
683  * @param already_expired pointer to a boolean to indicate if the password is
684  *        already expired.
685  *
686  * @return boolean Returns true if message has been sent, false if not.
687  */
688
689 static bool _pam_send_password_expiry_message(pam_handle_t *pamh,
690                                               int ctrl,
691                                               time_t next_change,
692                                               time_t now,
693                                               int warn_pwd_expire,
694                                               bool *already_expired)
695 {
696         int days = 0;
697         struct tm tm_now, tm_next_change;
698
699         if (already_expired) {
700                 *already_expired = false;
701         }
702
703         if (next_change <= now) {
704                 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PASSWORD_EXPIRED");
705                 if (already_expired) {
706                         *already_expired = true;
707                 }
708                 return true;
709         }
710
711         if ((next_change < 0) ||
712             (next_change > now + warn_pwd_expire * SECONDS_PER_DAY)) {
713                 return false;
714         }
715
716         if ((localtime_r(&now, &tm_now) == NULL) ||
717             (localtime_r(&next_change, &tm_next_change) == NULL)) {
718                 return false;
719         }
720
721         days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) -
722                (tm_now.tm_yday+tm_now.tm_year*365);
723
724         if (days == 0) {
725                 _make_remark(pamh, ctrl, PAM_TEXT_INFO,
726                              "Your password expires today");
727                 return true;
728         }
729
730         if (days > 0 && days < warn_pwd_expire) {
731                 _make_remark_format(pamh, ctrl, PAM_TEXT_INFO,
732                                     "Your password will expire in %d %s",
733                                     days, (days > 1) ? "days":"day");
734                 return true;
735         }
736
737         return false;
738 }
739
740 /**
741  * Send a warning if the password expires in the near future
742  *
743  * @param pamh PAM handle
744  * @param ctrl PAM winbind options.
745  * @param response The full authentication response structure.
746  * @param already_expired boolean, is the pwd already expired?
747  *
748  * @return void.
749  */
750
751 static void _pam_warn_password_expiry(pam_handle_t *pamh,
752                                       int flags,
753                                       const struct winbindd_response *response,
754                                       int warn_pwd_expire,
755                                       bool *already_expired)
756 {
757         time_t now = time(NULL);
758         time_t next_change = 0;
759
760         if (already_expired) {
761                 *already_expired = false;
762         }
763
764         /* accounts with ACB_PWNOEXP set never receive a warning */
765         if (response->data.auth.info3.acct_flags & ACB_PWNOEXP) {
766                 return;
767         }
768
769         /* no point in sending a warning if this is a grace logon */
770         if (PAM_WB_GRACE_LOGON(response->data.auth.info3.user_flgs)) {
771                 return;
772         }
773
774         /* check if the info3 must change timestamp has been set */
775         next_change = response->data.auth.info3.pass_must_change_time;
776
777         if (_pam_send_password_expiry_message(pamh, flags, next_change, now,
778                                               warn_pwd_expire,
779                                               already_expired)) {
780                 return;
781         }
782
783         /* now check for the global password policy */
784         /* good catch from Ralf Haferkamp: an expiry of "never" is translated
785          * to -1 */
786         if (response->data.auth.policy.expire <= 0) {
787                 return;
788         }
789
790         next_change = response->data.auth.info3.pass_last_set_time +
791                       response->data.auth.policy.expire;
792
793         if (_pam_send_password_expiry_message(pamh, flags, next_change, now,
794                                               warn_pwd_expire,
795                                               already_expired)) {
796                 return;
797         }
798
799         /* no warning sent */
800 }
801
802 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
803
804 /**
805  * Append a string, making sure not to overflow and to always return a
806  * NULL-terminated string.
807  *
808  * @param dest Destination string buffer (must already be NULL-terminated).
809  * @param src Source string buffer.
810  * @param dest_buffer_size Size of dest buffer in bytes.
811  *
812  * @return false if dest buffer is not big enough (no bytes copied), true on
813  * success.
814  */
815
816 static bool safe_append_string(char *dest,
817                                const char *src,
818                                int dest_buffer_size)
819 {
820         int dest_length = strlen(dest);
821         int src_length = strlen(src);
822
823         if (dest_length + src_length + 1 > dest_buffer_size) {
824                 return false;
825         }
826
827         memcpy(dest + dest_length, src, src_length + 1);
828         return true;
829 }
830
831 /**
832  * Convert a names into a SID string, appending it to a buffer.
833  *
834  * @param pamh PAM handle
835  * @param ctrl PAM winbind options.
836  * @param user User in PAM request.
837  * @param name Name to convert.
838  * @param sid_list_buffer Where to append the string sid.
839  * @param sid_list_buffer Size of sid_list_buffer (in bytes).
840  *
841  * @return false on failure, true on success.
842  */
843 static bool winbind_name_to_sid_string(pam_handle_t *pamh,
844                                        int ctrl,
845                                        const char *user,
846                                        const char *name,
847                                        char *sid_list_buffer,
848                                        int sid_list_buffer_size)
849 {
850         const char* sid_string;
851         struct winbindd_response sid_response;
852
853         /* lookup name? */
854         if (IS_SID_STRING(name)) {
855                 sid_string = name;
856         } else {
857                 struct winbindd_request sid_request;
858
859                 ZERO_STRUCT(sid_request);
860                 ZERO_STRUCT(sid_response);
861
862                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
863                                "no sid given, looking up: %s\n", name);
864
865                 /* fortunatly winbindd can handle non-separated names */
866                 strncpy(sid_request.data.name.name, name,
867                         sizeof(sid_request.data.name.name) - 1);
868
869                 if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME,
870                                             &sid_request, &sid_response,
871                                             user)) {
872                         _pam_log(pamh, ctrl, LOG_INFO,
873                                  "could not lookup name: %s\n", name);
874                         return false;
875                 }
876
877                 sid_string = sid_response.data.sid.sid;
878         }
879
880         if (!safe_append_string(sid_list_buffer, sid_string,
881                                 sid_list_buffer_size)) {
882                 return false;
883         }
884
885         return true;
886 }
887
888 /**
889  * Convert a list of names into a list of sids.
890  *
891  * @param pamh PAM handle
892  * @param ctrl PAM winbind options.
893  * @param user User in PAM request.
894  * @param name_list List of names or string sids, separated by commas.
895  * @param sid_list_buffer Where to put the list of string sids.
896  * @param sid_list_buffer Size of sid_list_buffer (in bytes).
897  *
898  * @return false on failure, true on success.
899  */
900 static bool winbind_name_list_to_sid_string_list(pam_handle_t *pamh,
901                                                  int ctrl,
902                                                  const char *user,
903                                                  const char *name_list,
904                                                  char *sid_list_buffer,
905                                                  int sid_list_buffer_size)
906 {
907         bool result = false;
908         char *current_name = NULL;
909         const char *search_location;
910         const char *comma;
911
912         if (sid_list_buffer_size > 0) {
913                 sid_list_buffer[0] = 0;
914         }
915
916         search_location = name_list;
917         while ((comma = strstr(search_location, ",")) != NULL) {
918                 current_name = strndup(search_location,
919                                        comma - search_location);
920                 if (NULL == current_name) {
921                         goto out;
922                 }
923
924                 if (!winbind_name_to_sid_string(pamh, ctrl, user,
925                                                 current_name,
926                                                 sid_list_buffer,
927                                                 sid_list_buffer_size)) {
928                         goto out;
929                 }
930
931                 SAFE_FREE(current_name);
932
933                 if (!safe_append_string(sid_list_buffer, ",",
934                                         sid_list_buffer_size)) {
935                         goto out;
936                 }
937
938                 search_location = comma + 1;
939         }
940
941         if (!winbind_name_to_sid_string(pamh, ctrl, user, search_location,
942                                         sid_list_buffer,
943                                         sid_list_buffer_size)) {
944                 goto out;
945         }
946
947         result = true;
948
949 out:
950         SAFE_FREE(current_name);
951         return result;
952 }
953
954 /**
955  * put krb5ccname variable into environment
956  *
957  * @param pamh PAM handle
958  * @param ctrl PAM winbind options.
959  * @param krb5ccname env variable retrieved from winbindd.
960  *
961  * @return void.
962  */
963
964 static void _pam_setup_krb5_env(pam_handle_t *pamh,
965                                 int ctrl,
966                                 const char *krb5ccname)
967 {
968         char var[PATH_MAX];
969         int ret;
970
971         if (off(ctrl, WINBIND_KRB5_AUTH)) {
972                 return;
973         }
974
975         if (!krb5ccname || (strlen(krb5ccname) == 0)) {
976                 return;
977         }
978
979         _pam_log_debug(pamh, ctrl, LOG_DEBUG,
980                        "request returned KRB5CCNAME: %s", krb5ccname);
981
982         if (snprintf(var, sizeof(var), "KRB5CCNAME=%s", krb5ccname) == -1) {
983                 return;
984         }
985
986         ret = pam_putenv(pamh, var);
987         if (ret) {
988                 _pam_log(pamh, ctrl, LOG_ERR,
989                          "failed to set KRB5CCNAME to %s: %s",
990                          var, pam_strerror(pamh, ret));
991         }
992 }
993
994 /**
995  * Set string into the PAM stack.
996  *
997  * @param pamh PAM handle
998  * @param ctrl PAM winbind options.
999  * @param data_name Key name for pam_set_data.
1000  * @param value String value.
1001  *
1002  * @return void.
1003  */
1004
1005 static void _pam_set_data_string(pam_handle_t *pamh,
1006                                  int ctrl,
1007                                  const char *data_name,
1008                                  const char *value)
1009 {
1010         int ret;
1011
1012         if (!data_name || !value || (strlen(data_name) == 0) ||
1013              (strlen(value) == 0)) {
1014                 return;
1015         }
1016
1017         ret = pam_set_data(pamh, data_name, (void *)strdup(value),
1018                            _pam_winbind_cleanup_func);
1019         if (ret) {
1020                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1021                                "Could not set data %s: %s\n",
1022                                data_name, pam_strerror(pamh, ret));
1023         }
1024
1025 }
1026
1027 /**
1028  * Set info3 strings into the PAM stack.
1029  *
1030  * @param pamh PAM handle
1031  * @param ctrl PAM winbind options.
1032  * @param data_name Key name for pam_set_data.
1033  * @param value String value.
1034  *
1035  * @return void.
1036  */
1037
1038 static void _pam_set_data_info3(pam_handle_t *pamh,
1039                                 int ctrl,
1040                                 struct winbindd_response *response)
1041 {
1042         _pam_set_data_string(pamh, ctrl, PAM_WINBIND_HOMEDIR,
1043                              response->data.auth.info3.home_dir);
1044         _pam_set_data_string(pamh, ctrl, PAM_WINBIND_LOGONSCRIPT,
1045                              response->data.auth.info3.logon_script);
1046         _pam_set_data_string(pamh, ctrl, PAM_WINBIND_LOGONSERVER,
1047                              response->data.auth.info3.logon_srv);
1048         _pam_set_data_string(pamh, ctrl, PAM_WINBIND_PROFILEPATH,
1049                              response->data.auth.info3.profile_path);
1050 }
1051
1052 /**
1053  * Free info3 strings in the PAM stack.
1054  *
1055  * @param pamh PAM handle
1056  *
1057  * @return void.
1058  */
1059
1060 static void _pam_free_data_info3(pam_handle_t *pamh)
1061 {
1062         pam_set_data(pamh, PAM_WINBIND_HOMEDIR, NULL, NULL);
1063         pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT, NULL, NULL);
1064         pam_set_data(pamh, PAM_WINBIND_LOGONSERVER, NULL, NULL);
1065         pam_set_data(pamh, PAM_WINBIND_PROFILEPATH, NULL, NULL);
1066 }
1067
1068 /**
1069  * Send PAM_ERROR_MSG for cached or grace logons.
1070  *
1071  * @param pamh PAM handle
1072  * @param ctrl PAM winbind options.
1073  * @param username User in PAM request.
1074  * @param info3_user_flgs Info3 flags containing logon type bits.
1075  *
1076  * @return void.
1077  */
1078
1079 static void _pam_warn_logon_type(pam_handle_t *pamh,
1080                                  int ctrl,
1081                                  const char *username,
1082                                  uint32_t info3_user_flgs)
1083 {
1084         /* inform about logon type */
1085         if (PAM_WB_GRACE_LOGON(info3_user_flgs)) {
1086
1087                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
1088                              "Grace login. "
1089                              "Please change your password as soon you're "
1090                              "online again");
1091                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1092                                "User %s logged on using grace logon\n",
1093                                username);
1094
1095         } else if (PAM_WB_CACHED_LOGON(info3_user_flgs)) {
1096
1097                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
1098                              "Domain Controller unreachable, "
1099                              "using cached credentials instead. "
1100                              "Network resources may be unavailable");
1101                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1102                                "User %s logged on using cached credentials\n",
1103                                username);
1104         }
1105 }
1106
1107 /**
1108  * Send PAM_ERROR_MSG for krb5 errors.
1109  *
1110  * @param pamh PAM handle
1111  * @param ctrl PAM winbind options.
1112  * @param username User in PAM request.
1113  * @param info3_user_flgs Info3 flags containing logon type bits.
1114  *
1115  * @return void.
1116  */
1117
1118 static void _pam_warn_krb5_failure(pam_handle_t *pamh,
1119                                    int ctrl,
1120                                    const char *username,
1121                                    uint32_t info3_user_flgs)
1122 {
1123         if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs)) {
1124                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
1125                              "Failed to establish your Kerberos Ticket cache "
1126                              "due time differences\n"
1127                              "with the domain controller.  "
1128                              "Please verify the system time.\n");
1129                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1130                                "User %s: Clock skew when getting Krb5 TGT\n",
1131                                username);
1132         }
1133 }
1134
1135 /**
1136  * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
1137  *
1138  * @param response The struct winbindd_response.
1139  *
1140  * @return string (caller needs to free).
1141  */
1142
1143 static char *_pam_compose_pwd_restriction_string(struct winbindd_response *response)
1144 {
1145         char *str = NULL;
1146         size_t offset = 0, ret = 0, str_size = 1024;
1147
1148         str = (char *)malloc(str_size);
1149         if (!str) {
1150                 return NULL;
1151         }
1152
1153         memset(str, '\0', str_size);
1154
1155         offset = snprintf(str, str_size, "Your password ");
1156         if (offset == -1) {
1157                 goto failed;
1158         }
1159
1160         if (response->data.auth.policy.min_length_password > 0) {
1161                 ret = snprintf(str+offset, str_size-offset,
1162                                "must be at least %d characters; ",
1163                                response->data.auth.policy.min_length_password);
1164                 if (ret == -1) {
1165                         goto failed;
1166                 }
1167                 offset += ret;
1168         }
1169
1170         if (response->data.auth.policy.password_history > 0) {
1171                 ret = snprintf(str+offset, str_size-offset,
1172                                "cannot repeat any of your previous %d "
1173                                "passwords; ",
1174                                response->data.auth.policy.password_history);
1175                 if (ret == -1) {
1176                         goto failed;
1177                 }
1178                 offset += ret;
1179         }
1180
1181         if (response->data.auth.policy.password_properties &
1182             DOMAIN_PASSWORD_COMPLEX) {
1183                 ret = snprintf(str+offset, str_size-offset,
1184                                "must contain capitals, numerals "
1185                                "or punctuation; "
1186                                "and cannot contain your account "
1187                                "or full name; ");
1188                 if (ret == -1) {
1189                         goto failed;
1190                 }
1191                 offset += ret;
1192         }
1193
1194         ret = snprintf(str+offset, str_size-offset,
1195                        "Please type a different password. "
1196                        "Type a password which meets these requirements in "
1197                        "both text boxes.");
1198         if (ret == -1) {
1199                 goto failed;
1200         }
1201
1202         return str;
1203
1204  failed:
1205         SAFE_FREE(str);
1206         return NULL;
1207 }
1208
1209 /* talk to winbindd */
1210 static int winbind_auth_request(pam_handle_t * pamh,
1211                                 int ctrl,
1212                                 const char *user,
1213                                 const char *pass,
1214                                 const char *member,
1215                                 const char *cctype,
1216                                 const int warn_pwd_expire,
1217                                 struct winbindd_response *p_response,
1218                                 time_t *pwd_last_set,
1219                                 char **user_ret)
1220 {
1221         struct winbindd_request request;
1222         struct winbindd_response response;
1223         int ret;
1224         bool already_expired = false;
1225
1226         ZERO_STRUCT(request);
1227         ZERO_STRUCT(response);
1228
1229         if (pwd_last_set) {
1230                 *pwd_last_set = 0;
1231         }
1232
1233         strncpy(request.data.auth.user, user,
1234                 sizeof(request.data.auth.user)-1);
1235
1236         strncpy(request.data.auth.pass, pass,
1237                 sizeof(request.data.auth.pass)-1);
1238
1239         request.data.auth.krb5_cc_type[0] = '\0';
1240         request.data.auth.uid = -1;
1241
1242         request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_GET_PWD_POLICY;
1243
1244         /* Krb5 auth always has to go against the KDC of the user's realm */
1245
1246         if (ctrl & WINBIND_KRB5_AUTH) {
1247                 request.flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
1248         }
1249
1250         if (ctrl & (WINBIND_KRB5_AUTH|WINBIND_CACHED_LOGIN)) {
1251                 struct passwd *pwd = NULL;
1252
1253                 pwd = getpwnam(user);
1254                 if (pwd == NULL) {
1255                         return PAM_USER_UNKNOWN;
1256                 }
1257                 request.data.auth.uid = pwd->pw_uid;
1258         }
1259
1260         if (ctrl & WINBIND_KRB5_AUTH) {
1261
1262                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1263                                "enabling krb5 login flag\n");
1264
1265                 request.flags |= WBFLAG_PAM_KRB5 |
1266                                  WBFLAG_PAM_FALLBACK_AFTER_KRB5;
1267         }
1268
1269         if (ctrl & WINBIND_CACHED_LOGIN) {
1270                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1271                                "enabling cached login flag\n");
1272                 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
1273         }
1274
1275         if (user_ret) {
1276                 *user_ret = NULL;
1277                 request.flags |= WBFLAG_PAM_UNIX_NAME;
1278         }
1279
1280         if (cctype != NULL) {
1281                 strncpy(request.data.auth.krb5_cc_type, cctype,
1282                         sizeof(request.data.auth.krb5_cc_type) - 1);
1283                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1284                                "enabling request for a %s krb5 ccache\n",
1285                                cctype);
1286         }
1287
1288         request.data.auth.require_membership_of_sid[0] = '\0';
1289
1290         if (member != NULL) {
1291
1292                 if (!winbind_name_list_to_sid_string_list(pamh, ctrl, user,
1293                         member,
1294                         request.data.auth.require_membership_of_sid,
1295                         sizeof(request.data.auth.require_membership_of_sid))) {
1296
1297                         _pam_log_debug(pamh, ctrl, LOG_ERR,
1298                                        "failed to serialize membership of sid "
1299                                        "\"%s\"\n", member);
1300                         return PAM_AUTH_ERR;
1301                 }
1302         }
1303
1304         ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH,
1305                                       &request, &response, user);
1306
1307         if (pwd_last_set) {
1308                 *pwd_last_set = response.data.auth.info3.pass_last_set_time;
1309         }
1310
1311         if (p_response) {
1312                 /* We want to process the response in the caller. */
1313                 *p_response = response;
1314                 return ret;
1315         }
1316
1317         if (ret) {
1318                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1319                                                  "NT_STATUS_PASSWORD_EXPIRED");
1320                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1321                                                  "NT_STATUS_PASSWORD_MUST_CHANGE");
1322                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1323                                                  "NT_STATUS_INVALID_WORKSTATION");
1324                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1325                                                  "NT_STATUS_INVALID_LOGON_HOURS");
1326                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1327                                                  "NT_STATUS_ACCOUNT_EXPIRED");
1328                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1329                                                  "NT_STATUS_ACCOUNT_DISABLED");
1330                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1331                                                  "NT_STATUS_ACCOUNT_LOCKED_OUT");
1332                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1333                                                  "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
1334                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1335                                                  "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
1336                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1337                                                  "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
1338                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1339                                                  "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1340                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1341                                                  "NT_STATUS_NO_LOGON_SERVERS");
1342                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1343                                                  "NT_STATUS_WRONG_PASSWORD");
1344                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1345                                                  "NT_STATUS_ACCESS_DENIED");
1346         }
1347
1348         if (ret == PAM_SUCCESS) {
1349
1350                 /* warn a user if the password is about to expire soon */
1351                 _pam_warn_password_expiry(pamh, ctrl, &response,
1352                                           warn_pwd_expire,
1353                                           &already_expired);
1354
1355                 if (already_expired == true) {
1356                         SMB_TIME_T last_set;
1357                         last_set = response.data.auth.info3.pass_last_set_time;
1358                         _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1359                                        "Password has expired "
1360                                        "(Password was last set: %lld, "
1361                                        "the policy says it should expire here "
1362                                        "%lld (now it's: %lu))\n",
1363                                        (long long int)last_set,
1364                                        (long long int)last_set +
1365                                        response.data.auth.policy.expire,
1366                                        time(NULL));
1367
1368                         return PAM_AUTHTOK_EXPIRED;
1369                 }
1370
1371                 /* inform about logon type */
1372                 _pam_warn_logon_type(pamh, ctrl, user,
1373                                      response.data.auth.info3.user_flgs);
1374
1375                 /* inform about krb5 failures */
1376                 _pam_warn_krb5_failure(pamh, ctrl, user,
1377                                        response.data.auth.info3.user_flgs);
1378
1379                 /* set some info3 info for other modules in the stack */
1380                 _pam_set_data_info3(pamh, ctrl, &response);
1381
1382                 /* put krb5ccname into env */
1383                 _pam_setup_krb5_env(pamh, ctrl, response.data.auth.krb5ccname);
1384
1385                 /* If winbindd returned a username, return the pointer to it
1386                  * here. */
1387                 if (user_ret && response.data.auth.unix_username[0]) {
1388                         /* We have to trust it's a null terminated string. */
1389                         *user_ret = strndup(response.data.auth.unix_username,
1390                                     sizeof(response.data.auth.unix_username) - 1);
1391                 }
1392         }
1393
1394         return ret;
1395 }
1396
1397 /* talk to winbindd */
1398 static int winbind_chauthtok_request(pam_handle_t * pamh,
1399                                      int ctrl,
1400                                      const char *user,
1401                                      const char *oldpass,
1402                                      const char *newpass,
1403                                      time_t pwd_last_set)
1404 {
1405         struct winbindd_request request;
1406         struct winbindd_response response;
1407         int ret;
1408
1409         ZERO_STRUCT(request);
1410         ZERO_STRUCT(response);
1411
1412         if (request.data.chauthtok.user == NULL) {
1413                 return -2;
1414         }
1415
1416         strncpy(request.data.chauthtok.user, user,
1417                 sizeof(request.data.chauthtok.user) - 1);
1418
1419         if (oldpass != NULL) {
1420                 strncpy(request.data.chauthtok.oldpass, oldpass,
1421                         sizeof(request.data.chauthtok.oldpass) - 1);
1422         } else {
1423                 request.data.chauthtok.oldpass[0] = '\0';
1424         }
1425
1426         if (newpass != NULL) {
1427                 strncpy(request.data.chauthtok.newpass, newpass,
1428                         sizeof(request.data.chauthtok.newpass) - 1);
1429         } else {
1430                 request.data.chauthtok.newpass[0] = '\0';
1431         }
1432
1433         if (ctrl & WINBIND_KRB5_AUTH) {
1434                 request.flags = WBFLAG_PAM_KRB5 |
1435                                 WBFLAG_PAM_CONTACT_TRUSTDOM;
1436         }
1437
1438         ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK,
1439                                       &request, &response, user);
1440
1441         if (ret == PAM_SUCCESS) {
1442                 return ret;
1443         }
1444
1445         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1446                                          "NT_STATUS_BACKUP_CONTROLLER");
1447         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1448                                          "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1449         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1450                                          "NT_STATUS_NO_LOGON_SERVERS");
1451         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1452                                          "NT_STATUS_ACCESS_DENIED");
1453
1454         /* TODO: tell the min pwd length ? */
1455         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1456                                          "NT_STATUS_PWD_TOO_SHORT");
1457
1458         /* TODO: tell the minage ? */
1459         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1460                                          "NT_STATUS_PWD_TOO_RECENT");
1461
1462         /* TODO: tell the history length ? */
1463         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response,
1464                                          "NT_STATUS_PWD_HISTORY_CONFLICT");
1465
1466         if (!strcasecmp(response.data.auth.nt_status_string,
1467                         "NT_STATUS_PASSWORD_RESTRICTION")) {
1468
1469                 char *pwd_restriction_string = NULL;
1470                 SMB_TIME_T min_pwd_age;
1471                 uint32_t reject_reason = response.data.auth.reject_reason;
1472                 min_pwd_age = response.data.auth.policy.min_passwordage;
1473
1474                 /* FIXME: avoid to send multiple PAM messages after another */
1475                 switch (reject_reason) {
1476                         case -1:
1477                                 break;
1478                         case SAMR_REJECT_OTHER:
1479                                 if ((min_pwd_age > 0) &&
1480                                     (pwd_last_set + min_pwd_age > time(NULL))) {
1481                                         PAM_WB_REMARK_DIRECT(pamh, ctrl,
1482                                              "NT_STATUS_PWD_TOO_RECENT");
1483                                 }
1484                                 break;
1485                         case SAMR_REJECT_TOO_SHORT:
1486                                 PAM_WB_REMARK_DIRECT(pamh, ctrl,
1487                                         "NT_STATUS_PWD_TOO_SHORT");
1488                                 break;
1489                         case SAMR_REJECT_IN_HISTORY:
1490                                 PAM_WB_REMARK_DIRECT(pamh, ctrl,
1491                                         "NT_STATUS_PWD_HISTORY_CONFLICT");
1492                                 break;
1493                         case SAMR_REJECT_COMPLEXITY:
1494                                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
1495                                              "Password does not meet "
1496                                              "complexity requirements");
1497                                 break;
1498                         default:
1499                                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1500                                                "unknown password change "
1501                                                "reject reason: %d",
1502                                                reject_reason);
1503                                 break;
1504                 }
1505
1506                 pwd_restriction_string =
1507                         _pam_compose_pwd_restriction_string(&response);
1508                 if (pwd_restriction_string) {
1509                         _make_remark(pamh, ctrl, PAM_ERROR_MSG,
1510                                      pwd_restriction_string);
1511                         SAFE_FREE(pwd_restriction_string);
1512                 }
1513         }
1514
1515         return ret;
1516 }
1517
1518 /*
1519  * Checks if a user has an account
1520  *
1521  * return values:
1522  *       1  = User not found
1523  *       0  = OK
1524  *      -1  = System error
1525  */
1526 static int valid_user(pam_handle_t *pamh,
1527                       int ctrl,
1528                       const char *user)
1529 {
1530         /* check not only if the user is available over NSS calls, also make
1531          * sure it's really a winbind user, this is important when stacking PAM
1532          * modules in the 'account' or 'password' facility. */
1533
1534         struct passwd *pwd = NULL;
1535         struct winbindd_request request;
1536         struct winbindd_response response;
1537         int ret;
1538
1539         ZERO_STRUCT(request);
1540         ZERO_STRUCT(response);
1541
1542         pwd = getpwnam(user);
1543         if (pwd == NULL) {
1544                 return 1;
1545         }
1546
1547         strncpy(request.data.username, user,
1548                 sizeof(request.data.username) - 1);
1549
1550         ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_GETPWNAM,
1551                                       &request, &response, user);
1552
1553         switch (ret) {
1554                 case PAM_USER_UNKNOWN:
1555                         return 1;
1556                 case PAM_SUCCESS:
1557                         return 0;
1558                 default:
1559                         break;
1560         }
1561         return -1;
1562 }
1563
1564 static char *_pam_delete(register char *xx)
1565 {
1566         _pam_overwrite(xx);
1567         _pam_drop(xx);
1568         return NULL;
1569 }
1570
1571 /*
1572  * obtain a password from the user
1573  */
1574
1575 static int _winbind_read_password(pam_handle_t * pamh,
1576                                   unsigned int ctrl,
1577                                   const char *comment,
1578                                   const char *prompt1,
1579                                   const char *prompt2,
1580                                   const char **pass)
1581 {
1582         int authtok_flag;
1583         int retval;
1584         const char *item;
1585         char *token;
1586
1587         _pam_log(pamh, ctrl, LOG_DEBUG, "getting password (0x%08x)", ctrl);
1588
1589         /*
1590          * make sure nothing inappropriate gets returned
1591          */
1592
1593         *pass = token = NULL;
1594
1595         /*
1596          * which authentication token are we getting?
1597          */
1598
1599         if (on(WINBIND__OLD_PASSWORD, ctrl)) {
1600                 authtok_flag = PAM_OLDAUTHTOK;
1601         } else {
1602                 authtok_flag = PAM_AUTHTOK;
1603         }
1604
1605         /*
1606          * should we obtain the password from a PAM item ?
1607          */
1608
1609         if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) ||
1610             on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1611                 retval = _pam_get_item(pamh, authtok_flag, &item);
1612                 if (retval != PAM_SUCCESS) {
1613                         /* very strange. */
1614                         _pam_log(pamh, ctrl, LOG_ALERT,
1615                                  "pam_get_item returned error "
1616                                  "to unix-read-password");
1617                         return retval;
1618                 } else if (item != NULL) {      /* we have a password! */
1619                         *pass = item;
1620                         item = NULL;
1621                         _pam_log(pamh, ctrl, LOG_DEBUG,
1622                                  "pam_get_item returned a password");
1623                         return PAM_SUCCESS;
1624                 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1625                         return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
1626                 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
1627                            && off(WINBIND__OLD_PASSWORD, ctrl)) {
1628                         return PAM_AUTHTOK_RECOVER_ERR;
1629                 }
1630         }
1631         /*
1632          * getting here implies we will have to get the password from the
1633          * user directly.
1634          */
1635
1636         {
1637                 struct pam_message msg[3], *pmsg[3];
1638                 struct pam_response *resp;
1639                 int i, replies;
1640
1641                 /* prepare to converse */
1642
1643                 if (comment != NULL && off(ctrl, WINBIND_SILENT)) {
1644                         pmsg[0] = &msg[0];
1645                         msg[0].msg_style = PAM_TEXT_INFO;
1646                         msg[0].msg = discard_const_p(char, comment);
1647                         i = 1;
1648                 } else {
1649                         i = 0;
1650                 }
1651
1652                 pmsg[i] = &msg[i];
1653                 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1654                 msg[i++].msg = discard_const_p(char, prompt1);
1655                 replies = 1;
1656
1657                 if (prompt2 != NULL) {
1658                         pmsg[i] = &msg[i];
1659                         msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1660                         msg[i++].msg = discard_const_p(char, prompt2);
1661                         ++replies;
1662                 }
1663                 /* so call the conversation expecting i responses */
1664                 resp = NULL;
1665                 retval = converse(pamh, i, pmsg, &resp);
1666                 if (resp == NULL) {
1667                         if (retval == PAM_SUCCESS) {
1668                                 retval = PAM_AUTHTOK_RECOVER_ERR;
1669                         }
1670                         goto done;
1671                 }
1672                 if (retval != PAM_SUCCESS) {
1673                         _pam_drop_reply(resp, i);
1674                         goto done;
1675                 }
1676
1677                 /* interpret the response */
1678
1679                 token = x_strdup(resp[i - replies].resp);
1680                 if (!token) {
1681                         _pam_log(pamh, ctrl, LOG_NOTICE,
1682                                  "could not recover "
1683                                  "authentication token");
1684                         retval = PAM_AUTHTOK_RECOVER_ERR;
1685                         goto done;
1686                 }
1687
1688                 if (replies == 2) {
1689                         /* verify that password entered correctly */
1690                         if (!resp[i - 1].resp ||
1691                             strcmp(token, resp[i - 1].resp)) {
1692                                 _pam_delete(token);     /* mistyped */
1693                                 retval = PAM_AUTHTOK_RECOVER_ERR;
1694                                 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
1695                                              MISTYPED_PASS);
1696                         }
1697                 }
1698
1699                 /*
1700                  * tidy up the conversation (resp_retcode) is ignored
1701                  * -- what is it for anyway? AGM
1702                  */
1703                 _pam_drop_reply(resp, i);
1704         }
1705
1706  done:
1707         if (retval != PAM_SUCCESS) {
1708                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1709                                "unable to obtain a password");
1710                 return retval;
1711         }
1712         /* 'token' is the entered password */
1713
1714         /* we store this password as an item */
1715
1716         retval = pam_set_item(pamh, authtok_flag, token);
1717         _pam_delete(token);     /* clean it up */
1718         if (retval != PAM_SUCCESS ||
1719             (retval = _pam_get_item(pamh, authtok_flag, &item)) != PAM_SUCCESS) {
1720
1721                 _pam_log(pamh, ctrl, LOG_CRIT, "error manipulating password");
1722                 return retval;
1723
1724         }
1725
1726         *pass = item;
1727         item = NULL;            /* break link to password */
1728
1729         return PAM_SUCCESS;
1730 }
1731
1732 static const char *get_conf_item_string(const pam_handle_t *pamh,
1733                                         int argc,
1734                                         const char **argv,
1735                                         int ctrl,
1736                                         dictionary *d,
1737                                         const char *item,
1738                                         int config_flag)
1739 {
1740         int i = 0;
1741         const char *parm_opt = NULL;
1742
1743         if (!(ctrl & config_flag)) {
1744                 goto out;
1745         }
1746
1747         /* let the pam opt take precedence over the pam_winbind.conf option */
1748         for (i=0; i<argc; i++) {
1749
1750                 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
1751                         char *p;
1752
1753                         if ((p = strchr(argv[i], '=')) == NULL) {
1754                                 _pam_log(pamh, ctrl, LOG_INFO,
1755                                          "no \"=\" delimiter for \"%s\" found\n",
1756                                          item);
1757                                 goto out;
1758                         }
1759                         _pam_log_debug(pamh, ctrl, LOG_INFO,
1760                                        "PAM config: %s '%s'\n", item, p+1);
1761                         return p + 1;
1762                 }
1763         }
1764
1765         if (d != NULL) {
1766                 char *key = NULL;
1767
1768                 if (!asprintf(&key, "global:%s", item)) {
1769                         goto out;
1770                 }
1771
1772                 parm_opt = iniparser_getstr(d, key);
1773                 SAFE_FREE(key);
1774
1775                 _pam_log_debug(pamh, ctrl, LOG_INFO, "CONFIG file: %s '%s'\n",
1776                                item, parm_opt);
1777         }
1778 out:
1779         return parm_opt;
1780 }
1781
1782 static int get_config_item_int(const pam_handle_t *pamh,
1783                                int argc,
1784                                const char **argv,
1785                                int ctrl,
1786                                dictionary *d,
1787                                const char *item,
1788                                int config_flag)
1789 {
1790         int i, parm_opt = -1;
1791
1792         if (!(ctrl & config_flag)) {
1793                 goto out;
1794         }
1795
1796         /* let the pam opt take precedence over the pam_winbind.conf option */
1797         for (i = 0; i < argc; i++) {
1798
1799                 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
1800                         char *p;
1801
1802                         if ((p = strchr(argv[i], '=')) == NULL) {
1803                                 _pam_log(pamh, ctrl, LOG_INFO,
1804                                          "no \"=\" delimiter for \"%s\" found\n",
1805                                          item);
1806                                 goto out;
1807                         }
1808                         parm_opt = atoi(p + 1);
1809                         _pam_log_debug(pamh, ctrl, LOG_INFO,
1810                                        "PAM config: %s '%d'\n",
1811                                        item, parm_opt);
1812                         return parm_opt;
1813                 }
1814         }
1815
1816         if (d != NULL) {
1817                 char *key = NULL;
1818
1819                 if (!asprintf(&key, "global:%s", item)) {
1820                         goto out;
1821                 }
1822
1823                 parm_opt = iniparser_getint(d, key, -1);
1824                 SAFE_FREE(key);
1825
1826                 _pam_log_debug(pamh, ctrl, LOG_INFO,
1827                                "CONFIG file: %s '%d'\n",
1828                                item, parm_opt);
1829         }
1830 out:
1831         return parm_opt;
1832 }
1833
1834 static const char *get_krb5_cc_type_from_config(const pam_handle_t *pamh,
1835                                                 int argc,
1836                                                 const char **argv,
1837                                                 int ctrl,
1838                                                 dictionary *d)
1839 {
1840         return get_conf_item_string(pamh, argc, argv, ctrl, d,
1841                                     "krb5_ccache_type",
1842                                     WINBIND_KRB5_CCACHE_TYPE);
1843 }
1844
1845 static const char *get_member_from_config(const pam_handle_t *pamh,
1846                                           int argc,
1847                                           const char **argv,
1848                                           int ctrl,
1849                                           dictionary *d)
1850 {
1851         const char *ret = NULL;
1852         ret = get_conf_item_string(pamh, argc, argv, ctrl, d,
1853                                    "require_membership_of",
1854                                    WINBIND_REQUIRED_MEMBERSHIP);
1855         if (ret) {
1856                 return ret;
1857         }
1858         return get_conf_item_string(pamh, argc, argv, ctrl, d,
1859                                     "require-membership-of",
1860                                     WINBIND_REQUIRED_MEMBERSHIP);
1861 }
1862
1863 static int get_warn_pwd_expire_from_config(const pam_handle_t *pamh,
1864                                            int argc,
1865                                            const char **argv,
1866                                            int ctrl,
1867                                            dictionary *d)
1868 {
1869         int ret;
1870         ret = get_config_item_int(pamh, argc, argv, ctrl, d,
1871                                   "warn_pwd_expire",
1872                                   WINBIND_WARN_PWD_EXPIRE);
1873         /* no or broken setting */
1874         if (ret <= 0) {
1875                 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES;
1876         }
1877         return ret;
1878 }
1879
1880 /**
1881  * Retrieve the winbind separator.
1882  *
1883  * @param pamh PAM handle
1884  * @param ctrl PAM winbind options.
1885  *
1886  * @return string separator character. NULL on failure.
1887  */
1888
1889 static char winbind_get_separator(pam_handle_t *pamh,
1890                                   int ctrl)
1891 {
1892         struct winbindd_request request;
1893         struct winbindd_response response;
1894
1895         ZERO_STRUCT(request);
1896         ZERO_STRUCT(response);
1897
1898         if (pam_winbind_request_log(pamh, ctrl, WINBINDD_INFO,
1899                                     &request, &response, NULL)) {
1900                 return '\0';
1901         }
1902
1903         return response.data.info.winbind_separator;
1904 }
1905
1906 /**
1907  * Convert a upn to a name.
1908  *
1909  * @param pamh PAM handle
1910  * @param ctrl PAM winbind options.
1911  * @param upn  USer UPN to be trabslated.
1912  *
1913  * @return converted name. NULL pointer on failure. Caller needs to free.
1914  */
1915
1916 static char* winbind_upn_to_username(pam_handle_t *pamh,
1917                                      int ctrl,
1918                                      const char *upn)
1919 {
1920         struct winbindd_request req;
1921         struct winbindd_response resp;
1922         int retval;
1923         char *account_name;
1924         int account_name_len;
1925         char sep;
1926
1927         /* This cannot work when the winbind separator = @ */
1928
1929         sep = winbind_get_separator(pamh, ctrl);
1930         if (!sep || sep == '@') {
1931                 return NULL;
1932         }
1933
1934         /* Convert the UPN to a SID */
1935
1936         ZERO_STRUCT(req);
1937         ZERO_STRUCT(resp);
1938
1939         strncpy(req.data.name.dom_name, "",
1940                 sizeof(req.data.name.dom_name) - 1);
1941         strncpy(req.data.name.name, upn,
1942                 sizeof(req.data.name.name) - 1);
1943         retval = pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME,
1944                                          &req, &resp, upn);
1945         if (retval != PAM_SUCCESS) {
1946                 return NULL;
1947         }
1948
1949         /* Convert the the SID back to the sAMAccountName */
1950
1951         ZERO_STRUCT(req);
1952         strncpy(req.data.sid, resp.data.sid.sid, sizeof(req.data.sid)-1);
1953         ZERO_STRUCT(resp);
1954         retval =  pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPSID,
1955                                           &req, &resp, upn);
1956         if (retval != PAM_SUCCESS) {
1957                 return NULL;
1958         }
1959
1960         account_name_len = asprintf(&account_name, "%s\\%s",
1961                                     resp.data.name.dom_name,
1962                                     resp.data.name.name);
1963
1964         return account_name;
1965 }
1966
1967 PAM_EXTERN
1968 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
1969                         int argc, const char **argv)
1970 {
1971         const char *username;
1972         const char *password;
1973         const char *member = NULL;
1974         const char *cctype = NULL;
1975         int warn_pwd_expire;
1976         int retval = PAM_AUTH_ERR;
1977         dictionary *d = NULL;
1978         char *username_ret = NULL;
1979         char *new_authtok_required = NULL;
1980         char *real_username = NULL;
1981
1982         /* parse arguments */
1983         int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1984         if (ctrl == -1) {
1985                 retval = PAM_SYSTEM_ERR;
1986                 goto out;
1987         }
1988
1989         _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", pamh, ctrl, flags);
1990
1991         /* Get the username */
1992         retval = pam_get_user(pamh, &username, NULL);
1993         if ((retval != PAM_SUCCESS) || (!username)) {
1994                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1995                                "can not get the username");
1996                 retval = PAM_SERVICE_ERR;
1997                 goto out;
1998         }
1999
2000
2001 #if defined(AIX)
2002         /* Decode the user name since AIX does not support logn user
2003            names by default.  The name is encoded as _#uid.  */
2004
2005         if (username[0] == '_') {
2006                 uid_t id = atoi(&username[1]);
2007                 struct passwd *pw = NULL;
2008
2009                 if ((id!=0) && ((pw = getpwuid(id)) != NULL)) {
2010                         real_username = strdup(pw->pw_name);
2011                 }
2012         }
2013 #endif
2014
2015         if (!real_username) {
2016                 /* Just making a copy of the username we got from PAM */
2017                 if ((real_username = strdup(username)) == NULL) {
2018                         _pam_log_debug(pamh, ctrl, LOG_DEBUG,
2019                                        "memory allocation failure when copying "
2020                                        "username");
2021                         retval = PAM_SERVICE_ERR;
2022                         goto out;
2023                 }
2024         }
2025
2026         /* Maybe this was a UPN */
2027
2028         if (strchr(real_username, '@') != NULL) {
2029                 char *samaccountname = NULL;
2030
2031                 samaccountname = winbind_upn_to_username(pamh, ctrl,
2032                                                          real_username);
2033                 if (samaccountname) {
2034                         free(real_username);
2035                         real_username = samaccountname;
2036                 }
2037         }
2038
2039         retval = _winbind_read_password(pamh, ctrl, NULL,
2040                                         "Password: ", NULL,
2041                                         &password);
2042
2043         if (retval != PAM_SUCCESS) {
2044                 _pam_log(pamh, ctrl, LOG_ERR,
2045                          "Could not retrieve user's password");
2046                 retval = PAM_AUTHTOK_ERR;
2047                 goto out;
2048         }
2049
2050         /* Let's not give too much away in the log file */
2051
2052 #ifdef DEBUG_PASSWORD
2053         _pam_log_debug(pamh, ctrl, LOG_INFO,
2054                        "Verify user '%s' with password '%s'",
2055                        real_username, password);
2056 #else
2057         _pam_log_debug(pamh, ctrl, LOG_INFO,
2058                        "Verify user '%s'", real_username);
2059 #endif
2060
2061         member = get_member_from_config(pamh, argc, argv, ctrl, d);
2062
2063         cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
2064
2065         warn_pwd_expire = get_warn_pwd_expire_from_config(pamh, argc, argv,
2066                                                           ctrl, d);
2067
2068         /* Now use the username to look up password */
2069         retval = winbind_auth_request(pamh, ctrl, real_username, password,
2070                                       member, cctype, warn_pwd_expire, NULL,
2071                                       NULL, &username_ret);
2072
2073         if (retval == PAM_NEW_AUTHTOK_REQD ||
2074             retval == PAM_AUTHTOK_EXPIRED) {
2075
2076                 char *new_authtok_required_during_auth = NULL;
2077
2078                 if (!asprintf(&new_authtok_required, "%d", retval)) {
2079                         retval = PAM_BUF_ERR;
2080                         goto out;
2081                 }
2082
2083                 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2084                              new_authtok_required,
2085                              _pam_winbind_cleanup_func);
2086
2087                 retval = PAM_SUCCESS;
2088
2089                 if (!asprintf(&new_authtok_required_during_auth, "%d", true)) {
2090                         retval = PAM_BUF_ERR;
2091                         goto out;
2092                 }
2093
2094                 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2095                              new_authtok_required_during_auth,
2096                              _pam_winbind_cleanup_func);
2097
2098                 goto out;
2099         }
2100
2101 out:
2102         if (username_ret) {
2103                 pam_set_item (pamh, PAM_USER, username_ret);
2104                 _pam_log_debug(pamh, ctrl, LOG_INFO,
2105                                "Returned user was '%s'", username_ret);
2106                 free(username_ret);
2107         }
2108
2109         if (real_username) {
2110                 free(real_username);
2111         }
2112
2113         if (d) {
2114                 iniparser_freedict(d);
2115         }
2116
2117         if (!new_authtok_required) {
2118                 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
2119         }
2120
2121         if (retval != PAM_SUCCESS) {
2122                 _pam_free_data_info3(pamh);
2123         }
2124
2125         _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", pamh, ctrl, retval);
2126
2127         return retval;
2128 }
2129
2130 PAM_EXTERN
2131 int pam_sm_setcred(pam_handle_t *pamh, int flags,
2132                    int argc, const char **argv)
2133 {
2134         int ret = PAM_SYSTEM_ERR;
2135         dictionary *d = NULL;
2136
2137         /* parse arguments */
2138         int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
2139         if (ctrl == -1) {
2140                 ret = PAM_SYSTEM_ERR;
2141                 goto out;
2142         }
2143
2144         _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", pamh, ctrl, flags);
2145
2146         switch (flags & ~PAM_SILENT) {
2147
2148                 case PAM_DELETE_CRED:
2149                         ret = pam_sm_close_session(pamh, flags, argc, argv);
2150                         break;
2151                 case PAM_REFRESH_CRED:
2152                         _pam_log_debug(pamh, ctrl, LOG_WARNING,
2153                                        "PAM_REFRESH_CRED not implemented");
2154                         ret = PAM_SUCCESS;
2155                         break;
2156                 case PAM_REINITIALIZE_CRED:
2157                         _pam_log_debug(pamh, ctrl, LOG_WARNING,
2158                                        "PAM_REINITIALIZE_CRED not implemented");
2159                         ret = PAM_SUCCESS;
2160                         break;
2161                 case PAM_ESTABLISH_CRED:
2162                         _pam_log_debug(pamh, ctrl, LOG_WARNING,
2163                                        "PAM_ESTABLISH_CRED not implemented");
2164                         ret = PAM_SUCCESS;
2165                         break;
2166                 default:
2167                         ret = PAM_SYSTEM_ERR;
2168                         break;
2169         }
2170
2171  out:
2172         if (d) {
2173                 iniparser_freedict(d);
2174         }
2175
2176         _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", pamh, ctrl, ret);
2177
2178         return ret;
2179 }
2180
2181 /*
2182  * Account management. We want to verify that the account exists
2183  * before returning PAM_SUCCESS
2184  */
2185 PAM_EXTERN
2186 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
2187                    int argc, const char **argv)
2188 {
2189         const char *username;
2190         int ret = PAM_USER_UNKNOWN;
2191         void *tmp = NULL;
2192         dictionary *d = NULL;
2193
2194         /* parse arguments */
2195         int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
2196         if (ctrl == -1) {
2197                 return PAM_SYSTEM_ERR;
2198         }
2199
2200         _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", pamh, ctrl, flags);
2201
2202
2203         /* Get the username */
2204         ret = pam_get_user(pamh, &username, NULL);
2205         if ((ret != PAM_SUCCESS) || (!username)) {
2206                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
2207                                "can not get the username");
2208                 ret = PAM_SERVICE_ERR;
2209                 goto out;
2210         }
2211
2212         /* Verify the username */
2213         ret = valid_user(pamh, ctrl, username);
2214         switch (ret) {
2215         case -1:
2216                 /* some sort of system error. The log was already printed */
2217                 ret = PAM_SERVICE_ERR;
2218                 goto out;
2219         case 1:
2220                 /* the user does not exist */
2221                 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found",
2222                                username);
2223                 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
2224                         ret = PAM_IGNORE;
2225                         goto out;
2226                 }
2227                 ret = PAM_USER_UNKNOWN;
2228                 goto out;
2229         case 0:
2230                 pam_get_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2231                              (const void **)&tmp);
2232                 if (tmp != NULL) {
2233                         ret = atoi((const char *)tmp);
2234                         switch (ret) {
2235                         case PAM_AUTHTOK_EXPIRED:
2236                                 /* fall through, since new token is required in this case */
2237                         case PAM_NEW_AUTHTOK_REQD:
2238                                 _pam_log(pamh, ctrl, LOG_WARNING,
2239                                          "pam_sm_acct_mgmt success but %s is set",
2240                                          PAM_WINBIND_NEW_AUTHTOK_REQD);
2241                                 _pam_log(pamh, ctrl, LOG_NOTICE,
2242                                          "user '%s' needs new password",
2243                                          username);
2244                                 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2245                                 ret = PAM_NEW_AUTHTOK_REQD;
2246                                 goto out;
2247                         default:
2248                                 _pam_log(pamh, ctrl, LOG_WARNING,
2249                                          "pam_sm_acct_mgmt success");
2250                                 _pam_log(pamh, ctrl, LOG_NOTICE,
2251                                          "user '%s' granted access", username);
2252                                 ret = PAM_SUCCESS;
2253                                 goto out;
2254                         }
2255                 }
2256
2257                 /* Otherwise, the authentication looked good */
2258                 _pam_log(pamh, ctrl, LOG_NOTICE,
2259                          "user '%s' granted access", username);
2260                 ret = PAM_SUCCESS;
2261                 goto out;
2262         default:
2263                 /* we don't know anything about this return value */
2264                 _pam_log(pamh, ctrl, LOG_ERR,
2265                          "internal module error (ret = %d, user = '%s')",
2266                          ret, username);
2267                 ret = PAM_SERVICE_ERR;
2268                 goto out;
2269         }
2270
2271         /* should not be reached */
2272         ret = PAM_IGNORE;
2273
2274  out:
2275
2276         if (d) {
2277                 iniparser_freedict(d);
2278         }
2279
2280         _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", pamh, ctrl, ret);
2281
2282         return ret;
2283 }
2284
2285 PAM_EXTERN
2286 int pam_sm_open_session(pam_handle_t *pamh, int flags,
2287                         int argc, const char **argv)
2288 {
2289         int ret = PAM_SYSTEM_ERR;
2290         dictionary *d = NULL;
2291
2292         /* parse arguments */
2293         int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
2294         if (ctrl == -1) {
2295                 ret = PAM_SYSTEM_ERR;
2296                 goto out;
2297         }
2298
2299         _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", pamh, ctrl, flags);
2300
2301         ret = PAM_SUCCESS;
2302
2303  out:
2304         if (d) {
2305                 iniparser_freedict(d);
2306         }
2307
2308         _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", pamh, ctrl, ret);
2309
2310         return ret;
2311 }
2312
2313 PAM_EXTERN
2314 int pam_sm_close_session(pam_handle_t *pamh, int flags,
2315                          int argc, const char **argv)
2316 {
2317         dictionary *d = NULL;
2318         int retval = PAM_SUCCESS;
2319
2320         /* parse arguments */
2321         int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
2322         if (ctrl == -1) {
2323                 retval = PAM_SYSTEM_ERR;
2324                 goto out;
2325         }
2326
2327         _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", pamh, ctrl, flags);
2328
2329         if (!(flags & PAM_DELETE_CRED)) {
2330                 retval = PAM_SUCCESS;
2331                 goto out;
2332         }
2333
2334         if (ctrl & WINBIND_KRB5_AUTH) {
2335
2336                 /* destroy the ccache here */
2337                 struct winbindd_request request;
2338                 struct winbindd_response response;
2339                 const char *user;
2340                 const char *ccname = NULL;
2341                 struct passwd *pwd = NULL;
2342
2343                 ZERO_STRUCT(request);
2344                 ZERO_STRUCT(response);
2345
2346                 retval = pam_get_user(pamh, &user, "Username: ");
2347                 if (retval) {
2348                         _pam_log(pamh, ctrl, LOG_ERR,
2349                                  "could not identify user");
2350                         goto out;
2351                 }
2352
2353                 if (user == NULL) {
2354                         _pam_log(pamh, ctrl, LOG_ERR,
2355                                  "username was NULL!");
2356                         retval = PAM_USER_UNKNOWN;
2357                         goto out;
2358                 }
2359
2360                 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
2361                                "username [%s] obtained", user);
2362
2363                 ccname = pam_getenv(pamh, "KRB5CCNAME");
2364                 if (ccname == NULL) {
2365                         _pam_log_debug(pamh, ctrl, LOG_DEBUG,
2366                                        "user has no KRB5CCNAME environment");
2367                 }
2368
2369                 strncpy(request.data.logoff.user, user,
2370                         sizeof(request.data.logoff.user) - 1);
2371
2372                 if (ccname) {
2373                         strncpy(request.data.logoff.krb5ccname, ccname,
2374                                 sizeof(request.data.logoff.krb5ccname) - 1);
2375                 }
2376
2377                 pwd = getpwnam(user);
2378                 if (pwd == NULL) {
2379                         retval = PAM_USER_UNKNOWN;
2380                         goto out;
2381                 }
2382                 request.data.logoff.uid = pwd->pw_uid;
2383
2384                 request.flags = WBFLAG_PAM_KRB5 |
2385                                 WBFLAG_PAM_CONTACT_TRUSTDOM;
2386
2387                 retval = pam_winbind_request_log(pamh, ctrl,
2388                                                  WINBINDD_PAM_LOGOFF,
2389                                                  &request, &response, user);
2390         }
2391
2392 out:
2393         if (d) {
2394                 iniparser_freedict(d);
2395         }
2396
2397         _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", pamh, ctrl, retval);
2398
2399         return retval;
2400 }
2401
2402 /**
2403  * evaluate whether we need to re-authenticate with kerberos after a
2404  * password change
2405  *
2406  * @param pamh PAM handle
2407  * @param ctrl PAM winbind options.
2408  * @param user The username
2409  *
2410  * @return boolean Returns true if required, false if not.
2411  */
2412
2413 static bool _pam_require_krb5_auth_after_chauthtok(pam_handle_t *pamh,
2414                                                    int ctrl,
2415                                                    const char *user)
2416 {
2417
2418         /* Make sure that we only do this if a) the chauthtok got initiated
2419          * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2420          * later password change via the "passwd" command if done by the user
2421          * itself */
2422
2423         char *new_authtok_reqd_during_auth = NULL;
2424         struct passwd *pwd = NULL;
2425
2426         if (!(ctrl & WINBIND_KRB5_AUTH)) {
2427                 return false;
2428         }
2429
2430         _pam_get_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2431                       &new_authtok_reqd_during_auth);
2432         pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2433                      NULL, NULL);
2434
2435         if (new_authtok_reqd_during_auth) {
2436                 return true;
2437         }
2438
2439         pwd = getpwnam(user);
2440         if (!pwd) {
2441                 return false;
2442         }
2443
2444         if (getuid() == pwd->pw_uid) {
2445                 return true;
2446         }
2447
2448         return false;
2449 }
2450
2451
2452 PAM_EXTERN
2453 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
2454                      int argc, const char **argv)
2455 {
2456         unsigned int lctrl;
2457         int ret;
2458         unsigned int ctrl;
2459
2460         /* <DO NOT free() THESE> */
2461         const char *user;
2462         char *pass_old, *pass_new;
2463         /* </DO NOT free() THESE> */
2464
2465         char *Announce;
2466
2467         int retry = 0;
2468         dictionary *d = NULL;
2469         char *username_ret = NULL;
2470         struct winbindd_response response;
2471
2472         ZERO_STRUCT(response);
2473
2474         ctrl = _pam_parse(pamh, flags, argc, argv, &d);
2475         if (ctrl == -1) {
2476                 ret = PAM_SYSTEM_ERR;
2477                 goto out;
2478         }
2479
2480         _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", pamh, ctrl, flags);
2481
2482         /* clearing offline bit for the auth in the password change */
2483         ctrl &= ~WINBIND_CACHED_LOGIN;
2484
2485         /*
2486          * First get the name of a user
2487          */
2488         ret = pam_get_user(pamh, &user, "Username: ");
2489         if (ret) {
2490                 _pam_log(pamh, ctrl, LOG_ERR,
2491                          "password - could not identify user");
2492                 goto out;
2493         }
2494
2495         if (user == NULL) {
2496                 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
2497                 ret = PAM_USER_UNKNOWN;
2498                 goto out;
2499         }
2500
2501         _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
2502
2503         /* check if this is really a user in winbindd, not only in NSS */
2504         ret = valid_user(pamh, ctrl, user);
2505         switch (ret) {
2506                 case 1:
2507                         ret = PAM_USER_UNKNOWN;
2508                         goto out;
2509                 case -1:
2510                         ret = PAM_SYSTEM_ERR;
2511                         goto out;
2512                 default:
2513                         break;
2514         }
2515
2516         /*
2517          * obtain and verify the current password (OLDAUTHTOK) for
2518          * the user.
2519          */
2520
2521         if (flags & PAM_PRELIM_CHECK) {
2522                 time_t pwdlastset_prelim = 0;
2523
2524                 /* instruct user what is happening */
2525 #define greeting "Changing password for "
2526                 Announce = (char *) malloc(sizeof(greeting) + strlen(user));
2527                 if (Announce == NULL) {
2528                         _pam_log(pamh, ctrl, LOG_CRIT,
2529                                  "password - out of memory");
2530                         ret = PAM_BUF_ERR;
2531                         goto out;
2532                 }
2533                 (void) strcpy(Announce, greeting);
2534                 (void) strcpy(Announce + sizeof(greeting) - 1, user);
2535 #undef greeting
2536
2537                 lctrl = ctrl | WINBIND__OLD_PASSWORD;
2538                 ret = _winbind_read_password(pamh, lctrl,
2539                                                 Announce,
2540                                                 "(current) NT password: ",
2541                                                 NULL,
2542                                                 (const char **) &pass_old);
2543                 if (ret != PAM_SUCCESS) {
2544                         _pam_log(pamh, ctrl, LOG_NOTICE,
2545                                  "password - (old) token not obtained");
2546                         goto out;
2547                 }
2548
2549                 /* verify that this is the password for this user */
2550
2551                 ret = winbind_auth_request(pamh, ctrl, user, pass_old,
2552                                            NULL, NULL, 0, &response,
2553                                            &pwdlastset_prelim, NULL);
2554
2555                 if (ret != PAM_ACCT_EXPIRED &&
2556                     ret != PAM_AUTHTOK_EXPIRED &&
2557                     ret != PAM_NEW_AUTHTOK_REQD &&
2558                     ret != PAM_SUCCESS) {
2559                         pass_old = NULL;
2560                         goto out;
2561                 }
2562
2563                 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET,
2564                              (void *)pwdlastset_prelim, NULL);
2565
2566                 ret = pam_set_item(pamh, PAM_OLDAUTHTOK,
2567                                    (const void *) pass_old);
2568                 pass_old = NULL;
2569                 if (ret != PAM_SUCCESS) {
2570                         _pam_log(pamh, ctrl, LOG_CRIT,
2571                                  "failed to set PAM_OLDAUTHTOK");
2572                 }
2573         } else if (flags & PAM_UPDATE_AUTHTOK) {
2574
2575                 time_t pwdlastset_update = 0;
2576
2577                 /*
2578                  * obtain the proposed password
2579                  */
2580
2581                 /*
2582                  * get the old token back.
2583                  */
2584
2585                 ret = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
2586
2587                 if (ret != PAM_SUCCESS) {
2588                         _pam_log(pamh, ctrl, LOG_NOTICE,
2589                                  "user not authenticated");
2590                         goto out;
2591                 }
2592
2593                 lctrl = ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
2594
2595                 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
2596                         lctrl |= WINBIND_USE_FIRST_PASS_ARG;
2597                 }
2598                 retry = 0;
2599                 ret = PAM_AUTHTOK_ERR;
2600                 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
2601                         /*
2602                          * use_authtok is to force the use of a previously entered
2603                          * password -- needed for pluggable password strength checking
2604                          */
2605
2606                         ret = _winbind_read_password(pamh, lctrl,
2607                                                      NULL,
2608                                                      "Enter new NT password: ",
2609                                                      "Retype new NT password: ",
2610                                                      (const char **)&pass_new);
2611
2612                         if (ret != PAM_SUCCESS) {
2613                                 _pam_log_debug(pamh, ctrl, LOG_ALERT,
2614                                                "password - "
2615                                                "new password not obtained");
2616                                 pass_old = NULL;/* tidy up */
2617                                 goto out;
2618                         }
2619
2620                         /*
2621                          * At this point we know who the user is and what they
2622                          * propose as their new password. Verify that the new
2623                          * password is acceptable.
2624                          */
2625
2626                         if (pass_new[0] == '\0') {/* "\0" password = NULL */
2627                                 pass_new = NULL;
2628                         }
2629                 }
2630
2631                 /*
2632                  * By reaching here we have approved the passwords and must now
2633                  * rebuild the password database file.
2634                  */
2635                 _pam_get_data(pamh, PAM_WINBIND_PWD_LAST_SET,
2636                               &pwdlastset_update);
2637
2638                 ret = winbind_chauthtok_request(pamh, ctrl, user, pass_old,
2639                                                 pass_new, pwdlastset_update);
2640                 if (ret) {
2641                         _pam_overwrite(pass_new);
2642                         _pam_overwrite(pass_old);
2643                         pass_old = pass_new = NULL;
2644                         goto out;
2645                 }
2646
2647                 if (_pam_require_krb5_auth_after_chauthtok(pamh, ctrl, user)) {
2648
2649                         const char *member = NULL;
2650                         const char *cctype = NULL;
2651                         int warn_pwd_expire;
2652
2653                         member = get_member_from_config(pamh, argc, argv,
2654                                                         ctrl, d);
2655                         cctype = get_krb5_cc_type_from_config(pamh, argc, argv,
2656                                                               ctrl, d);
2657                         warn_pwd_expire = get_warn_pwd_expire_from_config(pamh,
2658                                                                           argc,
2659                                                                           argv,
2660                                                                           ctrl,
2661                                                                           d);
2662
2663
2664                         ret = winbind_auth_request(pamh, ctrl, user, pass_new,
2665                                                    member, cctype, 0, &response,
2666                                                    NULL, &username_ret);
2667                         _pam_overwrite(pass_new);
2668                         _pam_overwrite(pass_old);
2669                         pass_old = pass_new = NULL;
2670
2671                         if (ret == PAM_SUCCESS) {
2672
2673                                 /* warn a user if the password is about to
2674                                  * expire soon */
2675                                 _pam_warn_password_expiry(pamh, ctrl, &response,
2676                                                           warn_pwd_expire,
2677                                                           NULL);
2678
2679                                 /* set some info3 info for other modules in the
2680                                  * stack */
2681                                 _pam_set_data_info3(pamh, ctrl, &response);
2682
2683                                 /* put krb5ccname into env */
2684                                 _pam_setup_krb5_env(pamh, ctrl,
2685                                                     response.data.auth.krb5ccname);
2686
2687                                 if (username_ret) {
2688                                         pam_set_item(pamh, PAM_USER,
2689                                                      username_ret);
2690                                         _pam_log_debug(pamh, ctrl, LOG_INFO,
2691                                                        "Returned user was '%s'",
2692                                                        username_ret);
2693                                         free(username_ret);
2694                                 }
2695                         }
2696
2697                         goto out;
2698                 }
2699         } else {
2700                 ret = PAM_SERVICE_ERR;
2701         }
2702
2703 out:
2704         if (d) {
2705                 iniparser_freedict(d);
2706         }
2707
2708         /* Deal with offline errors. */
2709         PAM_WB_REMARK_CHECK_RESPONSE(pamh, ctrl, response,
2710                                      "NT_STATUS_NO_LOGON_SERVERS");
2711         PAM_WB_REMARK_CHECK_RESPONSE(pamh, ctrl, response,
2712                                      "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
2713         PAM_WB_REMARK_CHECK_RESPONSE(pamh, ctrl, response,
2714                                      "NT_STATUS_ACCESS_DENIED");
2715
2716         _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", pamh, ctrl, ret);
2717
2718         return ret;
2719 }
2720
2721 #ifdef PAM_STATIC
2722
2723 /* static module data */
2724
2725 struct pam_module _pam_winbind_modstruct = {
2726         MODULE_NAME,
2727         pam_sm_authenticate,
2728         pam_sm_setcred,
2729         pam_sm_acct_mgmt,
2730         pam_sm_open_session,
2731         pam_sm_close_session,
2732         pam_sm_chauthtok
2733 };
2734
2735 #endif
2736
2737 /*
2738  * Copyright (c) Andrew Tridgell  <tridge@samba.org>   2000
2739  * Copyright (c) Tim Potter       <tpot@samba.org>     2000
2740  * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
2741  * Copyright (c) Guenther Deschner <gd@samba.org>      2005-2007
2742  * Copyright (c) Jan Rêkorajski 1999.
2743  * Copyright (c) Andrew G. Morgan 1996-8.
2744  * Copyright (c) Alex O. Yuriev, 1996.
2745  * Copyright (c) Cristian Gafton 1996.
2746  * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
2747  *
2748  * Redistribution and use in source and binary forms, with or without
2749  * modification, are permitted provided that the following conditions
2750  * are met:
2751  * 1. Redistributions of source code must retain the above copyright
2752  *    notice, and the entire permission notice in its entirety,
2753  *    including the disclaimer of warranties.
2754  * 2. Redistributions in binary form must reproduce the above copyright
2755  *    notice, this list of conditions and the following disclaimer in the
2756  *    documentation and/or other materials provided with the distribution.
2757  * 3. The name of the author may not be used to endorse or promote
2758  *    products derived from this software without specific prior
2759  *    written permission.
2760  *
2761  * ALTERNATIVELY, this product may be distributed under the terms of
2762  * the GNU Public License, in which case the provisions of the GPL are
2763  * required INSTEAD OF the above restrictions.  (This clause is
2764  * necessary due to a potential bad interaction between the GPL and
2765  * the restrictions contained in a BSD-style copyright.)
2766  *
2767  * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
2768  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2769  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2770  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2771  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2772  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2773  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2774  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2775  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2776  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2777  * OF THE POSSIBILITY OF SUCH DAMAGE.
2778  */