switch from rcS.d to rc[0-6].d
[debian/sudo] / getspwuid.c
1 /*
2  * Copyright (c) 1996, 1998-2005 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
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.
7  *
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.
15  *
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.
19  */
20
21 #include <config.h>
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/param.h>
26 #include <stdio.h>
27 #ifdef STDC_HEADERS
28 # include <stdlib.h>
29 # include <stddef.h>
30 #else
31 # ifdef HAVE_STDLIB_H
32 #  include <stdlib.h>
33 # endif
34 #endif /* STDC_HEADERS */
35 #ifdef HAVE_STRING_H
36 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
37 #  include <memory.h>
38 # endif
39 # include <string.h>
40 #else
41 # ifdef HAVE_STRINGS_H
42 #  include <strings.h>
43 # endif
44 #endif /* HAVE_STRING_H */
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif /* HAVE_UNISTD_H */
48 #include <pwd.h>
49 #include <grp.h>
50 #ifdef HAVE_GETSPNAM
51 # include <shadow.h>
52 #endif /* HAVE_GETSPNAM */
53 #ifdef HAVE_GETPRPWNAM
54 # ifdef __hpux
55 #  undef MAXINT
56 #  include <hpsecurity.h>
57 # else
58 #  include <sys/security.h>
59 # endif /* __hpux */
60 # include <prot.h>
61 #endif /* HAVE_GETPRPWNAM */
62 #ifdef HAVE_GETPWANAM
63 # include <sys/label.h>
64 # include <sys/audit.h>
65 # include <pwdadj.h>
66 #endif /* HAVE_GETPWANAM */
67 #ifdef HAVE_GETAUTHUID
68 # include <auth.h>
69 #endif /* HAVE_GETAUTHUID */
70
71 #include "sudo.h"
72
73 #ifndef lint
74 __unused static const char rcsid[] = "$Sudo: getspwuid.c,v 1.78 2005/02/12 22:56:06 millert Exp $";
75 #endif /* lint */
76
77 /*
78  * Exported for auth/secureware.c
79  */
80 #if defined(HAVE_GETPRPWNAM) && defined(__alpha)
81 int crypt_type = INT_MAX;
82 #endif /* HAVE_GETPRPWNAM && __alpha */
83
84 /*
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.
87  */
88 char *
89 sudo_getepw(pw)
90     const struct passwd *pw;
91 {
92     char *epw;
93
94     /* If there is a function to check for shadow enabled, use it... */
95 #ifdef HAVE_ISCOMSEC
96     if (!iscomsec())
97         return(estrdup(pw->pw_passwd));
98 #endif /* HAVE_ISCOMSEC */
99 #ifdef HAVE_ISSECURE
100     if (!issecure())
101         return(estrdup(pw->pw_passwd));
102 #endif /* HAVE_ISSECURE */
103
104     epw = NULL;
105 #ifdef HAVE_GETPRPWNAM
106     {
107         struct pr_passwd *spw;
108
109         if ((spw = getprpwnam(pw->pw_name)) && spw->ufld.fd_encrypt) {
110 # ifdef __alpha
111             crypt_type = spw->ufld.fd_oldcrypt;
112 # endif /* __alpha */
113             epw = estrdup(spw->ufld.fd_encrypt);
114         }
115         if (epw)
116             return(epw);
117     }
118 #endif /* HAVE_GETPRPWNAM */
119 #ifdef HAVE_GETSPNAM
120     {
121         struct spwd *spw;
122
123         if ((spw = getspnam(pw->pw_name)) && spw->sp_pwdp)
124             epw = estrdup(spw->sp_pwdp);
125         if (epw)
126             return(epw);
127     }
128 #endif /* HAVE_GETSPNAM */
129 #ifdef HAVE_GETSPWUID
130     {
131         struct s_passwd *spw;
132
133         if ((spw = getspwuid(pw->pw_uid)) && spw->pw_passwd)
134             epw = estrdup(spw->pw_passwd);
135         if (epw)
136             return(epw);
137     }
138 #endif /* HAVE_GETSPWUID */
139 #ifdef HAVE_GETPWANAM
140     {
141         struct passwd_adjunct *spw;
142
143         if ((spw = getpwanam(pw->pw_name)) && spw->pwa_passwd)
144             epw = estrdup(spw->pwa_passwd);
145         if (epw)
146             return(epw);
147     }
148 #endif /* HAVE_GETPWANAM */
149 #ifdef HAVE_GETAUTHUID
150     {
151         AUTHORIZATION *spw;
152
153         if ((spw = getauthuid(pw->pw_uid)) && spw->a_password)
154             epw = estrdup(spw->a_password);
155         if (epw)
156             return(epw);
157     }
158 #endif /* HAVE_GETAUTHUID */
159
160     /* Fall back on normal password. */
161     return(estrdup(pw->pw_passwd));
162 }
163
164 void
165 sudo_setspent()
166 {
167 #ifdef HAVE_GETPRPWNAM
168     setprpwent();
169 #endif
170 #ifdef HAVE_GETSPNAM
171     setspent();
172 #endif
173 #ifdef HAVE_GETSPWUID
174     setspwent();
175 #endif
176 #ifdef HAVE_GETPWANAM
177     setpwaent();
178 #endif
179 #ifdef HAVE_GETAUTHUID
180     setauthent();
181 #endif
182 }
183
184 void
185 sudo_endspent()
186 {
187 #ifdef HAVE_GETPRPWNAM
188     endprpwent();
189 #endif
190 #ifdef HAVE_GETSPNAM
191     endspent();
192 #endif
193 #ifdef HAVE_GETSPWUID
194     endspwent();
195 #endif
196 #ifdef HAVE_GETPWANAM
197     endpwaent();
198 #endif
199 #ifdef HAVE_GETAUTHUID
200     endauthent();
201 #endif
202 }