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

1.17    ! millert     1: /*     $OpenBSD: yp_passwd.c,v 1.16 2001/01/10 20:11:30 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.17    ! millert    37: static char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.16 2001/01/10 20:11:30 deraadt 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.17    ! millert    52: #include <login_cap.h>
1.1       deraadt    53: #include <rpc/rpc.h>
                     54: #include <rpcsvc/yp_prot.h>
                     55: #include <rpcsvc/ypclnt.h>
                     56: #define passwd yp_passwd_rec
                     57: #include <rpcsvc/yppasswd.h>
                     58: #undef passwd
                     59:
                     60: #ifndef _PASSWORD_LEN
                     61: #define _PASSWORD_LEN PASS_MAX
                     62: #endif
                     63:
1.17    ! millert    64: extern int pwd_gensalt __P((char *, int, struct passwd *, login_cap_t *, char));
        !            65: extern int pwd_check __P((struct passwd *, login_cap_t *, char *));
        !            66: extern int pwd_gettries __P((struct passwd *, login_cap_t *));
1.14      provos     67:
1.17    ! millert    68: char *ypgetnewpasswd __P((struct passwd *, login_cap_t *, char **));
1.15      millert    69: struct passwd *ypgetpwnam __P((char *));
1.1       deraadt    70:
                     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.15      millert    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;
1.15      millert    91:        uid_t uid;
1.1       deraadt    92:        struct yppasswd yppasswd;
                     93:        struct passwd *pw;
                     94:        struct timeval tv;
                     95:        CLIENT *client;
1.17    ! millert    96:        login_cap_t *lc;
1.1       deraadt    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.15      millert   103:                return(1);
1.1       deraadt   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.15      millert   113:                return(1);
1.1       deraadt   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.15      millert   123:                return(1);
1.1       deraadt   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.15      millert   131:                return(1);
1.1       deraadt   132:        }
                    133:
                    134:        /* Get user's login identity */
                    135:        if (!(pw = ypgetpwnam(username))) {
1.8       weingart  136:                warnx("unknown user %s.", username);
1.15      millert   137:                return(1);
1.1       deraadt   138:        }
1.17    ! millert   139:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
        !           140:                warnx("unable to get login class for user %s.", username);
        !           141:                return(1);
        !           142:        }
1.1       deraadt   143:
1.15      millert   144:        uid = getuid();
1.1       deraadt   145:        if (uid && uid != pw->pw_uid) {
1.8       weingart  146:                warnx("you may only change your own password: %s", strerror(EACCES));
1.15      millert   147:                return(1);
1.1       deraadt   148:        }
                    149:
                    150:        /* prompt for new password */
1.17    ! millert   151:        yppasswd.newpw.pw_passwd = ypgetnewpasswd(pw, lc, &yppasswd.oldpass);
1.1       deraadt   152:
                    153:        /* tell rpc.yppasswdd */
                    154:        yppasswd.newpw.pw_name  = pw->pw_name;
                    155:        yppasswd.newpw.pw_uid   = pw->pw_uid;
                    156:        yppasswd.newpw.pw_gid   = pw->pw_gid;
                    157:        yppasswd.newpw.pw_gecos = pw->pw_gecos;
                    158:        yppasswd.newpw.pw_dir   = pw->pw_dir;
                    159:        yppasswd.newpw.pw_shell = pw->pw_shell;
                    160:
                    161:        client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
                    162:        if (client==NULL) {
1.9       millert   163:                warnx("cannot contact yppasswdd on %s: Reason: %s",
1.1       deraadt   164:                    master, yperr_string(YPERR_YPBIND));
1.7       provos    165:                free(yppasswd.newpw.pw_passwd);
1.1       deraadt   166:                return(YPERR_YPBIND);
                    167:        }
                    168:        client->cl_auth = authunix_create_default();
                    169:        tv.tv_sec = 2;
                    170:        tv.tv_usec = 0;
                    171:        r = clnt_call(client, YPPASSWDPROC_UPDATE,
1.3       deraadt   172:            xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
1.1       deraadt   173:        if (r)
1.8       weingart  174:                warnx("rpc to yppasswdd failed.");
1.3       deraadt   175:        else if (status) {
1.1       deraadt   176:                printf("Couldn't change YP password.\n");
1.7       provos    177:                free(yppasswd.newpw.pw_passwd);
1.15      millert   178:                return(1);
1.3       deraadt   179:        }
                    180:        printf("The YP password has been changed on %s, the master YP passwd server.\n",
                    181:            master);
1.7       provos    182:        free(yppasswd.newpw.pw_passwd);
1.15      millert   183:        return(0);
1.1       deraadt   184: }
                    185:
1.15      millert   186: char *
1.17    ! millert   187: ypgetnewpasswd(pw, lc, old_pass)
1.15      millert   188:        struct passwd *pw;
1.17    ! millert   189:        login_cap_t *lc;
1.1       deraadt   190:        char **old_pass;
                    191: {
                    192:        static char buf[_PASSWORD_LEN+1];
1.15      millert   193:        char *p;
1.14      provos    194:        int tries, pwd_tries;
1.15      millert   195:        char salt[_PASSWORD_LEN];
1.1       deraadt   196:
1.3       deraadt   197:        printf("Changing YP password for %s.\n", pw->pw_name);
1.1       deraadt   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);
1.16      deraadt   210:                if (*old_pass == NULL)
                    211:                        pw_error(NULL, 1, 1);
1.1       deraadt   212:        }
1.3       deraadt   213:
1.17    ! millert   214:        pwd_tries = pwd_gettries(pw, lc);
1.14      provos    215:
1.1       deraadt   216:        for (buf[0] = '\0', tries = 0;;) {
                    217:                p = getpass("New password:");
                    218:                if (!*p) {
1.3       deraadt   219:                        printf("Password unchanged.\n");
1.1       deraadt   220:                        pw_error(NULL, 0, 0);
1.10      deraadt   221:                }
                    222:                if (strcmp(p, "s/key") == 0) {
                    223:                        printf("That password collides with a system feature. Choose another.\n");
                    224:                        continue;
1.1       deraadt   225:                }
1.14      provos    226:                if ((tries++ < pwd_tries || pwd_tries == 0)
1.17    ! millert   227:                    && pwd_check(pw, lc, p) == 0)
1.1       deraadt   228:                        continue;
1.12      deraadt   229:                strncpy(buf, p, sizeof buf-1);
                    230:                buf[sizeof buf-1] = '\0';
1.1       deraadt   231:                if (!strcmp(buf, getpass("Retype new password:")))
                    232:                        break;
                    233:                (void)printf("Mismatch; try again, EOF to quit.\n");
                    234:        }
1.17    ! millert   235:         if( !pwd_gensalt( salt, _PASSWORD_LEN, pw, lc, 'y' )) {
1.6       provos    236:                 (void)printf("Couldn't generate salt.\n");
                    237:                 pw_error(NULL, 0, 0);
                    238:         }
1.16      deraadt   239:        p = strdup(crypt(buf, salt));
                    240:        if (p == NULL)
                    241:                pw_error(NULL, 1, 1);
                    242:        return (p);
1.1       deraadt   243: }
                    244:
                    245: static char *
1.15      millert   246: pwskip(char *p)
1.1       deraadt   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: {
1.15      millert   258:        char    *p = line;
1.1       deraadt   259:
                    260:        pwent->pw_passwd = "*";
                    261:        pwent->pw_uid = 0;
                    262:        pwent->pw_gid = 0;
                    263:        pwent->pw_gecos = "";
                    264:        pwent->pw_dir = "";
                    265:        pwent->pw_shell = "";
                    266:        pwent->pw_change = 0;
                    267:        pwent->pw_expire = 0;
                    268:        pwent->pw_class = "";
                    269:
                    270:        /* line without colon separators is no good, so ignore it */
1.3       deraadt   271:        if(!strchr(p, ':'))
1.1       deraadt   272:                return(NULL);
                    273:
                    274:        pwent->pw_name = p;
                    275:        p = pwskip(p);
                    276:        pwent->pw_passwd = p;
                    277:        p = pwskip(p);
                    278:        pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
                    279:        p = pwskip(p);
                    280:        pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
                    281:        p = pwskip(p);
                    282:        pwent->pw_gecos = p;
                    283:        p = pwskip(p);
                    284:        pwent->pw_dir = p;
                    285:        p = pwskip(p);
                    286:        pwent->pw_shell = p;
                    287:        while (*p && *p != '\n')
                    288:                p++;
                    289:        *p = '\0';
                    290:        return (pwent);
                    291: }
                    292:
1.4       deraadt   293: static char *__yplin;
                    294:
1.15      millert   295: struct passwd *
1.1       deraadt   296: ypgetpwnam(nam)
                    297:        char *nam;
                    298: {
                    299:        static struct passwd pwent;
                    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';
1.4       deraadt   313:        if (__yplin)
                    314:                free(__yplin);
                    315:        __yplin = (char *)malloc(vallen + 1);
1.16      deraadt   316:        if (__yplin == NULL)
                    317:                pw_error(NULL, 1, 1);
1.12      deraadt   318:        strncpy(__yplin, val, vallen);
                    319:        __yplin[vallen] = '\0';
1.1       deraadt   320:        free(val);
                    321:
1.4       deraadt   322:        return(interpret(&pwent, __yplin));
1.1       deraadt   323: }
                    324:
                    325: #endif /* YP */