add doc about interaction with RAMRUN to README.Debian in response to #581393
[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 /*
74  * Exported for auth/secureware.c
75  */
76 #if defined(HAVE_GETPRPWNAM) && defined(__alpha)
77 int crypt_type = INT_MAX;
78 #endif /* HAVE_GETPRPWNAM && __alpha */
79
80 /*
81  * Return a copy of the encrypted password for the user described by pw.
82  * If shadow passwords are in use, look in the shadow file.
83  */
84 char *
85 sudo_getepw(pw)
86     const struct passwd *pw;
87 {
88     char *epw;
89
90     /* If there is a function to check for shadow enabled, use it... */
91 #ifdef HAVE_ISCOMSEC
92     if (!iscomsec())
93         return(estrdup(pw->pw_passwd));
94 #endif /* HAVE_ISCOMSEC */
95 #ifdef HAVE_ISSECURE
96     if (!issecure())
97         return(estrdup(pw->pw_passwd));
98 #endif /* HAVE_ISSECURE */
99
100     epw = NULL;
101 #ifdef HAVE_GETPRPWNAM
102     {
103         struct pr_passwd *spw;
104
105         if ((spw = getprpwnam(pw->pw_name)) && spw->ufld.fd_encrypt) {
106 # ifdef __alpha
107             crypt_type = spw->ufld.fd_oldcrypt;
108 # endif /* __alpha */
109             epw = estrdup(spw->ufld.fd_encrypt);
110         }
111         if (epw)
112             return(epw);
113     }
114 #endif /* HAVE_GETPRPWNAM */
115 #ifdef HAVE_GETSPNAM
116     {
117         struct spwd *spw;
118
119         if ((spw = getspnam(pw->pw_name)) && spw->sp_pwdp)
120             epw = estrdup(spw->sp_pwdp);
121         if (epw)
122             return(epw);
123     }
124 #endif /* HAVE_GETSPNAM */
125 #ifdef HAVE_GETSPWUID
126     {
127         struct s_passwd *spw;
128
129         if ((spw = getspwuid(pw->pw_uid)) && spw->pw_passwd)
130             epw = estrdup(spw->pw_passwd);
131         if (epw)
132             return(epw);
133     }
134 #endif /* HAVE_GETSPWUID */
135 #ifdef HAVE_GETPWANAM
136     {
137         struct passwd_adjunct *spw;
138
139         if ((spw = getpwanam(pw->pw_name)) && spw->pwa_passwd)
140             epw = estrdup(spw->pwa_passwd);
141         if (epw)
142             return(epw);
143     }
144 #endif /* HAVE_GETPWANAM */
145 #ifdef HAVE_GETAUTHUID
146     {
147         AUTHORIZATION *spw;
148
149         if ((spw = getauthuid(pw->pw_uid)) && spw->a_password)
150             epw = estrdup(spw->a_password);
151         if (epw)
152             return(epw);
153     }
154 #endif /* HAVE_GETAUTHUID */
155
156     /* Fall back on normal password. */
157     return(estrdup(pw->pw_passwd));
158 }
159
160 void
161 sudo_setspent()
162 {
163 #ifdef HAVE_GETPRPWNAM
164     setprpwent();
165 #endif
166 #ifdef HAVE_GETSPNAM
167     setspent();
168 #endif
169 #ifdef HAVE_GETSPWUID
170     setspwent();
171 #endif
172 #ifdef HAVE_GETPWANAM
173     setpwaent();
174 #endif
175 #ifdef HAVE_GETAUTHUID
176     setauthent();
177 #endif
178 }
179
180 void
181 sudo_endspent()
182 {
183 #ifdef HAVE_GETPRPWNAM
184     endprpwent();
185 #endif
186 #ifdef HAVE_GETSPNAM
187     endspent();
188 #endif
189 #ifdef HAVE_GETSPWUID
190     endspwent();
191 #endif
192 #ifdef HAVE_GETPWANAM
193     endpwaent();
194 #endif
195 #ifdef HAVE_GETAUTHUID
196     endauthent();
197 #endif
198 }