2007-10-07 20:06:51 +05:30
|
|
|
/*
|
2008-04-27 06:10:09 +05:30
|
|
|
* Copyright (c) 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 1996 - 1998, Marek Michałkiewicz
|
|
|
|
* Copyright (c) 2003 - 2005, Tomasz Kłoczko
|
2007-10-07 20:06:51 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2008-04-27 06:10:09 +05:30
|
|
|
* 3. The name of the copyright holders or contributors may not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
2007-10-07 20:06:51 +05:30
|
|
|
*
|
2008-04-27 06:10:09 +05:30
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-10-07 20:06:51 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#define FIELDS 9
|
|
|
|
#define OFIELDS 5
|
|
|
|
/*
|
|
|
|
* sgetspent - convert string in shadow file format to (struct spwd *)
|
|
|
|
*/
|
|
|
|
struct spwd *sgetspent (const char *string)
|
|
|
|
{
|
|
|
|
static char spwbuf[1024];
|
|
|
|
static struct spwd spwd;
|
|
|
|
char *fields[FIELDS];
|
|
|
|
char *cp;
|
|
|
|
char *cpp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy string to local buffer. It has to be tokenized and we
|
|
|
|
* have to do that to our private copy.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
if (strlen (string) >= sizeof spwbuf) {
|
2007-10-07 20:06:51 +05:30
|
|
|
return 0; /* fail if too long */
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
strcpy (spwbuf, string);
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
cp = strrchr (spwbuf, '\n');
|
|
|
|
if (NULL != cp) {
|
2007-10-07 20:06:51 +05:30
|
|
|
*cp = '\0';
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Tokenize the string into colon separated fields. Allow up to
|
|
|
|
* FIELDS different fields.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
for (cp = spwbuf, i = 0; ('\0' != *cp) && (i < FIELDS); i++) {
|
2007-10-07 20:06:51 +05:30
|
|
|
fields[i] = cp;
|
2008-05-26 15:09:29 +05:30
|
|
|
while (('\0' != *cp) && (':' != *cp)) {
|
2007-10-07 20:06:51 +05:30
|
|
|
cp++;
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
if ('\0' != *cp) {
|
|
|
|
*cp = '\0';
|
|
|
|
cp++;
|
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
}
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
if (i == (FIELDS - 1)) {
|
2007-10-07 20:06:51 +05:30
|
|
|
fields[i++] = cp;
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
if ( ((NULL != cp) && ('\0' != *cp)) ||
|
|
|
|
((i != FIELDS) && (i != OFIELDS)) ) {
|
2007-10-07 20:06:51 +05:30
|
|
|
return 0;
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Start populating the structure. The fields are all in
|
|
|
|
* static storage, as is the structure we pass back.
|
|
|
|
*/
|
|
|
|
|
|
|
|
spwd.sp_namp = fields[0];
|
|
|
|
spwd.sp_pwdp = fields[1];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the last changed date. For all of the integer fields,
|
|
|
|
* we check for proper format. It is an error to have an
|
|
|
|
* incorrectly formatted number.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
spwd.sp_lstchg = strtol (fields[2], &cpp, 10);
|
|
|
|
/* FIXME: (0 == spwd.sp_lstchg) does not look correct */
|
|
|
|
if ((0 == spwd.sp_lstchg) && ('\0' != *cpp)) {
|
2007-10-07 20:06:51 +05:30
|
|
|
return 0;
|
2008-05-26 15:09:29 +05:30
|
|
|
} else if (fields[2][0] == '\0') {
|
2007-10-07 20:06:51 +05:30
|
|
|
spwd.sp_lstchg = -1;
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the minimum period between password changes.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
spwd.sp_min = strtol (fields[3], &cpp, 10);
|
|
|
|
/* FIXME: (0 == spwd.sp_min) does not look correct */
|
|
|
|
if ((0 == spwd.sp_min) && ('\0' != *cpp)) {
|
2007-10-07 20:06:51 +05:30
|
|
|
return 0;
|
2008-05-26 15:09:29 +05:30
|
|
|
} else if (fields[3][0] == '\0') {
|
2007-10-07 20:06:51 +05:30
|
|
|
spwd.sp_min = -1;
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the maximum number of days a password is valid.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
spwd.sp_max = strtol (fields[4], &cpp, 10);
|
|
|
|
/* FIXME: (0 == spwd.sp_max) does not look correct */
|
|
|
|
if ((0 == spwd.sp_max) && ('\0' != *cpp)) {
|
2007-10-07 20:06:51 +05:30
|
|
|
return 0;
|
2008-05-26 15:09:29 +05:30
|
|
|
} else if (fields[4][0] == '\0') {
|
2007-10-07 20:06:51 +05:30
|
|
|
spwd.sp_max = -1;
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* If there are only OFIELDS fields (this is a SVR3.2 /etc/shadow
|
|
|
|
* formatted file), initialize the other field members to -1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (i == OFIELDS) {
|
2008-05-26 15:09:29 +05:30
|
|
|
spwd.sp_warn = -1;
|
|
|
|
spwd.sp_inact = -1;
|
|
|
|
spwd.sp_expire = -1;
|
|
|
|
spwd.sp_flag = -1;
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
return &spwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the number of days of password expiry warning.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
spwd.sp_warn = strtol (fields[5], &cpp, 10);
|
|
|
|
/* FIXME: (0 == spwd.sp_warn) does not look correct */
|
|
|
|
if ((0 == spwd.sp_warn) && ('\0' != *cpp)) {
|
2007-10-07 20:06:51 +05:30
|
|
|
return 0;
|
2008-05-26 15:09:29 +05:30
|
|
|
} else if (fields[5][0] == '\0') {
|
2007-10-07 20:06:51 +05:30
|
|
|
spwd.sp_warn = -1;
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the number of days of inactivity before an account is
|
|
|
|
* disabled.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
spwd.sp_inact = strtol (fields[6], &cpp, 10);
|
|
|
|
/* FIXME: (0 == spwd.sp_inact) does not look correct */
|
|
|
|
if ((0 == spwd.sp_inact) && ('\0' != *cpp)) {
|
2007-10-07 20:06:51 +05:30
|
|
|
return 0;
|
2008-05-26 15:09:29 +05:30
|
|
|
} else if (fields[6][0] == '\0') {
|
2007-10-07 20:06:51 +05:30
|
|
|
spwd.sp_inact = -1;
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the number of days after the epoch before the account is
|
|
|
|
* set to expire.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
spwd.sp_expire = strtol (fields[7], &cpp, 10);
|
|
|
|
/* FIXME: (0 == spwd.sp_expire) does not look correct */
|
|
|
|
if ((0 == spwd.sp_expire) && ('\0' != *cpp)) {
|
2007-10-07 20:06:51 +05:30
|
|
|
return 0;
|
2008-05-26 15:09:29 +05:30
|
|
|
} else if (fields[7][0] == '\0') {
|
2007-10-07 20:06:51 +05:30
|
|
|
spwd.sp_expire = -1;
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* This field is reserved for future use. But it isn't supposed
|
|
|
|
* to have anything other than a valid integer in it.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 15:09:29 +05:30
|
|
|
spwd.sp_flag = strtol (fields[8], &cpp, 10);
|
|
|
|
/* FIXME: (0 == spwd.sp_flag) does not look correct */
|
|
|
|
if ((0 == spwd.sp_flag) && ('\0' != *cpp)) {
|
2007-10-07 20:06:51 +05:30
|
|
|
return 0;
|
2008-05-26 15:09:29 +05:30
|
|
|
} else if (fields[8][0] == '\0') {
|
2007-10-07 20:06:51 +05:30
|
|
|
spwd.sp_flag = -1;
|
2008-05-26 15:09:29 +05:30
|
|
|
}
|
2007-10-07 20:06:51 +05:30
|
|
|
|
|
|
|
return (&spwd);
|
|
|
|
}
|
2008-05-26 15:09:29 +05:30
|
|
|
|