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

1.5     ! deraadt     1: /*     $OpenBSD: edit.c,v 1.4 1996/07/12 02:09:48 downsj 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.5     ! deraadt    41: static char rcsid[] = "$OpenBSD: edit.c,v 1.4 1996/07/12 02:09:48 downsj 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>
                     51: #include <paths.h>
                     52: #include <pwd.h>
                     53: #include <stdio.h>
                     54: #include <stdlib.h>
                     55: #include <string.h>
                     56: #include <unistd.h>
1.2       deraadt    57: #include <util.h>
1.1       deraadt    58:
                     59: #include "chpass.h"
                     60:
                     61: void
1.2       deraadt    62: edit(tempname, pw)
                     63:        char *tempname;
1.1       deraadt    64:        struct passwd *pw;
                     65: {
                     66:        struct stat begin, end;
                     67:
                     68:        for (;;) {
                     69:                if (stat(tempname, &begin))
                     70:                        pw_error(tempname, 1, 1);
1.2       deraadt    71:                pw_edit(1, tempname);
1.1       deraadt    72:                if (stat(tempname, &end))
                     73:                        pw_error(tempname, 1, 1);
                     74:                if (begin.st_mtime == end.st_mtime) {
                     75:                        warnx("no changes made");
1.2       deraadt    76:                        unlink(tempname);
1.1       deraadt    77:                        pw_error(NULL, 0, 0);
                     78:                }
1.2       deraadt    79:                if (verify(tempname, pw))
1.1       deraadt    80:                        break;
                     81:                pw_prompt();
                     82:        }
                     83: }
                     84:
                     85: /*
                     86:  * display --
                     87:  *     print out the file for the user to edit; strange side-effect:
                     88:  *     set conditional flag if the user gets to edit the shell.
                     89:  */
                     90: void
1.2       deraadt    91: display(tempname, fd, pw)
                     92:        char *tempname;
1.1       deraadt    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);
1.4       downsj    106:                (void)fprintf(fp, "Encrypted password: %s\n", pw->pw_passwd);
1.1       deraadt   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
1.2       deraadt   143: verify(tempname, pw)
                    144:        char *tempname;
1.1       deraadt   145:        struct passwd *pw;
                    146: {
                    147:        ENTRY *ep;
                    148:        char *p;
                    149:        struct stat sb;
                    150:        FILE *fp;
                    151:        int len;
                    152:        static char buf[LINE_MAX];
                    153:
                    154:        if (!(fp = fopen(tempname, "r")))
                    155:                pw_error(tempname, 1, 1);
                    156:        if (fstat(fileno(fp), &sb))
                    157:                pw_error(tempname, 1, 1);
                    158:        if (sb.st_size == 0) {
                    159:                warnx("corrupted temporary file");
                    160:                goto bad;
                    161:        }
                    162:        while (fgets(buf, sizeof(buf), fp)) {
                    163:                if (!buf[0] || buf[0] == '#')
                    164:                        continue;
                    165:                if (!(p = strchr(buf, '\n'))) {
                    166:                        warnx("line too long");
                    167:                        goto bad;
                    168:                }
                    169:                *p = '\0';
                    170:                for (ep = list;; ++ep) {
                    171:                        if (!ep->prompt) {
                    172:                                warnx("unrecognized field");
                    173:                                goto bad;
                    174:                        }
                    175:                        if (!strncasecmp(buf, ep->prompt, ep->len)) {
                    176:                                if (ep->restricted && uid) {
                    177:                                        warnx(
                    178:                                            "you may not change the %s field",
                    179:                                                ep->prompt);
                    180:                                        goto bad;
                    181:                                }
                    182:                                if (!(p = strchr(buf, ':'))) {
                    183:                                        warnx("line corrupted");
                    184:                                        goto bad;
                    185:                                }
                    186:                                while (isspace(*++p));
                    187:                                if (ep->except && strpbrk(p, ep->except)) {
                    188:                                        warnx(
                    189:                                   "illegal character in the \"%s\" field",
                    190:                                            ep->prompt);
                    191:                                        goto bad;
                    192:                                }
                    193:                                if ((ep->func)(p, pw, ep)) {
                    194: bad:                                   (void)fclose(fp);
                    195:                                        return (0);
                    196:                                }
                    197:                                break;
                    198:                        }
                    199:                }
                    200:        }
                    201:        (void)fclose(fp);
                    202:
                    203:        /* Build the gecos field. */
                    204:        len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
                    205:            strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
                    206:        if (!(p = malloc(len)))
                    207:                err(1, NULL);
                    208:        (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
                    209:            list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
                    210:
                    211:        if (snprintf(buf, sizeof(buf),
                    212:            "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
                    213:            pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
                    214:            pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
                    215:            pw->pw_shell) >= sizeof(buf)) {
                    216:                warnx("entries too long");
                    217:                return (0);
                    218:        }
1.5     ! deraadt   219:        free(p);
1.1       deraadt   220:        return (pw_scan(buf, pw, (int *)NULL));
                    221: }