2 * Copyright (c) 1999-2005, 2010-2013 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 #include <sys/types.h>
32 #endif /* STDC_HEADERS */
35 #endif /* HAVE_STRING_H */
38 #endif /* HAVE_STRINGS_H */
41 #endif /* HAVE_UNISTD_H */
45 #include "sudo_auth.h"
48 #define HAS_AGEINFO(p, l) (l == 18 && p[DESLEN] == ',')
51 sudo_passwd_init(struct passwd *pw, sudo_auth *auth)
53 debug_decl(sudo_passwd_init, SUDO_DEBUG_AUTH)
55 #ifdef HAVE_SKEYACCESS
56 if (skeyaccess(pw, user_tty, NULL, NULL) == 0)
57 debug_return_int(AUTH_FAILURE);
60 auth->data = sudo_getepw(pw);
62 debug_return_int(AUTH_SUCCESS);
66 sudo_passwd_verify(struct passwd *pw, char *pass, sudo_auth *auth)
69 char *pw_epasswd = auth->data;
72 debug_decl(sudo_passwd_verify, SUDO_DEBUG_AUTH)
74 pw_len = strlen(pw_epasswd);
76 #ifdef HAVE_GETAUTHUID
77 /* Ultrix shadow passwords may use crypt16() */
78 epass = (char *) crypt16(pass, pw_epasswd);
79 if (epass != NULL && strcmp(pw_epasswd, epass) == 0)
80 debug_return_int(AUTH_SUCCESS);
81 #endif /* HAVE_GETAUTHUID */
84 * Truncate to 8 chars if standard DES since not all crypt()'s do this.
85 * If this turns out not to be safe we will have to use OS #ifdef's (sigh).
88 if (pw_len == DESLEN || HAS_AGEINFO(pw_epasswd, pw_len))
92 * Normal UN*X password check.
93 * HP-UX may add aging info (separated by a ',') at the end so
94 * only compare the first DESLEN characters in that case.
96 epass = (char *) crypt(pass, pw_epasswd);
99 if (HAS_AGEINFO(pw_epasswd, pw_len) && strlen(epass) == DESLEN)
100 matched = !strncmp(pw_epasswd, epass, DESLEN);
102 matched = !strcmp(pw_epasswd, epass);
105 debug_return_int(matched ? AUTH_SUCCESS : AUTH_FAILURE);
109 sudo_passwd_cleanup(pw, auth)
113 char *pw_epasswd = auth->data;
114 debug_decl(sudo_passwd_cleanup, SUDO_DEBUG_AUTH)
116 if (pw_epasswd != NULL) {
117 zero_bytes(pw_epasswd, strlen(pw_epasswd));
120 debug_return_int(AUTH_SUCCESS);