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

1.26    ! millert     1: /*     $OpenBSD: edit.c,v 1.25 2003/02/07 23:28:18 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.
                     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[] = "@(#)edit.c     8.3 (Berkeley) 4/2/94";
                     40: #else
1.26    ! millert    41: static char rcsid[] = "$OpenBSD: edit.c,v 1.25 2003/02/07 23:28:18 millert Exp $";
1.1       deraadt    42: #endif
                     43: #endif /* not lint */
                     44:
                     45: #include <sys/param.h>
                     46: #include <sys/stat.h>
                     47:
                     48: #include <ctype.h>
                     49: #include <err.h>
                     50: #include <errno.h>
1.24      millert    51: #include <fcntl.h>
1.1       deraadt    52: #include <paths.h>
                     53: #include <pwd.h>
                     54: #include <stdio.h>
                     55: #include <stdlib.h>
                     56: #include <string.h>
                     57: #include <unistd.h>
1.2       deraadt    58: #include <util.h>
1.1       deraadt    59:
                     60: #include "chpass.h"
                     61:
1.23      millert    62: int
1.2       deraadt    63: edit(tempname, pw)
                     64:        char *tempname;
1.1       deraadt    65:        struct passwd *pw;
                     66: {
                     67:        struct stat begin, end;
                     68:
                     69:        for (;;) {
1.6       deraadt    70:                if (lstat(tempname, &begin) == -1 || S_ISLNK(begin.st_mode))
1.23      millert    71:                        return (EDIT_ERROR);
1.2       deraadt    72:                pw_edit(1, tempname);
1.7       deraadt    73:                if (lstat(tempname, &end) == -1 || S_ISLNK(end.st_mode))
1.23      millert    74:                        return (EDIT_ERROR);
1.17      ho         75:                if (begin.st_mtime == end.st_mtime &&
                     76:                    begin.st_size == end.st_size) {
1.1       deraadt    77:                        warnx("no changes made");
1.23      millert    78:                        return (EDIT_NOCHANGE);
1.1       deraadt    79:                }
1.18      millert    80:                if (verify(tempname, pw))
1.1       deraadt    81:                        break;
                     82:                pw_prompt();
                     83:        }
1.23      millert    84:        return(EDIT_OK);
1.1       deraadt    85: }
                     86:
                     87: /*
                     88:  * display --
                     89:  *     print out the file for the user to edit; strange side-effect:
                     90:  *     set conditional flag if the user gets to edit the shell.
                     91:  */
                     92: void
1.2       deraadt    93: display(tempname, fd, pw)
                     94:        char *tempname;
1.1       deraadt    95:        int fd;
                     96:        struct passwd *pw;
                     97: {
                     98:        FILE *fp;
1.13      deraadt    99:        char *bp, *p;
                    100:        char chngstr[256];
1.1       deraadt   101:
                    102:        if (!(fp = fdopen(fd, "w")))
                    103:                pw_error(tempname, 1, 1);
                    104:
                    105:        (void)fprintf(fp,
1.20      aaron     106:            "# Changing user database information for %s.\n", pw->pw_name);
1.1       deraadt   107:        if (!uid) {
                    108:                (void)fprintf(fp, "Login: %s\n", pw->pw_name);
1.4       downsj    109:                (void)fprintf(fp, "Encrypted password: %s\n", pw->pw_passwd);
1.16      deraadt   110:                (void)fprintf(fp, "Uid [#]: %u\n", pw->pw_uid);
                    111:                (void)fprintf(fp, "Gid [# or name]: %u\n", pw->pw_gid);
1.1       deraadt   112:                (void)fprintf(fp, "Change [month day year]: %s\n",
1.13      deraadt   113:                    ttoa(chngstr, sizeof(chngstr), pw->pw_change));
1.1       deraadt   114:                (void)fprintf(fp, "Expire [month day year]: %s\n",
1.13      deraadt   115:                    ttoa(chngstr, sizeof(chngstr), pw->pw_expire));
1.1       deraadt   116:                (void)fprintf(fp, "Class: %s\n", pw->pw_class);
                    117:                (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
                    118:                (void)fprintf(fp, "Shell: %s\n",
                    119:                    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
                    120:        }
                    121:        /* Only admin can change "restricted" shells. */
                    122:        else if (ok_shell(pw->pw_shell))
                    123:                /*
                    124:                 * Make shell a restricted field.  Ugly with a
                    125:                 * necklace, but there's not much else to do.
                    126:                 */
                    127:                (void)fprintf(fp, "Shell: %s\n",
                    128:                    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
                    129:        else
                    130:                list[E_SHELL].restricted = 1;
                    131:        bp = pw->pw_gecos;
                    132:        p = strsep(&bp, ",");
                    133:        (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
                    134:        p = strsep(&bp, ",");
1.19      aaron     135:        (void)fprintf(fp, "Office Location: %s\n", p ? p : "");
1.1       deraadt   136:        p = strsep(&bp, ",");
                    137:        (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
                    138:        p = strsep(&bp, ",");
                    139:        (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
                    140:
                    141:        (void)fchown(fd, getuid(), getgid());
                    142:        (void)fclose(fp);
                    143: }
                    144:
                    145: int
1.2       deraadt   146: verify(tempname, pw)
                    147:        char *tempname;
1.1       deraadt   148:        struct passwd *pw;
                    149: {
1.22      deraadt   150:        unsigned int len, alen, line;
                    151:        static char buf[LINE_MAX];
                    152:        struct stat sb;
                    153:        char *p, *q;
1.1       deraadt   154:        ENTRY *ep;
                    155:        FILE *fp;
1.24      millert   156:        int fd;
1.1       deraadt   157:
1.24      millert   158:        if ((fd = open(tempname, O_RDONLY|O_NOFOLLOW)) == -1 ||
                    159:            (fp = fdopen(fd, "r")) == NULL)
1.1       deraadt   160:                pw_error(tempname, 1, 1);
1.24      millert   161:        if (fstat(fd, &sb))
1.1       deraadt   162:                pw_error(tempname, 1, 1);
1.25      millert   163:        if (sb.st_size == 0 || sb.st_nlink != 1 || sb.st_uid != uid) {
1.1       deraadt   164:                warnx("corrupted temporary file");
                    165:                goto bad;
                    166:        }
1.11      deraadt   167:        line = 0;
1.1       deraadt   168:        while (fgets(buf, sizeof(buf), fp)) {
1.11      deraadt   169:                line++;
1.1       deraadt   170:                if (!buf[0] || buf[0] == '#')
                    171:                        continue;
1.26    ! millert   172:                if ((p = strchr(buf, '\n')) != NULL)
        !           173:                        *p = '\0';
        !           174:                else if (!feof(fp)) {
1.22      deraadt   175:                        warnx("line %u too long", line);
1.1       deraadt   176:                        goto bad;
                    177:                }
                    178:                for (ep = list;; ++ep) {
                    179:                        if (!ep->prompt) {
1.22      deraadt   180:                                warnx("unrecognized field on line %u", line);
1.1       deraadt   181:                                goto bad;
                    182:                        }
                    183:                        if (!strncasecmp(buf, ep->prompt, ep->len)) {
                    184:                                if (ep->restricted && uid) {
                    185:                                        warnx(
                    186:                                            "you may not change the %s field",
                    187:                                                ep->prompt);
                    188:                                        goto bad;
                    189:                                }
                    190:                                if (!(p = strchr(buf, ':'))) {
1.22      deraadt   191:                                        warnx("line %u corrupted", line);
1.1       deraadt   192:                                        goto bad;
                    193:                                }
                    194:                                while (isspace(*++p));
1.20      aaron     195:                                for (q = p; *q && isprint(*q); q++) {
                    196:                                        if (ep->except && strchr(ep->except,*q))
                    197:                                                break;
                    198:                                }
                    199:                                if (*q) {
1.1       deraadt   200:                                        warnx(
                    201:                                   "illegal character in the \"%s\" field",
                    202:                                            ep->prompt);
                    203:                                        goto bad;
                    204:                                }
                    205:                                if ((ep->func)(p, pw, ep)) {
                    206: bad:                                   (void)fclose(fp);
                    207:                                        return (0);
                    208:                                }
                    209:                                break;
                    210:                        }
                    211:                }
                    212:        }
                    213:        (void)fclose(fp);
1.14      deraadt   214:
                    215:        if (list[E_NAME].save == NULL)
                    216:                list[E_NAME].save = "";
                    217:        if (list[E_BPHONE].save == NULL)
                    218:                list[E_BPHONE].save = "";
                    219:        if (list[E_HPHONE].save == NULL)
                    220:                list[E_HPHONE].save = "";
                    221:        if (list[E_LOCATE].save == NULL)
                    222:                list[E_LOCATE].save = "";
1.1       deraadt   223:
                    224:        /* Build the gecos field. */
                    225:        len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
                    226:            strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
1.9       deraadt   227:        for (alen = 0, p = list[E_NAME].save; *p; p++)
                    228:                if (*p == '&')
                    229:                        alen = alen + strlen(pw->pw_name) - 1;
1.1       deraadt   230:        if (!(p = malloc(len)))
                    231:                err(1, NULL);
1.22      deraadt   232:        (void)snprintf(p, len, "%s,%s,%s,%s", list[E_NAME].save,
1.1       deraadt   233:            list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
1.22      deraadt   234:        pw->pw_gecos = p;
1.1       deraadt   235:
                    236:        if (snprintf(buf, sizeof(buf),
1.16      deraadt   237:            "%s:%s:%u:%u:%s:%ld:%ld:%s:%s:%s",
1.1       deraadt   238:            pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
1.21      deraadt   239:            (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
1.10      deraadt   240:            pw->pw_shell) >= 1023 ||
                    241:            strlen(buf) + alen >= 1023) {
1.1       deraadt   242:                warnx("entries too long");
1.9       deraadt   243:                free(p);
1.1       deraadt   244:                return (0);
                    245:        }
1.9       deraadt   246:        free(p);
                    247:
1.12      kstailey  248:        return (pw_scan(buf, pw, NULL));
1.1       deraadt   249: }