Annotation of src/usr.bin/chpass/pw_yp.c, Revision 1.2
1.2 ! deraadt 1: /* $OpenBSD: pw_yp.c,v 1.5 1995/03/26 04:55:33 glass 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.2 ! deraadt 40: static char rcsid[] = "$OpenBSD: pw_yp.c,v 1.5 1995/03/26 04:55:33 glass 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;
68: char *pp;
69: int r, rpcport, status;
70: struct yppasswd yppasswd;
71: struct timeval tv;
72: CLIENT *client;
73: extern char *getpass();
74:
75: /*
76: * Get local domain
77: */
78: if (!domain && (r = yp_get_default_domain(&domain))) {
79: fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
80: progname, yperr_string(r));
81: return(0);
82: }
83:
84: /*
85: * Find the host for the passwd map; it should be running
86: * the daemon.
87: */
88: if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
89: fprintf(stderr,
90: "%s: can't find the master YP server. Reason: %s\n",
91: progname, yperr_string(r));
92: return(0);
93: }
94:
95: /*
96: * Ask the portmapper for the port of the daemon.
97: */
98: if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
99: IPPROTO_UDP)) == 0) {
100: fprintf(stderr,
101: "%s: master YP server not running yppasswd daemon.\n",
102: progname);
103: fprintf(stderr, "\tCan't change password.\n");
104: return(0);
105: }
106:
107: /*
108: * Be sure the port is priviledged
109: */
110: if (rpcport >= IPPORT_RESERVED) {
111: (void)fprintf(stderr,
112: "%s: yppasswd daemon running on an invalid port.\n",
113: progname);
114: return(0);
115: }
116:
117: /* prompt for old password */
118: bzero(&yppasswd, sizeof yppasswd);
119: yppasswd.oldpass = "none";
120: yppasswd.oldpass = getpass("Old password:");
121: if (!yppasswd.oldpass) {
122: (void)fprintf(stderr, "Cancelled.\n");
123: return(0);
124: }
125:
126: /* tell rpc.yppasswdd */
127: yppasswd.newpw.pw_name = pw->pw_name;
128: yppasswd.newpw.pw_passwd= pw->pw_passwd;
129: yppasswd.newpw.pw_uid = pw->pw_uid;
130: yppasswd.newpw.pw_gid = pw->pw_gid;
131: yppasswd.newpw.pw_gecos = pw->pw_gecos;
132: yppasswd.newpw.pw_dir = pw->pw_dir;
133: yppasswd.newpw.pw_shell = pw->pw_shell;
134:
135: client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
136: if (client==NULL) {
137: fprintf(stderr, "can't contact yppasswdd on %s: Reason: %s\n",
138: master, yperr_string(YPERR_YPBIND));
139: return(0);
140: }
141: client->cl_auth = authunix_create_default();
142: tv.tv_sec = 5;
143: tv.tv_usec = 0;
144: r = clnt_call(client, YPPASSWDPROC_UPDATE,
145: xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
146: if (r) {
147: fprintf(stderr, "%s: rpc to yppasswdd failed. %d\n", progname, r);
148: return(0);
149: } else if (status) {
150: printf("Couldn't change YP password information.\n");
151: return(0);
152: }
153: printf("The YP password information has been changed on %s, the master YP passwd server.\n", master);
154:
155: return(1);
156: }
157:
158: static char *
159: pwskip(p)
160: register char *p;
161: {
162: while (*p && *p != ':' && *p != '\n')
163: ++p;
164: if (*p)
165: *p++ = 0;
166: return (p);
167: }
168:
169: static struct passwd *
170: interpret(pwent, line)
171: struct passwd *pwent;
172: char *line;
173: {
174: register char *p = line;
175: register int c;
176:
177: pwent->pw_passwd = "*";
178: pwent->pw_uid = 0;
179: pwent->pw_gid = 0;
180: pwent->pw_gecos = "";
181: pwent->pw_dir = "";
182: pwent->pw_shell = "";
183: pwent->pw_change = 0;
184: pwent->pw_expire = 0;
185: pwent->pw_class = "";
186:
187: /* line without colon separators is no good, so ignore it */
188: if(!strchr(p,':'))
189: return(NULL);
190:
191: pwent->pw_name = p;
192: p = pwskip(p);
193: pwent->pw_passwd = p;
194: p = pwskip(p);
195: pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
196: p = pwskip(p);
197: pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
198: p = pwskip(p);
199: pwent->pw_gecos = p;
200: p = pwskip(p);
201: pwent->pw_dir = p;
202: p = pwskip(p);
203: pwent->pw_shell = p;
204: while (*p && *p != '\n')
205: p++;
206: *p = '\0';
207: return (pwent);
208: }
209:
210: struct passwd *
211: ypgetpwnam(nam)
212: char *nam;
213: {
214: static struct passwd pwent;
215: static char line[1024];
216: char *val;
217: int reason, vallen;
218:
219: /*
220: * Get local domain
221: */
222: if (!domain && (reason = yp_get_default_domain(&domain))) {
223: fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
224: progname, yperr_string(reason));
225: exit(1);
226: }
227:
228: reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
229: &val, &vallen);
230: switch(reason) {
231: case 0:
232: break;
233: default:
234: return (NULL);
235: break;
236: }
237: val[vallen] = '\0';
238: strcpy(line, val);
239: free(val);
240:
241: return(interpret(&pwent, line));
242: }
243:
244: struct passwd *
245: ypgetpwuid(uid)
246: uid_t uid;
247: {
248: static struct passwd pwent;
249: static char line[1024];
250: char *val;
251: int reason, vallen;
252: char namebuf[16];
253:
254: if (!domain && (reason = yp_get_default_domain(&domain))) {
255: fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
256: progname, yperr_string(reason));
257: exit(1);
258: }
259:
260: sprintf(namebuf, "%d", uid);
261: reason = yp_match(domain, "passwd.byuid", namebuf, strlen(namebuf),
262: &val, &vallen);
263: switch(reason) {
264: case 0:
265: break;
266: default:
267: return (NULL);
268: break;
269: }
270: val[vallen] = '\0';
271: strcpy(line, val);
272: free(val);
273:
274: return(interpret(&pwent, line));
275: }
276:
277: #endif /* YP */