1 /* vi: set sw=4 ts=4: */
3 * Copyright 1989 - 1994, Julianne Frances Haugh
4 * <jockgrrl@austin.rr.com>, <jfh@austin.ibm.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* TODO: fgetspent_r.c getspent_r.c getspnam_r.c sgetspent_r.c
45 static char spwbuf[BUFSIZ];
46 static struct spwd spwd;
51 /* setspent - initialize access to shadow text and DBM files */
57 if ((shadow = fopen("/etc/shadow", "r")) == NULL)
58 perror_msg_and_die("/etc/shadow");
62 /* endspent - terminate access to shadow text and DBM files */
66 (void) fclose(shadow);
70 /* getspent - get a (struct spwd *) from the current shadow file */
71 struct spwd *getspent(void)
75 return (fgetspent(shadow));
78 /* getspnam - get a shadow entry by name */
79 struct spwd *getspnam(const char *name)
83 if (!name || !strlen(name))
87 while ((sp = getspent()) != NULL) {
88 if (strcmp(name, sp->sp_namp) == 0)
96 /* sgetspent - convert string in shadow file format to (struct spwd *) */
97 /* returns NULL on error */
98 struct spwd *sgetspent(const char *string)
100 char *fields[FIELDS];
106 * Copy string to local buffer. It has to be tokenized and we
107 * have to do that to our private copy.
110 if (strlen(string) >= sizeof spwbuf)
113 strcpy(spwbuf, string);
115 if ((cp = strrchr(spwbuf, '\n')))
119 * Tokenize the string into colon separated fields. Allow up to
120 * FIELDS different fields.
123 for (cp = spwbuf, i = 0; *cp && i < FIELDS; i++) {
125 while (*cp && *cp != ':')
133 * It is acceptable for the last SVR4 field to be blank. This
134 * results in the loop being terminated early. In which case,
135 * we just make the last field be blank and be done with it.
138 if (i == (FIELDS - 1))
141 if ((cp && *cp) || (i != FIELDS && i != OFIELDS))
146 * Start populating the structure. The fields are all in
147 * static storage, as is the structure we pass back. If we
148 * ever see a name with '+' as the first character, we try
149 * to turn on NIS processing.
152 spwd.sp_namp = fields[0];
153 spwd.sp_pwdp = fields[1];
156 * Get the last changed date. For all of the integer fields,
157 * we check for proper format. It is an error to have an
158 * incorrectly formatted number, unless we are using NIS.
161 if ((spwd.sp_lstchg = strtol(fields[2], &cpp, 10)) == 0 && *cpp) {
164 } else if (fields[2][0] == '\0')
168 * Get the minimum period between password changes.
171 if ((spwd.sp_min = strtol(fields[3], &cpp, 10)) == 0 && *cpp) {
174 } else if (fields[3][0] == '\0')
178 * Get the maximum number of days a password is valid.
181 if ((spwd.sp_max = strtol(fields[4], &cpp, 10)) == 0 && *cpp) {
184 } else if (fields[4][0] == '\0')
188 * If there are only OFIELDS fields (this is a SVR3.2 /etc/shadow
189 * formatted file), initialize the other field members to -1.
193 spwd.sp_warn = spwd.sp_inact = spwd.sp_expire = spwd.sp_flag = -1;
199 * The rest of the fields are mandatory for SVR4, but optional
200 * for anything else. However, if one is present the others
205 * Get the number of days of password expiry warning.
208 if ((spwd.sp_warn = strtol(fields[5], &cpp, 10)) == 0 && *cpp) {
211 } else if (fields[5][0] == '\0')
215 * Get the number of days of inactivity before an account is
219 if ((spwd.sp_inact = strtol(fields[6], &cpp, 10)) == 0 && *cpp) {
222 } else if (fields[6][0] == '\0')
226 * Get the number of days after the epoch before the account is
230 if ((spwd.sp_expire = strtol(fields[7], &cpp, 10)) == 0 && *cpp) {
233 } else if (fields[7][0] == '\0')
237 * This field is reserved for future use. But it isn't supposed
238 * to have anything other than a valid integer in it.
241 if ((spwd.sp_flag = strtol(fields[8], &cpp, 10)) == 0 && *cpp) {
244 } else if (fields[8][0] == '\0')
250 /* fgetspent - get an entry from an /etc/shadow formatted stream */
251 struct spwd *fgetspent(FILE *fp)
260 if (fgets(buf, sizeof buf, fp) != (char *) 0) {
261 if ((cp = strchr(buf, '\n')))
263 return (sgetspent(buf));
270 * putspent - put a (struct spwd *) into the (FILE *) you provide.
272 * this was described in shadow_.h but not implemented, so here
276 int putspent(const struct spwd *sp, FILE *fp)
281 ret = fseek(fp, 0, SEEK_END);
287 /* powered by fprintf */
288 fprintf(fp, "%s:%s:%ld:%ld:%ld:%ld:%ld:%ld:%s\n", sp->sp_namp, /* login name */
289 sp->sp_pwdp, /* encrypted password */
290 sp->sp_lstchg, /* date of last change */
291 sp->sp_min, /* minimum number of days between changes */
292 sp->sp_max, /* maximum number of days between changes */
293 sp->sp_warn, /* number of days of warning before password expires */
294 sp->sp_inact, /* number of days after password expires until
295 the account becomes unusable */
296 sp->sp_expire, /* days since 1/1/70 until account expires */