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

Annotation of src/usr.bin/chpass/chpass.c, Revision 1.34

1.34    ! jmc         1: /*     $OpenBSD: chpass.c,v 1.33 2007/01/15 08:14:21 otto Exp $        */
1.2       deraadt     2: /*     $NetBSD: chpass.c,v 1.8 1996/05/15 21:50:43 jtc Exp $   */
1.1       deraadt     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.
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: static char copyright[] =
                     35: "@(#) Copyright (c) 1988, 1993, 1994\n\
                     36:        The Regents of the University of California.  All rights reserved.\n";
                     37: #endif /* not lint */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)chpass.c   8.4 (Berkeley) 4/2/94";
1.23      deraadt    42: #else
1.34    ! jmc        43: static char rcsid[] = "$OpenBSD: chpass.c,v 1.33 2007/01/15 08:14:21 otto Exp $";
1.1       deraadt    44: #endif
                     45: #endif /* not lint */
                     46:
                     47: #include <sys/param.h>
1.17      millert    48: #include <sys/resource.h>
1.1       deraadt    49: #include <sys/stat.h>
                     50: #include <sys/time.h>
1.17      millert    51: #include <sys/uio.h>
1.1       deraadt    52:
                     53: #include <err.h>
                     54: #include <errno.h>
                     55: #include <fcntl.h>
1.28      avsm       56: #include <paths.h>
1.1       deraadt    57: #include <pwd.h>
1.17      millert    58: #include <signal.h>
1.1       deraadt    59: #include <stdio.h>
                     60: #include <stdlib.h>
                     61: #include <string.h>
                     62: #include <unistd.h>
1.2       deraadt    63: #include <util.h>
1.1       deraadt    64:
                     65: #include "chpass.h"
                     66:
1.25      deraadt    67: extern char *__progname;
                     68:
1.17      millert    69: enum { NEWSH, LOADENTRY, EDITENTRY } op;
1.1       deraadt    70: uid_t uid;
                     71: #ifdef YP
1.25      deraadt    72: int    use_yp;
                     73: int    force_yp = 0;
1.1       deraadt    74: #endif
                     75:
1.21      millert    76: void   baduser(void);
                     77: void   kbintr(int);
                     78: void   usage(void);
1.1       deraadt    79:
                     80: int
1.23      deraadt    81: main(int argc, char *argv[])
1.1       deraadt    82: {
1.33      otto       83:        struct passwd *pw = NULL, *opw = NULL, lpw;
1.20      millert    84:        int i, ch, pfd, tfd, dfd;
1.23      deraadt    85:        char *arg = NULL;
1.17      millert    86:        sigset_t fullset;
1.1       deraadt    87:
                     88: #ifdef YP
                     89:        use_yp = _yp_check(NULL);
                     90: #endif
                     91:
                     92:        op = EDITENTRY;
1.7       millert    93:        while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
1.1       deraadt    94:                switch(ch) {
                     95:                case 'a':
                     96:                        op = LOADENTRY;
                     97:                        arg = optarg;
                     98:                        break;
                     99:                case 's':
                    100:                        op = NEWSH;
                    101:                        arg = optarg;
                    102:                        break;
                    103: #ifdef YP
                    104:                case 'l':
                    105:                        use_yp = 0;
                    106:                        break;
                    107:                case 'y':
                    108:                        if (!use_yp) {
                    109:                                warnx("YP not in use.");
                    110:                                usage();
                    111:                        }
                    112:                        force_yp = 1;
                    113:                        break;
                    114: #endif
                    115:                case '?':
                    116:                default:
                    117:                        usage();
                    118:                }
                    119:        argc -= optind;
                    120:        argv += optind;
                    121:
                    122: #ifdef YP
                    123:        if (op == LOADENTRY && use_yp)
1.23      deraadt   124:                errx(1, "cannot load using YP, use -l to load local.");
1.1       deraadt   125: #endif
                    126:        uid = getuid();
                    127:
                    128:        if (op == EDITENTRY || op == NEWSH)
                    129:                switch(argc) {
                    130:                case 0:
                    131:                        pw = getpwuid(uid);
                    132: #ifdef YP
                    133:                        if (pw && !force_yp)
                    134:                                use_yp = 0;
                    135:                        else if (use_yp)
                    136:                                pw = ypgetpwuid(uid);
                    137: #endif /* YP */
                    138:                        if (!pw)
1.22      mpech     139:                                errx(1, "unknown user: uid %u", uid);
1.1       deraadt   140:                        break;
                    141:                case 1:
                    142:                        pw = getpwnam(*argv);
                    143: #ifdef YP
                    144:                        if (pw && !force_yp)
                    145:                                use_yp = 0;
                    146:                        else if (use_yp)
                    147:                                pw = ypgetpwnam(*argv);
                    148: #endif /* YP */
                    149:                        if (!pw)
                    150:                                errx(1, "unknown user: %s", *argv);
                    151:                        if (uid && uid != pw->pw_uid)
                    152:                                baduser();
                    153:                        break;
                    154:                default:
                    155:                        usage();
                    156:                }
                    157:
                    158:        if (op == LOADENTRY) {
1.33      otto      159:                if (argc != 0)
                    160:                        errx(1, "option -a does not accept user argument");
1.1       deraadt   161:                if (uid)
                    162:                        baduser();
                    163:                pw = &lpw;
1.9       kstailey  164:                if (!pw_scan(arg, pw, NULL))
1.1       deraadt   165:                        exit(1);
1.33      otto      166:                opw = getpwnam(pw->pw_name);
1.1       deraadt   167:        }
1.33      otto      168:        if (opw == NULL && (opw = pw_dup(pw)) == NULL)
1.30      millert   169:                err(1, NULL);
1.1       deraadt   170:
1.2       deraadt   171:        /* Edit the user passwd information if requested. */
1.1       deraadt   172:        if (op == EDITENTRY) {
1.29      espie     173:                char tempname[] = _PATH_VARTMP "pw.XXXXXXXXXX";
1.26      millert   174:                int edit_status;
1.24      millert   175:
1.30      millert   176:                if ((pw = pw_dup(pw)) == NULL)
                    177:                        pw_error(NULL, 1, 1);
1.2       deraadt   178:                dfd = mkstemp(tempname);
1.12      millert   179:                if (dfd == -1 || fcntl(dfd, F_SETFD, 1) == -1)
1.2       deraadt   180:                        pw_error(tempname, 1, 1);
                    181:                display(tempname, dfd, pw);
1.26      millert   182:                edit_status = edit(tempname, pw);
1.24      millert   183:                close(dfd);
                    184:                unlink(tempname);
1.26      millert   185:
                    186:                switch (edit_status) {
                    187:                case EDIT_OK:
                    188:                        break;
                    189:                case EDIT_NOCHANGE:
                    190:                        pw_error(NULL, 0, 0);
                    191:                        break;
                    192:                case EDIT_ERROR:
                    193:                default:
                    194:                        pw_error(tempname, 1, 1);
                    195:                        break;
                    196:                }
1.31      wilfried  197:        }
                    198:
                    199:        if (op == NEWSH) {
                    200:                /* protect p_shell -- it thinks NULL is /bin/sh */
                    201:                if (!arg[0])
                    202:                        usage();
                    203:                if (p_shell(arg, pw, NULL))
                    204:                        pw_error(NULL, 0, 1);
1.1       deraadt   205:        }
1.2       deraadt   206:
1.17      millert   207:        /* Drop user's real uid and block all signals to avoid a DoS. */
                    208:        setuid(0);
                    209:        sigfillset(&fullset);
                    210:        sigdelset(&fullset, SIGINT);
                    211:        sigprocmask(SIG_BLOCK, &fullset, NULL);
                    212:
                    213:        /* Get the passwd lock file and open the passwd file for reading. */
                    214:        pw_init();
1.20      millert   215:        for (i = 1; (tfd = pw_lock(0)) == -1; i++) {
                    216:                if (i == 4)
                    217:                        (void)fputs("Attempting lock password file, "
                    218:                            "please wait or press ^C to abort", stderr);
                    219:                (void)signal(SIGINT, kbintr);
                    220:                if (i % 16 == 0)
1.17      millert   221:                        fputc('.', stderr);
1.20      millert   222:                usleep(250000);
1.17      millert   223:                (void)signal(SIGINT, SIG_IGN);
                    224:        }
1.20      millert   225:        if (i >= 4)
                    226:                fputc('\n', stderr);
1.17      millert   227:        pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
                    228:        if (pfd == -1 || fcntl(pfd, F_SETFD, 1) == -1)
                    229:                pw_error(_PATH_MASTERPASSWD, 1, 1);
                    230:
1.1       deraadt   231: #ifdef YP
                    232:        if (use_yp) {
1.17      millert   233:                if (pw_yp(pw, uid))
1.9       kstailey  234:                        pw_error(NULL, 0, 1);
1.17      millert   235:                else {
1.5       deraadt   236:                        pw_abort();
1.1       deraadt   237:                        exit(0);
1.5       deraadt   238:                }
1.4       deraadt   239:        } else
1.1       deraadt   240: #endif /* YP */
1.8       deraadt   241:        {
                    242:                /* Copy the passwd file to the lock file, updating pw. */
1.30      millert   243:                pw_copy(pfd, tfd, pw, opw);
                    244:
                    245:                /* If username changed we need to rebuild the entire db. */
                    246:                arg = !strcmp(opw->pw_name, pw->pw_name) ? pw->pw_name : NULL;
1.2       deraadt   247:
1.8       deraadt   248:                /* Now finish the passwd file update. */
1.30      millert   249:                if (pw_mkdb(arg, 0) == -1)
1.9       kstailey  250:                        pw_error(NULL, 0, 1);
1.8       deraadt   251:        }
1.1       deraadt   252:
                    253:        exit(0);
                    254: }
                    255:
                    256: void
1.23      deraadt   257: baduser(void)
1.1       deraadt   258: {
                    259:
                    260:        errx(1, "%s", strerror(EACCES));
1.14      millert   261: }
                    262:
1.32      deraadt   263: /* ARGSUSED */
1.14      millert   264: void
1.23      deraadt   265: kbintr(int signo)
1.17      millert   266: {
                    267:        struct iovec iv[5];
                    268:
                    269:        iv[0].iov_base = "\n";
                    270:        iv[0].iov_len = 1;
                    271:        iv[1].iov_base = __progname;
                    272:        iv[1].iov_len = strlen(__progname);
                    273:        iv[2].iov_base = ": ";
                    274:        iv[2].iov_len = 2;
                    275:        iv[3].iov_base = _PATH_MASTERPASSWD;
                    276:        iv[3].iov_len = sizeof(_PATH_MASTERPASSWD) - 1;
                    277:        iv[4].iov_base = " unchanged\n";
                    278:        iv[4].iov_len = 11;
                    279:        writev(STDERR_FILENO, iv, 5);
                    280:
                    281:        _exit(1);
1.1       deraadt   282: }
                    283:
                    284: void
1.23      deraadt   285: usage(void)
1.1       deraadt   286: {
                    287:
                    288: #ifdef YP
1.15      aaron     289:        (void)fprintf(stderr,
1.34    ! jmc       290:            "usage: %s [-l%s] [-s newshell] [user]\n",
1.15      aaron     291:            __progname, use_yp ? "y" : "");
1.34    ! jmc       292:        (void)fprintf(stderr,
        !           293:            "usage: %s [-l] -a list\n", __progname);
1.1       deraadt   294: #else
1.34    ! jmc       295:        (void)fprintf(stderr, "usage: %s [-s newshell] [user]\n", __progname);
        !           296:        (void)fprintf(stderr, "usage: %s -a list\n", __progname);
1.1       deraadt   297: #endif
                    298:        exit(1);
                    299: }