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

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