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

1.12    ! millert     1: /*     $OpenBSD: chpass.c,v 1.11 1998/05/29 16:37:51 millert 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.
                     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: static char copyright[] =
                     39: "@(#) Copyright (c) 1988, 1993, 1994\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)chpass.c   8.4 (Berkeley) 4/2/94";
                     46: #else
1.12    ! millert    47: static char rcsid[] = "$OpenBSD: chpass.c,v 1.11 1998/05/29 16:37:51 millert Exp $";
1.1       deraadt    48: #endif
                     49: #endif /* not lint */
                     50:
                     51: #include <sys/param.h>
                     52: #include <sys/stat.h>
                     53: #include <sys/time.h>
                     54: #include <sys/resource.h>
                     55:
                     56: #include <ctype.h>
                     57: #include <err.h>
                     58: #include <errno.h>
                     59: #include <fcntl.h>
                     60: #include <pwd.h>
                     61: #include <stdio.h>
                     62: #include <stdlib.h>
                     63: #include <string.h>
                     64: #include <unistd.h>
1.2       deraadt    65: #include <util.h>
1.1       deraadt    66:
                     67: #include "chpass.h"
                     68: #include "pathnames.h"
                     69:
                     70: char *progname = "chpass";
                     71: char *tempname;
                     72: uid_t uid;
                     73:
                     74: #ifdef YP
                     75: int use_yp;
                     76: int force_yp = 0;
                     77: extern struct passwd *ypgetpwnam(), *ypgetpwuid();
1.10      deraadt    78: int _yp_check __P((char **));
                     79: int pw_yp __P((struct passwd *, uid_t));
1.1       deraadt    80: #endif
                     81:
                     82: void   baduser __P((void));
                     83: void   usage __P((void));
                     84:
                     85: int
                     86: main(argc, argv)
                     87:        int argc;
                     88:        char **argv;
                     89: {
                     90:        enum { NEWSH, LOADENTRY, EDITENTRY } op;
                     91:        struct passwd *pw, lpw;
1.2       deraadt    92:        int ch, pfd, tfd, dfd;
1.11      millert    93:        char *arg, tempname[] = __CONCAT(_PATH_VARTMP,"pw.XXXXXXXX");
1.1       deraadt    94:
                     95: #ifdef YP
                     96:        use_yp = _yp_check(NULL);
                     97: #endif
                     98:
                     99:        op = EDITENTRY;
1.7       millert   100:        while ((ch = getopt(argc, argv, "a:s:ly")) != -1)
1.1       deraadt   101:                switch(ch) {
                    102:                case 'a':
                    103:                        op = LOADENTRY;
                    104:                        arg = optarg;
                    105:                        break;
                    106:                case 's':
                    107:                        op = NEWSH;
                    108:                        arg = optarg;
                    109:                        break;
                    110: #ifdef YP
                    111:                case 'l':
                    112:                        use_yp = 0;
                    113:                        break;
                    114:                case 'y':
                    115:                        if (!use_yp) {
                    116:                                warnx("YP not in use.");
                    117:                                usage();
                    118:                        }
                    119:                        force_yp = 1;
                    120:                        break;
                    121: #endif
                    122:                case '?':
                    123:                default:
                    124:                        usage();
                    125:                }
                    126:        argc -= optind;
                    127:        argv += optind;
                    128:
                    129: #ifdef YP
                    130:        if (op == LOADENTRY && use_yp)
                    131:                errx(1, "cannot load entry using NIS.\n\tUse the -l flag to load local.");
                    132: #endif
                    133:        uid = getuid();
                    134:
                    135:        if (op == EDITENTRY || op == NEWSH)
                    136:                switch(argc) {
                    137:                case 0:
                    138:                        pw = getpwuid(uid);
                    139: #ifdef YP
                    140:                        if (pw && !force_yp)
                    141:                                use_yp = 0;
                    142:                        else if (use_yp)
                    143:                                pw = ypgetpwuid(uid);
                    144: #endif /* YP */
                    145:                        if (!pw)
                    146:                                errx(1, "unknown user: uid %u\n", uid);
                    147:                        break;
                    148:                case 1:
                    149:                        pw = getpwnam(*argv);
                    150: #ifdef YP
                    151:                        if (pw && !force_yp)
                    152:                                use_yp = 0;
                    153:                        else if (use_yp)
                    154:                                pw = ypgetpwnam(*argv);
                    155: #endif /* YP */
                    156:                        if (!pw)
                    157:                                errx(1, "unknown user: %s", *argv);
                    158:                        if (uid && uid != pw->pw_uid)
                    159:                                baduser();
                    160:                        break;
                    161:                default:
                    162:                        usage();
                    163:                }
                    164:
                    165:        if (op == NEWSH) {
                    166:                /* protect p_shell -- it thinks NULL is /bin/sh */
                    167:                if (!arg[0])
                    168:                        usage();
1.9       kstailey  169:                if (p_shell(arg, pw, NULL))
                    170:                        pw_error(NULL, 0, 1);
1.1       deraadt   171:        }
                    172:
                    173:        if (op == LOADENTRY) {
                    174:                if (uid)
                    175:                        baduser();
                    176:                pw = &lpw;
1.9       kstailey  177:                if (!pw_scan(arg, pw, NULL))
1.1       deraadt   178:                        exit(1);
                    179:        }
                    180:
1.2       deraadt   181:        /* Get the passwd lock file and open the passwd file for reading. */
1.1       deraadt   182:        pw_init();
1.2       deraadt   183:        tfd = pw_lock(0);
1.12    ! millert   184:        if (tfd == -1 || fcntl(tfd, F_SETFD, 1) == -1) {
1.6       millert   185:                if (errno == EEXIST)
                    186:                        errx(1, "the passwd file is busy.");
                    187:                else
                    188:                        err(1, "can't open passwd temp file");
                    189:        }
1.2       deraadt   190:        pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
1.12    ! millert   191:        if (pfd == -1 || fcntl(pfd, F_SETFD, 1) == -1)
1.2       deraadt   192:                pw_error(_PATH_MASTERPASSWD, 1, 1);
1.1       deraadt   193:
1.2       deraadt   194:        /* Edit the user passwd information if requested. */
1.1       deraadt   195:        if (op == EDITENTRY) {
1.2       deraadt   196:                dfd = mkstemp(tempname);
1.12    ! millert   197:                if (dfd == -1 || fcntl(dfd, F_SETFD, 1) == -1)
1.2       deraadt   198:                        pw_error(tempname, 1, 1);
                    199:                display(tempname, dfd, pw);
                    200:                edit(tempname, pw);
1.1       deraadt   201:                (void)unlink(tempname);
                    202:        }
1.2       deraadt   203:
1.1       deraadt   204: #ifdef YP
                    205:        if (use_yp) {
1.4       deraadt   206:                if (pw_yp(pw, uid)) {
1.9       kstailey  207:                        pw_error(NULL, 0, 1);
1.4       deraadt   208:                        exit(1);
1.5       deraadt   209:                } else {
                    210:                        pw_abort();
1.1       deraadt   211:                        exit(0);
1.5       deraadt   212:                }
1.4       deraadt   213:        } else
1.1       deraadt   214: #endif /* YP */
1.8       deraadt   215:        {
                    216:                /* Copy the passwd file to the lock file, updating pw. */
                    217:                pw_copy(pfd, tfd, pw);
1.2       deraadt   218:
1.8       deraadt   219:                /* Now finish the passwd file update. */
1.12    ! millert   220:                if (pw_mkdb() == -1)
1.9       kstailey  221:                        pw_error(NULL, 0, 1);
1.8       deraadt   222:        }
1.1       deraadt   223:
                    224:        exit(0);
                    225: }
                    226:
                    227: void
                    228: baduser()
                    229: {
                    230:
                    231:        errx(1, "%s", strerror(EACCES));
                    232: }
                    233:
                    234: void
                    235: usage()
                    236: {
                    237:
                    238: #ifdef YP
                    239:        (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [-l]%s [user]\n", use_yp?" [-y]":"");
                    240: #else
                    241:        (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n");
                    242: #endif
                    243:        exit(1);
                    244: }