Initial import from FreeBSD RELENG_4:
[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  */
33
34 #ifdef YP
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <netdb.h>
39 #include <time.h>
40 #include <sys/types.h>
41 #include <pwd.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <limits.h>
45 #include <rpc/rpc.h>
46 #include <rpcsvc/yp_prot.h>
47 #include <rpcsvc/ypclnt.h>
48 #include <rpcsvc/yppasswd.h>
49 #include <pw_yp.h>
50 #include <err.h>
51 #include "yppasswd_private.h"
52
53 extern char *getnewpasswd(struct passwd *, int);
54
55 int
56 yp_passwd(char *user)
57 {
58         struct yppasswd yppasswd;
59         struct master_yppasswd master_yppasswd;
60         struct passwd *pw;
61         CLIENT *clnt;
62         struct rpc_err err;
63         char   *master;
64         int    *status = NULL;
65         uid_t   uid;
66         char                    *sockname = YP_SOCKNAME;
67
68         _use_yp = 1;
69
70         uid = getuid();
71
72         if ((master = get_yp_master(1)) == NULL) {
73                 warnx("failed to find NIS master server");
74                 return(1);
75         }
76
77         /*
78          * It is presumed that by the time we get here, use_yp()
79          * has been called and that we have verified that the user
80          * actually exists. This being the case, the yp_password
81          * stucture has already been filled in for us.
82          */
83
84         /* Use the correct password */
85         pw = (struct passwd *)&yp_password;
86
87         if (pw->pw_uid != uid && uid != 0) {
88                 warnx("only the super-user may change account information \
89 for other users");
90                 return(1);
91         }
92
93         pw->pw_change = 0;
94
95         /* Initialize password information */
96         if (suser_override) {
97                 master_yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
98                 master_yppasswd.newpw.pw_name = strdup(pw->pw_name);
99                 master_yppasswd.newpw.pw_uid = pw->pw_uid;
100                 master_yppasswd.newpw.pw_gid = pw->pw_gid;
101                 master_yppasswd.newpw.pw_expire = pw->pw_expire;
102                 master_yppasswd.newpw.pw_change = pw->pw_change;
103                 master_yppasswd.newpw.pw_fields = pw->pw_fields;
104                 master_yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
105                 master_yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
106                 master_yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
107                 master_yppasswd.newpw.pw_class = pw->pw_class != NULL ?
108                                         strdup(pw->pw_class) : "";
109                 master_yppasswd.oldpass = "";
110                 master_yppasswd.domain = yp_domain;
111         } else {
112                 yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
113                 yppasswd.newpw.pw_name = strdup(pw->pw_name);
114                 yppasswd.newpw.pw_uid = pw->pw_uid;
115                 yppasswd.newpw.pw_gid = pw->pw_gid;
116                 yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
117                 yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
118                 yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
119                 yppasswd.oldpass = "";
120         }
121
122         if (suser_override)
123                 printf("Changing NIS password for %s on %s in domain %s.\n",
124                         pw->pw_name, master, yp_domain);
125         else
126                 printf("Changing NIS password for %s on %s.\n",
127                                                         pw->pw_name, master);
128
129         /* Get old password */
130
131         if (pw->pw_passwd[0] && !suser_override) {
132                 yppasswd.oldpass = strdup(getpass("Old Password: "));
133                 if (strcmp(crypt(yppasswd.oldpass, pw->pw_passwd),
134                                                         pw->pw_passwd)) {
135                         errx(1, "sorry");
136                 }
137
138         }
139
140         if (suser_override) {
141                 if ((master_yppasswd.newpw.pw_passwd = getnewpasswd(pw, 1)) == NULL)
142                         return(1);
143         } else {
144                 if ((yppasswd.newpw.pw_passwd = getnewpasswd(pw, 1)) == NULL)
145                         return(1);
146         }
147
148         if (suser_override) {
149                 if ((clnt = clnt_create(sockname, MASTER_YPPASSWDPROG,
150                                 MASTER_YPPASSWDVERS, "unix")) == NULL) {
151                         warnx("failed to contact rpc.yppasswdd on host %s: %s",
152                                 master, clnt_spcreateerror(""));
153                         return(1);
154                 }
155         } else {
156                 if ((clnt = clnt_create(master, YPPASSWDPROG,
157                                 YPPASSWDVERS, "udp")) == NULL) {
158                         warnx("failed to contact rpc.yppasswdd on host %s: %s",
159                                 master, clnt_spcreateerror(""));
160                         return(1);
161                 }
162         }
163         /*
164          * The yppasswd.x file said `unix authentication required',
165          * so I added it. This is the only reason it is in here.
166          * My yppasswdd doesn't use it, but maybe some others out there
167          * do.                                  --okir
168          */
169         clnt->cl_auth = authunix_create_default();
170
171         if (suser_override)
172                 status = yppasswdproc_update_master_1(&master_yppasswd, clnt);
173         else
174                 status = yppasswdproc_update_1(&yppasswd, clnt);
175
176         clnt_geterr(clnt, &err);
177
178         auth_destroy(clnt->cl_auth);
179         clnt_destroy(clnt);
180
181         if (err.re_status != RPC_SUCCESS || status == NULL || *status) {
182                 errx(1, "failed to change NIS password: %s",
183                         clnt_sperrno(err.re_status));
184         }
185
186         printf("\nNIS password has%s been changed on %s.\n",
187                 (err.re_status != RPC_SUCCESS || status == NULL || *status) ?
188                 " not" : "", master);
189
190         return ((err.re_status || status == NULL || *status));
191 }
192 #endif /* YP */