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

1.14    ! provos      1: /*     $OpenBSD: yp_passwd.c,v 1.13 2000/06/30 16:00:20 millert 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.14    ! provos     37: static char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.13 2000/06/30 16:00:20 millert Exp $";
1.1       deraadt    38: #endif /* not lint */
                     39:
                     40: #ifdef YP
                     41:
                     42: #include <stdio.h>
1.8       weingart   43: #include <stdlib.h>
1.1       deraadt    44: #include <string.h>
1.8       weingart   45: #include <unistd.h>
1.1       deraadt    46: #include <netdb.h>
                     47: #include <time.h>
                     48: #include <pwd.h>
1.8       weingart   49: #include <err.h>
1.1       deraadt    50: #include <errno.h>
1.8       weingart   51: #include <ctype.h>
1.1       deraadt    52: #include <rpc/rpc.h>
                     53: #include <rpcsvc/yp_prot.h>
                     54: #include <rpcsvc/ypclnt.h>
                     55: #define passwd yp_passwd_rec
                     56: #include <rpcsvc/yppasswd.h>
                     57: #undef passwd
                     58:
                     59: #ifndef _PASSWORD_LEN
                     60: #define _PASSWORD_LEN PASS_MAX
                     61: #endif
                     62:
1.14    ! provos     63: extern int pwd_gensalt __P(( char *, int, struct passwd *, char));
        !            64: extern int pwd_check __P((struct passwd *, char *));
        !            65: extern int pwd_gettries __P((struct passwd *));
        !            66:
1.1       deraadt    67: static char *getnewpasswd();
                     68: static struct passwd *ypgetpwnam();
                     69:
                     70: static uid_t uid;
                     71: char *domain;
                     72:
1.8       weingart   73: static int
1.1       deraadt    74: pw_error(name, err, eval)
                     75:        char *name;
                     76:        int err, eval;
                     77: {
1.8       weingart   78:        if(err)
1.13      millert    79:                warn("%s", name);
1.1       deraadt    80:
1.8       weingart   81:        warnx("YP passwd database unchanged.");
1.1       deraadt    82:        exit(eval);
                     83: }
                     84:
1.8       weingart   85: int
1.1       deraadt    86: yp_passwd(username)
                     87:        char *username;
                     88: {
                     89:        char *master;
                     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:         */
1.8       weingart  101:        if ((r = yp_get_default_domain(&domain)) != 0) {
                    102:                warnx("can't get local YP domain. Reason: %s", yperr_string(r));
1.1       deraadt   103:                exit(1);
                    104:        }
                    105:
                    106:        /*
                    107:         * Find the host for the passwd map; it should be running
                    108:         * the daemon.
                    109:         */
                    110:        if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
1.9       millert   111:                warnx("can't find the master YP server. Reason: %s",
                    112:                    yperr_string(r));
1.1       deraadt   113:                exit(1);
                    114:        }
                    115:
                    116:        /*
                    117:         * Ask the portmapper for the port of the daemon.
                    118:         */
                    119:        if ((rpcport = getrpcport(master, YPPASSWDPROG,
                    120:            YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
1.8       weingart  121:                warnx("master YP server not running yppasswd daemon.");
                    122:                warnx("Can't change password.");
1.1       deraadt   123:                exit(1);
                    124:        }
                    125:
                    126:        /*
                    127:         * Be sure the port is priviledged
                    128:         */
                    129:        if (rpcport >= IPPORT_RESERVED) {
1.8       weingart  130:                warnx("yppasswd daemon is on an invalid port.");
1.1       deraadt   131:                exit(1);
                    132:        }
                    133:
                    134:        /* Get user's login identity */
                    135:        if (!(pw = ypgetpwnam(username))) {
1.8       weingart  136:                warnx("unknown user %s.", username);
1.1       deraadt   137:                exit(1);
                    138:        }
                    139:
                    140:        if (uid && uid != pw->pw_uid) {
1.8       weingart  141:                warnx("you may only change your own password: %s", strerror(EACCES));
1.1       deraadt   142:                exit(1);
                    143:        }
                    144:
                    145:        /* prompt for new password */
                    146:        yppasswd.newpw.pw_passwd = getnewpasswd(pw, &yppasswd.oldpass);
                    147:
                    148:        /* tell rpc.yppasswdd */
                    149:        yppasswd.newpw.pw_name  = pw->pw_name;
                    150:        yppasswd.newpw.pw_uid   = pw->pw_uid;
                    151:        yppasswd.newpw.pw_gid   = pw->pw_gid;
                    152:        yppasswd.newpw.pw_gecos = pw->pw_gecos;
                    153:        yppasswd.newpw.pw_dir   = pw->pw_dir;
                    154:        yppasswd.newpw.pw_shell = pw->pw_shell;
                    155:
                    156:        client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
                    157:        if (client==NULL) {
1.9       millert   158:                warnx("cannot contact yppasswdd on %s: Reason: %s",
1.1       deraadt   159:                    master, yperr_string(YPERR_YPBIND));
1.7       provos    160:                free(yppasswd.newpw.pw_passwd);
1.1       deraadt   161:                return(YPERR_YPBIND);
                    162:        }
                    163:        client->cl_auth = authunix_create_default();
                    164:        tv.tv_sec = 2;
                    165:        tv.tv_usec = 0;
                    166:        r = clnt_call(client, YPPASSWDPROC_UPDATE,
1.3       deraadt   167:            xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
1.1       deraadt   168:        if (r)
1.8       weingart  169:                warnx("rpc to yppasswdd failed.");
1.3       deraadt   170:        else if (status) {
1.1       deraadt   171:                printf("Couldn't change YP password.\n");
1.7       provos    172:                free(yppasswd.newpw.pw_passwd);
1.3       deraadt   173:                exit(1);
                    174:        }
                    175:        printf("The YP password has been changed on %s, the master YP passwd server.\n",
                    176:            master);
1.7       provos    177:        free(yppasswd.newpw.pw_passwd);
1.1       deraadt   178:        exit(0);
                    179: }
                    180:
                    181: static char *
                    182: getnewpasswd(pw, old_pass)
                    183:        register struct passwd *pw;
                    184:        char **old_pass;
                    185: {
                    186:        static char buf[_PASSWORD_LEN+1];
                    187:        register char *p, *t;
1.14    ! provos    188:        int tries, pwd_tries;
1.5       provos    189:        char salt[_PASSWORD_LEN], *crypt(), *getpass();
1.1       deraadt   190:
1.3       deraadt   191:        printf("Changing YP password for %s.\n", pw->pw_name);
1.1       deraadt   192:
                    193:        if (old_pass) {
                    194:                *old_pass = NULL;
                    195:
                    196:                if (pw->pw_passwd[0]) {
1.3       deraadt   197:                        p = getpass("Old password:");
                    198:                        if (strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
                    199:                                errno = EACCES;
                    200:                                pw_error(NULL, 1, 1);
1.1       deraadt   201:                        }
1.3       deraadt   202:                } else
1.1       deraadt   203:                        p = "";
                    204:                *old_pass = strdup(p);
                    205:        }
1.3       deraadt   206:
1.14    ! provos    207:        pwd_tries = pwd_gettries(pw);
        !           208:
1.1       deraadt   209:        for (buf[0] = '\0', tries = 0;;) {
                    210:                p = getpass("New password:");
                    211:                if (!*p) {
1.3       deraadt   212:                        printf("Password unchanged.\n");
1.1       deraadt   213:                        pw_error(NULL, 0, 0);
1.10      deraadt   214:                }
                    215:                if (strcmp(p, "s/key") == 0) {
                    216:                        printf("That password collides with a system feature. Choose another.\n");
                    217:                        continue;
1.1       deraadt   218:                }
1.14    ! provos    219:                if ((tries++ < pwd_tries || pwd_tries == 0)
        !           220:                    && pwd_check(pw, p) == 0)
1.1       deraadt   221:                        continue;
1.12      deraadt   222:                strncpy(buf, p, sizeof buf-1);
                    223:                buf[sizeof buf-1] = '\0';
1.1       deraadt   224:                if (!strcmp(buf, getpass("Retype new password:")))
                    225:                        break;
                    226:                (void)printf("Mismatch; try again, EOF to quit.\n");
                    227:        }
1.6       provos    228:         if( !pwd_gensalt( salt, _PASSWORD_LEN, pw, 'y' )) {
                    229:                 (void)printf("Couldn't generate salt.\n");
                    230:                 pw_error(NULL, 0, 0);
                    231:         }
1.1       deraadt   232:        return(strdup(crypt(buf, salt)));
                    233: }
                    234:
                    235: static char *
                    236: pwskip(register char *p)
                    237: {
                    238:        while (*p && *p != ':' && *p != '\n')
                    239:                ++p;
                    240:        if (*p)
                    241:                *p++ = 0;
                    242:        return (p);
                    243: }
                    244:
                    245: struct passwd *
                    246: interpret(struct passwd *pwent, char *line)
                    247: {
                    248:        register char   *p = line;
                    249:
                    250:        pwent->pw_passwd = "*";
                    251:        pwent->pw_uid = 0;
                    252:        pwent->pw_gid = 0;
                    253:        pwent->pw_gecos = "";
                    254:        pwent->pw_dir = "";
                    255:        pwent->pw_shell = "";
                    256:        pwent->pw_change = 0;
                    257:        pwent->pw_expire = 0;
                    258:        pwent->pw_class = "";
                    259:
                    260:        /* line without colon separators is no good, so ignore it */
1.3       deraadt   261:        if(!strchr(p, ':'))
1.1       deraadt   262:                return(NULL);
                    263:
                    264:        pwent->pw_name = p;
                    265:        p = pwskip(p);
                    266:        pwent->pw_passwd = p;
                    267:        p = pwskip(p);
                    268:        pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
                    269:        p = pwskip(p);
                    270:        pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
                    271:        p = pwskip(p);
                    272:        pwent->pw_gecos = p;
                    273:        p = pwskip(p);
                    274:        pwent->pw_dir = p;
                    275:        p = pwskip(p);
                    276:        pwent->pw_shell = p;
                    277:        while (*p && *p != '\n')
                    278:                p++;
                    279:        *p = '\0';
                    280:        return (pwent);
                    281: }
                    282:
1.4       deraadt   283: static char *__yplin;
                    284:
1.1       deraadt   285: static struct passwd *
                    286: ypgetpwnam(nam)
                    287:        char *nam;
                    288: {
                    289:        static struct passwd pwent;
                    290:        char *val;
                    291:        int reason, vallen;
                    292:
                    293:        reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
1.3       deraadt   294:            &val, &vallen);
1.1       deraadt   295:        switch(reason) {
                    296:        case 0:
                    297:                break;
                    298:        default:
                    299:                return (NULL);
                    300:                break;
                    301:        }
                    302:        val[vallen] = '\0';
1.4       deraadt   303:        if (__yplin)
                    304:                free(__yplin);
                    305:        __yplin = (char *)malloc(vallen + 1);
1.12      deraadt   306:        strncpy(__yplin, val, vallen);
                    307:        __yplin[vallen] = '\0';
1.1       deraadt   308:        free(val);
                    309:
1.4       deraadt   310:        return(interpret(&pwent, __yplin));
1.1       deraadt   311: }
                    312:
                    313: #endif /* YP */