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