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