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

Annotation of src/usr.bin/passwd/yp_passwd.c, Revision 1.3

1.3     ! deraadt     1: /*     $OpenBSD: yp_passwd.c,v 1.2 1996/06/26 05:37:48 deraadt Exp $   */
1.2       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1988 The Regents of the University of California.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35: #ifndef lint
                     36: /*static char sccsid[] = "from: @(#)yp_passwd.c        1.0 2/2/93";*/
1.3     ! deraadt    37: static char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.2 1996/06/26 05:37:48 deraadt Exp $";
1.1       deraadt    38: #endif /* not lint */
                     39:
                     40: #ifdef YP
                     41:
                     42: #include <stdio.h>
                     43: #include <string.h>
                     44: #include <netdb.h>
                     45: #include <time.h>
                     46: #include <pwd.h>
                     47: #include <errno.h>
                     48: #include <rpc/rpc.h>
                     49: #include <rpcsvc/yp_prot.h>
                     50: #include <rpcsvc/ypclnt.h>
                     51: #define passwd yp_passwd_rec
                     52: #include <rpcsvc/yppasswd.h>
                     53: #undef passwd
                     54:
                     55: #ifndef _PASSWORD_LEN
                     56: #define _PASSWORD_LEN PASS_MAX
                     57: #endif
                     58:
                     59: extern char *progname;
                     60: static char *getnewpasswd();
                     61: static struct passwd *ypgetpwnam();
                     62:
                     63: static uid_t uid;
                     64: char *domain;
                     65:
                     66: static
                     67: pw_error(name, err, eval)
                     68:        char *name;
                     69:        int err, eval;
                     70: {
                     71:        int sverrno;
                     72:
                     73:        if (err) {
                     74:                sverrno = errno;
                     75:                (void)fprintf(stderr, "%s: ", progname);
                     76:                if (name)
                     77:                        (void)fprintf(stderr, "%s: ", name);
                     78:                (void)fprintf(stderr, "%s\n", strerror(sverrno));
                     79:        }
                     80:        (void)fprintf(stderr,
                     81:            "%s: YP passwd database unchanged\n", progname);
                     82:        exit(eval);
                     83: }
                     84:
                     85: yp_passwd(username)
                     86:        char *username;
                     87: {
                     88:        char *master;
                     89:        char *pp;
                     90:        int r, rpcport, status;
                     91:        struct yppasswd yppasswd;
                     92:        struct passwd *pw;
                     93:        struct timeval tv;
                     94:        CLIENT *client;
                     95:
                     96:        uid = getuid();
                     97:
                     98:        /*
                     99:         * Get local domain
                    100:         */
                    101:        if (r = yp_get_default_domain(&domain)) {
                    102:                fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
                    103:                    progname, yperr_string(r));
                    104:                exit(1);
                    105:        }
                    106:
                    107:        /*
                    108:         * Find the host for the passwd map; it should be running
                    109:         * the daemon.
                    110:         */
                    111:        if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
                    112:                fprintf(stderr,
                    113:                    "%s: can't find the master YP server. Reason: %s\n",
                    114:                    progname, yperr_string(r));
                    115:                exit(1);
                    116:        }
                    117:
                    118:        /*
                    119:         * Ask the portmapper for the port of the daemon.
                    120:         */
                    121:        if ((rpcport = getrpcport(master, YPPASSWDPROG,
                    122:            YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
                    123:                fprintf(stderr,
                    124:                    "%s: master YP server not running yppasswd daemon.\n",
                    125:                    progname);
                    126:                fprintf(stderr, "\tCan't change password.\n");
                    127:                exit(1);
                    128:        }
                    129:
                    130:        /*
                    131:         * Be sure the port is priviledged
                    132:         */
                    133:        if (rpcport >= IPPORT_RESERVED) {
                    134:                fprintf(stderr, "%s: yppasswd daemon is on an invalid port.\n",
                    135:                    progname);
                    136:                exit(1);
                    137:        }
                    138:
                    139:        /* Get user's login identity */
                    140:        if (!(pw = ypgetpwnam(username))) {
                    141:                (void)fprintf(stderr, "%s: unknown user %s.\n",
                    142:                    progname, username);
                    143:                exit(1);
                    144:        }
                    145:
                    146:        if (uid && uid != pw->pw_uid) {
                    147:                fprintf(stderr,
                    148:                    "%s: you may only change your own password: %s\n",
                    149:                    progname, strerror(EACCES));
                    150:                exit(1);
                    151:        }
                    152:
                    153:        /* prompt for new password */
                    154:        yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
                    155:
                    156:        /* tell rpc.yppasswdd */
                    157:        yppasswd.newpw.pw_name  = pw->pw_name;
                    158:        yppasswd.newpw.pw_uid   = pw->pw_uid;
                    159:        yppasswd.newpw.pw_gid   = pw->pw_gid;
                    160:        yppasswd.newpw.pw_gecos = pw->pw_gecos;
                    161:        yppasswd.newpw.pw_dir   = pw->pw_dir;
                    162:        yppasswd.newpw.pw_shell = pw->pw_shell;
                    163:
                    164:        client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
                    165:        if (client==NULL) {
                    166:                fprintf(stderr, "cannot contact yppasswdd on %s: Reason: %s\n",
                    167:                    master, yperr_string(YPERR_YPBIND));
                    168:                return(YPERR_YPBIND);
                    169:        }
                    170:        client->cl_auth = authunix_create_default();
                    171:        tv.tv_sec = 2;
                    172:        tv.tv_usec = 0;
                    173:        r = clnt_call(client, YPPASSWDPROC_UPDATE,
1.3     ! deraadt   174:            xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
1.1       deraadt   175:        if (r)
                    176:                fprintf(stderr, "%s: rpc to yppasswdd failed.\n", progname);
1.3     ! deraadt   177:        else if (status) {
1.1       deraadt   178:                printf("Couldn't change YP password.\n");
1.3     ! deraadt   179:                exit(1);
        !           180:        }
        !           181:        printf("The YP password has been changed on %s, the master YP passwd server.\n",
        !           182:            master);
1.1       deraadt   183:        exit(0);
                    184: }
                    185:
                    186: static char *
                    187: getnewpasswd(pw, old_pass)
                    188:        register struct passwd *pw;
                    189:        char **old_pass;
                    190: {
                    191:        static char buf[_PASSWORD_LEN+1];
                    192:        register char *p, *t;
                    193:        int tries;
                    194:        char salt[9], *crypt(), *getpass();
                    195:
1.3     ! deraadt   196:        printf("Changing YP password for %s.\n", pw->pw_name);
1.1       deraadt   197:
                    198:        if (old_pass) {
                    199:                *old_pass = NULL;
                    200:
                    201:                if (pw->pw_passwd[0]) {
1.3     ! deraadt   202:                        p = getpass("Old password:");
        !           203:                        if (strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
        !           204:                                errno = EACCES;
        !           205:                                pw_error(NULL, 1, 1);
1.1       deraadt   206:                        }
1.3     ! deraadt   207:                } else
1.1       deraadt   208:                        p = "";
                    209:                *old_pass = strdup(p);
                    210:        }
1.3     ! deraadt   211:
1.1       deraadt   212:        for (buf[0] = '\0', tries = 0;;) {
                    213:                p = getpass("New password:");
                    214:                if (!*p) {
1.3     ! deraadt   215:                        printf("Password unchanged.\n");
1.1       deraadt   216:                        pw_error(NULL, 0, 0);
                    217:                }
                    218:                if (strlen(p) <= 5 && ++tries < 2) {
1.3     ! deraadt   219:                        printf("Please enter a longer password.\n");
1.1       deraadt   220:                        continue;
                    221:                }
1.3     ! deraadt   222:                for (t = p; *t && islower(*t); ++t)
        !           223:                        ;
1.1       deraadt   224:                if (!*t && ++tries < 2) {
1.3     ! deraadt   225:                        printf("Please don't use an all-lower case password.\nUnusual capitalization, control characters or digits are suggested.\n");
1.1       deraadt   226:                        continue;
                    227:                }
1.3     ! deraadt   228:                strcpy(buf, p);
1.1       deraadt   229:                if (!strcmp(buf, getpass("Retype new password:")))
                    230:                        break;
                    231:                (void)printf("Mismatch; try again, EOF to quit.\n");
                    232:        }
                    233:        /* grab a random printable character that isn't a colon */
                    234:        (void)srandom((int)time((time_t *)NULL));
                    235: #ifdef NEWSALT
                    236:        salt[0] = _PASSWORD_EFMT1;
                    237:        to64(&salt[1], (long)(29 * 25), 4);
                    238:        to64(&salt[5], random(), 4);
                    239: #else
                    240:        to64(&salt[0], random(), 2);
                    241: #endif
                    242:        return(strdup(crypt(buf, salt)));
                    243: }
                    244:
                    245: static char *
                    246: pwskip(register char *p)
                    247: {
                    248:        while (*p && *p != ':' && *p != '\n')
                    249:                ++p;
                    250:        if (*p)
                    251:                *p++ = 0;
                    252:        return (p);
                    253: }
                    254:
                    255: struct passwd *
                    256: interpret(struct passwd *pwent, char *line)
                    257: {
                    258:        register char   *p = line;
                    259:        register int    c;
                    260:
                    261:        pwent->pw_passwd = "*";
                    262:        pwent->pw_uid = 0;
                    263:        pwent->pw_gid = 0;
                    264:        pwent->pw_gecos = "";
                    265:        pwent->pw_dir = "";
                    266:        pwent->pw_shell = "";
                    267:        pwent->pw_change = 0;
                    268:        pwent->pw_expire = 0;
                    269:        pwent->pw_class = "";
                    270:
                    271:        /* line without colon separators is no good, so ignore it */
1.3     ! deraadt   272:        if(!strchr(p, ':'))
1.1       deraadt   273:                return(NULL);
                    274:
                    275:        pwent->pw_name = p;
                    276:        p = pwskip(p);
                    277:        pwent->pw_passwd = p;
                    278:        p = pwskip(p);
                    279:        pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
                    280:        p = pwskip(p);
                    281:        pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
                    282:        p = pwskip(p);
                    283:        pwent->pw_gecos = p;
                    284:        p = pwskip(p);
                    285:        pwent->pw_dir = p;
                    286:        p = pwskip(p);
                    287:        pwent->pw_shell = p;
                    288:        while (*p && *p != '\n')
                    289:                p++;
                    290:        *p = '\0';
                    291:        return (pwent);
                    292: }
                    293:
                    294: static struct passwd *
                    295: ypgetpwnam(nam)
                    296:        char *nam;
                    297: {
                    298:        static struct passwd pwent;
                    299:        static char line[1024];
                    300:        char *val;
                    301:        int reason, vallen;
                    302:
                    303:        reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
1.3     ! deraadt   304:            &val, &vallen);
1.1       deraadt   305:        switch(reason) {
                    306:        case 0:
                    307:                break;
                    308:        default:
                    309:                return (NULL);
                    310:                break;
                    311:        }
                    312:        val[vallen] = '\0';
                    313:        strcpy(line, val);
                    314:        free(val);
                    315:
                    316:        return(interpret(&pwent, line));
                    317: }
                    318:
                    319: #endif /* YP */