[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.27

1.27    ! millert     1: /*     $OpenBSD: edit.c,v 1.26 2003/04/03 17:16:32 millert Exp $       */
1.2       deraadt     2: /*     $NetBSD: edit.c,v 1.6 1996/05/15 21:50:45 jtc Exp $     */
1.1       deraadt     3:
                      4: /*-
                      5:  * Copyright (c) 1990, 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.
1.27    ! millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: #if 0
                     35: static char sccsid[] = "@(#)edit.c     8.3 (Berkeley) 4/2/94";
                     36: #else
1.27    ! millert    37: static char rcsid[] = "$OpenBSD: edit.c,v 1.26 2003/04/03 17:16:32 millert Exp $";
1.1       deraadt    38: #endif
                     39: #endif /* not lint */
                     40:
                     41: #include <sys/param.h>
                     42: #include <sys/stat.h>
                     43:
                     44: #include <ctype.h>
                     45: #include <err.h>
                     46: #include <errno.h>
1.24      millert    47: #include <fcntl.h>
1.1       deraadt    48: #include <paths.h>
                     49: #include <pwd.h>
                     50: #include <stdio.h>
                     51: #include <stdlib.h>
                     52: #include <string.h>
                     53: #include <unistd.h>
1.2       deraadt    54: #include <util.h>
1.1       deraadt    55:
                     56: #include "chpass.h"
                     57:
1.23      millert    58: int
1.2       deraadt    59: edit(tempname, pw)
                     60:        char *tempname;
1.1       deraadt    61:        struct passwd *pw;
                     62: {
                     63:        struct stat begin, end;
                     64:
                     65:        for (;;) {
1.6       deraadt    66:                if (lstat(tempname, &begin) == -1 || S_ISLNK(begin.st_mode))
1.23      millert    67:                        return (EDIT_ERROR);
1.2       deraadt    68:                pw_edit(1, tempname);
1.7       deraadt    69:                if (lstat(tempname, &end) == -1 || S_ISLNK(end.st_mode))
1.23      millert    70:                        return (EDIT_ERROR);
1.17      ho         71:                if (begin.st_mtime == end.st_mtime &&
                     72:                    begin.st_size == end.st_size) {
1.1       deraadt    73:                        warnx("no changes made");
1.23      millert    74:                        return (EDIT_NOCHANGE);
1.1       deraadt    75:                }
1.18      millert    76:                if (verify(tempname, pw))
1.1       deraadt    77:                        break;
                     78:                pw_prompt();
                     79:        }
1.23      millert    80:        return(EDIT_OK);
1.1       deraadt    81: }
                     82:
                     83: /*
                     84:  * display --
                     85:  *     print out the file for the user to edit; strange side-effect:
                     86:  *     set conditional flag if the user gets to edit the shell.
                     87:  */
                     88: void
1.2       deraadt    89: display(tempname, fd, pw)
                     90:        char *tempname;
1.1       deraadt    91:        int fd;
                     92:        struct passwd *pw;
                     93: {
                     94:        FILE *fp;
1.13      deraadt    95:        char *bp, *p;
                     96:        char chngstr[256];
1.1       deraadt    97:
                     98:        if (!(fp = fdopen(fd, "w")))
                     99:                pw_error(tempname, 1, 1);
                    100:
                    101:        (void)fprintf(fp,
1.20      aaron     102:            "# Changing user database information for %s.\n", pw->pw_name);
1.1       deraadt   103:        if (!uid) {
                    104:                (void)fprintf(fp, "Login: %s\n", pw->pw_name);
1.4       downsj    105:                (void)fprintf(fp, "Encrypted password: %s\n", pw->pw_passwd);
1.16      deraadt   106:                (void)fprintf(fp, "Uid [#]: %u\n", pw->pw_uid);
                    107:                (void)fprintf(fp, "Gid [# or name]: %u\n", pw->pw_gid);
1.1       deraadt   108:                (void)fprintf(fp, "Change [month day year]: %s\n",
1.13      deraadt   109:                    ttoa(chngstr, sizeof(chngstr), pw->pw_change));
1.1       deraadt   110:                (void)fprintf(fp, "Expire [month day year]: %s\n",
1.13      deraadt   111:                    ttoa(chngstr, sizeof(chngstr), pw->pw_expire));
1.1       deraadt   112:                (void)fprintf(fp, "Class: %s\n", pw->pw_class);
                    113:                (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
                    114:                (void)fprintf(fp, "Shell: %s\n",
                    115:                    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
                    116:        }
                    117:        /* Only admin can change "restricted" shells. */
                    118:        else if (ok_shell(pw->pw_shell))
                    119:                /*
                    120:                 * Make shell a restricted field.  Ugly with a
                    121:                 * necklace, but there's not much else to do.
                    122:                 */
                    123:                (void)fprintf(fp, "Shell: %s\n",
                    124:                    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
                    125:        else
                    126:                list[E_SHELL].restricted = 1;
                    127:        bp = pw->pw_gecos;
                    128:        p = strsep(&bp, ",");
                    129:        (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
                    130:        p = strsep(&bp, ",");
1.19      aaron     131:        (void)fprintf(fp, "Office Location: %s\n", p ? p : "");
1.1       deraadt   132:        p = strsep(&bp, ",");
                    133:        (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
                    134:        p = strsep(&bp, ",");
                    135:        (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
                    136:
                    137:        (void)fchown(fd, getuid(), getgid());
                    138:        (void)fclose(fp);
                    139: }
                    140:
                    141: int
1.2       deraadt   142: verify(tempname, pw)
                    143:        char *tempname;
1.1       deraadt   144:        struct passwd *pw;
                    145: {
1.22      deraadt   146:        unsigned int len, alen, line;
                    147:        static char buf[LINE_MAX];
                    148:        struct stat sb;
                    149:        char *p, *q;
1.1       deraadt   150:        ENTRY *ep;
                    151:        FILE *fp;
1.24      millert   152:        int fd;
1.1       deraadt   153:
1.24      millert   154:        if ((fd = open(tempname, O_RDONLY|O_NOFOLLOW)) == -1 ||
                    155:            (fp = fdopen(fd, "r")) == NULL)
1.1       deraadt   156:                pw_error(tempname, 1, 1);
1.24      millert   157:        if (fstat(fd, &sb))
1.1       deraadt   158:                pw_error(tempname, 1, 1);
1.25      millert   159:        if (sb.st_size == 0 || sb.st_nlink != 1 || sb.st_uid != uid) {
1.1       deraadt   160:                warnx("corrupted temporary file");
                    161:                goto bad;
                    162:        }
1.11      deraadt   163:        line = 0;
1.1       deraadt   164:        while (fgets(buf, sizeof(buf), fp)) {
1.11      deraadt   165:                line++;
1.1       deraadt   166:                if (!buf[0] || buf[0] == '#')
                    167:                        continue;
1.26      millert   168:                if ((p = strchr(buf, '\n')) != NULL)
                    169:                        *p = '\0';
                    170:                else if (!feof(fp)) {
1.22      deraadt   171:                        warnx("line %u too long", line);
1.1       deraadt   172:                        goto bad;
                    173:                }
                    174:                for (ep = list;; ++ep) {
                    175:                        if (!ep->prompt) {
1.22      deraadt   176:                                warnx("unrecognized field on line %u", line);
1.1       deraadt   177:                                goto bad;
                    178:                        }
                    179:                        if (!strncasecmp(buf, ep->prompt, ep->len)) {
                    180:                                if (ep->restricted && uid) {
                    181:                                        warnx(
                    182:                                            "you may not change the %s field",
                    183:                                                ep->prompt);
                    184:                                        goto bad;
                    185:                                }
                    186:                                if (!(p = strchr(buf, ':'))) {
1.22      deraadt   187:                                        warnx("line %u corrupted", line);
1.1       deraadt   188:                                        goto bad;
                    189:                                }
                    190:                                while (isspace(*++p));
1.20      aaron     191:                                for (q = p; *q && isprint(*q); q++) {
                    192:                                        if (ep->except && strchr(ep->except,*q))
                    193:                                                break;
                    194:                                }
                    195:                                if (*q) {
1.1       deraadt   196:                                        warnx(
                    197:                                   "illegal character in the \"%s\" field",
                    198:                                            ep->prompt);
                    199:                                        goto bad;
                    200:                                }
                    201:                                if ((ep->func)(p, pw, ep)) {
                    202: bad:                                   (void)fclose(fp);
                    203:                                        return (0);
                    204:                                }
                    205:                                break;
                    206:                        }
                    207:                }
                    208:        }
                    209:        (void)fclose(fp);
1.14      deraadt   210:
                    211:        if (list[E_NAME].save == NULL)
                    212:                list[E_NAME].save = "";
                    213:        if (list[E_BPHONE].save == NULL)
                    214:                list[E_BPHONE].save = "";
                    215:        if (list[E_HPHONE].save == NULL)
                    216:                list[E_HPHONE].save = "";
                    217:        if (list[E_LOCATE].save == NULL)
                    218:                list[E_LOCATE].save = "";
1.1       deraadt   219:
                    220:        /* Build the gecos field. */
                    221:        len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
                    222:            strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
1.9       deraadt   223:        for (alen = 0, p = list[E_NAME].save; *p; p++)
                    224:                if (*p == '&')
                    225:                        alen = alen + strlen(pw->pw_name) - 1;
1.1       deraadt   226:        if (!(p = malloc(len)))
                    227:                err(1, NULL);
1.22      deraadt   228:        (void)snprintf(p, len, "%s,%s,%s,%s", list[E_NAME].save,
1.1       deraadt   229:            list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
1.22      deraadt   230:        pw->pw_gecos = p;
1.1       deraadt   231:
                    232:        if (snprintf(buf, sizeof(buf),
1.16      deraadt   233:            "%s:%s:%u:%u:%s:%ld:%ld:%s:%s:%s",
1.1       deraadt   234:            pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
1.21      deraadt   235:            (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
1.10      deraadt   236:            pw->pw_shell) >= 1023 ||
                    237:            strlen(buf) + alen >= 1023) {
1.1       deraadt   238:                warnx("entries too long");
1.9       deraadt   239:                free(p);
1.1       deraadt   240:                return (0);
                    241:        }
1.9       deraadt   242:        free(p);
                    243:
1.12      kstailey  244:        return (pw_scan(buf, pw, NULL));
1.1       deraadt   245: }