Remove no longer needed catman periodic via 'make upgrade'.
[dragonfly.git] / usr.bin / chpass / chpass.c
1 /*-
2  * Copyright (c) 1988, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2002 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * Portions of this software were developed for the FreeBSD Project by
8  * ThinkSec AS and NAI Labs, the Security Research Division of Network
9  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
10  * ("CBOSS"), as part of the DARPA CHATS research program.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. 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  * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California.  All rights reserved.
37  * @(#)chpass.c 8.4 (Berkeley) 4/2/94
38  * $FreeBSD: src/usr.bin/chpass/chpass.c,v 1.28 2006/09/25 15:06:24 marck Exp $
39  */
40
41 #include <sys/param.h>
42
43 #include <err.h>
44 #include <errno.h>
45 #include <pwd.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #ifdef YP
51 #include <ypclnt.h>
52 #endif
53
54 #include <pw_scan.h>
55 #include <libutil.h>
56
57 #include "chpass.h"
58
59 int master_mode;
60
61 static void     baduser(void);
62 static void     usage(void);
63
64 int
65 main(int argc, char **argv)
66 {
67         enum { NEWSH, LOADENTRY, EDITENTRY, NEWPW, NEWEXP } op;
68         struct passwd lpw, *old_pw, *pw;
69         int ch, pfd, tfd;
70         const char *password;
71         char *cryptpw;
72         char *arg = NULL;
73         uid_t uid;
74 #ifdef YP
75         struct ypclnt *ypclnt;
76         const char *yp_domain = NULL, *yp_host = NULL;
77 #endif
78
79         pw = old_pw = NULL;
80         op = EDITENTRY;
81 #ifdef YP
82         while ((ch = getopt(argc, argv, "a:p:s:e:d:h:loy")) != -1)
83 #else
84         while ((ch = getopt(argc, argv, "a:p:s:e:")) != -1)
85 #endif
86                 switch (ch) {
87                 case 'a':
88                         op = LOADENTRY;
89                         arg = optarg;
90                         break;
91                 case 's':
92                         op = NEWSH;
93                         arg = optarg;
94                         break;
95                 case 'p':
96                         op = NEWPW;
97                         arg = optarg;
98                         break;
99                 case 'e':
100                         op = NEWEXP;
101                         arg = optarg;
102                         break;
103 #ifdef YP
104                 case 'd':
105                         yp_domain = optarg;
106                         break;
107                 case 'h':
108                         yp_host = optarg;
109                         break;
110                 case 'l':
111                 case 'o':
112                 case 'y':
113                         /* compatibility */
114                         break;
115 #endif
116                 case '?':
117                 default:
118                         usage();
119                 }
120
121         argc -= optind;
122         argv += optind;
123
124         if (argc > 1)
125                 usage();
126
127         uid = getuid();
128
129         if (op == EDITENTRY || op == NEWSH || op == NEWPW || op == NEWEXP) {
130                 if (argc == 0) {
131                         if ((pw = getpwuid(uid)) == NULL)
132                                 errx(1, "unknown user: uid %lu",
133                                     (unsigned long)uid);
134                 } else {
135                         if ((pw = getpwnam(*argv)) == NULL)
136                                 errx(1, "unknown user: %s", *argv);
137                         if (uid != 0 && uid != pw->pw_uid)
138                                 baduser();
139                 }
140
141                 /* Make a copy for later verification */
142                 if ((pw = pw_dup(pw)) == NULL ||
143                     (old_pw = pw_dup(pw)) == NULL)
144                         err(1, "pw_dup");
145         }
146
147 #ifdef YP
148         if (pw != NULL && (pw->pw_fields & _PWF_SOURCE) == _PWF_NIS) {
149                 ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host);
150                 master_mode = (ypclnt != NULL &&
151                     ypclnt_connect(ypclnt) != -1 &&
152                     ypclnt_havepasswdd(ypclnt) == 1);
153                 ypclnt_free(ypclnt);
154         } else
155 #endif
156         master_mode = (uid == 0);
157
158         if (op == NEWSH) {
159                 /* protect p_shell -- it thinks NULL is /bin/sh */
160                 if (!arg[0])
161                         usage();
162                 if (p_shell(arg, pw, NULL) == -1)
163                         exit(1);
164         }
165
166         if (op == NEWEXP) {
167                 if (uid)        /* only root can change expire */
168                         baduser();
169                 if (p_expire(arg, pw, NULL) == -1)
170                         exit(1);
171         }
172
173         if (op == LOADENTRY) {
174                 if (uid)
175                         baduser();
176                 pw = &lpw;
177                 old_pw = NULL;
178                 if (!__pw_scan(arg, pw, _PWSCAN_WARN|_PWSCAN_MASTER))
179                         exit(1);
180         }
181
182         if (op == NEWPW) {
183                 if (uid)
184                         baduser();
185
186                 if (strchr(arg, ':'))
187                         errx(1, "invalid format for password");
188                 pw->pw_passwd = arg;
189         }
190
191         if (op == EDITENTRY) {
192                 /*
193                  * We don't really need pw_*() here, but pw_edit() (used
194                  * by edit()) is just too useful...
195                  */
196                 if (pw_init(NULL, NULL))
197                         err(1, "pw_init()");
198                 if ((tfd = pw_tmp(-1)) == -1) {
199                         pw_fini();
200                         err(1, "pw_tmp()");
201                 }
202                 free(pw);
203                 pw = edit(pw_tempname(), old_pw);
204                 pw_fini();
205                 if (pw == NULL)
206                         err(1, "edit()");
207                 /*
208                  * pw_equal does not check for crypted passwords, so we
209                  * should do it explicitly
210                  */
211                 if (pw_equal(old_pw, pw) &&
212                     strcmp(old_pw->pw_passwd, pw->pw_passwd) == 0)
213                         errx(0, "user information unchanged");
214         }
215
216         if (old_pw && !master_mode) {
217                 password = getpass("Password: ");
218                 cryptpw = crypt(password, old_pw->pw_passwd);
219                 if (cryptpw == NULL || strcmp(cryptpw, old_pw->pw_passwd) != 0)
220                         baduser();
221         } else {
222                 password = "";
223         }
224
225         if (old_pw != NULL)
226                 pw->pw_fields |= (old_pw->pw_fields & _PWF_SOURCE);
227         switch (pw->pw_fields & _PWF_SOURCE) {
228 #ifdef YP
229         case _PWF_NIS:
230                 ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host);
231                 if (ypclnt == NULL) {
232                         warnx("ypclnt_new failed");
233                         exit(1);
234                 }
235                 if (ypclnt_connect(ypclnt) == -1 ||
236                     ypclnt_passwd(ypclnt, pw, password) == -1) {
237                         warnx("%s", ypclnt->error);
238                         ypclnt_free(ypclnt);
239                         exit(1);
240                 }
241                 ypclnt_free(ypclnt);
242                 errx(0, "NIS user information updated");
243 #endif /* YP */
244         case 0:
245         case _PWF_FILES:
246                 if (pw_init(NULL, NULL))
247                         err(1, "pw_init()");
248                 if ((pfd = pw_lock()) == -1) {
249                         pw_fini();
250                         err(1, "pw_lock()");
251                 }
252                 if ((tfd = pw_tmp(-1)) == -1) {
253                         pw_fini();
254                         err(1, "pw_tmp()");
255                 }
256                 if (pw_copy(pfd, tfd, pw, old_pw) == -1) {
257                         pw_fini();
258                         err(1, "pw_copy");
259                 }
260                 if (pw_mkdb(pw->pw_name) == -1) {
261                         pw_fini();
262                         err(1, "pw_mkdb()");
263                 }
264                 pw_fini();
265                 errx(0, "user information updated");
266                 break;
267         default:
268                 errx(1, "unsupported passwd source");
269         }
270 }
271
272 static void
273 baduser(void)
274 {
275
276         errx(1, "%s", strerror(EACCES));
277 }
278
279 static void
280 usage(void)
281 {
282
283         fprintf(stderr,
284             "usage: chpass%s %s [user]\n",
285 #ifdef YP
286             " [-d domain] [-h host]",
287 #else
288             "",
289 #endif
290             "[-a list] [-p encpass] [-s shell] [-e mmm dd yy]");
291         exit(1);
292 }