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

1.19    ! millert     1: /*     $OpenBSD: yp_passwd.c,v 1.18 2001/07/07 00:10:49 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
1.18      millert    36: /*static const char sccsid[] = "from: @(#)yp_passwd.c  1.0 2/2/93";*/
1.19    ! millert    37: static const char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.18 2001/07/07 00:10:49 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.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.19    ! millert    67: extern void kbintr __P((int));
1.14      provos     68:
1.17      millert    69: char *ypgetnewpasswd __P((struct passwd *, login_cap_t *, char **));
1.15      millert    70: struct passwd *ypgetpwnam __P((char *));
1.1       deraadt    71:
                     72: char *domain;
                     73:
1.8       weingart   74: static int
1.1       deraadt    75: pw_error(name, err, eval)
                     76:        char *name;
                     77:        int err, eval;
                     78: {
1.15      millert    79:        if (err)
1.13      millert    80:                warn("%s", name);
1.1       deraadt    81:
1.8       weingart   82:        warnx("YP passwd database unchanged.");
1.1       deraadt    83:        exit(eval);
                     84: }
                     85:
1.8       weingart   86: int
1.1       deraadt    87: yp_passwd(username)
                     88:        char *username;
                     89: {
                     90:        char *master;
                     91:        int r, rpcport, status;
1.15      millert    92:        uid_t uid;
1.1       deraadt    93:        struct yppasswd yppasswd;
                     94:        struct passwd *pw;
                     95:        struct timeval tv;
                     96:        CLIENT *client;
1.17      millert    97:        login_cap_t *lc;
1.1       deraadt    98:
                     99:        /*
                    100:         * Get local domain
                    101:         */
1.8       weingart  102:        if ((r = yp_get_default_domain(&domain)) != 0) {
                    103:                warnx("can't get local YP domain. Reason: %s", yperr_string(r));
1.15      millert   104:                return(1);
1.1       deraadt   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) {
1.9       millert   112:                warnx("can't find the master YP server. Reason: %s",
                    113:                    yperr_string(r));
1.15      millert   114:                return(1);
1.1       deraadt   115:        }
                    116:
                    117:        /*
                    118:         * Ask the portmapper for the port of the daemon.
                    119:         */
                    120:        if ((rpcport = getrpcport(master, YPPASSWDPROG,
                    121:            YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
1.8       weingart  122:                warnx("master YP server not running yppasswd daemon.");
                    123:                warnx("Can't change password.");
1.15      millert   124:                return(1);
1.1       deraadt   125:        }
                    126:
                    127:        /*
                    128:         * Be sure the port is priviledged
                    129:         */
                    130:        if (rpcport >= IPPORT_RESERVED) {
1.8       weingart  131:                warnx("yppasswd daemon is on an invalid port.");
1.15      millert   132:                return(1);
1.1       deraadt   133:        }
                    134:
                    135:        /* Get user's login identity */
                    136:        if (!(pw = ypgetpwnam(username))) {
1.8       weingart  137:                warnx("unknown user %s.", username);
1.15      millert   138:                return(1);
1.1       deraadt   139:        }
1.17      millert   140:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                    141:                warnx("unable to get login class for user %s.", username);
                    142:                return(1);
                    143:        }
1.1       deraadt   144:
1.15      millert   145:        uid = getuid();
1.1       deraadt   146:        if (uid && uid != pw->pw_uid) {
1.8       weingart  147:                warnx("you may only change your own password: %s", strerror(EACCES));
1.15      millert   148:                return(1);
1.1       deraadt   149:        }
                    150:
                    151:        /* prompt for new password */
1.17      millert   152:        yppasswd.newpw.pw_passwd = ypgetnewpasswd(pw, lc, &yppasswd.oldpass);
1.1       deraadt   153:
                    154:        /* tell rpc.yppasswdd */
                    155:        yppasswd.newpw.pw_name  = pw->pw_name;
                    156:        yppasswd.newpw.pw_uid   = pw->pw_uid;
                    157:        yppasswd.newpw.pw_gid   = pw->pw_gid;
                    158:        yppasswd.newpw.pw_gecos = pw->pw_gecos;
                    159:        yppasswd.newpw.pw_dir   = pw->pw_dir;
                    160:        yppasswd.newpw.pw_shell = pw->pw_shell;
                    161:
                    162:        client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
                    163:        if (client==NULL) {
1.9       millert   164:                warnx("cannot contact yppasswdd on %s: Reason: %s",
1.1       deraadt   165:                    master, yperr_string(YPERR_YPBIND));
1.7       provos    166:                free(yppasswd.newpw.pw_passwd);
1.1       deraadt   167:                return(YPERR_YPBIND);
                    168:        }
                    169:        client->cl_auth = authunix_create_default();
                    170:        tv.tv_sec = 2;
                    171:        tv.tv_usec = 0;
                    172:        r = clnt_call(client, YPPASSWDPROC_UPDATE,
1.3       deraadt   173:            xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
1.1       deraadt   174:        if (r)
1.8       weingart  175:                warnx("rpc to yppasswdd failed.");
1.3       deraadt   176:        else if (status) {
1.1       deraadt   177:                printf("Couldn't change YP password.\n");
1.7       provos    178:                free(yppasswd.newpw.pw_passwd);
1.15      millert   179:                return(1);
1.3       deraadt   180:        }
                    181:        printf("The YP password has been changed on %s, the master YP passwd server.\n",
                    182:            master);
1.7       provos    183:        free(yppasswd.newpw.pw_passwd);
1.15      millert   184:        return(0);
1.1       deraadt   185: }
                    186:
1.15      millert   187: char *
1.17      millert   188: ypgetnewpasswd(pw, lc, old_pass)
1.15      millert   189:        struct passwd *pw;
1.17      millert   190:        login_cap_t *lc;
1.1       deraadt   191:        char **old_pass;
                    192: {
                    193:        static char buf[_PASSWORD_LEN+1];
1.15      millert   194:        char *p;
1.14      provos    195:        int tries, pwd_tries;
1.15      millert   196:        char salt[_PASSWORD_LEN];
1.19    ! millert   197:        sig_t saveint, savequit;
        !           198:
        !           199:        saveint = signal(SIGINT, kbintr);
        !           200:        savequit = signal(SIGQUIT, kbintr);
1.1       deraadt   201:
1.3       deraadt   202:        printf("Changing YP password for %s.\n", pw->pw_name);
1.1       deraadt   203:        if (old_pass) {
                    204:                *old_pass = NULL;
                    205:
                    206:                if (pw->pw_passwd[0]) {
1.3       deraadt   207:                        p = getpass("Old password:");
                    208:                        if (strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
                    209:                                errno = EACCES;
                    210:                                pw_error(NULL, 1, 1);
1.1       deraadt   211:                        }
1.3       deraadt   212:                } else
1.1       deraadt   213:                        p = "";
                    214:                *old_pass = strdup(p);
1.16      deraadt   215:                if (*old_pass == NULL)
                    216:                        pw_error(NULL, 1, 1);
1.1       deraadt   217:        }
1.3       deraadt   218:
1.17      millert   219:        pwd_tries = pwd_gettries(pw, lc);
1.14      provos    220:
1.1       deraadt   221:        for (buf[0] = '\0', tries = 0;;) {
                    222:                p = getpass("New password:");
                    223:                if (!*p) {
1.3       deraadt   224:                        printf("Password unchanged.\n");
1.1       deraadt   225:                        pw_error(NULL, 0, 0);
1.10      deraadt   226:                }
                    227:                if (strcmp(p, "s/key") == 0) {
                    228:                        printf("That password collides with a system feature. Choose another.\n");
                    229:                        continue;
1.1       deraadt   230:                }
1.14      provos    231:                if ((tries++ < pwd_tries || pwd_tries == 0)
1.17      millert   232:                    && pwd_check(pw, lc, p) == 0)
1.1       deraadt   233:                        continue;
1.12      deraadt   234:                strncpy(buf, p, sizeof buf-1);
                    235:                buf[sizeof buf-1] = '\0';
1.1       deraadt   236:                if (!strcmp(buf, getpass("Retype new password:")))
                    237:                        break;
                    238:                (void)printf("Mismatch; try again, EOF to quit.\n");
                    239:        }
1.17      millert   240:         if( !pwd_gensalt( salt, _PASSWORD_LEN, pw, lc, 'y' )) {
1.6       provos    241:                 (void)printf("Couldn't generate salt.\n");
                    242:                 pw_error(NULL, 0, 0);
                    243:         }
1.16      deraadt   244:        p = strdup(crypt(buf, salt));
                    245:        if (p == NULL)
                    246:                pw_error(NULL, 1, 1);
1.19    ! millert   247:        (void)signal(SIGINT, saveint);
        !           248:        (void)signal(SIGQUIT, savequit);
        !           249:
1.16      deraadt   250:        return (p);
1.1       deraadt   251: }
                    252:
                    253: static char *
1.15      millert   254: pwskip(char *p)
1.1       deraadt   255: {
                    256:        while (*p && *p != ':' && *p != '\n')
                    257:                ++p;
                    258:        if (*p)
                    259:                *p++ = 0;
                    260:        return (p);
                    261: }
                    262:
                    263: struct passwd *
                    264: interpret(struct passwd *pwent, char *line)
                    265: {
1.15      millert   266:        char    *p = line;
1.1       deraadt   267:
                    268:        pwent->pw_passwd = "*";
                    269:        pwent->pw_uid = 0;
                    270:        pwent->pw_gid = 0;
                    271:        pwent->pw_gecos = "";
                    272:        pwent->pw_dir = "";
                    273:        pwent->pw_shell = "";
                    274:        pwent->pw_change = 0;
                    275:        pwent->pw_expire = 0;
                    276:        pwent->pw_class = "";
                    277:
                    278:        /* line without colon separators is no good, so ignore it */
1.3       deraadt   279:        if(!strchr(p, ':'))
1.1       deraadt   280:                return(NULL);
                    281:
                    282:        pwent->pw_name = p;
                    283:        p = pwskip(p);
                    284:        pwent->pw_passwd = p;
                    285:        p = pwskip(p);
                    286:        pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
                    287:        p = pwskip(p);
                    288:        pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
                    289:        p = pwskip(p);
                    290:        pwent->pw_gecos = p;
                    291:        p = pwskip(p);
                    292:        pwent->pw_dir = p;
                    293:        p = pwskip(p);
                    294:        pwent->pw_shell = p;
                    295:        while (*p && *p != '\n')
                    296:                p++;
                    297:        *p = '\0';
                    298:        return (pwent);
                    299: }
                    300:
1.4       deraadt   301: static char *__yplin;
                    302:
1.15      millert   303: struct passwd *
1.1       deraadt   304: ypgetpwnam(nam)
                    305:        char *nam;
                    306: {
                    307:        static struct passwd pwent;
                    308:        char *val;
                    309:        int reason, vallen;
                    310:
                    311:        reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
1.3       deraadt   312:            &val, &vallen);
1.1       deraadt   313:        switch(reason) {
                    314:        case 0:
                    315:                break;
                    316:        default:
                    317:                return (NULL);
                    318:                break;
                    319:        }
                    320:        val[vallen] = '\0';
1.4       deraadt   321:        if (__yplin)
                    322:                free(__yplin);
                    323:        __yplin = (char *)malloc(vallen + 1);
1.16      deraadt   324:        if (__yplin == NULL)
                    325:                pw_error(NULL, 1, 1);
1.12      deraadt   326:        strncpy(__yplin, val, vallen);
                    327:        __yplin[vallen] = '\0';
1.1       deraadt   328:        free(val);
                    329:
1.4       deraadt   330:        return(interpret(&pwent, __yplin));
1.1       deraadt   331: }
                    332:
                    333: #endif /* YP */