Bring in a PAM version of passwd.
[dragonfly.git] / usr.bin / chpass / pw_yp.c
1 /*
2  * Copyright (c) 1995
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
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 Bill Paul 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 Bill Paul 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  * NIS interface routines for chpass
33  *
34  * Written by Bill Paul <wpaul@ctr.columbia.edu>
35  * Center for Telecommunications Research
36  * Columbia University, New York City
37  *
38  * $FreeBSD: src/usr.bin/chpass/pw_yp.c,v 1.16.2.1 2002/02/15 00:46:56 des Exp $
39  * $DragonFly: src/usr.bin/chpass/pw_yp.c,v 1.5 2005/05/07 22:29:39 corecode Exp $
40  */
41
42 #ifdef YP
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <netdb.h>
47 #include <time.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <pwd.h>
51 #include <errno.h>
52 #include <err.h>
53 #include <unistd.h>
54 #include <db.h>
55 #include <fcntl.h>
56 #include <utmp.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/param.h>
60 #include <limits.h>
61 #include <rpc/rpc.h>
62 #include <rpcsvc/yp.h>
63 struct dom_binding {};
64 #include <rpcsvc/ypclnt.h>
65 #include <rpcsvc/yppasswd.h>
66 #include <pw_util.h>
67 #include "pw_yp.h"
68 #include "ypxfr_extern.h"
69 #include "yppasswd_private.h"
70
71 #define PERM_SECURE (S_IRUSR|S_IWUSR)
72 HASHINFO openinfo = {
73         4096,           /* bsize */
74         32,             /* ffactor */
75         256,            /* nelem */
76         2048 * 1024,    /* cachesize */
77         NULL,           /* hash */
78         0,              /* lorder */
79 };
80
81 int force_old = 0;
82 int _use_yp = 0;
83 int suser_override = 0;
84 int yp_in_pw_file = 0;
85 char *yp_domain = NULL;
86 char *yp_server = NULL;
87
88 extern char *tempname;
89
90 /* Save the local and NIS password information */
91 struct passwd local_password;
92 struct passwd yp_password;
93
94 void copy_yp_pass(char *p, int x, int m)
95 {
96         char *t, *s = p;
97         static char *buf;
98
99         yp_password.pw_fields = 0;
100
101         buf = (char *)realloc(buf, m + 10);
102         bzero(buf, m + 10);
103
104         /* Turn all colons into NULLs */
105         while (strchr(s, ':')) {
106                 s = (strchr(s, ':') + 1);
107                 *(s - 1)= '\0';
108         }
109
110         t = buf;
111 #define EXPAND(e)       e = t; while ((*t++ = *p++));
112         EXPAND(yp_password.pw_name);
113         yp_password.pw_fields |= _PWF_NAME;
114         EXPAND(yp_password.pw_passwd);
115         yp_password.pw_fields |= _PWF_PASSWD;
116         yp_password.pw_uid = atoi(p);
117         p += (strlen(p) + 1);
118         yp_password.pw_fields |= _PWF_UID;
119         yp_password.pw_gid = atoi(p);
120         p += (strlen(p) + 1);
121         yp_password.pw_fields |= _PWF_GID;
122         if (x) {
123                 EXPAND(yp_password.pw_class);
124                 yp_password.pw_fields |= _PWF_CLASS;
125                 yp_password.pw_change = atol(p);
126                 p += (strlen(p) + 1);
127                 yp_password.pw_fields |= _PWF_CHANGE;
128                 yp_password.pw_expire = atol(p);
129                 p += (strlen(p) + 1);
130                 yp_password.pw_fields |= _PWF_EXPIRE;
131         }
132         EXPAND(yp_password.pw_gecos);
133         yp_password.pw_fields |= _PWF_GECOS;
134         EXPAND(yp_password.pw_dir);
135         yp_password.pw_fields |= _PWF_DIR;
136         EXPAND(yp_password.pw_shell);
137         yp_password.pw_fields |= _PWF_SHELL;
138
139         return;
140 }
141
142 void copy_local_pass(char *p, int m)
143 {
144         char *t;
145         static char *buf;
146
147         buf = (char *)realloc(buf, m + 10);
148         bzero(buf, m + 10);
149
150         t = buf;
151         EXPAND(local_password.pw_name);
152         EXPAND(local_password.pw_passwd);
153         bcopy(p, (char *)&local_password.pw_uid, sizeof(int));
154         p += sizeof(int);
155         bcopy(p, (char *)&local_password.pw_gid, sizeof(int));
156         p += sizeof(int);
157         bcopy(p, (char *)&local_password.pw_change, sizeof(time_t));
158         p += sizeof(time_t);
159         EXPAND(local_password.pw_class);
160         EXPAND(local_password.pw_gecos);
161         EXPAND(local_password.pw_dir);
162         EXPAND(local_password.pw_shell);
163         bcopy(p, (char *)&local_password.pw_expire, sizeof(time_t));
164         p += sizeof(time_t);
165         bcopy(p, (char *)&local_password.pw_fields, sizeof local_password.pw_fields);
166         p += sizeof local_password.pw_fields;
167
168         return;
169 }
170
171 /*
172  * It is not mandatory that an NIS master server also be a client.
173  * However, if the NIS master is not configured as a client, then the
174  * domain name will not be set and ypbind will not be running, so we
175  * will be unable to use the ypclnt routines inside libc. We therefore
176  * need our own magic version of yp_match() which we can use in any
177  * environment.
178  */
179 static int my_yp_match(char *server, char *domain, char *map, char *key,
180                        unsigned long keylen, char **result, unsigned long *resultlen)
181 {
182         ypreq_key ypkey;
183         ypresp_val *ypval;
184         CLIENT *clnt;
185         static char buf[YPMAXRECORD + 2];
186
187         bzero((char *)buf, sizeof(buf));
188
189         /*
190          * Don't make this a fatal error. The inability to contact
191          * a server is, for our purposes, equivalent to not finding
192          * the record we were looking for. Letting use_yp() know
193          * that the lookup failed is sufficient.
194          */
195         if ((clnt = clnt_create(server, YPPROG,YPVERS,"udp")) == NULL) {
196                 return(1);
197 #ifdef notdef
198                 warnx("failed to create UDP handle: %s",
199                                         clnt_spcreateerror(server));
200                 pw_error(tempname, 0, 1);
201 #endif
202         }
203
204         ypkey.domain = domain;
205         ypkey.map = map;
206         ypkey.key.keydat_len = keylen;
207         ypkey.key.keydat_val = key;
208
209         if ((ypval = ypproc_match_2(&ypkey, clnt)) == NULL) {
210                 clnt_destroy(clnt);
211                 return(1);
212 #ifdef notdef
213                 warnx("%s",clnt_sperror(clnt,"YPPROC_MATCH failed"));
214                 pw_error(tempname, 0, 1);
215 #endif
216         }
217
218         clnt_destroy(clnt);
219
220         if (ypval->stat != YP_TRUE) {
221                 xdr_free(xdr_ypresp_val, (char *)ypval);
222                 return(1);
223 #ifdef notdef
224                 int stat = ypval->stat;
225                 xdr_free(xdr_ypresp_val, (char *)ypval);
226                 if (stat == YP_NOMAP && strstr(map, "master.passwd"))
227                         return(1);
228                 if (stat == YP_NOKEY)
229                         return(1);
230                 warnx("ypmatch failed: %s", yperr_string(ypprot_err(stat)));
231                 pw_error(tempname, 0, 1);
232 #endif
233         }
234
235
236         strncpy((char *)&buf, ypval->val.valdat_val, ypval->val.valdat_len);
237
238         *result = (char *)&buf;
239         *resultlen = ypval->val.valdat_len;
240
241         xdr_free(xdr_ypresp_val, (char *)ypval);
242
243         return(0);
244 }
245
246 /*
247  * Check if the user we're working with is local or in NIS.
248  */
249 /* which: 0 = use username, 1 = use uid */
250 int use_yp (char *user, uid_t uid, int which)
251 {
252         int user_local = 0, user_yp = 0, user_exists = 0;
253         DB *dbp;
254         DBT key,data;
255         char bf[UT_NAMESIZE + 2];
256         char *result;
257         char *server;
258         long resultlen;
259         char ubuf[UT_NAMESIZE + 2];
260
261         if (which) {
262                 snprintf(ubuf, sizeof(ubuf), "%lu", (unsigned long)uid);
263                 user = (char *)&ubuf;
264         }
265
266         /* Grope around for the user in the usual way */
267         if (which) {
268                 if (getpwuid(uid) != NULL)
269                         user_exists = 1;
270         } else {
271                 if (getpwnam(user) != NULL)
272                         user_exists = 1;
273         }
274
275         /* Now grope directly through the user database */
276         if ((dbp = dbopen(_PATH_SMP_DB, O_RDONLY, PERM_SECURE,
277                         DB_HASH, &openinfo)) == NULL) {
278                         warn("error opening database: %s.", _PATH_MP_DB);
279                         pw_error(tempname, 0, 1);
280         }
281
282         /* Is NIS turned on */
283         bf[0] = _PW_KEYYPENABLED;
284         key.data = (u_char *)bf;
285         key.size = 1;
286         yp_in_pw_file = !(dbp->get)(dbp,&key,&data,0);
287         if (_yp_check(NULL) || (yp_domain && yp_server)) {
288                 server = get_yp_master(0);
289
290                 /* Is the user in the NIS passwd map */
291                 if (!my_yp_match(server, yp_domain, which ? "passwd.byuid" :
292                                         "passwd.byname", user, strlen(user),
293                     &result, &resultlen)) {
294                         user_yp = user_exists = 1;
295                         *(char *)(result + resultlen) = '\0';
296                         copy_yp_pass(result, 0, resultlen);
297                 }
298                 /* Is the user in the NIS master.passwd map */
299                 if (user_yp && !my_yp_match(server, yp_domain, which ?
300                         "master.passwd.byuid" : "master.passwd.byname",
301                     user, strlen(user),
302                     &result, &resultlen)) {
303                         *(char *)(result + resultlen) = '\0';
304                         copy_yp_pass(result, 1, resultlen);
305                 }
306         }
307
308         /* Is the user in the local password database */
309
310         bf[0] = which ? _PW_KEYBYUID : _PW_KEYBYNAME;
311         if (which)
312                 bcopy((char *)&uid, bf + 1, sizeof(uid));
313         else
314                 bcopy((char *)user, bf + 1, MIN(strlen(user), UT_NAMESIZE));
315         key.data = (u_char *)bf;
316         key.size = which ? sizeof(uid) + 1 : strlen(user) + 1;
317         if (!(dbp->get)(dbp,&key,&data,0)) {
318                 user_local = 1;
319                 copy_local_pass(data.data, data.size);
320         }
321
322         (dbp->close)(dbp);
323
324         if (user_local && user_yp && user_exists)
325                 return(USER_YP_AND_LOCAL);
326         else if (!user_local && user_yp && user_exists)
327                 return(USER_YP_ONLY);
328         else if (user_local && !user_yp && user_exists)
329                 return(USER_LOCAL_ONLY);
330         else if (!user_exists)
331                 return(USER_UNKNOWN);
332
333         return(-1);
334 }
335
336 /*
337  * Find the name of the NIS master server for this domain
338  * and make sure it's running yppasswdd.
339  */
340 char *get_yp_master(int getserver)
341 {
342         char *mastername;
343         int rval, localport;
344         struct stat st;
345         char                    *sockname = YP_SOCKNAME;
346
347         /*
348          * Sometimes we are called just to probe for rpc.yppasswdd and
349          * set the suser_override flag. Just return NULL and leave
350          * suser_override at 0 if _use_yp doesn't indicate that NIS is
351          * in use and we weren't called from use_yp() itself.
352          * Without this check, we might try probing and fail with an NIS
353          * error in non-NIS environments.
354          */
355         if ((_use_yp == USER_UNKNOWN || _use_yp == USER_LOCAL_ONLY) &&
356                                                                 getserver)
357                 return(NULL);
358
359         /* Get default NIS domain. */
360
361         if (yp_domain == NULL && (rval = yp_get_default_domain(&yp_domain))) {
362                 warnx("can't get local NIS domain name: %s",yperr_string(rval));
363                 pw_error(tempname, 0, 1);
364         }
365
366         /* Get master server of passwd map. */
367
368         if ((mastername = ypxfr_get_master(yp_domain, "passwd.byname",
369                                 yp_server, yp_server ? 0 : 1)) == NULL) {
370                 warnx("can't get name of master NIS server");
371                 pw_error(tempname, 0, 1);
372         }
373
374         if (!getserver)
375                 return(mastername);
376
377         /* Check if yppasswdd is out there. */
378
379         if ((rval = getrpcport(mastername, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
380                 IPPROTO_UDP)) == 0) {
381                 warnx("rpc.yppasswdd is not running on the NIS master server");
382                 pw_error(tempname, 0, 1);
383         }
384
385         /*
386          * Make sure it's on a reserved port.
387          * XXX Might break with yppasswdd servers running on Solaris 2.x.
388          */
389
390         if (rval >= IPPORT_RESERVED) {
391                 warnx("rpc.yppasswdd server not running on reserved port");
392                 pw_error(tempname, 0, 1);
393         }
394
395         /* See if _we_ are the master server. */
396         if (!force_old && !getuid() && (localport = getrpcport("localhost",
397                 YPPASSWDPROG, YPPASSWDPROC_UPDATE, IPPROTO_UDP)) != 0) {
398                 if (localport == rval && stat(sockname, &st) != -1) {
399                         suser_override = 1;
400                         mastername = "localhost";
401                 }
402         }
403
404         /* Everything checks out: return the name of the server. */
405
406         return (mastername);
407 }
408
409 /*
410  * Ask the user for his NIS password and submit the new information
411  * to yppasswdd. Note that rpc.yppasswdd requires password authentication
412  * and only allows changes to existing records rather than the addition
413  * of new records. (To do actual updates we would need something like
414  * secure RPC and ypupdated, which FreeBSD doesn't have yet.) The FreeBSD
415  * rpc.yppasswdd has some special hooks to allow the superuser update
416  * information without specifying a password, however this only works
417  * for the superuser on the NIS master server.
418  */
419 void yp_submit(struct passwd *pw)
420 {
421         struct yppasswd yppasswd;
422         struct master_yppasswd master_yppasswd;
423         CLIENT *clnt;
424         char *master, *password;
425         int *status = NULL;
426         struct rpc_err err;
427         char                    *sockname = YP_SOCKNAME;
428
429         _use_yp = 1;
430
431         /* Get NIS master server name */
432
433         master = get_yp_master(1);
434
435         /* Populate the yppasswd structure that gets handed to yppasswdd. */
436
437         if (suser_override) {
438                 master_yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
439                 master_yppasswd.newpw.pw_name = strdup(pw->pw_name);
440                 master_yppasswd.newpw.pw_uid = pw->pw_uid;
441                 master_yppasswd.newpw.pw_gid = pw->pw_gid;
442                 master_yppasswd.newpw.pw_expire = pw->pw_expire;
443                 master_yppasswd.newpw.pw_change = pw->pw_change;
444                 master_yppasswd.newpw.pw_fields = pw->pw_fields;
445                 master_yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
446                 master_yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
447                 master_yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
448                 master_yppasswd.newpw.pw_class = pw->pw_class != NULL ?
449                                                 strdup(pw->pw_class) : "";
450                 master_yppasswd.oldpass = ""; /* not really needed */
451                 master_yppasswd.domain = yp_domain;
452         } else {
453                 yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
454                 yppasswd.newpw.pw_name = strdup(pw->pw_name);
455                 yppasswd.newpw.pw_uid = pw->pw_uid;
456                 yppasswd.newpw.pw_gid = pw->pw_gid;
457                 yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
458                 yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
459                 yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
460                 yppasswd.oldpass = "";
461         }
462
463         /* Get the user's password for authentication purposes. */
464
465         printf ("Changing NIS information for %s on %s\n",
466                                         pw->pw_name, master);
467
468         if (pw->pw_passwd[0] && !suser_override) {
469                 password = getpass("Please enter password: ");
470                 if (strncmp(crypt(password,pw->pw_passwd),
471                                 pw->pw_passwd,strlen(pw->pw_passwd))) {
472                         warnx("Password incorrect.");
473                         pw_error(tempname, 0, 1);
474                 }
475                 yppasswd.oldpass = password;    /* XXX */
476         }
477
478         if (suser_override) {
479                 /* Talk to server via AF_UNIX socket. */
480                 clnt = clnt_create(sockname, MASTER_YPPASSWDPROG,
481                                         MASTER_YPPASSWDVERS, "unix");
482                 if (clnt == NULL) {
483                         warnx("failed to contact rpc.yppasswdd: %s",
484                                 clnt_spcreateerror(master));
485                         pw_error(tempname, 0, 1);
486                 }
487         } else {
488                 /* Create a handle to yppasswdd. */
489
490                 if ((clnt = clnt_create(master, YPPASSWDPROG,
491                                         YPPASSWDVERS, "udp")) == NULL) {
492                         warnx("failed to contact rpc.yppasswdd: %s",
493                                 clnt_spcreateerror(master));
494                         pw_error(tempname, 0, 1);
495                 }
496         }
497
498         clnt->cl_auth = authunix_create_default();
499
500         if (suser_override)
501                 status = yppasswdproc_update_master_1(&master_yppasswd, clnt);
502         else
503                 status = yppasswdproc_update_1(&yppasswd, clnt);
504
505         clnt_geterr(clnt, &err);
506
507         auth_destroy(clnt->cl_auth);
508         clnt_destroy(clnt);
509
510         /* Call failed: signal the error. */
511
512         if (err.re_status != RPC_SUCCESS || status == NULL || *status) {
513                 warnx("NIS update failed: %s", clnt_sperrno(err.re_status));
514                 pw_error(NULL, 0, 1);
515         }
516
517         /* Success. */
518
519         if (suser_override)
520                 warnx("NIS information changed on host %s, domain %s",
521                         master, yp_domain);
522         else
523                 warnx("NIS information changed on host %s", master);
524
525         return;
526 }
527 #endif /* YP */