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

Annotation of src/usr.bin/chpass/edit.c, Revision 1.1.1.1

1.1       deraadt     1: /*     $NetBSD: edit.c,v 1.5 1995/07/28 07:03:41 phil Exp $    */
                      2:
                      3: /*-
                      4:  * Copyright (c) 1990, 1993, 1994
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     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. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: #if 0
                     38: static char sccsid[] = "@(#)edit.c     8.3 (Berkeley) 4/2/94";
                     39: #else
                     40: static char rcsid[] = "$NetBSD: edit.c,v 1.5 1995/07/28 07:03:41 phil Exp $";
                     41: #endif
                     42: #endif /* not lint */
                     43:
                     44: #include <sys/param.h>
                     45: #include <sys/stat.h>
                     46:
                     47: #include <ctype.h>
                     48: #include <err.h>
                     49: #include <errno.h>
                     50: #include <paths.h>
                     51: #include <pwd.h>
                     52: #include <stdio.h>
                     53: #include <stdlib.h>
                     54: #include <string.h>
                     55: #include <unistd.h>
                     56:
                     57: #include <pw_scan.h>
                     58: #include <pw_util.h>
                     59:
                     60: #include "chpass.h"
                     61:
                     62: extern char *tempname;
                     63:
                     64: void
                     65: edit(pw)
                     66:        struct passwd *pw;
                     67: {
                     68:        struct stat begin, end;
                     69:
                     70:        for (;;) {
                     71:                if (stat(tempname, &begin))
                     72:                        pw_error(tempname, 1, 1);
                     73:                pw_edit(1);
                     74:                if (stat(tempname, &end))
                     75:                        pw_error(tempname, 1, 1);
                     76:                if (begin.st_mtime == end.st_mtime) {
                     77:                        warnx("no changes made");
                     78:                        pw_error(NULL, 0, 0);
                     79:                }
                     80:                if (verify(pw))
                     81:                        break;
                     82:                pw_prompt();
                     83:        }
                     84: }
                     85:
                     86: /*
                     87:  * display --
                     88:  *     print out the file for the user to edit; strange side-effect:
                     89:  *     set conditional flag if the user gets to edit the shell.
                     90:  */
                     91: void
                     92: display(fd, pw)
                     93:        int fd;
                     94:        struct passwd *pw;
                     95: {
                     96:        FILE *fp;
                     97:        char *bp, *p, *ttoa();
                     98:
                     99:        if (!(fp = fdopen(fd, "w")))
                    100:                pw_error(tempname, 1, 1);
                    101:
                    102:        (void)fprintf(fp,
                    103:            "#Changing user database information for %s.\n", pw->pw_name);
                    104:        if (!uid) {
                    105:                (void)fprintf(fp, "Login: %s\n", pw->pw_name);
                    106:                (void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
                    107:                (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
                    108:                (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
                    109:                (void)fprintf(fp, "Change [month day year]: %s\n",
                    110:                    ttoa(pw->pw_change));
                    111:                (void)fprintf(fp, "Expire [month day year]: %s\n",
                    112:                    ttoa(pw->pw_expire));
                    113:                (void)fprintf(fp, "Class: %s\n", pw->pw_class);
                    114:                (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
                    115:                (void)fprintf(fp, "Shell: %s\n",
                    116:                    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
                    117:        }
                    118:        /* Only admin can change "restricted" shells. */
                    119:        else if (ok_shell(pw->pw_shell))
                    120:                /*
                    121:                 * Make shell a restricted field.  Ugly with a
                    122:                 * necklace, but there's not much else to do.
                    123:                 */
                    124:                (void)fprintf(fp, "Shell: %s\n",
                    125:                    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
                    126:        else
                    127:                list[E_SHELL].restricted = 1;
                    128:        bp = pw->pw_gecos;
                    129:        p = strsep(&bp, ",");
                    130:        (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
                    131:        p = strsep(&bp, ",");
                    132:        (void)fprintf(fp, "Location: %s\n", p ? p : "");
                    133:        p = strsep(&bp, ",");
                    134:        (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
                    135:        p = strsep(&bp, ",");
                    136:        (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
                    137:
                    138:        (void)fchown(fd, getuid(), getgid());
                    139:        (void)fclose(fp);
                    140: }
                    141:
                    142: int
                    143: verify(pw)
                    144:        struct passwd *pw;
                    145: {
                    146:        ENTRY *ep;
                    147:        char *p;
                    148:        struct stat sb;
                    149:        FILE *fp;
                    150:        int len;
                    151:        static char buf[LINE_MAX];
                    152:
                    153:        if (!(fp = fopen(tempname, "r")))
                    154:                pw_error(tempname, 1, 1);
                    155:        if (fstat(fileno(fp), &sb))
                    156:                pw_error(tempname, 1, 1);
                    157:        if (sb.st_size == 0) {
                    158:                warnx("corrupted temporary file");
                    159:                goto bad;
                    160:        }
                    161:        while (fgets(buf, sizeof(buf), fp)) {
                    162:                if (!buf[0] || buf[0] == '#')
                    163:                        continue;
                    164:                if (!(p = strchr(buf, '\n'))) {
                    165:                        warnx("line too long");
                    166:                        goto bad;
                    167:                }
                    168:                *p = '\0';
                    169:                for (ep = list;; ++ep) {
                    170:                        if (!ep->prompt) {
                    171:                                warnx("unrecognized field");
                    172:                                goto bad;
                    173:                        }
                    174:                        if (!strncasecmp(buf, ep->prompt, ep->len)) {
                    175:                                if (ep->restricted && uid) {
                    176:                                        warnx(
                    177:                                            "you may not change the %s field",
                    178:                                                ep->prompt);
                    179:                                        goto bad;
                    180:                                }
                    181:                                if (!(p = strchr(buf, ':'))) {
                    182:                                        warnx("line corrupted");
                    183:                                        goto bad;
                    184:                                }
                    185:                                while (isspace(*++p));
                    186:                                if (ep->except && strpbrk(p, ep->except)) {
                    187:                                        warnx(
                    188:                                   "illegal character in the \"%s\" field",
                    189:                                            ep->prompt);
                    190:                                        goto bad;
                    191:                                }
                    192:                                if ((ep->func)(p, pw, ep)) {
                    193: bad:                                   (void)fclose(fp);
                    194:                                        return (0);
                    195:                                }
                    196:                                break;
                    197:                        }
                    198:                }
                    199:        }
                    200:        (void)fclose(fp);
                    201:
                    202:        /* Build the gecos field. */
                    203:        len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
                    204:            strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
                    205:        if (!(p = malloc(len)))
                    206:                err(1, NULL);
                    207:        (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
                    208:            list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
                    209:
                    210:        if (snprintf(buf, sizeof(buf),
                    211:            "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
                    212:            pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
                    213:            pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
                    214:            pw->pw_shell) >= sizeof(buf)) {
                    215:                warnx("entries too long");
                    216:                return (0);
                    217:        }
                    218:        return (pw_scan(buf, pw, (int *)NULL));
                    219: }