Merge branch 'vendor/LIBPCAP'
[dragonfly.git] / lib / pam_module / pam_unix / pam_unix.c
1 /*-
2  * Copyright 1998 Juniper Networks, Inc.
3  * All rights reserved.
4  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * Portions of this software was developed for the FreeBSD Project by
8  * ThinkSec AS and NAI Labs, the Security Research Division of Network
9  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
10  * ("CBOSS"), as part of the DARPA CHATS research program.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote
21  *    products derived from this software without specific prior written
22  *    permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/lib/libpam/modules/pam_unix/pam_unix.c,v 1.56 2011/11/05 10:00:29 ed Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <sys/time.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44
45 #include <login_cap.h>
46 #include <netdb.h>
47 #include <pwd.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <stdio.h>
51 #include <syslog.h>
52 #include <time.h>
53 #include <unistd.h>
54
55 #include <libutil.h>
56
57 #ifdef YP
58 #include <ypclnt.h>
59 #endif
60
61 #define PAM_SM_AUTH
62 #define PAM_SM_ACCOUNT
63 #define PAM_SM_PASSWORD
64
65 #include <security/pam_appl.h>
66 #include <security/pam_modules.h>
67 #include <security/pam_mod_misc.h>
68
69 #define PASSWORD_HASH           "sha512"
70 #define DEFAULT_WARN            (2L * 7L * 86400L)  /* Two weeks */
71 #define SALTSIZE                32
72
73 #define LOCKED_PREFIX           "*LOCKED*"
74 #define LOCKED_PREFIX_LEN       (sizeof(LOCKED_PREFIX) - 1)
75
76 static void makesalt(char []);
77
78 static char password_hash[] =           PASSWORD_HASH;
79
80 #define PAM_OPT_LOCAL_PASS      "local_pass"
81 #define PAM_OPT_NIS_PASS        "nis_pass"
82
83 /*
84  * authentication management
85  */
86 PAM_EXTERN int
87 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
88     int argc __unused, const char *argv[] __unused)
89 {
90         login_cap_t *lc;
91         struct passwd *pwd;
92         int retval;
93         const char *pass, *user, *realpw, *prompt;
94
95         if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
96                 pwd = getpwnam(getlogin());
97         } else {
98                 retval = pam_get_user(pamh, &user, NULL);
99                 if (retval != PAM_SUCCESS)
100                         return (retval);
101                 pwd = getpwnam(user);
102         }
103
104         PAM_LOG("Got user: %s", user);
105
106         if (pwd != NULL) {
107                 PAM_LOG("Doing real authentication");
108                 realpw = pwd->pw_passwd;
109                 if (realpw[0] == '\0') {
110                         if (!(flags & PAM_DISALLOW_NULL_AUTHTOK) &&
111                             openpam_get_option(pamh, PAM_OPT_NULLOK))
112                                 return (PAM_SUCCESS);
113                         realpw = "*";
114                 }
115                 lc = login_getpwclass(pwd);
116         } else {
117                 PAM_LOG("Doing dummy authentication");
118                 realpw = "*";
119                 lc = login_getclass(NULL);
120         }
121         prompt = login_getcapstr(lc, "passwd_prompt", NULL, NULL);
122         retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, prompt);
123         login_close(lc);
124         if (retval != PAM_SUCCESS)
125                 return (retval);
126         PAM_LOG("Got password");
127         if (strcmp(crypt(pass, realpw), realpw) == 0)
128                 return (PAM_SUCCESS);
129
130         PAM_VERBOSE_ERROR("UNIX authentication refused");
131         return (PAM_AUTH_ERR);
132 }
133
134 PAM_EXTERN int
135 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
136     int argc __unused, const char *argv[] __unused)
137 {
138
139         return (PAM_SUCCESS);
140 }
141
142 /*
143  * account management
144  */
145 PAM_EXTERN int
146 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
147     int argc __unused, const char *argv[] __unused)
148 {
149         struct addrinfo hints, *res;
150         struct passwd *pwd;
151         struct timeval tp;
152         login_cap_t *lc;
153         time_t warntime;
154         int retval;
155         const char *user;
156         const void *rhost, *tty;
157         char rhostip[MAXHOSTNAMELEN] = "";
158
159         retval = pam_get_user(pamh, &user, NULL);
160         if (retval != PAM_SUCCESS)
161                 return (retval);
162
163         if (user == NULL || (pwd = getpwnam(user)) == NULL)
164                 return (PAM_SERVICE_ERR);
165
166         PAM_LOG("Got user: %s", user);
167
168         retval = pam_get_item(pamh, PAM_RHOST, &rhost);
169         if (retval != PAM_SUCCESS)
170                 return (retval);
171
172         retval = pam_get_item(pamh, PAM_TTY, &tty);
173         if (retval != PAM_SUCCESS)
174                 return (retval);
175
176         if (*pwd->pw_passwd == '\0' &&
177             (flags & PAM_DISALLOW_NULL_AUTHTOK) != 0)
178                 return (PAM_NEW_AUTHTOK_REQD);
179
180         if (strncmp(pwd->pw_passwd, LOCKED_PREFIX, LOCKED_PREFIX_LEN) == 0)
181                 return (PAM_AUTH_ERR);
182
183         lc = login_getpwclass(pwd);
184         if (lc == NULL) {
185                 PAM_LOG("Unable to get login class for user %s", user);
186                 return (PAM_SERVICE_ERR);
187         }
188
189         PAM_LOG("Got login_cap");
190
191         if (pwd->pw_change || pwd->pw_expire)
192                 gettimeofday(&tp, NULL);
193
194         /*
195          * Check pw_expire before pw_change - no point in letting the
196          * user change the password on an expired account.
197          */
198
199         if (pwd->pw_expire) {
200                 warntime = login_getcaptime(lc, "warnexpire",
201                     DEFAULT_WARN, DEFAULT_WARN);
202                 if (tp.tv_sec >= pwd->pw_expire) {
203                         login_close(lc);
204                         return (PAM_ACCT_EXPIRED);
205                 } else if (pwd->pw_expire - tp.tv_sec < warntime &&
206                     (flags & PAM_SILENT) == 0) {
207                         pam_error(pamh, "Warning: your account expires on %s",
208                             ctime(&pwd->pw_expire));
209                 }
210         }
211
212         retval = PAM_SUCCESS;
213         if (pwd->pw_change) {
214                 warntime = login_getcaptime(lc, "warnpassword",
215                     DEFAULT_WARN, DEFAULT_WARN);
216                 if (tp.tv_sec >= pwd->pw_change) {
217                         retval = PAM_NEW_AUTHTOK_REQD;
218                 } else if (pwd->pw_change - tp.tv_sec < warntime &&
219                     (flags & PAM_SILENT) == 0) {
220                         pam_error(pamh, "Warning: your password expires on %s",
221                             ctime(&pwd->pw_change));
222                 }
223         }
224
225         /*
226          * From here on, we must leave retval untouched (unless we
227          * know we're going to fail), because we need to remember
228          * whether we're supposed to return PAM_SUCCESS or
229          * PAM_NEW_AUTHTOK_REQD.
230          */
231
232         if (rhost && *(const char *)rhost != '\0') {
233                 memset(&hints, 0, sizeof(hints));
234                 hints.ai_family = AF_UNSPEC;
235                 if (getaddrinfo(rhost, NULL, &hints, &res) == 0) {
236                         getnameinfo(res->ai_addr, res->ai_addrlen,
237                             rhostip, sizeof(rhostip), NULL, 0,
238                             NI_NUMERICHOST);
239                 }
240                 if (res != NULL)
241                         freeaddrinfo(res);
242         }
243
244         /*
245          * Check host / tty / time-of-day restrictions
246          */
247
248         if (!auth_hostok(lc, rhost, rhostip) ||
249             !auth_ttyok(lc, tty) ||
250             !auth_timeok(lc, time(NULL)))
251                 retval = PAM_AUTH_ERR;
252
253         login_close(lc);
254
255         return (retval);
256 }
257
258 /*
259  * password management
260  *
261  * standard Unix and NIS password changing
262  */
263 PAM_EXTERN int
264 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
265     int argc __unused, const char *argv[] __unused)
266 {
267 #ifdef YP
268         struct ypclnt *ypclnt;
269         const void *yp_domain, *yp_server;
270 #endif
271         char salt[SALTSIZE + 1];
272         login_cap_t *lc;
273         struct passwd *pwd, *old_pwd;
274         const char *user, *old_pass, *new_pass;
275         char *encrypted;
276         time_t passwordtime;
277         int pfd, tfd, retval;
278
279         if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
280                 pwd = getpwnam(getlogin());
281         else {
282                 retval = pam_get_user(pamh, &user, NULL);
283                 if (retval != PAM_SUCCESS)
284                         return (retval);
285                 pwd = getpwnam(user);
286         }
287
288         if (pwd == NULL)
289                 return (PAM_AUTHTOK_RECOVERY_ERR);
290
291         PAM_LOG("Got user: %s", user);
292
293         if (flags & PAM_PRELIM_CHECK) {
294
295                 PAM_LOG("PRELIM round");
296
297                 if (getuid() == 0 &&
298                     (pwd->pw_fields & _PWF_SOURCE) == _PWF_FILES)
299                         /* root doesn't need the old password */
300                         return (pam_set_item(pamh, PAM_OLDAUTHTOK, ""));
301 #ifdef YP
302                 if (getuid() == 0 &&
303                     (pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
304
305                         yp_domain = yp_server = NULL;
306                         pam_get_data(pamh, "yp_domain", &yp_domain);
307                         pam_get_data(pamh, "yp_server", &yp_server);
308
309                         ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_server);
310                         if (ypclnt == NULL)
311                                 return (PAM_BUF_ERR);
312
313                         if (ypclnt_connect(ypclnt) == -1) {
314                                 ypclnt_free(ypclnt);
315                                 return (PAM_SERVICE_ERR);
316                         }
317
318                         retval = ypclnt_havepasswdd(ypclnt);
319                         ypclnt_free(ypclnt);
320                         if (retval == 1)
321                                 return (pam_set_item(pamh, PAM_OLDAUTHTOK, ""));
322                         else if (retval == -1)
323                                 return (PAM_SERVICE_ERR);
324                 }
325 #endif
326                 if (pwd->pw_passwd[0] == '\0'
327                     && openpam_get_option(pamh, PAM_OPT_NULLOK)) {
328                         /*
329                          * No password case. XXX Are we giving too much away
330                          * by not prompting for a password?
331                          * XXX check PAM_DISALLOW_NULL_AUTHTOK
332                          */
333                         old_pass = "";
334                 } else {
335                         retval = pam_get_authtok(pamh,
336                             PAM_OLDAUTHTOK, &old_pass, NULL);
337                         if (retval != PAM_SUCCESS)
338                                 return (retval);
339                 }
340                 PAM_LOG("Got old password");
341                 /* always encrypt first */
342                 encrypted = crypt(old_pass, pwd->pw_passwd);
343                 if (old_pass[0] == '\0' &&
344                     !openpam_get_option(pamh, PAM_OPT_NULLOK))
345                         return (PAM_PERM_DENIED);
346                 if (strcmp(encrypted, pwd->pw_passwd) != 0)
347                         return (PAM_PERM_DENIED);
348         }
349         else if (flags & PAM_UPDATE_AUTHTOK) {
350                 PAM_LOG("UPDATE round");
351
352                 retval = pam_get_authtok(pamh,
353                     PAM_OLDAUTHTOK, &old_pass, NULL);
354                 if (retval != PAM_SUCCESS)
355                         return (retval);
356                 PAM_LOG("Got old password");
357
358                 /* get new password */
359                 for (;;) {
360                         retval = pam_get_authtok(pamh,
361                             PAM_AUTHTOK, &new_pass, NULL);
362                         if (retval != PAM_TRY_AGAIN)
363                                 break;
364                         pam_error(pamh, "Mismatch; try again, EOF to quit.");
365                 }
366                 PAM_LOG("Got new password");
367                 if (retval != PAM_SUCCESS) {
368                         PAM_VERBOSE_ERROR("Unable to get new password");
369                         return (retval);
370                 }
371
372                 if (getuid() != 0 && new_pass[0] == '\0' &&
373                     !openpam_get_option(pamh, PAM_OPT_NULLOK))
374                         return (PAM_PERM_DENIED);
375
376                 if ((old_pwd = pw_dup(pwd)) == NULL)
377                         return (PAM_BUF_ERR);
378
379                 lc = login_getclass(pwd->pw_class);
380                 if (login_setcryptfmt(lc, password_hash, NULL) == NULL)
381                         openpam_log(PAM_LOG_ERROR,
382                             "can't set password cipher, relying on default");
383
384                 /* set password expiry date */
385                 pwd->pw_change = 0;
386                 passwordtime = login_getcaptime(lc, "passwordtime", 0, 0);
387                 if (passwordtime > 0)
388                         pwd->pw_change = time(NULL) + passwordtime;
389
390                 login_close(lc);
391                 makesalt(salt);
392                 pwd->pw_passwd = crypt(new_pass, salt);
393 #ifdef YP
394                 switch (old_pwd->pw_fields & _PWF_SOURCE) {
395                 case _PWF_FILES:
396 #endif
397                         retval = PAM_SERVICE_ERR;
398                         if (pw_init(NULL, NULL))
399                                 openpam_log(PAM_LOG_ERROR, "pw_init() failed");
400                         else if ((pfd = pw_lock()) == -1)
401                                 openpam_log(PAM_LOG_ERROR, "pw_lock() failed");
402                         else if ((tfd = pw_tmp(-1)) == -1)
403                                 openpam_log(PAM_LOG_ERROR, "pw_tmp() failed");
404                         else if (pw_copy(pfd, tfd, pwd, old_pwd) == -1)
405                                 openpam_log(PAM_LOG_ERROR, "pw_copy() failed");
406                         else if (pw_mkdb(pwd->pw_name) == -1)
407                                 openpam_log(PAM_LOG_ERROR, "pw_mkdb() failed");
408                         else
409                                 retval = PAM_SUCCESS;
410                         pw_fini();
411 #ifdef YP
412                         break;
413                 case _PWF_NIS:
414                         yp_domain = yp_server = NULL;
415                         pam_get_data(pamh, "yp_domain", &yp_domain);
416                         pam_get_data(pamh, "yp_server", &yp_server);
417                         ypclnt = ypclnt_new(yp_domain,
418                             "passwd.byname", yp_server);
419                         if (ypclnt == NULL) {
420                                 retval = PAM_BUF_ERR;
421                         } else if (ypclnt_connect(ypclnt) == -1 ||
422                             ypclnt_passwd(ypclnt, pwd, old_pass) == -1) {
423                                 openpam_log(PAM_LOG_ERROR, "%s", ypclnt->error);
424                                 retval = PAM_SERVICE_ERR;
425                         } else {
426                                 retval = PAM_SUCCESS;
427                         }
428                         ypclnt_free(ypclnt);
429                         break;
430                 default:
431                         openpam_log(PAM_LOG_ERROR, "unsupported source 0x%x",
432                             pwd->pw_fields & _PWF_SOURCE);
433                         retval = PAM_SERVICE_ERR;
434                 }
435 #endif
436                 free(old_pwd);
437         }
438         else {
439                 /* Very bad juju */
440                 retval = PAM_ABORT;
441                 PAM_LOG("Illegal 'flags'");
442         }
443
444         return (retval);
445 }
446
447 /* Mostly stolen from passwd(1)'s local_passwd.c - markm */
448
449 static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
450         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
451
452 static void
453 to64(char *s, long v, int n)
454 {
455         while (--n >= 0) {
456                 *s++ = itoa64[v&0x3f];
457                 v >>= 6;
458         }
459 }
460
461 /* Salt suitable for traditional DES and MD5 */
462 void
463 makesalt(char salt[SALTSIZE])
464 {
465         int i;
466
467         /* These are not really random numbers, they are just
468          * numbers that change to thwart construction of a
469          * dictionary. This is exposed to the public.
470          */
471         for (i = 0; i < SALTSIZE; i += 4)
472                 to64(&salt[i], arc4random(), 4);
473         salt[SALTSIZE] = '\0';
474 }
475
476 PAM_MODULE_ENTRY("pam_unix");