Annotation of src/usr.bin/sudo/getspwuid.c, Revision 1.11
1.1 millert 1: /*
1.9 millert 2: * Copyright (c) 1996, 1998-2005 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 millert 3: *
1.8 millert 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.
1.1 millert 7: *
1.8 millert 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.
1.7 millert 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.
1.1 millert 19: */
20:
1.9 millert 21: #include <config.h>
1.1 millert 22:
1.5 millert 23: #include <sys/types.h>
24: #include <sys/stat.h>
25: #include <sys/param.h>
1.1 millert 26: #include <stdio.h>
27: #ifdef STDC_HEADERS
28: # include <stdlib.h>
1.5 millert 29: # include <stddef.h>
30: #else
31: # ifdef HAVE_STDLIB_H
32: # include <stdlib.h>
33: # endif
1.1 millert 34: #endif /* STDC_HEADERS */
35: #ifdef HAVE_STRING_H
1.5 millert 36: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
37: # include <memory.h>
38: # endif
1.1 millert 39: # include <string.h>
1.5 millert 40: #else
41: # ifdef HAVE_STRINGS_H
42: # include <strings.h>
43: # endif
1.1 millert 44: #endif /* HAVE_STRING_H */
45: #ifdef HAVE_UNISTD_H
46: # include <unistd.h>
47: #endif /* HAVE_UNISTD_H */
48: #include <pwd.h>
1.10 millert 49: #include <grp.h>
1.1 millert 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: /*
1.10 millert 74: * Exported for auth/secureware.c
1.1 millert 75: */
76: #if defined(HAVE_GETPRPWNAM) && defined(__alpha)
77: int crypt_type = INT_MAX;
78: #endif /* HAVE_GETPRPWNAM && __alpha */
79:
80: /*
1.5 millert 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.
1.1 millert 83: */
1.2 millert 84: char *
1.1 millert 85: sudo_getepw(pw)
1.8 millert 86: const struct passwd *pw;
1.1 millert 87: {
1.5 millert 88: char *epw;
1.1 millert 89:
90: /* If there is a function to check for shadow enabled, use it... */
91: #ifdef HAVE_ISCOMSEC
92: if (!iscomsec())
1.5 millert 93: return(estrdup(pw->pw_passwd));
1.1 millert 94: #endif /* HAVE_ISCOMSEC */
95: #ifdef HAVE_ISSECURE
96: if (!issecure())
1.5 millert 97: return(estrdup(pw->pw_passwd));
1.1 millert 98: #endif /* HAVE_ISSECURE */
99:
1.5 millert 100: epw = NULL;
1.1 millert 101: #ifdef HAVE_GETPRPWNAM
102: {
103: struct pr_passwd *spw;
104:
1.5 millert 105: if ((spw = getprpwnam(pw->pw_name)) && spw->ufld.fd_encrypt) {
1.1 millert 106: # ifdef __alpha
107: crypt_type = spw->ufld.fd_oldcrypt;
108: # endif /* __alpha */
1.5 millert 109: epw = estrdup(spw->ufld.fd_encrypt);
1.1 millert 110: }
1.5 millert 111: if (epw)
112: return(epw);
1.1 millert 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)
1.5 millert 120: epw = estrdup(spw->sp_pwdp);
121: if (epw)
122: return(epw);
1.1 millert 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)
1.5 millert 130: epw = estrdup(spw->pw_passwd);
131: if (epw)
132: return(epw);
1.1 millert 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)
1.5 millert 140: epw = estrdup(spw->pwa_passwd);
141: if (epw)
142: return(epw);
1.1 millert 143: }
144: #endif /* HAVE_GETPWANAM */
145: #ifdef HAVE_GETAUTHUID
146: {
147: AUTHORIZATION *spw;
148:
149: if ((spw = getauthuid(pw->pw_uid)) && spw->a_password)
1.5 millert 150: epw = estrdup(spw->a_password);
151: if (epw)
152: return(epw);
1.1 millert 153: }
154: #endif /* HAVE_GETAUTHUID */
155:
156: /* Fall back on normal password. */
1.5 millert 157: return(estrdup(pw->pw_passwd));
1.1 millert 158: }
159:
1.10 millert 160: void
161: sudo_setspent()
1.1 millert 162: {
1.10 millert 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();
1.8 millert 174: #endif
1.10 millert 175: #ifdef HAVE_GETAUTHUID
176: setauthent();
1.4 millert 177: #endif
1.3 millert 178: }
179:
1.10 millert 180: void
181: sudo_endspent()
1.3 millert 182: {
1.10 millert 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
1.1 millert 198: }