2 * Copyright (c) 1996, 1998-2001 Todd C. Miller <Todd.Miller@courtesan.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * 4. Products derived from this software may not be called "Sudo" nor
20 * may "Sudo" appear in their names without specific prior written
21 * permission from the author.
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
39 #include <sys/param.h>
48 #endif /* STDC_HEADERS */
50 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
55 # ifdef HAVE_STRINGS_H
58 #endif /* HAVE_STRING_H */
61 #endif /* HAVE_UNISTD_H */
65 #endif /* HAVE_GETSPNAM */
66 #ifdef HAVE_GETPRPWNAM
69 # include <hpsecurity.h>
71 # include <sys/security.h>
74 #endif /* HAVE_GETPRPWNAM */
76 # include <sys/label.h>
77 # include <sys/audit.h>
79 #endif /* HAVE_GETPWANAM */
80 #ifdef HAVE_GETAUTHUID
82 #endif /* HAVE_GETAUTHUID */
87 static const char rcsid[] = "$Sudo: getspwuid.c,v 1.62 2002/01/15 23:43:59 millert Exp $";
91 * Global variables (yuck)
93 #if defined(HAVE_GETPRPWNAM) && defined(__alpha)
94 int crypt_type = INT_MAX;
95 #endif /* HAVE_GETPRPWNAM && __alpha */
99 * Local functions not visible outside getspwuid.c
101 static struct passwd *sudo_pwdup __P((struct passwd *));
105 * Return a copy of the encrypted password for the user described by pw.
106 * If shadow passwords are in use, look in the shadow file.
114 /* If there is a function to check for shadow enabled, use it... */
117 return(estrdup(pw->pw_passwd));
118 #endif /* HAVE_ISCOMSEC */
121 return(estrdup(pw->pw_passwd));
122 #endif /* HAVE_ISSECURE */
125 #ifdef HAVE_GETPRPWNAM
127 struct pr_passwd *spw;
130 if ((spw = getprpwnam(pw->pw_name)) && spw->ufld.fd_encrypt) {
132 crypt_type = spw->ufld.fd_oldcrypt;
133 # endif /* __alpha */
134 epw = estrdup(spw->ufld.fd_encrypt);
140 #endif /* HAVE_GETPRPWNAM */
146 if ((spw = getspnam(pw->pw_name)) && spw->sp_pwdp)
147 epw = estrdup(spw->sp_pwdp);
152 #endif /* HAVE_GETSPNAM */
153 #ifdef HAVE_GETSPWUID
155 struct s_passwd *spw;
158 if ((spw = getspwuid(pw->pw_uid)) && spw->pw_passwd)
159 epw = estrdup(spw->pw_passwd);
164 #endif /* HAVE_GETSPWUID */
165 #ifdef HAVE_GETPWANAM
167 struct passwd_adjunct *spw;
170 if ((spw = getpwanam(pw->pw_name)) && spw->pwa_passwd)
171 epw = estrdup(spw->pwa_passwd);
176 #endif /* HAVE_GETPWANAM */
177 #ifdef HAVE_GETAUTHUID
182 if ((spw = getauthuid(pw->pw_uid)) && spw->a_password)
183 epw = estrdup(spw->a_password);
188 #endif /* HAVE_GETAUTHUID */
190 /* Fall back on normal password. */
191 return(estrdup(pw->pw_passwd));
195 * Dynamically allocate space for a struct password and the constituent parts
196 * that we care about. Fills in pw_passwd from shadow file if necessary.
198 static struct passwd *
202 struct passwd *local_pw;
204 /* Allocate space for a local copy of pw. */
205 local_pw = (struct passwd *) emalloc(sizeof(struct passwd));
208 * Copy the struct passwd and the interesting strings...
210 (void) memcpy(local_pw, pw, sizeof(struct passwd));
211 local_pw->pw_name = estrdup(pw->pw_name);
212 local_pw->pw_dir = estrdup(pw->pw_dir);
213 local_pw->pw_gecos = estrdup(pw->pw_gecos);
214 #ifdef HAVE_LOGIN_CAP_H
215 local_pw->pw_class = estrdup(pw->pw_class);
218 /* If shell field is empty, expand to _PATH_BSHELL. */
219 if (local_pw->pw_shell[0] == '\0')
220 local_pw->pw_shell = _PATH_BSHELL;
222 local_pw->pw_shell = estrdup(pw->pw_shell);
224 /* pw_passwd gets a shadow password if applicable */
225 local_pw->pw_passwd = sudo_getepw(pw);
231 * Get a password entry by uid and allocate space for it.
232 * Fills in pw_passwd from shadow file if necessary.
240 if ((pw = getpwuid(uid)) == NULL)
243 return(sudo_pwdup(pw));
247 * Get a password entry by name and allocate space for it.
248 * Fills in pw_passwd from shadow file if necessary.
256 if ((pw = getpwnam(name)) == NULL)
259 return(sudo_pwdup(pw));