2 * Copyright 1989 - 1994, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* Newer versions of Linux libc already have shadow support. */
37 #include <sys/types.h>
38 #include "prototypes.h"
43 static int nis_ignore;
44 static enum { native, start, middle, native2 } nis_state;
46 static char *nis_domain;
48 static int nis_keylen;
50 static int nis_vallen;
52 #define IS_NISCHAR(c) ((c)=='+')
56 static char spwbuf[BUFSIZ];
57 static struct spwd spwd;
65 * __setspNIS - turn on or off NIS searches
68 void __setspNIS (int flag)
77 * bind_nis - bind to NIS server
80 static int bind_nis (void)
82 if (yp_get_default_domain (&nis_domain))
91 * setspent - initialize access to shadow text and DBM files
99 shadow = fopen (SHADOW_FILE, "r");
107 * endspent - terminate access to shadow text and DBM files
113 (void) fclose (shadow);
119 * my_sgetspent - convert string in shadow file format to (struct spwd *)
122 static struct spwd *my_sgetspent (const char *string)
124 char *fields[FIELDS];
130 * Copy string to local buffer. It has to be tokenized and we
131 * have to do that to our private copy.
134 if (strlen (string) >= sizeof spwbuf)
136 strcpy (spwbuf, string);
138 cp = strrchr (spwbuf, '\n');
143 * Tokenize the string into colon separated fields. Allow up to
144 * FIELDS different fields.
147 for (cp = spwbuf, i = 0; *cp && i < FIELDS; i++) {
149 while (*cp && *cp != ':')
156 if (i == (FIELDS - 1))
159 if ((cp && *cp) || (i != FIELDS && i != OFIELDS))
163 * Start populating the structure. The fields are all in
164 * static storage, as is the structure we pass back. If we
165 * ever see a name with '+' as the first character, we try
166 * to turn on NIS processing.
169 spwd.sp_namp = fields[0];
171 if (IS_NISCHAR (fields[0][0]))
174 spwd.sp_pwdp = fields[1];
177 * Get the last changed date. For all of the integer fields,
178 * we check for proper format. It is an error to have an
179 * incorrectly formatted number, unless we are using NIS.
182 spwd.sp_lstchg = strtol (fields[2], &cpp, 10);
183 if ((spwd.sp_lstchg == 0) && *cpp) {
192 } else if (fields[2][0] == '\0')
196 * Get the minimum period between password changes.
199 spwd.sp_min = strtol (fields[3], &cpp, 10);
200 if ((spwd.sp_min == 0) && *cpp) {
209 } else if (fields[3][0] == '\0')
213 * Get the maximum number of days a password is valid.
216 spwd.sp_max = strtol (fields[4], &cpp, 10);
217 if ((spwd.sp_max == 0) && *cpp) {
226 } else if (fields[4][0] == '\0')
230 * If there are only OFIELDS fields (this is a SVR3.2 /etc/shadow
231 * formatted file), initialize the other field members to -1.
235 spwd.sp_warn = spwd.sp_inact = spwd.sp_expire =
242 * Get the number of days of password expiry warning.
245 spwd.sp_warn = strtol (fields[5], &cpp, 10);
246 if ((spwd.sp_warn == 0) && *cpp) {
255 } else if (fields[5][0] == '\0')
259 * Get the number of days of inactivity before an account is
263 spwd.sp_inact = strtol (fields[6], &cpp, 10);
264 if ((spwd.sp_inact == 0) && *cpp) {
273 } else if (fields[6][0] == '\0')
277 * Get the number of days after the epoch before the account is
281 spwd.sp_expire = strtol (fields[7], &cpp, 10);
282 if ((spwd.sp_expire == 0) && *cpp) {
291 } else if (fields[7][0] == '\0')
295 * This field is reserved for future use. But it isn't supposed
296 * to have anything other than a valid integer in it.
299 spwd.sp_flag = strtol (fields[8], &cpp, 10);
300 if ((spwd.sp_flag == 0) && *cpp) {
309 } else if (fields[8][0] == '\0')
316 * fgetspent - get an entry from a /etc/shadow formatted stream
319 struct spwd *fgetspent (FILE * fp)
328 while (fgets (buf, sizeof buf, fp) != (char *) 0)
330 if (fgets (buf, sizeof buf, fp) != (char *) 0)
333 cp = strchr (buf, '\n');
337 if (nis_ignore && IS_NISCHAR (buf[0]))
340 return my_sgetspent (buf);
346 * getspent - get a (struct spwd *) from the current shadow file
349 struct spwd *getspent (void)
362 * See if we are reading from the local file.
365 if (nis_state == native || nis_state == native2) {
368 * Get the next entry from the shadow file. Return NULL
369 * right away if there is none.
372 val = fgetspent (shadow);
377 * If this entry began with a NIS escape character, we have
378 * to see if this is just a single user, or if the entire
379 * map is being asked for.
382 if (IS_NISCHAR (val->sp_namp[0])) {
390 * If this isn't a NIS user and this isn't an escape to go
391 * use a NIS map, it must be a regular local user.
394 if (nis_1_user == 0 && nis_state != start)
398 * If this is an escape to use an NIS map, switch over to
399 * that bunch of code.
402 if (nis_state == start)
406 * NEEDSWORK. Here we substitute pieces-parts of this entry.
411 if (nis_bound == 0) {
417 if (nis_state == start) {
418 if (yp_first (nis_domain, "shadow.bynam", &nis_key,
419 &nis_keylen, &nis_val, &nis_vallen)) {
424 } else if (nis_state == middle) {
425 if (yp_next (nis_domain, "shadow.bynam", nis_key,
426 nis_keylen, &nis_key, &nis_keylen,
427 &nis_val, &nis_vallen)) {
432 return my_sgetspent (nis_val);
435 return (fgetspent (shadow));
440 * getspnam - get a shadow entry by name
443 struct spwd *getspnam (const char *name)
449 static char save_name[16];
450 int nis_disabled = 0;
457 * Search the shadow.byname map for this user.
460 if (!nis_ignore && !nis_bound)
463 if (!nis_ignore && nis_bound) {
466 if (yp_match (nis_domain, "shadow.byname", name,
467 strlen (name), &nis_val, &nis_vallen) == 0) {
469 cp = strchr (nis_val, '\n');
474 sp = my_sgetspent (nis_val);
476 strcpy (save_name, sp->sp_namp);
478 nis_keylen = strlen (save_name);
488 * NEEDSWORK -- this is a mess, and it is the same mess in the
489 * other three files. I can't just blindly turn off NIS because
490 * this might be the first pass through the local files. In
491 * that case, I never discover that NIS is present.
499 while ((sp = getspent ()) != (struct spwd *) 0) {
500 if (strcmp (name, sp->sp_namp) == 0)
511 extern int errno; /* warning: ANSI C forbids an empty source file */