Imported Upstream version 1.7.4
[debian/sudo] / getspwuid.c
1 /*
2  * Copyright (c) 1996, 1998-2005, 2010
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * Sponsored in part by the Defense Advanced Research Projects
18  * Agency (DARPA) and Air Force Research Laboratory, Air Force
19  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
20  */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
27 #include <stdio.h>
28 #ifdef STDC_HEADERS
29 # include <stdlib.h>
30 # include <stddef.h>
31 #else
32 # ifdef HAVE_STDLIB_H
33 #  include <stdlib.h>
34 # endif
35 #endif /* STDC_HEADERS */
36 #ifdef HAVE_STRING_H
37 # include <string.h>
38 #endif /* HAVE_STRING_H */
39 #ifdef HAVE_STRINGS_H
40 # include <strings.h>
41 #endif /* HAVE_STRINGS_H */
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif /* HAVE_UNISTD_H */
45 #include <pwd.h>
46 #include <grp.h>
47 #ifdef HAVE_GETSPNAM
48 # include <shadow.h>
49 #endif /* HAVE_GETSPNAM */
50 #ifdef HAVE_GETPRPWNAM
51 # ifdef __hpux
52 #  undef MAXINT
53 #  include <hpsecurity.h>
54 # else
55 #  include <sys/security.h>
56 # endif /* __hpux */
57 # include <prot.h>
58 #endif /* HAVE_GETPRPWNAM */
59 #ifdef HAVE_GETPWANAM
60 # include <sys/label.h>
61 # include <sys/audit.h>
62 # include <pwdadj.h>
63 #endif /* HAVE_GETPWANAM */
64 #ifdef HAVE_GETAUTHUID
65 # include <auth.h>
66 #endif /* HAVE_GETAUTHUID */
67
68 #include "sudo.h"
69
70 /*
71  * Exported for auth/secureware.c
72  */
73 #if defined(HAVE_GETPRPWNAM) && defined(__alpha)
74 int crypt_type = INT_MAX;
75 #endif /* HAVE_GETPRPWNAM && __alpha */
76
77 /*
78  * Return a copy of the encrypted password for the user described by pw.
79  * If shadow passwords are in use, look in the shadow file.
80  */
81 char *
82 sudo_getepw(pw)
83     const struct passwd *pw;
84 {
85     char *epw;
86
87     /* If there is a function to check for shadow enabled, use it... */
88 #ifdef HAVE_ISCOMSEC
89     if (!iscomsec())
90         return(estrdup(pw->pw_passwd));
91 #endif /* HAVE_ISCOMSEC */
92 #ifdef HAVE_ISSECURE
93     if (!issecure())
94         return(estrdup(pw->pw_passwd));
95 #endif /* HAVE_ISSECURE */
96
97     epw = NULL;
98 #ifdef HAVE_GETPRPWNAM
99     {
100         struct pr_passwd *spw;
101
102         if ((spw = getprpwnam(pw->pw_name)) && spw->ufld.fd_encrypt) {
103 # ifdef __alpha
104             crypt_type = spw->ufld.fd_oldcrypt;
105 # endif /* __alpha */
106             epw = estrdup(spw->ufld.fd_encrypt);
107         }
108         if (epw)
109             return(epw);
110     }
111 #endif /* HAVE_GETPRPWNAM */
112 #ifdef HAVE_GETSPNAM
113     {
114         struct spwd *spw;
115
116         if ((spw = getspnam(pw->pw_name)) && spw->sp_pwdp)
117             epw = estrdup(spw->sp_pwdp);
118         if (epw)
119             return(epw);
120     }
121 #endif /* HAVE_GETSPNAM */
122 #ifdef HAVE_GETSPWUID
123     {
124         struct s_passwd *spw;
125
126         if ((spw = getspwuid(pw->pw_uid)) && spw->pw_passwd)
127             epw = estrdup(spw->pw_passwd);
128         if (epw)
129             return(epw);
130     }
131 #endif /* HAVE_GETSPWUID */
132 #ifdef HAVE_GETPWANAM
133     {
134         struct passwd_adjunct *spw;
135
136         if ((spw = getpwanam(pw->pw_name)) && spw->pwa_passwd)
137             epw = estrdup(spw->pwa_passwd);
138         if (epw)
139             return(epw);
140     }
141 #endif /* HAVE_GETPWANAM */
142 #ifdef HAVE_GETAUTHUID
143     {
144         AUTHORIZATION *spw;
145
146         if ((spw = getauthuid(pw->pw_uid)) && spw->a_password)
147             epw = estrdup(spw->a_password);
148         if (epw)
149             return(epw);
150     }
151 #endif /* HAVE_GETAUTHUID */
152
153     /* Fall back on normal password. */
154     return(estrdup(pw->pw_passwd));
155 }
156
157 void
158 sudo_setspent()
159 {
160 #ifdef HAVE_GETPRPWNAM
161     setprpwent();
162 #endif
163 #ifdef HAVE_GETSPNAM
164     setspent();
165 #endif
166 #ifdef HAVE_GETSPWUID
167     setspwent();
168 #endif
169 #ifdef HAVE_GETPWANAM
170     setpwaent();
171 #endif
172 #ifdef HAVE_GETAUTHUID
173     setauthent();
174 #endif
175 }
176
177 void
178 sudo_endspent()
179 {
180 #ifdef HAVE_GETPRPWNAM
181     endprpwent();
182 #endif
183 #ifdef HAVE_GETSPNAM
184     endspent();
185 #endif
186 #ifdef HAVE_GETSPWUID
187     endspwent();
188 #endif
189 #ifdef HAVE_GETPWANAM
190     endpwaent();
191 #endif
192 #ifdef HAVE_GETAUTHUID
193     endauthent();
194 #endif
195 }