[BACK]Return to field.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / chpass

Annotation of src/usr.bin/chpass/field.c, Revision 1.4

1.4     ! deraadt     1: /*     $OpenBSD: field.c,v 1.3 1998/05/29 22:26:46 downsj Exp $        */
1.1       deraadt     2: /*     $NetBSD: field.c,v 1.3 1995/03/26 04:55:28 glass Exp $  */
                      3:
                      4: /*
                      5:  * Copyright (c) 1988, 1993, 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char sccsid[] = "@(#)field.c    8.4 (Berkeley) 4/2/94";
1.4     ! deraadt    40: #else
        !            41: static char rcsid[] = "$OpenBSD: field.c,v 1.3 1998/05/29 22:26:46 downsj Exp $";
1.1       deraadt    42: #endif
                     43: #endif /* not lint */
                     44:
                     45: #include <sys/param.h>
                     46:
                     47: #include <ctype.h>
                     48: #include <err.h>
                     49: #include <errno.h>
                     50: #include <grp.h>
                     51: #include <pwd.h>
                     52: #include <stdio.h>
                     53: #include <stdlib.h>
                     54: #include <string.h>
                     55: #include <unistd.h>
                     56:
                     57: #include "chpass.h"
                     58: #include "pathnames.h"
                     59:
                     60: /* ARGSUSED */
                     61: int
1.4     ! deraadt    62: p_login(char *p, struct passwd *pw, ENTRY *ep)
1.1       deraadt    63: {
                     64:        if (!*p) {
                     65:                warnx("empty login field");
                     66:                return (1);
                     67:        }
                     68:        if (*p == '-') {
                     69:                warnx("login names may not begin with a hyphen");
                     70:                return (1);
                     71:        }
                     72:        if (!(pw->pw_name = strdup(p))) {
                     73:                warnx("can't save entry");
                     74:                return (1);
                     75:        }
                     76:        if (strchr(p, '.'))
                     77:                warnx("\'.\' is dangerous in a login name");
                     78:        for (; *p; ++p)
                     79:                if (isupper(*p)) {
                     80:                        warnx("upper-case letters are dangerous in a login name");
                     81:                        break;
                     82:                }
                     83:        return (0);
                     84: }
                     85:
                     86: /* ARGSUSED */
                     87: int
1.4     ! deraadt    88: p_passwd(char *p, struct passwd *pw, ENTRY *ep)
1.1       deraadt    89: {
                     90:        if (!*p)
                     91:                pw->pw_passwd = "";     /* "NOLOGIN"; */
                     92:        else if (!(pw->pw_passwd = strdup(p))) {
                     93:                warnx("can't save password entry");
                     94:                return (1);
                     95:        }
1.4     ! deraadt    96:
1.1       deraadt    97:        return (0);
                     98: }
                     99:
                    100: /* ARGSUSED */
                    101: int
1.4     ! deraadt   102: p_uid(char *p, struct passwd *pw, ENTRY *ep)
1.1       deraadt   103: {
                    104:        uid_t id;
                    105:        char *np;
                    106:
                    107:        if (!*p) {
                    108:                warnx("empty uid field");
                    109:                return (1);
                    110:        }
                    111:        if (!isdigit(*p)) {
                    112:                warnx("illegal uid");
                    113:                return (1);
                    114:        }
                    115:        errno = 0;
                    116:        id = strtoul(p, &np, 10);
                    117:        if (*np || (id == ULONG_MAX && errno == ERANGE)) {
                    118:                warnx("illegal uid");
                    119:                return (1);
                    120:        }
                    121:        pw->pw_uid = id;
                    122:        return (0);
                    123: }
                    124:
                    125: /* ARGSUSED */
                    126: int
1.4     ! deraadt   127: p_gid(char *p, struct passwd *pw, ENTRY *ep)
1.1       deraadt   128: {
                    129:        struct group *gr;
                    130:        gid_t id;
                    131:        char *np;
                    132:
                    133:        if (!*p) {
                    134:                warnx("empty gid field");
                    135:                return (1);
                    136:        }
                    137:        if (!isdigit(*p)) {
                    138:                if (!(gr = getgrnam(p))) {
                    139:                        warnx("unknown group %s", p);
                    140:                        return (1);
                    141:                }
                    142:                pw->pw_gid = gr->gr_gid;
                    143:                return (0);
                    144:        }
                    145:        errno = 0;
                    146:        id = strtoul(p, &np, 10);
                    147:        if (*np || (id == ULONG_MAX && errno == ERANGE)) {
                    148:                warnx("illegal gid");
                    149:                return (1);
                    150:        }
                    151:        pw->pw_gid = id;
                    152:        return (0);
                    153: }
                    154:
                    155: /* ARGSUSED */
                    156: int
1.4     ! deraadt   157: p_class(char *p, struct passwd *pw, ENTRY *ep)
1.1       deraadt   158: {
                    159:        if (!*p)
                    160:                pw->pw_class = "";
                    161:        else if (!(pw->pw_class = strdup(p))) {
                    162:                warnx("can't save entry");
                    163:                return (1);
                    164:        }
1.4     ! deraadt   165:
1.1       deraadt   166:        return (0);
                    167: }
                    168:
                    169: /* ARGSUSED */
                    170: int
1.4     ! deraadt   171: p_change(char *p, struct passwd *pw, ENTRY *ep)
1.1       deraadt   172: {
                    173:        if (!atot(p, &pw->pw_change))
                    174:                return (0);
                    175:        warnx("illegal date for change field");
                    176:        return (1);
                    177: }
                    178:
                    179: /* ARGSUSED */
                    180: int
1.4     ! deraadt   181: p_expire(char *p, struct passwd *pw, ENTRY *ep)
1.1       deraadt   182: {
                    183:        if (!atot(p, &pw->pw_expire))
                    184:                return (0);
                    185:        warnx("illegal date for expire field");
                    186:        return (1);
                    187: }
                    188:
                    189: /* ARGSUSED */
                    190: int
1.4     ! deraadt   191: p_gecos(char *p, struct passwd *pw, ENTRY *ep)
1.1       deraadt   192: {
                    193:        if (!*p)
                    194:                ep->save = "";
                    195:        else if (!(ep->save = strdup(p))) {
                    196:                warnx("can't save entry");
                    197:                return (1);
                    198:        }
                    199:        return (0);
                    200: }
                    201:
                    202: /* ARGSUSED */
                    203: int
1.4     ! deraadt   204: p_hdir(char *p, struct passwd *pw, ENTRY *ep)
1.1       deraadt   205: {
                    206:        if (!*p) {
                    207:                warnx("empty home directory field");
                    208:                return (1);
                    209:        }
                    210:        if (!(pw->pw_dir = strdup(p))) {
                    211:                warnx("can't save entry");
                    212:                return (1);
                    213:        }
                    214:        return (0);
                    215: }
                    216:
                    217: /* ARGSUSED */
                    218: int
1.4     ! deraadt   219: p_shell(char *p, struct passwd *pw, ENTRY *ep)
1.1       deraadt   220: {
1.3       downsj    221:        char *t;
1.1       deraadt   222:
                    223:        if (!*p) {
                    224:                pw->pw_shell = _PATH_BSHELL;
                    225:                return (0);
                    226:        }
                    227:        /* only admin can change from or to "restricted" shells */
                    228:        if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
                    229:                warnx("%s: current shell non-standard", pw->pw_shell);
                    230:                return (1);
                    231:        }
                    232:        if (!(t = ok_shell(p))) {
                    233:                if (uid) {
                    234:                        warnx("%s: non-standard shell", p);
                    235:                        return (1);
                    236:                }
1.4     ! deraadt   237:        } else
1.1       deraadt   238:                p = t;
                    239:        if (!(pw->pw_shell = strdup(p))) {
                    240:                warnx("can't save entry");
                    241:                return (1);
                    242:        }
                    243:        return (0);
                    244: }