2008-04-27 06:10:09 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 1990 - 1994, Julianne Frances Haugh
|
|
|
|
* SPDX-FileCopyrightText: 1996 - 2000, Marek Michałkiewicz
|
|
|
|
* SPDX-FileCopyrightText: 2001 , Michał Moskal
|
|
|
|
* SPDX-FileCopyrightText: 2003 - 2005, Tomasz Kłoczko
|
|
|
|
* SPDX-FileCopyrightText: 2007 - 2009, Nicolas François
|
2008-04-27 06:10:09 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2008-04-27 06:10:09 +05:30
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "commonio.h"
|
|
|
|
#include "pwio.h"
|
|
|
|
|
* libmisc/xgetXXbyYY.c, libmisc/myname.c, libmisc/getgr_nam_gid.c,
libmisc/salt.c, libmisc/list.c, libmisc/cleanup.c, src/login.c,
lib/getdef.h, lib/groupio.c, lib/getlong.c, lib/gshadow_.h,
lib/sgroupio.c, lib/shadowio.c, lib/pwio.c, lib/commonio.h,
lib/fputsx.c, lib/prototypes.h: Added splint annotations.
* lib/groupio.c: Avoid implicit conversion of pointers to
booleans.
* lib/groupio.c: Free allocated buffers in case of failure.
2009-04-23 15:27:03 +05:30
|
|
|
static /*@null@*/ /*@only@*/void *passwd_dup (const void *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const struct passwd *pw = ent;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
|
|
|
return __pw_dup (pw);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
* libmisc/utmp.c, libmisc/age.c, libmisc/shell.c, lib/groupio.c,
lib/groupio.h, lib/sgroupio.c, lib/sgroupio.h, lib/shadowio.c,
lib/pwio.c, lib/commonio.c, lib/shadowio.h, lib/pwio.h,
lib/commonio.h, lib/prototypes.h: Added splint annotations.
2009-04-23 02:51:14 +05:30
|
|
|
static void passwd_free (/*@out@*/ /*@only@*/void *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct passwd *pw = ent;
|
|
|
|
|
2009-04-22 03:44:10 +05:30
|
|
|
pw_free (pw);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static const char *passwd_getname (const void *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const struct passwd *pw = ent;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
return pw->pw_name;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static void *passwd_parse (const char *line)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2023-02-01 18:20:48 +05:30
|
|
|
return sgetpwent (line);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static int passwd_put (const void *ent, FILE * file)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const struct passwd *pw = ent;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
2011-02-17 02:02:16 +05:30
|
|
|
if ( (NULL == pw)
|
|
|
|
|| (valid_field (pw->pw_name, ":\n") == -1)
|
|
|
|
|| (valid_field (pw->pw_passwd, ":\n") == -1)
|
|
|
|
|| (pw->pw_uid == (uid_t)-1)
|
|
|
|
|| (pw->pw_gid == (gid_t)-1)
|
|
|
|
|| (valid_field (pw->pw_gecos, ":\n") == -1)
|
|
|
|
|| (valid_field (pw->pw_dir, ":\n") == -1)
|
2022-10-07 16:06:59 +05:30
|
|
|
|| (valid_field (pw->pw_shell, ":\n") == -1)
|
|
|
|
|| (strlen (pw->pw_name) + strlen (pw->pw_passwd) +
|
|
|
|
strlen (pw->pw_gecos) + strlen (pw->pw_dir) +
|
|
|
|
strlen (pw->pw_shell) + 100 > PASSWD_ENTRY_MAX_LENGTH)) {
|
2011-02-17 02:02:16 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
return (putpwent (pw, file) == -1) ? -1 : 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static struct commonio_ops passwd_ops = {
|
|
|
|
passwd_dup,
|
|
|
|
passwd_free,
|
|
|
|
passwd_getname,
|
|
|
|
passwd_parse,
|
|
|
|
passwd_put,
|
|
|
|
fgets,
|
2007-11-23 05:37:59 +05:30
|
|
|
fputs,
|
|
|
|
NULL, /* open_hook */
|
|
|
|
NULL /* close_hook */
|
2007-10-07 17:14:02 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static struct commonio_db passwd_db = {
|
2007-10-07 17:16:07 +05:30
|
|
|
PASSWD_FILE, /* filename */
|
|
|
|
&passwd_ops, /* ops */
|
|
|
|
NULL, /* fp */
|
2007-11-20 01:55:36 +05:30
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
NULL, /* scontext */
|
|
|
|
#endif
|
2015-02-27 21:56:57 +05:30
|
|
|
0644, /* st_mode */
|
|
|
|
0, /* st_uid */
|
|
|
|
0, /* st_gid */
|
2007-10-07 17:16:07 +05:30
|
|
|
NULL, /* head */
|
|
|
|
NULL, /* tail */
|
|
|
|
NULL, /* cursor */
|
2008-05-26 14:19:44 +05:30
|
|
|
false, /* changed */
|
|
|
|
false, /* isopen */
|
|
|
|
false, /* locked */
|
2019-05-02 18:03:06 +05:30
|
|
|
false, /* readonly */
|
|
|
|
false /* setname */
|
2007-10-07 17:14:02 +05:30
|
|
|
};
|
|
|
|
|
* lib/groupio.c, lib/groupio.h, lib/pwio.c, lib/pwio.h,
lib/sgroupio.c, lib/sgroupio.h, lib/shadowio.c, lib/shadowio.h:
Added *_dbname() functions to retrieve the name of the databases.
* lib/groupio.c, lib/groupio.h, lib/pwio.c, lib/pwio.h,
lib/sgroupio.c, lib/sgroupio.h, lib/shadowio.c, lib/shadowio.h:
*_name() functions renamed *setname().
* src/grpck.c, src/pwck.c: Likewise.
* lib/groupio.h, lib/pwio.h, lib/sgroupio.h, lib/shadowio.h: Added
the name of the arguments to the prototypes.
* src/chage, src/chfn.c, src/chgpasswd.c, src/chpasswd.c,
src/chsh.c, src/gpasswd.c, src/groupadd.c, src/groupdel.c,
src/groupmod.c, src/grpck.c, src/grpconv.c, src/grpunconv.c,
src/newusers.c, src/passwd.c, src/pwck.c, src/pwconv.c,
src/pwunconv.c, src/useradd.c, src/userdel.c, src/usermod.c:
Harmonize the erro & syslog messages in case of failure of the
*_lock(), *_open(), *_close(), *_unlock(), *_remove() functions.
* src/chgpasswd.c, src/chpasswd.c, src/usermod.c: Avoid
capitalized messages.
* src/chpasswd.c, src/useradd.c, src/usermod.c: Harmonize messages
in case of inexistent entries.
* src/usermod.c: Harmonize messages in case of already existing
entries.
* src/newusers.c, src/useradd.c: Simplify PAM error handling.
* src/useradd.c: Report failures to unlock files (stderr, syslog,
and audit). But do not fail (continue).
* src/useradd.c (open_files): Do not report to syslog & audit
failures to lock or open the databases. This might be harmless,
and the logs were not already informed that a change was
requested.
* src/usermod.c: It's not the account which is unlocked, but its
password.
2008-08-06 21:27:31 +05:30
|
|
|
int pw_setdbname (const char *filename)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_setname (&passwd_db, filename);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
* libmisc/utmp.c, libmisc/age.c, libmisc/shell.c, lib/groupio.c,
lib/groupio.h, lib/sgroupio.c, lib/sgroupio.h, lib/shadowio.c,
lib/pwio.c, lib/commonio.c, lib/shadowio.h, lib/pwio.h,
lib/commonio.h, lib/prototypes.h: Added splint annotations.
2009-04-23 02:51:14 +05:30
|
|
|
/*@observer@*/const char *pw_dbname (void)
|
* lib/groupio.c, lib/groupio.h, lib/pwio.c, lib/pwio.h,
lib/sgroupio.c, lib/sgroupio.h, lib/shadowio.c, lib/shadowio.h:
Added *_dbname() functions to retrieve the name of the databases.
* lib/groupio.c, lib/groupio.h, lib/pwio.c, lib/pwio.h,
lib/sgroupio.c, lib/sgroupio.h, lib/shadowio.c, lib/shadowio.h:
*_name() functions renamed *setname().
* src/grpck.c, src/pwck.c: Likewise.
* lib/groupio.h, lib/pwio.h, lib/sgroupio.h, lib/shadowio.h: Added
the name of the arguments to the prototypes.
* src/chage, src/chfn.c, src/chgpasswd.c, src/chpasswd.c,
src/chsh.c, src/gpasswd.c, src/groupadd.c, src/groupdel.c,
src/groupmod.c, src/grpck.c, src/grpconv.c, src/grpunconv.c,
src/newusers.c, src/passwd.c, src/pwck.c, src/pwconv.c,
src/pwunconv.c, src/useradd.c, src/userdel.c, src/usermod.c:
Harmonize the erro & syslog messages in case of failure of the
*_lock(), *_open(), *_close(), *_unlock(), *_remove() functions.
* src/chgpasswd.c, src/chpasswd.c, src/usermod.c: Avoid
capitalized messages.
* src/chpasswd.c, src/useradd.c, src/usermod.c: Harmonize messages
in case of inexistent entries.
* src/usermod.c: Harmonize messages in case of already existing
entries.
* src/newusers.c, src/useradd.c: Simplify PAM error handling.
* src/useradd.c: Report failures to unlock files (stderr, syslog,
and audit). But do not fail (continue).
* src/useradd.c (open_files): Do not report to syslog & audit
failures to lock or open the databases. This might be harmless,
and the logs were not already informed that a change was
requested.
* src/usermod.c: It's not the account which is unlocked, but its
password.
2008-08-06 21:27:31 +05:30
|
|
|
{
|
|
|
|
return passwd_db.filename;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_lock (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_lock (&passwd_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_open (int mode)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_open (&passwd_db, mode);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 02:49:02 +05:30
|
|
|
/*@observer@*/ /*@null@*/const struct passwd *pw_locate (const char *name)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_locate (&passwd_db, name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 02:49:02 +05:30
|
|
|
/*@observer@*/ /*@null@*/const struct passwd *pw_locate_uid (uid_t uid)
|
2008-02-03 22:20:14 +05:30
|
|
|
{
|
|
|
|
const struct passwd *pwd;
|
|
|
|
|
|
|
|
pw_rewind ();
|
|
|
|
while ( ((pwd = pw_next ()) != NULL)
|
|
|
|
&& (pwd->pw_uid != uid)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return pwd;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_update (const struct passwd *pw)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_update (&passwd_db, (const void *) pw);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_remove (const char *name)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_remove (&passwd_db, name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_rewind (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_rewind (&passwd_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 02:49:02 +05:30
|
|
|
/*@observer@*/ /*@null@*/const struct passwd *pw_next (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_next (&passwd_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_close (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_close (&passwd_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_unlock (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_unlock (&passwd_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
* libmisc/utmp.c, libmisc/age.c, libmisc/shell.c, lib/groupio.c,
lib/groupio.h, lib/sgroupio.c, lib/sgroupio.h, lib/shadowio.c,
lib/pwio.c, lib/commonio.c, lib/shadowio.h, lib/pwio.h,
lib/commonio.h, lib/prototypes.h: Added splint annotations.
2009-04-23 02:51:14 +05:30
|
|
|
/*@null@*/struct commonio_entry *__pw_get_head (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
return passwd_db.head;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
void __pw_del_entry (const struct commonio_entry *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
commonio_del_entry (&passwd_db, ent);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:51 +05:30
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
struct commonio_db *__pw_get_db (void)
|
2007-10-07 17:14:51 +05:30
|
|
|
{
|
|
|
|
return &passwd_db;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static int pw_cmp (const void *p1, const void *p2)
|
2007-10-07 17:14:51 +05:30
|
|
|
{
|
|
|
|
uid_t u1, u2;
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
if ((*(struct commonio_entry **) p1)->eptr == NULL)
|
2007-10-07 17:14:51 +05:30
|
|
|
return 1;
|
2007-10-07 17:16:07 +05:30
|
|
|
if ((*(struct commonio_entry **) p2)->eptr == NULL)
|
2007-10-07 17:14:51 +05:30
|
|
|
return -1;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
|
|
|
u1 = ((struct passwd *) (*(struct commonio_entry **) p1)->eptr)->pw_uid;
|
|
|
|
u2 = ((struct passwd *) (*(struct commonio_entry **) p2)->eptr)->pw_uid;
|
2007-10-07 17:14:51 +05:30
|
|
|
|
|
|
|
if (u1 < u2)
|
|
|
|
return -1;
|
|
|
|
else if (u1 > u2)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:25 +05:30
|
|
|
/* Sort entries by UID */
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_sort ()
|
2007-10-07 17:14:51 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_sort (&passwd_db, pw_cmp);
|
2007-10-07 17:14:51 +05:30
|
|
|
}
|