r13316: Let the carnage begin....
[tprouty/samba.git] / source / 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-2006
7
8    largely based on pam_userdb by Cristian Gafton <gafton@redhat.com> 
9    also contains large slabs of code from pam_unix by Elliot Lee <sopwith@redhat.com>
10    (see copyright below for full details)
11 */
12
13 #include "includes.h"
14 #include "pam_winbind.h"
15
16 /* data tokens */
17
18 #define MAX_PASSWD_TRIES        3
19
20 /* some syslogging */
21 static void _pam_log(int err, const char *format, ...)
22 {
23         va_list args;
24
25         va_start(args, format);
26         openlog(MODULE_NAME, LOG_CONS|LOG_PID, LOG_AUTH);
27         vsyslog(err, format, args);
28         va_end(args);
29         closelog();
30 }
31
32 static void _pam_log_debug(int ctrl, int err, const char *format, ...)
33 {
34         va_list args;
35
36         if (!(ctrl & WINBIND_DEBUG_ARG)) {
37                 return;
38         }
39
40         va_start(args, format);
41         openlog(MODULE_NAME, LOG_CONS|LOG_PID, LOG_AUTH);
42         vsyslog(err, format, args);
43         va_end(args);
44         closelog();
45 }
46
47 static int _pam_parse(int argc, const char **argv)
48 {
49         int ctrl = 0;
50
51         load_case_tables();
52
53         if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
54                 return -1;
55         }
56
57         if (lp_parm_bool(-1, "pam_winbind", "cached_login", False)) {
58                 ctrl |= WINBIND_CACHED_LOGIN;
59         }
60         if (lp_parm_bool(-1, "pam_winbind", "krb5_auth", False)) {
61                 ctrl |= WINBIND_KRB5_AUTH;
62         }
63         if (lp_parm_const_string(-1, "pam_winbind", "krb5_ccache_type", NULL) != NULL) {
64                 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
65         }
66         if ((lp_parm_const_string(-1, "pam_winbind", "require-membership-of", NULL) != NULL) || 
67             (lp_parm_const_string(-1, "pam_winbind", "require_membership_of", NULL) != NULL)) { 
68                 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
69         }
70         if (lp_parm_bool(-1, "pam_winbind", "create_homedir", False)) {
71                 ctrl |= WINBIND_CREATE_HOMEDIR;
72         }
73
74         /* step through arguments */
75         for (; argc-- > 0; ++argv) {
76
77                 /* generic options */
78
79                 if (!StrCaseCmp(*argv, "debug"))
80                         ctrl |= WINBIND_DEBUG_ARG;
81                 else if (strequal(*argv, "use_authtok"))
82                         ctrl |= WINBIND_USE_AUTHTOK_ARG;
83                 else if (strequal(*argv, "use_first_pass"))
84                         ctrl |= WINBIND_USE_FIRST_PASS_ARG;
85                 else if (strequal(*argv, "try_first_pass"))
86                         ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
87                 else if (strequal(*argv, "unknown_ok"))
88                         ctrl |= WINBIND_UNKNOWN_OK_ARG;
89                 else if (strnequal(*argv, "require_membership_of", strlen("require_membership_of")))
90                         ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
91                 else if (strnequal(*argv, "require-membership-of", strlen("require-membership-of")))
92                         ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
93                 else if (strequal(*argv, "krb5_auth"))
94                         ctrl |= WINBIND_KRB5_AUTH;
95                 else if (strnequal(*argv, "krb5_ccache_type", strlen("krb5_ccache_type")))
96                         ctrl |= WINBIND_KRB5_CCACHE_TYPE;
97                 else if (strequal(*argv, "cached_login"))
98                         ctrl |= WINBIND_CACHED_LOGIN;
99                 else if (strequal(*argv, "create_homedir"))
100                         ctrl |= WINBIND_CREATE_HOMEDIR;
101                 else {
102                         _pam_log(LOG_ERR, "pam_parse: unknown option; %s", *argv);
103                 }
104
105         }
106         return ctrl;
107 };
108
109 static void _pam_winbind_cleanup_func(pam_handle_t *pamh, void *data, int error_status)
110 {
111         SAFE_FREE(data);
112 }
113
114 static const struct ntstatus_errors {
115         const char *ntstatus_string;
116         const char *error_string;
117 } ntstatus_errors[] = {
118         {"NT_STATUS_OK", "Success"},
119         {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
120         {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
121         {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
122         {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
123         {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
124         {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
125         {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
126         {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
127         {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
128         {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
129         {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
130         {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
131         {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
132         {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
133         {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
134         {NULL, NULL}
135 };
136
137 const char *_get_ntstatus_error_string(const char *nt_status_string) 
138 {
139         int i;
140         for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
141                 if (strequal(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
142                         return ntstatus_errors[i].error_string;
143                 }
144         }
145         return NULL;
146 }
147
148 /* --- authentication management functions --- */
149
150 /* Attempt a conversation */
151
152 static int converse(pam_handle_t *pamh, int nargs,
153                     struct pam_message **message,
154                     struct pam_response **response)
155 {
156         int retval;
157         struct pam_conv *conv;
158
159         retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv );
160         if (retval == PAM_SUCCESS) {
161                 retval = conv->conv(nargs, (const struct pam_message **)message,
162                                     response, conv->appdata_ptr);
163         }
164         
165         return retval; /* propagate error status */
166 }
167
168
169 static int _make_remark(pam_handle_t * pamh, int type, const char *text)
170 {
171         int retval = PAM_SUCCESS;
172
173         struct pam_message *pmsg[1], msg[1];
174         struct pam_response *resp;
175         
176         pmsg[0] = &msg[0];
177         msg[0].msg = text;
178         msg[0].msg_style = type;
179         
180         resp = NULL;
181         retval = converse(pamh, 1, pmsg, &resp);
182         
183         if (resp) {
184                 _pam_drop_reply(resp, 1);
185         }
186         return retval;
187 }
188
189 static int _make_remark_format(pam_handle_t * pamh, int type, const char *format, ...)
190 {
191         va_list args;
192         char *var;
193
194         va_start(args, format);
195         vasprintf(&var, format, args);
196         va_end(args);
197
198         return _make_remark(pamh, type, var);
199 }
200
201 static int pam_winbind_request(pam_handle_t * pamh, int ctrl,
202                                enum winbindd_cmd req_type,
203                                struct winbindd_request *request,
204                                struct winbindd_response *response)
205 {
206         /* Fill in request and send down pipe */
207         init_request(request, req_type);
208         
209         if (write_sock(request, sizeof(*request), 0) == -1) {
210                 _pam_log(LOG_ERR, "write to socket failed!");
211                 close_sock();
212                 return PAM_SERVICE_ERR;
213         }
214         
215         /* Wait for reply */
216         if (read_reply(response) == -1) {
217                 _pam_log(LOG_ERR, "read from socket failed!");
218                 close_sock();
219                 return PAM_SERVICE_ERR;
220         }
221
222         /* We are done with the socket - close it and avoid mischeif */
223         close_sock();
224
225         /* Copy reply data from socket */
226         if (response->result != WINBINDD_OK) {
227                 if (response->data.auth.pam_error != PAM_SUCCESS) {
228                         _pam_log(LOG_ERR, "request failed: %s, PAM error was %d, NT error was %s", 
229                                  response->data.auth.error_string,
230                                  response->data.auth.pam_error,
231                                  response->data.auth.nt_status_string);
232                         return response->data.auth.pam_error;
233                 } else {
234                         _pam_log(LOG_ERR, "request failed, but PAM error 0!");
235                         return PAM_SERVICE_ERR;
236                 }
237         }
238
239         return PAM_SUCCESS;
240 }
241
242 static int pam_winbind_request_log(pam_handle_t * pamh, 
243                                    int ctrl,
244                                    enum winbindd_cmd req_type,
245                                    struct winbindd_request *request,
246                                    struct winbindd_response *response,
247                                    const char *user)
248 {
249         int retval;
250
251         retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
252
253         switch (retval) {
254         case PAM_AUTH_ERR:
255                 /* incorrect password */
256                 _pam_log(LOG_WARNING, "user `%s' denied access (incorrect password or invalid membership)", user);
257                 return retval;
258         case PAM_ACCT_EXPIRED:
259                 /* account expired */
260                 _pam_log(LOG_WARNING, "user `%s' account expired", user);
261                 return retval;
262         case PAM_AUTHTOK_EXPIRED:
263                 /* password expired */
264                 _pam_log(LOG_WARNING, "user `%s' password expired", user);
265                 return retval;
266         case PAM_NEW_AUTHTOK_REQD:
267                 /* new password required */
268                 _pam_log(LOG_WARNING, "user `%s' new password required", user);
269                 return retval;
270         case PAM_USER_UNKNOWN:
271                 /* the user does not exist */
272                 _pam_log_debug(ctrl, LOG_NOTICE, "user `%s' not found",
273                                  user);
274                 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
275                         return PAM_IGNORE;
276                 }        
277                 return retval;
278         case PAM_SUCCESS:
279                 if (req_type == WINBINDD_PAM_AUTH) {
280                         /* Otherwise, the authentication looked good */
281                         _pam_log(LOG_NOTICE, "user '%s' granted access", user);
282                 } else if (req_type == WINBINDD_PAM_CHAUTHTOK) {
283                         /* Otherwise, the authentication looked good */
284                         _pam_log(LOG_NOTICE, "user '%s' password changed", user);
285                 } else { 
286                         /* Otherwise, the authentication looked good */
287                         _pam_log(LOG_NOTICE, "user '%s' OK", user);
288                 }
289         
290                 return retval;
291         default:
292                 /* we don't know anything about this return value */
293                 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s')",
294                          retval, user);
295                 return retval;
296         }
297 }
298
299 /* talk to winbindd */
300 static int winbind_auth_request(pam_handle_t * pamh, 
301                                 int ctrl, 
302                                 const char *user, 
303                                 const char *pass, 
304                                 const char *member, 
305                                 const char *cctype,
306                                 int process_result)
307 {
308         struct winbindd_request request;
309         struct winbindd_response response;
310         int ret;
311
312         ZERO_STRUCT(request);
313         ZERO_STRUCT(response);
314
315         strncpy(request.data.auth.user, user, 
316                 sizeof(request.data.auth.user)-1);
317
318         strncpy(request.data.auth.pass, pass, 
319                 sizeof(request.data.auth.pass)-1);
320
321         request.data.auth.krb5_cc_type[0] = '\0';
322         request.data.auth.uid = -1;
323         
324         request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_CONTACT_TRUSTDOM;
325
326         if (ctrl & WINBIND_KRB5_AUTH) {
327
328                 struct passwd *pwd = NULL;
329
330                 _pam_log_debug(ctrl, LOG_DEBUG, "enabling krb5 login flag\n"); 
331
332                 request.flags |= WBFLAG_PAM_KRB5 | WBFLAG_PAM_FALLBACK_AFTER_KRB5;
333
334                 pwd = getpwnam(user);
335                 if (pwd == NULL) {
336                         return PAM_USER_UNKNOWN;
337                 }
338                 request.data.auth.uid = pwd->pw_uid;
339         }
340
341         if (ctrl & WINBIND_CACHED_LOGIN) {
342                 _pam_log_debug(ctrl, LOG_DEBUG, "enabling cached login flag\n"); 
343                 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
344         }
345
346         if (cctype != NULL) {
347                 strncpy(request.data.auth.krb5_cc_type, cctype, 
348                         sizeof(request.data.auth.krb5_cc_type) - 1);
349                 _pam_log_debug(ctrl, LOG_DEBUG, "enabling request for a %s krb5 ccache\n", cctype); 
350         }
351
352         request.data.auth.require_membership_of_sid[0] = '\0';
353
354         if (member != NULL) {
355                 strncpy(request.data.auth.require_membership_of_sid, member, 
356                         sizeof(request.data.auth.require_membership_of_sid)-1);
357         }
358
359         /* lookup name? */ 
360         if ( (member != NULL) && (strncmp("S-", member, 2) != 0) ) {
361                 
362                 struct winbindd_request sid_request;
363                 struct winbindd_response sid_response;
364
365                 ZERO_STRUCT(sid_request);
366                 ZERO_STRUCT(sid_response);
367
368                 _pam_log_debug(ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", member);
369
370                 /* fortunatly winbindd can handle non-separated names */
371                 fstrcpy(sid_request.data.name.name, member);
372
373                 if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME, &sid_request, &sid_response, user)) {
374                         _pam_log(LOG_INFO, "could not lookup name: %s\n", member); 
375                         return PAM_AUTH_ERR;
376                 }
377
378                 member = sid_response.data.sid.sid;
379
380                 strncpy(request.data.auth.require_membership_of_sid, member, 
381                         sizeof(request.data.auth.require_membership_of_sid)-1);
382         }
383         
384         ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
385
386         if ((ctrl & WINBIND_KRB5_AUTH) && 
387             response.data.auth.krb5ccname[0] != '\0') {
388
389                 char var[PATH_MAX];
390
391                 _pam_log_debug(ctrl, LOG_DEBUG, "request returned KRB5CCNAME: %s", 
392                                response.data.auth.krb5ccname);
393         
394                 snprintf(var, sizeof(var), "KRB5CCNAME=%s", response.data.auth.krb5ccname);
395         
396                 ret = pam_putenv(pamh, var);
397                 if (ret != PAM_SUCCESS) {
398                         _pam_log(LOG_ERR, "failed to set KRB5CCNAME to %s", var);
399                         return ret;
400                 }
401         }
402
403         if (!process_result) {
404                 return ret;
405         }
406
407         if (ret) {
408                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_EXPIRED");
409                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_MUST_CHANGE");
410                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_WORKSTATION");
411                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_LOGON_HOURS");
412                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_EXPIRED");
413                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_DISABLED");
414                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_LOCKED_OUT");
415                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
416                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
417                 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
418         }
419
420         /* handle the case where the auth was ok, but the password must expire right now */
421         /* good catch from Ralf Haferkamp: an expiry of "never" is translated to -1 */
422         if ((response.data.auth.policy.expire > 0) && 
423             (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire < time(NULL))) {
424
425                 ret = PAM_AUTHTOK_EXPIRED;
426
427                 _pam_log_debug(ctrl, LOG_DEBUG,"Password has expired (Password was last set: %d, "
428                                "the policy says it should expire here %d (now it's: %d)\n",
429                                response.data.auth.info3.pass_last_set_time,
430                                response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire,
431                                time(NULL));
432
433                 PAM_WB_REMARK_DIRECT_RET(pamh, "NT_STATUS_PASSWORD_EXPIRED");
434
435         }
436
437         /* warn a user if the password is about to expire soon */
438         if ((response.data.auth.policy.expire) && 
439             (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire > time(NULL) ) ) {
440
441                 int days = response.data.auth.policy.expire / SECONDS_PER_DAY;
442                 if (days <= DAYS_TO_WARN_BEFORE_PWD_EXPIRES) {
443                         _make_remark_format(pamh, PAM_TEXT_INFO, "Your password will expire in %d days", days);
444                 }
445         }
446
447         if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
448                 _make_remark(pamh, PAM_ERROR_MSG, "Logging on using cached account. Network ressources can be unavailable");
449         }
450
451         /* save the CIFS homedir for pam_cifs / pam_mount */
452         if (response.data.auth.info3.home_dir[0] != '\0') {
453                 char *buf;
454
455                 if (!asprintf(&buf, "%s", response.data.auth.info3.home_dir)) {
456                         return PAM_BUF_ERR;
457                 }
458
459                 pam_set_data( pamh, PAM_WINBIND_HOMEDIR, (void *)buf, _pam_winbind_cleanup_func);
460         }
461
462         return ret;
463 }
464
465 /* talk to winbindd */
466 static int winbind_chauthtok_request(pam_handle_t * pamh,
467                                      int ctrl,
468                                      const char *user, 
469                                      const char *oldpass,
470                                      const char *newpass) 
471 {
472         struct winbindd_request request;
473         struct winbindd_response response;
474         int ret;
475
476         ZERO_STRUCT(request);
477         ZERO_STRUCT(response);
478
479         if (request.data.chauthtok.user == NULL) return -2;
480
481         strncpy(request.data.chauthtok.user, user, 
482                 sizeof(request.data.chauthtok.user) - 1);
483
484         if (oldpass != NULL) {
485                 strncpy(request.data.chauthtok.oldpass, oldpass, 
486                         sizeof(request.data.chauthtok.oldpass) - 1);
487         } else {
488                 request.data.chauthtok.oldpass[0] = '\0';
489         }
490         
491         if (newpass != NULL) {
492                 strncpy(request.data.chauthtok.newpass, newpass, 
493                         sizeof(request.data.chauthtok.newpass) - 1);
494         } else {
495                 request.data.chauthtok.newpass[0] = '\0';
496         }
497
498         if (ctrl & WINBIND_KRB5_AUTH) {
499                 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
500         }
501
502         ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
503
504         if (ret == PAM_SUCCESS) {
505                 return ret;
506         }
507
508         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_BACKUP_CONTROLLER");
509         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCESS_DENIED");
510
511         /* TODO: tell the min pwd length ? */
512         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_SHORT");
513
514         /* TODO: tell the minage ? */
515         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_RECENT");
516
517         /* TODO: tell the history length ? */
518         PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
519
520         if (strequal(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
521
522                 /* FIXME: avoid to send multiple PAM messages after another */
523                 switch (response.data.auth.reject_reason) {
524                         case 0:
525                                 break;
526                         case REJECT_REASON_TOO_SHORT:
527                                 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_SHORT");
528                                 break;
529                         case REJECT_REASON_IN_HISTORY:
530                                 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_HISTORY_CONFLICT");
531                                 break;
532                         case REJECT_REASON_NOT_COMPLEX:
533                                 _make_remark(pamh, PAM_ERROR_MSG, "Password does not meet complexity requirements");
534                                 break;
535                         default:
536                                 _pam_log_debug(ctrl, LOG_DEBUG,
537                                                "unknown password change reject reason: %d", 
538                                                response.data.auth.reject_reason);
539                                 break;
540                 }
541
542                 _make_remark_format(pamh, PAM_ERROR_MSG,  
543                         "Your password must be at least %d characters; "
544                         "cannot repeat any of the your previous %d passwords"
545                         "%s. "
546                         "Please type a different password. "
547                         "Type a password which meets these requirements in both text boxes.",
548                         response.data.auth.policy.min_length_password,
549                         response.data.auth.policy.password_history,
550                         (response.data.auth.policy.password_properties & DOMAIN_PASSWORD_COMPLEX) ? 
551                                 "; must contain capitals, numerals or punctuation; and cannot contain your account or full name" : 
552                                 "");
553
554         }
555
556         return ret;
557 }
558
559 /*
560  * Checks if a user has an account
561  *
562  * return values:
563  *       1  = User not found
564  *       0  = OK
565  *      -1  = System error
566  */
567 static int valid_user(const char *user)
568 {
569         if (getpwnam(user)) return 0;
570         return 1;
571 }
572
573 static char *_pam_delete(register char *xx)
574 {
575         _pam_overwrite(xx);
576         _pam_drop(xx);
577         return NULL;
578 }
579
580 /*
581  * obtain a password from the user
582  */
583
584 static int _winbind_read_password(pam_handle_t * pamh,
585                                   unsigned int ctrl,
586                                   const char *comment,
587                                   const char *prompt1,
588                                   const char *prompt2,
589                                   const char **pass)
590 {
591         int authtok_flag;
592         int retval;
593         const char *item;
594         char *token;
595
596         /*
597          * make sure nothing inappropriate gets returned
598          */
599
600         *pass = token = NULL;
601
602         /*
603          * which authentication token are we getting?
604          */
605
606         authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
607
608         /*
609          * should we obtain the password from a PAM item ?
610          */
611
612         if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
613                 retval = pam_get_item(pamh, authtok_flag, (const void **) &item);
614                 if (retval != PAM_SUCCESS) {
615                         /* very strange. */
616                         _pam_log(LOG_ALERT, 
617                                  "pam_get_item returned error to unix-read-password"
618                             );
619                         return retval;
620                 } else if (item != NULL) {      /* we have a password! */
621                         *pass = item;
622                         item = NULL;
623                         return PAM_SUCCESS;
624                 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
625                         return PAM_AUTHTOK_RECOVER_ERR;         /* didn't work */
626                 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
627                            && off(WINBIND__OLD_PASSWORD, ctrl)) {
628                         return PAM_AUTHTOK_RECOVER_ERR;
629                 }
630         }
631         /*
632          * getting here implies we will have to get the password from the
633          * user directly.
634          */
635
636         {
637                 struct pam_message msg[3], *pmsg[3];
638                 struct pam_response *resp;
639                 int i, replies;
640
641                 /* prepare to converse */
642
643                 if (comment != NULL) {
644                         pmsg[0] = &msg[0];
645                         msg[0].msg_style = PAM_TEXT_INFO;
646                         msg[0].msg = comment;
647                         i = 1;
648                 } else {
649                         i = 0;
650                 }
651
652                 pmsg[i] = &msg[i];
653                 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
654                 msg[i++].msg = prompt1;
655                 replies = 1;
656
657                 if (prompt2 != NULL) {
658                         pmsg[i] = &msg[i];
659                         msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
660                         msg[i++].msg = prompt2;
661                         ++replies;
662                 }
663                 /* so call the conversation expecting i responses */
664                 resp = NULL;
665                 retval = converse(pamh, i, pmsg, &resp);
666
667                 if (resp != NULL) {
668
669                         /* interpret the response */
670
671                         if (retval == PAM_SUCCESS) {    /* a good conversation */
672
673                                 token = SMB_STRDUP(resp[i - replies].resp);
674                                 if (token != NULL) {
675                                         if (replies == 2) {
676                                                 /* verify that password entered correctly */
677                                                 if (!resp[i - 1].resp
678                                                     || StrCaseCmp(token, resp[i - 1].resp)) {
679                                                         _pam_delete(token);     /* mistyped */
680                                                         retval = PAM_AUTHTOK_RECOVER_ERR;
681                                                         _make_remark(pamh, PAM_ERROR_MSG, MISTYPED_PASS);
682                                                 }
683                                         }
684                                 } else {
685                                         _pam_log(LOG_NOTICE
686                                                  ,"could not recover authentication token");
687                                 }
688
689                         }
690                         /*
691                          * tidy up the conversation (resp_retcode) is ignored
692                          * -- what is it for anyway? AGM
693                          */
694
695                         _pam_drop_reply(resp, i);
696
697                 } else {
698                         retval = (retval == PAM_SUCCESS)
699                             ? PAM_AUTHTOK_RECOVER_ERR : retval;
700                 }
701         }
702
703         if (retval != PAM_SUCCESS) {
704                 _pam_log_debug(ctrl, LOG_DEBUG,
705                                  "unable to obtain a password");
706                 return retval;
707         }
708         /* 'token' is the entered password */
709
710         /* we store this password as an item */
711         
712         retval = pam_set_item(pamh, authtok_flag, token);
713         _pam_delete(token);     /* clean it up */
714         if (retval != PAM_SUCCESS || 
715             (retval = pam_get_item(pamh, authtok_flag, (const void **) &item)) != PAM_SUCCESS) {
716                 
717                 _pam_log(LOG_CRIT, "error manipulating password");
718                 return retval;
719                 
720         }
721
722         *pass = item;
723         item = NULL;            /* break link to password */
724
725         return PAM_SUCCESS;
726 }
727
728 const char *get_conf_item_string(int argc, 
729                                  const char **argv, 
730                                  int ctrl, 
731                                  const char *item, 
732                                  int flag)
733 {
734         int i = 0;
735         char *parm = NULL;
736         const char *parm_opt = NULL;
737
738         if (!(ctrl & flag)) {
739                 goto out;
740         }
741
742         /* let the pam opt take precedence over the smb.conf option */
743         parm_opt = lp_parm_const_string(-1, "pam_winbind", item, NULL);
744
745         for ( i=0; i<argc; i++ ) {
746
747                 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
748                         char *p;
749
750                         parm = SMB_STRDUP(argv[i]);
751
752                         if ( (p = strchr( parm, '=' )) == NULL) {
753                                 _pam_log(LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
754                                 goto out;
755                         }
756                         SAFE_FREE(parm);
757                         _pam_log_debug(ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
758                         return p + 1;
759                 }
760         }
761
762         _pam_log_debug(ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
763 out:
764         SAFE_FREE(parm);
765         return parm_opt;
766 }
767
768 const char *get_krb5_cc_type_from_config(int argc, const char **argv, int ctrl)
769 {
770         return get_conf_item_string(argc, argv, ctrl, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
771 }
772
773 const char *get_member_from_config(int argc, const char **argv, int ctrl)
774 {
775         const char *ret = NULL;
776         ret = get_conf_item_string(argc, argv, ctrl, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
777         if (ret) {
778                 return ret;
779         }
780         return get_conf_item_string(argc, argv, ctrl, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
781 }
782
783 PAM_EXTERN
784 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
785                         int argc, const char **argv)
786 {
787         const char *username;
788         const char *password;
789         const char *member = NULL;
790         const char *cctype = NULL;
791         int retval = PAM_AUTH_ERR;
792
793         /* parse arguments */
794         int ctrl = _pam_parse(argc, argv);
795         if (ctrl == -1) {
796                 return PAM_SYSTEM_ERR;
797         }
798
799         _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_authenticate");
800
801         /* Get the username */
802         retval = pam_get_user(pamh, &username, NULL);
803         if ((retval != PAM_SUCCESS) || (!username)) {
804                 _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
805                 return PAM_SERVICE_ERR;
806         }
807
808         retval = _winbind_read_password(pamh, ctrl, NULL, 
809                                         "Password: ", NULL,
810                                         &password);
811
812         if (retval != PAM_SUCCESS) {
813                 _pam_log(LOG_ERR, "Could not retrieve user's password");
814                 return PAM_AUTHTOK_ERR;
815         }
816
817         /* Let's not give too much away in the log file */
818
819 #ifdef DEBUG_PASSWORD
820         _pam_log_debug(ctrl, LOG_INFO, "Verify user `%s' with password `%s'", 
821                        username, password);
822 #else
823         _pam_log_debug(ctrl, LOG_INFO, "Verify user `%s'", username);
824 #endif
825
826         member = get_member_from_config(argc, argv, ctrl);
827
828         cctype = get_krb5_cc_type_from_config(argc, argv, ctrl);
829
830         /* Now use the username to look up password */
831         retval = winbind_auth_request(pamh, ctrl, username, password, member, cctype, True);
832
833         if (retval == PAM_NEW_AUTHTOK_REQD ||
834             retval == PAM_AUTHTOK_EXPIRED) {
835
836                 char *buf;
837
838                 if (!asprintf(&buf, "%d", retval)) {
839                         return PAM_BUF_ERR;
840                 }
841
842                 pam_set_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (void *)buf, _pam_winbind_cleanup_func);
843
844                 return PAM_SUCCESS;
845         }
846
847         return retval;
848 }
849
850 PAM_EXTERN
851 int pam_sm_setcred(pam_handle_t *pamh, int flags,
852                    int argc, const char **argv)
853 {
854         /* parse arguments */
855         int ctrl = _pam_parse(argc, argv);
856         if (ctrl == -1) {
857                 return PAM_SYSTEM_ERR;
858         }
859
860         _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_setcred");
861
862         if (flags & PAM_DELETE_CRED) {
863                 return pam_sm_close_session(pamh, flags, argc, argv);
864         }
865
866         return PAM_SUCCESS;
867 }
868
869 /*
870  * Account management. We want to verify that the account exists 
871  * before returning PAM_SUCCESS
872  */
873 PAM_EXTERN
874 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
875                    int argc, const char **argv)
876 {
877         const char *username;
878         int retval = PAM_USER_UNKNOWN;
879         void *tmp = NULL;
880
881         /* parse arguments */
882         int ctrl = _pam_parse(argc, argv);
883         if (ctrl == -1) {
884                 return PAM_SYSTEM_ERR;
885         }
886
887         _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_acct_mgmt");
888
889
890         /* Get the username */
891         retval = pam_get_user(pamh, &username, NULL);
892         if ((retval != PAM_SUCCESS) || (!username)) {
893                 _pam_log_debug(ctrl, LOG_DEBUG,"can not get the username");
894                 return PAM_SERVICE_ERR;
895         }
896
897         /* Verify the username */
898         retval = valid_user(username);
899         switch (retval) {
900         case -1:
901                 /* some sort of system error. The log was already printed */
902                 return PAM_SERVICE_ERR;
903         case 1:
904                 /* the user does not exist */
905                 _pam_log_debug(ctrl, LOG_NOTICE, "user `%s' not found", username);
906                 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
907                         return PAM_IGNORE;
908                 }
909                 return PAM_USER_UNKNOWN;
910         case 0:
911                 pam_get_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (const void **)&tmp);
912                 if (tmp != NULL) {
913                         retval = atoi(tmp);
914                         switch (retval) {
915                         case PAM_AUTHTOK_EXPIRED:
916                                 /* fall through, since new token is required in this case */
917                         case PAM_NEW_AUTHTOK_REQD:
918                                 _pam_log(LOG_WARNING, "pam_sm_acct_mgmt success but %s is set", 
919                                          PAM_WINBIND_NEW_AUTHTOK_REQD);
920                                 _pam_log(LOG_NOTICE, "user '%s' needs new password", username);
921                                 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
922                                 return PAM_NEW_AUTHTOK_REQD; 
923                         default:
924                                 _pam_log(LOG_WARNING, "pam_sm_acct_mgmt success");
925                                 _pam_log(LOG_NOTICE, "user '%s' granted access", username);
926                                 return PAM_SUCCESS;
927                         }
928                 }
929
930                 /* Otherwise, the authentication looked good */
931                 _pam_log(LOG_NOTICE, "user '%s' granted access", username);
932                 return PAM_SUCCESS;
933         default:
934                 /* we don't know anything about this return value */
935                 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s')", 
936                          retval, username);
937                 return PAM_SERVICE_ERR;
938         }
939
940         /* should not be reached */
941         return PAM_IGNORE;
942 }
943
944 PAM_EXTERN
945 int pam_sm_open_session(pam_handle_t *pamh, int flags,
946                         int argc, const char **argv)
947 {
948         /* parse arguments */
949         int ctrl = _pam_parse(argc, argv);
950         if (ctrl == -1) {
951                 return PAM_SYSTEM_ERR;
952         }
953
954         _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_open_session handler");
955
956
957         if (ctrl & WINBIND_CREATE_HOMEDIR) {
958
959                 struct passwd *pwd = NULL;
960                 const char *username;
961                 int ret;
962                 fstring tok;
963                 fstring create_dir;
964                 SMB_STRUCT_STAT sbuf;
965
966                 /* Get the username */
967                 ret = pam_get_user(pamh, &username, NULL);
968                 if ((ret != PAM_SUCCESS) || (!username)) {
969                         _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
970                         return PAM_SERVICE_ERR;
971                 }
972
973                 pwd = getpwnam(username);
974                 if (pwd == NULL) {
975                         _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
976                         return PAM_SERVICE_ERR;
977                 }
978
979                 _pam_log_debug(ctrl, LOG_DEBUG, "homedir is: %s", pwd->pw_dir);
980
981                 if (directory_exist(pwd->pw_dir, &sbuf)) {
982                         return PAM_SUCCESS;
983                 }
984
985                 fstrcpy(create_dir, "/");
986                 while (next_token((const char **)&pwd->pw_dir, tok, "/", sizeof(tok))) {
987                         
988                         mode_t mode = 0755;
989
990                         fstrcat(create_dir, tok);
991                         fstrcat(create_dir, "/");
992
993                         if (!directory_exist(create_dir, &sbuf)) {
994                                 if (mkdir(create_dir, mode) != 0) {
995                                         _pam_log(LOG_ERR, "could not create dir: %s (%s)", 
996                                                  create_dir, strerror(errno));
997                                         return PAM_SERVICE_ERR;
998                                 }
999                         } 
1000                 }
1001
1002                 if (sys_chown(create_dir, pwd->pw_uid, pwd->pw_gid) != 0) {
1003                         _pam_log(LOG_ERR, "failed to chown user homedir: %s (%s)", 
1004                                  create_dir, strerror(errno));
1005                         return PAM_SERVICE_ERR;
1006                 }
1007         }
1008
1009         return PAM_SUCCESS;
1010 }
1011
1012 PAM_EXTERN
1013 int pam_sm_close_session(pam_handle_t *pamh, int flags,
1014                          int argc, const char **argv)
1015 {
1016         /* parse arguments */
1017         int ctrl = _pam_parse(argc, argv);
1018         if (ctrl == -1) {
1019                 return PAM_SYSTEM_ERR;
1020         }
1021
1022         _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_close_session handler");
1023
1024         if (!(flags & PAM_DELETE_CRED)) {
1025                 return PAM_SUCCESS;
1026         }
1027
1028         if (ctrl & WINBIND_KRB5_AUTH) {
1029
1030                 /* destroy the ccache here */
1031                 struct winbindd_request request;
1032                 struct winbindd_response response;
1033                 const char *user;
1034                 const char *ccname = NULL;
1035                 struct passwd *pwd = NULL;
1036
1037                 int retval;
1038
1039                 ZERO_STRUCT(request);
1040                 ZERO_STRUCT(response);
1041
1042                 retval = pam_get_user(pamh, &user, "Username: ");
1043                 if (retval == PAM_SUCCESS) {
1044                         if (user == NULL) {
1045                                 _pam_log(LOG_ERR, "username was NULL!");
1046                                 return PAM_USER_UNKNOWN;
1047                         }
1048                         if (retval == PAM_SUCCESS) {
1049                                 _pam_log_debug(ctrl, LOG_DEBUG, "username [%s] obtained", user);
1050                         }
1051                 } else {
1052                         _pam_log_debug(ctrl, LOG_DEBUG, "could not identify user");
1053                         return retval;
1054                 }
1055
1056                 ccname = pam_getenv(pamh, "KRB5CCNAME");
1057                 if (ccname == NULL) {
1058                         _pam_log_debug(ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1059                         return PAM_BUF_ERR;
1060                 }
1061
1062                 fstrcpy(request.data.logoff.user, user);
1063                 fstrcpy(request.data.logoff.krb5ccname, ccname);
1064
1065                 pwd = getpwnam(user);
1066                 if (pwd == NULL) {
1067                         return PAM_USER_UNKNOWN;
1068                 }
1069                 request.data.logoff.uid = pwd->pw_uid;
1070
1071                 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1072
1073                 return pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1074         }
1075         
1076         return PAM_SUCCESS;
1077 }
1078
1079
1080
1081 PAM_EXTERN 
1082 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1083                      int argc, const char **argv)
1084 {
1085         unsigned int lctrl;
1086         int retval;
1087         unsigned int ctrl;
1088
1089         /* <DO NOT free() THESE> */
1090         const char *user;
1091         char *pass_old, *pass_new;
1092         /* </DO NOT free() THESE> */
1093
1094         fstring Announce;
1095         
1096         int retry = 0;
1097
1098         ctrl = _pam_parse(argc, argv);
1099         if (ctrl == -1) {
1100                 return PAM_SYSTEM_ERR;
1101         }
1102
1103         _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_chauthtok");
1104
1105         /*
1106          * First get the name of a user
1107          */
1108         retval = pam_get_user(pamh, &user, "Username: ");
1109         if (retval == PAM_SUCCESS) {
1110                 if (user == NULL) {
1111                         _pam_log(LOG_ERR, "username was NULL!");
1112                         return PAM_USER_UNKNOWN;
1113                 }
1114                 if (retval == PAM_SUCCESS) {
1115                         _pam_log_debug(ctrl, LOG_DEBUG, "username [%s] obtained",
1116                                  user);
1117                 }
1118         } else {
1119                 _pam_log_debug(ctrl, LOG_DEBUG,
1120                          "password - could not identify user");
1121                 return retval;
1122         }
1123
1124         /*
1125          * obtain and verify the current password (OLDAUTHTOK) for
1126          * the user.
1127          */
1128
1129         if (flags & PAM_PRELIM_CHECK) {
1130                 
1131                 /* instruct user what is happening */
1132 #define greeting "Changing password for "
1133                 fstrcpy(Announce, greeting);
1134                 fstrcat(Announce, user);
1135 #undef greeting
1136                 
1137                 lctrl = ctrl | WINBIND__OLD_PASSWORD;
1138                 retval = _winbind_read_password(pamh, lctrl,
1139                                                 Announce,
1140                                                 "(current) NT password: ",
1141                                                 NULL,
1142                                                 (const char **) &pass_old);
1143                 if (retval != PAM_SUCCESS) {
1144                         _pam_log(LOG_NOTICE, "password - (old) token not obtained");
1145                         return retval;
1146                 }
1147                 /* verify that this is the password for this user */
1148                 
1149                 retval = winbind_auth_request(pamh, ctrl, user, pass_old, NULL, NULL, False);
1150
1151                 if (retval != PAM_ACCT_EXPIRED 
1152                     && retval != PAM_AUTHTOK_EXPIRED
1153                     && retval != PAM_NEW_AUTHTOK_REQD 
1154                     && retval != PAM_SUCCESS) {
1155                         pass_old = NULL;
1156                         return retval;
1157                 }
1158                 
1159                 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
1160                 pass_old = NULL;
1161                 if (retval != PAM_SUCCESS) {
1162                         _pam_log(LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
1163                 }
1164         } else if (flags & PAM_UPDATE_AUTHTOK) {
1165         
1166                 /*
1167                  * obtain the proposed password
1168                  */
1169                 
1170                 /*
1171                  * get the old token back. 
1172                  */
1173                 
1174                 retval = pam_get_item(pamh, PAM_OLDAUTHTOK,
1175                                       (const void **) &pass_old);
1176                 
1177                 if (retval != PAM_SUCCESS) {
1178                         _pam_log(LOG_NOTICE, "user not authenticated");
1179                         return retval;
1180                 }
1181                 
1182                 lctrl = ctrl;
1183                 
1184                 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
1185                         lctrl |= WINBIND_USE_FIRST_PASS_ARG;
1186                 }
1187                 retry = 0;
1188                 retval = PAM_AUTHTOK_ERR;
1189                 while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
1190                         /*
1191                          * use_authtok is to force the use of a previously entered
1192                          * password -- needed for pluggable password strength checking
1193                          */
1194                         
1195                         retval = _winbind_read_password(pamh, lctrl,
1196                                                         NULL,
1197                                                         "Enter new NT password: ",
1198                                                         "Retype new NT password: ",
1199                                                         (const char **) &pass_new);
1200                         
1201                         if (retval != PAM_SUCCESS) {
1202                                 _pam_log_debug(ctrl, LOG_ALERT
1203                                          ,"password - new password not obtained");
1204                                 pass_old = NULL;/* tidy up */
1205                                 return retval;
1206                         }
1207
1208                         /*
1209                          * At this point we know who the user is and what they
1210                          * propose as their new password. Verify that the new
1211                          * password is acceptable.
1212                          */
1213                         
1214                         if (pass_new[0] == '\0') {/* "\0" password = NULL */
1215                                 pass_new = NULL;
1216                         }
1217                 }
1218                 
1219                 /*
1220                  * By reaching here we have approved the passwords and must now
1221                  * rebuild the password database file.
1222                  */
1223
1224                 retval = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new);
1225                 if (retval) {
1226                         _pam_overwrite(pass_new);
1227                         _pam_overwrite(pass_old);
1228                         pass_old = pass_new = NULL;
1229                         return retval;
1230                 }
1231
1232                 /* just in case we need krb5 creds after a password change over msrpc */
1233
1234                 if (ctrl & WINBIND_KRB5_AUTH) {
1235
1236                         const char *member = get_member_from_config(argc, argv, ctrl);
1237                         const char *cctype = get_krb5_cc_type_from_config(argc, argv, ctrl);
1238
1239                         retval = winbind_auth_request(pamh, ctrl, user, pass_new, member, cctype, False);
1240                         _pam_overwrite(pass_new);
1241                         _pam_overwrite(pass_old);
1242                         pass_old = pass_new = NULL;
1243                 }
1244         } else {
1245                 retval = PAM_SERVICE_ERR;
1246         }
1247
1248         return retval;
1249 }
1250
1251 #ifdef PAM_STATIC
1252
1253 /* static module data */
1254
1255 struct pam_module _pam_winbind_modstruct = {
1256         MODULE_NAME,
1257         pam_sm_authenticate,
1258         pam_sm_setcred,
1259         pam_sm_acct_mgmt,
1260         pam_sm_open_session,
1261         pam_sm_close_session,
1262         pam_sm_chauthtok
1263 };
1264
1265 #endif
1266
1267 /*
1268  * Copyright (c) Andrew Tridgell  <tridge@samba.org>   2000
1269  * Copyright (c) Tim Potter       <tpot@samba.org>     2000
1270  * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1271  * Copyright (c) Guenther Deschner <gd@samba.org>      2005-2006
1272  * Copyright (c) Jan Rêkorajski 1999.
1273  * Copyright (c) Andrew G. Morgan 1996-8.
1274  * Copyright (c) Alex O. Yuriev, 1996.
1275  * Copyright (c) Cristian Gafton 1996.
1276  * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software. 
1277  *
1278  * Redistribution and use in source and binary forms, with or without
1279  * modification, are permitted provided that the following conditions
1280  * are met:
1281  * 1. Redistributions of source code must retain the above copyright
1282  *    notice, and the entire permission notice in its entirety,
1283  *    including the disclaimer of warranties.
1284  * 2. Redistributions in binary form must reproduce the above copyright
1285  *    notice, this list of conditions and the following disclaimer in the
1286  *    documentation and/or other materials provided with the distribution.
1287  * 3. The name of the author may not be used to endorse or promote
1288  *    products derived from this software without specific prior
1289  *    written permission.
1290  *
1291  * ALTERNATIVELY, this product may be distributed under the terms of
1292  * the GNU Public License, in which case the provisions of the GPL are
1293  * required INSTEAD OF the above restrictions.  (This clause is
1294  * necessary due to a potential bad interaction between the GPL and
1295  * the restrictions contained in a BSD-style copyright.)
1296  *
1297  * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
1298  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1299  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1300  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1301  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1302  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1303  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1304  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1305  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1306  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1307  * OF THE POSSIBILITY OF SUCH DAMAGE.
1308  */