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

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