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

Annotation of src/usr.bin/chpass/pw_yp.c, Revision 1.18

1.18    ! millert     1: /*     $OpenBSD: pw_yp.c,v 1.17 2003/04/17 07:37:04 pvalchev Exp $     */
1.1       deraadt     2: /*     $NetBSD: pw_yp.c,v 1.5 1995/03/26 04:55:33 glass Exp $  */
                      3:
                      4: /*
                      5:  * Copyright (c) 1988 The Regents of the University of California.
                      6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.18    ! millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32: #ifndef lint
                     33: #if 0
                     34: static char sccsid[] = "@(#)pw_yp.c    1.0 2/2/93";
                     35: #else
1.18    ! millert    36: static char rcsid[] = "$OpenBSD: pw_yp.c,v 1.17 2003/04/17 07:37:04 pvalchev Exp $";
1.1       deraadt    37: #endif
                     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>
1.8       deraadt    47: #include <err.h>
1.1       deraadt    48: #include <errno.h>
1.16      deraadt    49: #include <unistd.h>
1.7       niklas     50: #include <stdlib.h>
1.1       deraadt    51: #include <rpc/rpc.h>
                     52: #include <rpcsvc/yp_prot.h>
                     53: #include <rpcsvc/ypclnt.h>
                     54: #define passwd yp_passwd_rec
                     55: #include <rpcsvc/yppasswd.h>
                     56: #undef passwd
                     57:
1.9       millert    58: extern char *__progname;
1.1       deraadt    59:
                     60: static char *domain;
                     61:
1.8       deraadt    62: int
1.16      deraadt    63: pw_yp(struct passwd *pw, uid_t uid)
1.1       deraadt    64: {
1.17      pvalchev   65:        char buf[11], *master, *p;
1.5       deraadt    66:        int r, rpcport, status, alen;
1.1       deraadt    67:        struct yppasswd yppasswd;
                     68:        struct timeval tv;
                     69:        CLIENT *client;
1.16      deraadt    70:
1.1       deraadt    71:        /*
                     72:         * Get local domain
                     73:         */
                     74:        if (!domain && (r = yp_get_default_domain(&domain))) {
                     75:                fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
1.9       millert    76:                    __progname, yperr_string(r));
1.1       deraadt    77:                return(0);
                     78:        }
                     79:
                     80:        /*
                     81:         * Find the host for the passwd map; it should be running
                     82:         * the daemon.
                     83:         */
                     84:        if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
                     85:                fprintf(stderr,
                     86:                    "%s: can't find the master YP server. Reason: %s\n",
1.9       millert    87:                    __progname, yperr_string(r));
1.1       deraadt    88:                return(0);
                     89:        }
                     90:
                     91:        /*
                     92:         * Ask the portmapper for the port of the daemon.
                     93:         */
                     94:        if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
                     95:            IPPROTO_UDP)) == 0) {
                     96:                fprintf(stderr,
                     97:                    "%s: master YP server not running yppasswd daemon.\n",
1.9       millert    98:                    __progname);
1.1       deraadt    99:                fprintf(stderr, "\tCan't change password.\n");
                    100:                return(0);
                    101:        }
                    102:
                    103:        /*
1.15      deraadt   104:         * Be sure the port is privileged
1.1       deraadt   105:         */
                    106:        if (rpcport >= IPPORT_RESERVED) {
                    107:                (void)fprintf(stderr,
                    108:                    "%s: yppasswd daemon running on an invalid port.\n",
1.9       millert   109:                    __progname);
1.1       deraadt   110:                return(0);
                    111:        }
                    112:
                    113:        /* prompt for old password */
                    114:        bzero(&yppasswd, sizeof yppasswd);
                    115:        yppasswd.oldpass = "none";
                    116:        yppasswd.oldpass = getpass("Old password:");
                    117:        if (!yppasswd.oldpass) {
                    118:                (void)fprintf(stderr, "Cancelled.\n");
                    119:                return(0);
                    120:        }
1.16      deraadt   121:
1.5       deraadt   122:        for (alen = 0, p = pw->pw_gecos; *p; p++)
                    123:                if (*p == '&')
                    124:                        alen = alen + strlen(pw->pw_name) - 1;
                    125:        if (strlen(pw->pw_name) + 1 + strlen(pw->pw_passwd) + 1 +
1.14      deraadt   126:            strlen((snprintf(buf, sizeof buf, "%u", pw->pw_uid), buf)) + 1 +
                    127:            strlen((snprintf(buf, sizeof buf, "%u", pw->pw_gid), buf)) + 1 +
1.5       deraadt   128:            strlen(pw->pw_gecos) + alen + 1 + strlen(pw->pw_dir) + 1 +
                    129:            strlen(pw->pw_shell) >= 1023) {
                    130:                warnx("entries too long");
                    131:                return (0);
                    132:        }
                    133:
1.1       deraadt   134:        /* tell rpc.yppasswdd */
                    135:        yppasswd.newpw.pw_name  = pw->pw_name;
                    136:        yppasswd.newpw.pw_passwd= pw->pw_passwd;
1.16      deraadt   137:        yppasswd.newpw.pw_uid   = pw->pw_uid;
1.1       deraadt   138:        yppasswd.newpw.pw_gid   = pw->pw_gid;
                    139:        yppasswd.newpw.pw_gecos = pw->pw_gecos;
                    140:        yppasswd.newpw.pw_dir   = pw->pw_dir;
                    141:        yppasswd.newpw.pw_shell = pw->pw_shell;
1.5       deraadt   142:
1.1       deraadt   143:        client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
                    144:        if (client==NULL) {
                    145:                fprintf(stderr, "can't contact yppasswdd on %s: Reason: %s\n",
                    146:                    master, yperr_string(YPERR_YPBIND));
1.6       deraadt   147:                return(1);
1.1       deraadt   148:        }
                    149:        client->cl_auth = authunix_create_default();
                    150:        tv.tv_sec = 5;
                    151:        tv.tv_usec = 0;
                    152:        r = clnt_call(client, YPPASSWDPROC_UPDATE,
                    153:            xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
                    154:        if (r) {
1.9       millert   155:                fprintf(stderr, "%s: rpc to yppasswdd failed. %d\n",
                    156:                    __progname, r);
1.4       deraadt   157:                clnt_destroy(client);
1.6       deraadt   158:                return(1);
1.1       deraadt   159:        } else if (status) {
                    160:                printf("Couldn't change YP password information.\n");
1.4       deraadt   161:                clnt_destroy(client);
1.6       deraadt   162:                return(1);
1.1       deraadt   163:        }
                    164:        printf("The YP password information has been changed on %s, the master YP passwd server.\n", master);
                    165:
1.4       deraadt   166:        clnt_destroy(client);
1.6       deraadt   167:        return(0);
1.1       deraadt   168: }
                    169:
                    170: static char *
1.16      deraadt   171: pwskip(char *p)
1.1       deraadt   172: {
                    173:        while (*p && *p != ':' && *p != '\n')
                    174:                ++p;
                    175:        if (*p)
                    176:                *p++ = 0;
                    177:        return (p);
                    178: }
                    179:
                    180: static struct passwd *
1.16      deraadt   181: interpret(struct passwd *pwent, char *line)
1.1       deraadt   182: {
1.13      mpech     183:        char    *p = line;
1.1       deraadt   184:
                    185:        pwent->pw_passwd = "*";
                    186:        pwent->pw_uid = 0;
                    187:        pwent->pw_gid = 0;
                    188:        pwent->pw_gecos = "";
                    189:        pwent->pw_dir = "";
                    190:        pwent->pw_shell = "";
                    191:        pwent->pw_change = 0;
                    192:        pwent->pw_expire = 0;
                    193:        pwent->pw_class = "";
1.16      deraadt   194:
1.1       deraadt   195:        /* line without colon separators is no good, so ignore it */
                    196:        if(!strchr(p,':'))
                    197:                return(NULL);
                    198:
                    199:        pwent->pw_name = p;
                    200:        p = pwskip(p);
                    201:        pwent->pw_passwd = p;
                    202:        p = pwskip(p);
                    203:        pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
                    204:        p = pwskip(p);
                    205:        pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
                    206:        p = pwskip(p);
                    207:        pwent->pw_gecos = p;
                    208:        p = pwskip(p);
                    209:        pwent->pw_dir = p;
                    210:        p = pwskip(p);
                    211:        pwent->pw_shell = p;
                    212:        while (*p && *p != '\n')
                    213:                p++;
                    214:        *p = '\0';
                    215:        return (pwent);
                    216: }
                    217:
1.3       deraadt   218: static char *__yplin;
                    219:
1.1       deraadt   220: struct passwd *
1.16      deraadt   221: ypgetpwnam(char *nam)
1.1       deraadt   222: {
                    223:        static struct passwd pwent;
1.16      deraadt   224:        int reason, vallen;
1.1       deraadt   225:        char *val;
1.16      deraadt   226:
1.1       deraadt   227:        /*
                    228:         * Get local domain
                    229:         */
                    230:        if (!domain && (reason = yp_get_default_domain(&domain))) {
                    231:                fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
1.9       millert   232:                    __progname, yperr_string(reason));
1.1       deraadt   233:                exit(1);
                    234:        }
                    235:
                    236:        reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
                    237:            &val, &vallen);
                    238:        switch(reason) {
                    239:        case 0:
                    240:                break;
                    241:        default:
                    242:                return (NULL);
                    243:                break;
                    244:        }
                    245:        val[vallen] = '\0';
1.3       deraadt   246:        if (__yplin)
                    247:                free(__yplin);
1.11      alex      248:        if (!(__yplin = (char *)malloc(vallen + 1)))
                    249:                err(1, NULL);
1.16      deraadt   250:        strlcpy(__yplin, val, vallen + 1);
1.1       deraadt   251:        free(val);
                    252:
1.3       deraadt   253:        return(interpret(&pwent, __yplin));
1.1       deraadt   254: }
                    255:
                    256: struct passwd *
1.16      deraadt   257: ypgetpwuid(uid_t uid)
1.1       deraadt   258: {
                    259:        static struct passwd pwent;
                    260:        int reason, vallen;
1.16      deraadt   261:        char namebuf[16], *val;
                    262:
1.1       deraadt   263:        if (!domain && (reason = yp_get_default_domain(&domain))) {
                    264:                fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
1.9       millert   265:                    __progname, yperr_string(reason));
1.1       deraadt   266:                exit(1);
                    267:        }
                    268:
1.16      deraadt   269:        snprintf(namebuf, sizeof namebuf, "%u", (u_int)uid);
1.1       deraadt   270:        reason = yp_match(domain, "passwd.byuid", namebuf, strlen(namebuf),
                    271:            &val, &vallen);
                    272:        switch(reason) {
                    273:        case 0:
                    274:                break;
                    275:        default:
                    276:                return (NULL);
                    277:                break;
                    278:        }
                    279:        val[vallen] = '\0';
1.3       deraadt   280:        if (__yplin)
                    281:                free(__yplin);
1.11      alex      282:        if (!(__yplin = (char *)malloc(vallen + 1)))
                    283:                err(1, NULL);
1.16      deraadt   284:        strlcpy(__yplin, val, vallen + 1);
1.1       deraadt   285:        free(val);
                    286:
1.3       deraadt   287:        return(interpret(&pwent, __yplin));
1.1       deraadt   288: }
                    289:
                    290: #endif /* YP */