Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / passwd / yp_passwd.c
1 /*
2  * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3  * Copyright (c) 1994 Olaf Kirch <okir@monad.swb.de>
4  * Copyright (c) 1995 Bill Paul <wpaul@ctr.columbia.edu>
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. The name of the author may not be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: src/usr.bin/passwd/yp_passwd.c,v 1.15.6.1 2002/02/15 00:46:56 des Exp $
32  * $DragonFly: src/usr.bin/passwd/yp_passwd.c,v 1.2 2003/06/17 04:29:30 dillon Exp $
33  */
34
35 #ifdef YP
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <netdb.h>
40 #include <time.h>
41 #include <sys/types.h>
42 #include <pwd.h>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <limits.h>
46 #include <rpc/rpc.h>
47 #include <rpcsvc/yp_prot.h>
48 #include <rpcsvc/ypclnt.h>
49 #include <rpcsvc/yppasswd.h>
50 #include <pw_yp.h>
51 #include <err.h>
52 #include "yppasswd_private.h"
53
54 extern char *getnewpasswd(struct passwd *, int);
55
56 int
57 yp_passwd(char *user)
58 {
59         struct yppasswd yppasswd;
60         struct master_yppasswd master_yppasswd;
61         struct passwd *pw;
62         CLIENT *clnt;
63         struct rpc_err err;
64         char   *master;
65         int    *status = NULL;
66         uid_t   uid;
67         char                    *sockname = YP_SOCKNAME;
68
69         _use_yp = 1;
70
71         uid = getuid();
72
73         if ((master = get_yp_master(1)) == NULL) {
74                 warnx("failed to find NIS master server");
75                 return(1);
76         }
77
78         /*
79          * It is presumed that by the time we get here, use_yp()
80          * has been called and that we have verified that the user
81          * actually exists. This being the case, the yp_password
82          * stucture has already been filled in for us.
83          */
84
85         /* Use the correct password */
86         pw = (struct passwd *)&yp_password;
87
88         if (pw->pw_uid != uid && uid != 0) {
89                 warnx("only the super-user may change account information \
90 for other users");
91                 return(1);
92         }
93
94         pw->pw_change = 0;
95
96         /* Initialize password information */
97         if (suser_override) {
98                 master_yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
99                 master_yppasswd.newpw.pw_name = strdup(pw->pw_name);
100                 master_yppasswd.newpw.pw_uid = pw->pw_uid;
101                 master_yppasswd.newpw.pw_gid = pw->pw_gid;
102                 master_yppasswd.newpw.pw_expire = pw->pw_expire;
103                 master_yppasswd.newpw.pw_change = pw->pw_change;
104                 master_yppasswd.newpw.pw_fields = pw->pw_fields;
105                 master_yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
106                 master_yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
107                 master_yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
108                 master_yppasswd.newpw.pw_class = pw->pw_class != NULL ?
109                                         strdup(pw->pw_class) : "";
110                 master_yppasswd.oldpass = "";
111                 master_yppasswd.domain = yp_domain;
112         } else {
113                 yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
114                 yppasswd.newpw.pw_name = strdup(pw->pw_name);
115                 yppasswd.newpw.pw_uid = pw->pw_uid;
116                 yppasswd.newpw.pw_gid = pw->pw_gid;
117                 yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
118                 yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
119                 yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
120                 yppasswd.oldpass = "";
121         }
122
123         if (suser_override)
124                 printf("Changing NIS password for %s on %s in domain %s.\n",
125                         pw->pw_name, master, yp_domain);
126         else
127                 printf("Changing NIS password for %s on %s.\n",
128                                                         pw->pw_name, master);
129
130         /* Get old password */
131
132         if (pw->pw_passwd[0] && !suser_override) {
133                 yppasswd.oldpass = strdup(getpass("Old Password: "));
134                 if (strcmp(crypt(yppasswd.oldpass, pw->pw_passwd),
135                                                         pw->pw_passwd)) {
136                         errx(1, "sorry");
137                 }
138
139         }
140
141         if (suser_override) {
142                 if ((master_yppasswd.newpw.pw_passwd = getnewpasswd(pw, 1)) == NULL)
143                         return(1);
144         } else {
145                 if ((yppasswd.newpw.pw_passwd = getnewpasswd(pw, 1)) == NULL)
146                         return(1);
147         }
148
149         if (suser_override) {
150                 if ((clnt = clnt_create(sockname, MASTER_YPPASSWDPROG,
151                                 MASTER_YPPASSWDVERS, "unix")) == NULL) {
152                         warnx("failed to contact rpc.yppasswdd on host %s: %s",
153                                 master, clnt_spcreateerror(""));
154                         return(1);
155                 }
156         } else {
157                 if ((clnt = clnt_create(master, YPPASSWDPROG,
158                                 YPPASSWDVERS, "udp")) == NULL) {
159                         warnx("failed to contact rpc.yppasswdd on host %s: %s",
160                                 master, clnt_spcreateerror(""));
161                         return(1);
162                 }
163         }
164         /*
165          * The yppasswd.x file said `unix authentication required',
166          * so I added it. This is the only reason it is in here.
167          * My yppasswdd doesn't use it, but maybe some others out there
168          * do.                                  --okir
169          */
170         clnt->cl_auth = authunix_create_default();
171
172         if (suser_override)
173                 status = yppasswdproc_update_master_1(&master_yppasswd, clnt);
174         else
175                 status = yppasswdproc_update_1(&yppasswd, clnt);
176
177         clnt_geterr(clnt, &err);
178
179         auth_destroy(clnt->cl_auth);
180         clnt_destroy(clnt);
181
182         if (err.re_status != RPC_SUCCESS || status == NULL || *status) {
183                 errx(1, "failed to change NIS password: %s",
184                         clnt_sperrno(err.re_status));
185         }
186
187         printf("\nNIS password has%s been changed on %s.\n",
188                 (err.re_status != RPC_SUCCESS || status == NULL || *status) ?
189                 " not" : "", master);
190
191         return ((err.re_status || status == NULL || *status));
192 }
193 #endif /* YP */