2 * Copyright (c) 1996, 1998-2005 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>
25 #include <sys/param.h>
34 #endif /* STDC_HEADERS */
36 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
41 # ifdef HAVE_STRINGS_H
44 #endif /* HAVE_STRING_H */
47 #endif /* HAVE_UNISTD_H */
51 #endif /* HAVE_GETSPNAM */
52 #ifdef HAVE_GETPRPWNAM
55 # include <hpsecurity.h>
57 # include <sys/security.h>
60 #endif /* HAVE_GETPRPWNAM */
62 # include <sys/label.h>
63 # include <sys/audit.h>
65 #endif /* HAVE_GETPWANAM */
66 #ifdef HAVE_GETAUTHUID
68 #endif /* HAVE_GETAUTHUID */
73 __unused static const char rcsid[] = "$Sudo: getspwuid.c,v 1.65.2.2 2007/06/12 01:28:41 millert Exp $";
77 * Global variables (yuck)
79 #if defined(HAVE_GETPRPWNAM) && defined(__alpha)
80 int crypt_type = INT_MAX;
81 #endif /* HAVE_GETPRPWNAM && __alpha */
85 * Return a copy of the encrypted password for the user described by pw.
86 * If shadow passwords are in use, look in the shadow file.
90 const struct passwd *pw;
94 /* If there is a function to check for shadow enabled, use it... */
97 return(estrdup(pw->pw_passwd));
98 #endif /* HAVE_ISCOMSEC */
101 return(estrdup(pw->pw_passwd));
102 #endif /* HAVE_ISSECURE */
105 #ifdef HAVE_GETPRPWNAM
107 struct pr_passwd *spw;
110 if ((spw = getprpwnam(pw->pw_name)) && spw->ufld.fd_encrypt) {
112 crypt_type = spw->ufld.fd_oldcrypt;
113 # endif /* __alpha */
114 epw = estrdup(spw->ufld.fd_encrypt);
120 #endif /* HAVE_GETPRPWNAM */
126 if ((spw = getspnam(pw->pw_name)) && spw->sp_pwdp)
127 epw = estrdup(spw->sp_pwdp);
132 #endif /* HAVE_GETSPNAM */
133 #ifdef HAVE_GETSPWUID
135 struct s_passwd *spw;
138 if ((spw = getspwuid(pw->pw_uid)) && spw->pw_passwd)
139 epw = estrdup(spw->pw_passwd);
144 #endif /* HAVE_GETSPWUID */
145 #ifdef HAVE_GETPWANAM
147 struct passwd_adjunct *spw;
150 if ((spw = getpwanam(pw->pw_name)) && spw->pwa_passwd)
151 epw = estrdup(spw->pwa_passwd);
156 #endif /* HAVE_GETPWANAM */
157 #ifdef HAVE_GETAUTHUID
162 if ((spw = getauthuid(pw->pw_uid)) && spw->a_password)
163 epw = estrdup(spw->a_password);
168 #endif /* HAVE_GETAUTHUID */
170 /* Fall back on normal password. */
171 return(estrdup(pw->pw_passwd));
175 * Dynamically allocate space for a struct password and the constituent parts
176 * that we care about. Fills in pw_passwd from shadow file if necessary.
180 const struct passwd *pw;
183 const char *pw_passwd, *pw_shell;
184 size_t nsize, psize, csize, gsize, dsize, ssize, total;
185 struct passwd *newpw;
187 /* Get shadow password if available. */
188 pw_passwd = sudo_getepw(pw);
190 /* If shell field is empty, expand to _PATH_BSHELL. */
191 pw_shell = (pw->pw_shell == NULL || pw->pw_shell[0] == '\0')
192 ? _PATH_BSHELL : pw->pw_shell;
194 /* Allocate in one big chunk for easy freeing. */
195 nsize = psize = csize = gsize = dsize = ssize = 0;
196 total = sizeof(struct passwd);
198 nsize = strlen(pw->pw_name) + 1;
202 psize = strlen(pw_passwd) + 1;
205 #ifdef HAVE_LOGIN_CAP_H
207 csize = strlen(pw->pw_class) + 1;
212 gsize = strlen(pw->pw_gecos) + 1;
216 dsize = strlen(pw->pw_dir) + 1;
220 ssize = strlen(pw_shell) + 1;
223 if ((cp = malloc(total)) == NULL)
225 newpw = (struct passwd *)cp;
228 * Copy in passwd contents and make strings relative to space
229 * at the end of the buffer.
231 (void)memcpy(newpw, pw, sizeof(struct passwd));
232 cp += sizeof(struct passwd);
234 (void)memcpy(cp, pw->pw_name, nsize);
239 (void)memcpy(cp, pw_passwd, psize);
240 newpw->pw_passwd = cp;
243 #ifdef HAVE_LOGIN_CAP_H
245 (void)memcpy(cp, pw->pw_class, csize);
246 newpw->pw_class = cp;
251 (void)memcpy(cp, pw->pw_gecos, gsize);
252 newpw->pw_gecos = cp;
256 (void)memcpy(cp, pw->pw_dir, dsize);
261 (void)memcpy(cp, pw_shell, ssize);
262 newpw->pw_shell = cp;
270 * Get a password entry by uid and allocate space for it.
271 * Fills in pw_passwd from shadow file if necessary.
279 if ((pw = getpwuid(uid)) == NULL)
282 return(sudo_pwdup(pw));
286 * Get a password entry by name and allocate space for it.
287 * Fills in pw_passwd from shadow file if necessary.
295 if ((pw = getpwnam(name)) == NULL)
298 return(sudo_pwdup(pw));