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