NetBSD/usr.bin/chpass/field.c

289 lines
5.7 KiB
C
Raw Normal View History

/* $NetBSD: field.c,v 1.8 2002/05/09 01:58:39 simonb Exp $ */
1995-03-26 08:55:22 +04:00
1993-03-21 12:45:37 +03:00
/*
1995-03-26 08:55:22 +04:00
* Copyright (c) 1988, 1993, 1994
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1995-03-26 08:55:22 +04:00
#if 0
static char sccsid[] = "@(#)field.c 8.4 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: field.c,v 1.8 2002/05/09 01:58:39 simonb Exp $");
1995-03-26 08:55:22 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
1995-03-26 08:55:22 +04:00
#include <ctype.h>
#include <err.h>
#include <errno.h>
1993-03-21 12:45:37 +03:00
#include <grp.h>
1995-03-26 08:55:22 +04:00
#include <pwd.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
1995-03-26 08:55:22 +04:00
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
1993-03-21 12:45:37 +03:00
#include "chpass.h"
#include "pathnames.h"
/* ARGSUSED */
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
p_login(p, pw, ep)
1998-07-26 18:57:56 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
ENTRY *ep;
{
1998-07-05 18:26:06 +04:00
1993-03-21 12:45:37 +03:00
if (!*p) {
1995-03-26 08:55:22 +04:00
warnx("empty login field");
return (1);
1993-03-21 12:45:37 +03:00
}
if (*p == '-') {
1995-03-26 08:55:22 +04:00
warnx("login names may not begin with a hyphen");
return (1);
1993-03-21 12:45:37 +03:00
}
if (!(pw->pw_name = strdup(p))) {
1995-03-26 08:55:22 +04:00
warnx("can't save entry");
return (1);
1993-03-21 12:45:37 +03:00
}
1995-03-26 08:55:22 +04:00
if (strchr(p, '.'))
warnx("\'.\' is dangerous in a login name");
1993-03-21 12:45:37 +03:00
for (; *p; ++p)
if (isupper(*p)) {
1995-03-26 08:55:22 +04:00
warnx("upper-case letters are dangerous in a login name");
1993-03-21 12:45:37 +03:00
break;
}
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
p_passwd(p, pw, ep)
1998-07-26 18:57:56 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
ENTRY *ep;
{
1998-07-05 18:26:06 +04:00
1993-03-21 12:45:37 +03:00
if (!*p)
pw->pw_passwd = ""; /* "NOLOGIN"; */
else if (!(pw->pw_passwd = strdup(p))) {
1995-03-26 08:55:22 +04:00
warnx("can't save password entry");
return (1);
1993-03-21 12:45:37 +03:00
}
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
p_uid(p, pw, ep)
1998-07-26 18:57:56 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
ENTRY *ep;
{
unsigned long id;
1995-03-26 08:55:22 +04:00
char *np;
1993-03-21 12:45:37 +03:00
if (!*p) {
1995-03-26 08:55:22 +04:00
warnx("empty uid field");
return (1);
1993-03-21 12:45:37 +03:00
}
if (!isdigit(*p)) {
1995-03-26 08:55:22 +04:00
warnx("illegal uid");
return (1);
1993-03-21 12:45:37 +03:00
}
1995-03-26 08:55:22 +04:00
errno = 0;
id = strtoul(p, &np, 10);
/*
* We don't need to check the return value of strtoul()
* since ULONG_MAX is greater than UID_MAX.
*/
if (*np || id > UID_MAX) {
1995-03-26 08:55:22 +04:00
warnx("illegal uid");
return (1);
1993-03-21 12:45:37 +03:00
}
pw->pw_uid = (uid_t)id;
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
p_gid(p, pw, ep)
1998-07-26 18:57:56 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
ENTRY *ep;
{
struct group *gr;
unsigned long id;
1995-03-26 08:55:22 +04:00
char *np;
1993-03-21 12:45:37 +03:00
if (!*p) {
1995-03-26 08:55:22 +04:00
warnx("empty gid field");
return (1);
1993-03-21 12:45:37 +03:00
}
if (!isdigit(*p)) {
if (!(gr = getgrnam(p))) {
1995-03-26 08:55:22 +04:00
warnx("unknown group %s", p);
return (1);
1993-03-21 12:45:37 +03:00
}
pw->pw_gid = gr->gr_gid;
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1995-03-26 08:55:22 +04:00
errno = 0;
id = strtoul(p, &np, 10);
/*
* We don't need to check the return value of strtoul()
* since ULONG_MAX is greater than GID_MAX.
*/
if (*np || id > GID_MAX) {
1995-03-26 08:55:22 +04:00
warnx("illegal gid");
return (1);
1993-03-21 12:45:37 +03:00
}
pw->pw_gid = (gid_t)id;
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
p_class(p, pw, ep)
1998-07-26 18:57:56 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
ENTRY *ep;
{
1998-07-05 18:26:06 +04:00
1993-03-21 12:45:37 +03:00
if (!*p)
pw->pw_class = "";
else if (!(pw->pw_class = strdup(p))) {
1995-03-26 08:55:22 +04:00
warnx("can't save entry");
return (1);
1993-03-21 12:45:37 +03:00
}
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
p_change(p, pw, ep)
1998-07-26 18:57:56 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
ENTRY *ep;
{
1998-07-05 18:26:06 +04:00
1993-03-21 12:45:37 +03:00
if (!atot(p, &pw->pw_change))
1995-03-26 08:55:22 +04:00
return (0);
warnx("illegal date for change field");
return (1);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
p_expire(p, pw, ep)
1998-07-26 18:57:56 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
ENTRY *ep;
{
1998-07-05 18:26:06 +04:00
1993-03-21 12:45:37 +03:00
if (!atot(p, &pw->pw_expire))
1995-03-26 08:55:22 +04:00
return (0);
warnx("illegal date for expire field");
return (1);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
p_gecos(p, pw, ep)
1998-07-26 18:57:56 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
ENTRY *ep;
{
1998-07-05 18:26:06 +04:00
if (!(ep->save = strdup(p))) {
1995-03-26 08:55:22 +04:00
warnx("can't save entry");
return (1);
1993-03-21 12:45:37 +03:00
}
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
p_hdir(p, pw, ep)
1998-07-26 18:57:56 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
ENTRY *ep;
{
1998-07-05 18:26:06 +04:00
1993-03-21 12:45:37 +03:00
if (!*p) {
1995-03-26 08:55:22 +04:00
warnx("empty home directory field");
return (1);
1993-03-21 12:45:37 +03:00
}
if (!(pw->pw_dir = strdup(p))) {
1995-03-26 08:55:22 +04:00
warnx("can't save entry");
return (1);
1993-03-21 12:45:37 +03:00
}
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
p_shell(p, pw, ep)
1998-07-26 18:57:56 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
struct passwd *pw;
ENTRY *ep;
{
1998-07-26 18:57:56 +04:00
const char *t;
1993-03-21 12:45:37 +03:00
if (!*p) {
pw->pw_shell = _PATH_BSHELL;
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* only admin can change from or to "restricted" shells */
if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
1995-03-26 08:55:22 +04:00
warnx("%s: current shell non-standard", pw->pw_shell);
return (1);
1993-03-21 12:45:37 +03:00
}
if (!(t = ok_shell(p))) {
if (uid) {
1995-03-26 08:55:22 +04:00
warnx("%s: non-standard shell", p);
return (1);
1993-03-21 12:45:37 +03:00
}
}
else
p = t;
if (!(pw->pw_shell = strdup(p))) {
1995-03-26 08:55:22 +04:00
warnx("can't save entry");
return (1);
1993-03-21 12:45:37 +03:00
}
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}