]> git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/vipw.c
scriptreplay: cleanup usage()
[thirdparty/util-linux.git] / login-utils / vipw.c
1 /*
2 * Copyright (c) 1987 Regents of the University of California.
3 * 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 the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * Updated Thu Oct 12 09:56:55 1995 by faith@cs.unc.edu with security
34 * patches from Zefram <A.Main@dcs.warwick.ac.uk>
35 *
36 * Updated Thu Nov 9 21:58:53 1995 by Martin Schulze
37 * <joey@finlandia.infodrom.north.de>. Support for vigr.
38 *
39 * Martin Schulze's patches adapted to Util-Linux by Nicolai Langfeldt.
40 *
41 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
42 * - added Native Language Support
43 * Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
44 * - fixed strerr(errno) in gettext calls
45 */
46
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <paths.h>
50 #include <pwd.h>
51 #include <shadow.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <sys/file.h>
57 #include <sys/param.h>
58 #include <sys/resource.h>
59 #include <sys/stat.h>
60 #include <sys/time.h>
61 #include <sys/types.h>
62 #include <sys/wait.h>
63 #include <unistd.h>
64 #include <getopt.h>
65
66 #include "c.h"
67 #include "fileutils.h"
68 #include "closestream.h"
69 #include "nls.h"
70 #include "setpwnam.h"
71 #include "strutils.h"
72 #include "xalloc.h"
73 #include "rpmatch.h"
74
75 #ifdef HAVE_LIBSELINUX
76 # include <selinux/selinux.h>
77 #endif
78
79 #define FILENAMELEN 67
80
81 enum {
82 VIPW,
83 VIGR
84 };
85 static int program;
86 static char orig_file[FILENAMELEN]; /* original file /etc/passwd or /etc/group */
87 static char *tmp_file; /* tmp file */
88
89 void pw_error (char *, int, int);
90
91 static void copyfile(int from, int to)
92 {
93 int nr, nw, off;
94 char buf[8 * 1024];
95
96 while ((nr = read(from, buf, sizeof(buf))) > 0)
97 for (off = 0; off < nr; nr -= nw, off += nw)
98 if ((nw = write(to, buf + off, nr)) < 0)
99 pw_error(tmp_file, 1, 1);
100
101 if (nr < 0)
102 pw_error(orig_file, 1, 1);
103 #ifdef HAVE_EXPLICIT_BZERO
104 explicit_bzero(buf, sizeof(buf));
105 #endif
106 }
107
108 static void pw_init(void)
109 {
110 struct rlimit rlim;
111
112 /* Unlimited resource limits. */
113 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
114 (void)setrlimit(RLIMIT_CPU, &rlim);
115 (void)setrlimit(RLIMIT_FSIZE, &rlim);
116 (void)setrlimit(RLIMIT_STACK, &rlim);
117 (void)setrlimit(RLIMIT_DATA, &rlim);
118 (void)setrlimit(RLIMIT_RSS, &rlim);
119
120 /* Don't drop core (not really necessary, but GP's). */
121 rlim.rlim_cur = rlim.rlim_max = 0;
122 (void)setrlimit(RLIMIT_CORE, &rlim);
123
124 /* Turn off signals. */
125 (void)signal(SIGALRM, SIG_IGN);
126 (void)signal(SIGHUP, SIG_IGN);
127 (void)signal(SIGINT, SIG_IGN);
128 (void)signal(SIGPIPE, SIG_IGN);
129 (void)signal(SIGQUIT, SIG_IGN);
130 (void)signal(SIGTERM, SIG_IGN);
131 (void)signal(SIGTSTP, SIG_IGN);
132 (void)signal(SIGTTOU, SIG_IGN);
133
134 /* Create with exact permissions. */
135 (void)umask(0);
136 }
137
138 static FILE * pw_tmpfile(int lockfd)
139 {
140 FILE *fd;
141 char *tmpname = NULL;
142
143 if ((fd = xfmkstemp(&tmpname, "/etc", ".vipw")) == NULL) {
144 ulckpwdf();
145 err(EXIT_FAILURE, _("can't open temporary file"));
146 }
147
148 copyfile(lockfd, fileno(fd));
149 tmp_file = tmpname;
150 return fd;
151 }
152
153 static void pw_write(void)
154 {
155 char tmp[FILENAMELEN + 4];
156
157 sprintf(tmp, "%s%s", orig_file, ".OLD");
158 unlink(tmp);
159
160 if (link(orig_file, tmp))
161 warn(_("%s: create a link to %s failed"), orig_file, tmp);
162
163 #ifdef HAVE_LIBSELINUX
164 if (is_selinux_enabled() > 0) {
165 security_context_t passwd_context = NULL;
166 int ret = 0;
167 if (getfilecon(orig_file, &passwd_context) < 0) {
168 warnx(_("Can't get context for %s"), orig_file);
169 pw_error(orig_file, 1, 1);
170 }
171 ret = setfilecon(tmp_file, passwd_context);
172 freecon(passwd_context);
173 if (ret != 0) {
174 warnx(_("Can't set context for %s"), tmp_file);
175 pw_error(tmp_file, 1, 1);
176 }
177 }
178 #endif
179
180 if (rename(tmp_file, orig_file) == -1) {
181 int errsv = errno;
182 errx(EXIT_FAILURE,
183 ("cannot write %s: %s (your changes are still in %s)"),
184 orig_file, strerror(errsv), tmp_file);
185 }
186 unlink(tmp_file);
187 free(tmp_file);
188 tmp_file = NULL;
189 }
190
191 static void pw_edit(void)
192 {
193 int pstat;
194 pid_t pid;
195 char *p, *editor, *tk;
196
197 editor = getenv("EDITOR");
198 editor = xstrdup(editor ? editor : _PATH_VI);
199
200 tk = strtok(editor, " \t");
201 if (tk && (p = strrchr(tk, '/')) != NULL)
202 ++p;
203 else
204 p = editor;
205
206 pid = fork();
207 if (pid < 0)
208 err(EXIT_FAILURE, _("fork failed"));
209
210 if (!pid) {
211 execlp(editor, p, tmp_file, NULL);
212 errexec(editor);
213 }
214 for (;;) {
215 pid = waitpid(pid, &pstat, WUNTRACED);
216 if (WIFSTOPPED(pstat)) {
217 /* the editor suspended, so suspend us as well */
218 kill(getpid(), SIGSTOP);
219 kill(pid, SIGCONT);
220 } else {
221 break;
222 }
223 }
224 if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
225 pw_error(editor, 1, 1);
226
227 free(editor);
228 }
229
230 void __attribute__((__noreturn__))
231 pw_error(char *name, int err, int eval)
232 {
233 if (err) {
234 if (name)
235 warn("%s: ", name);
236 else
237 warn(NULL);
238 }
239 warnx(_("%s unchanged"), orig_file);
240
241 if (tmp_file)
242 unlink(tmp_file);
243 ulckpwdf();
244 exit(eval);
245 }
246
247 static void edit_file(int is_shadow)
248 {
249 struct stat begin, end;
250 int passwd_file, ch_ret;
251 FILE *tmp_fd;
252
253 pw_init();
254
255 /* acquire exclusive lock */
256 if (lckpwdf() < 0)
257 err(EXIT_FAILURE, _("cannot get lock"));
258
259 passwd_file = open(orig_file, O_RDONLY | O_CLOEXEC, 0);
260 if (passwd_file < 0)
261 err(EXIT_FAILURE, _("cannot open %s"), orig_file);
262 tmp_fd = pw_tmpfile(passwd_file);
263
264 if (fstat(fileno(tmp_fd), &begin))
265 pw_error(tmp_file, 1, 1);
266
267 pw_edit();
268
269 if (fstat(fileno(tmp_fd), &end))
270 pw_error(tmp_file, 1, 1);
271 /* Some editors, such as Vim with 'writebackup' mode enabled,
272 * use "atomic save" in which the old file is deleted and a new
273 * one with the same name created in its place. */
274 if (end.st_nlink == 0) {
275 if (close_stream(tmp_fd) != 0)
276 err(EXIT_FAILURE, _("write error"));
277 tmp_fd = fopen(tmp_file, "r" UL_CLOEXECSTR);
278 if (!tmp_fd)
279 err(EXIT_FAILURE, _("cannot open %s"), tmp_file);
280 if (fstat(fileno(tmp_fd), &end))
281 pw_error(tmp_file, 1, 1);
282 }
283 if (begin.st_mtime == end.st_mtime) {
284 warnx(_("no changes made"));
285 pw_error((char *)NULL, 0, 0);
286 }
287 /* pw_tmpfile() will create the file with mode 600 */
288 if (!is_shadow)
289 ch_ret = fchmod(fileno(tmp_fd), 0644);
290 else
291 ch_ret = fchmod(fileno(tmp_fd), 0400);
292 if (ch_ret < 0)
293 err(EXIT_FAILURE, "%s: %s", _("cannot chmod file"), orig_file);
294 if (close_stream(tmp_fd) != 0)
295 err(EXIT_FAILURE, _("write error"));
296 pw_write();
297 close(passwd_file);
298 ulckpwdf();
299 }
300
301 static void __attribute__((__noreturn__)) usage(void)
302 {
303 FILE *out = stdout;
304 fputs(USAGE_HEADER, out);
305 fprintf(out, " %s\n", program_invocation_short_name);
306
307 fputs(USAGE_SEPARATOR, out);
308 fputs(_("Edit the password or group file.\n"), out);
309
310 fputs(USAGE_OPTIONS, out);
311 printf(USAGE_HELP_OPTIONS(16));
312 printf(USAGE_MAN_TAIL("vipw(8)"));
313 exit(EXIT_SUCCESS);
314 }
315
316 int main(int argc, char *argv[])
317 {
318 int c;
319 static const struct option longopts[] = {
320 {"version", no_argument, NULL, 'V'},
321 {"help", no_argument, NULL, 'h'},
322 {NULL, 0, NULL, 0}
323 };
324
325 setlocale(LC_ALL, "");
326 bindtextdomain(PACKAGE, LOCALEDIR);
327 textdomain(PACKAGE);
328 close_stdout_atexit();
329
330 if (!strcmp(program_invocation_short_name, "vigr")) {
331 program = VIGR;
332 xstrncpy(orig_file, GROUP_FILE, sizeof(orig_file));
333 } else {
334 program = VIPW;
335 xstrncpy(orig_file, PASSWD_FILE, sizeof(orig_file));
336 }
337
338 while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) {
339 switch (c) {
340 case 'V':
341 print_version(EXIT_SUCCESS);
342 case 'h':
343 usage();
344 default:
345 errtryhelp(EXIT_FAILURE);
346 }
347 }
348
349 edit_file(0);
350
351 if (program == VIGR)
352 xstrncpy(orig_file, SGROUP_FILE, sizeof(orig_file));
353 else
354 xstrncpy(orig_file, SHADOW_FILE, sizeof(orig_file));
355
356 if (access(orig_file, F_OK) == 0) {
357 char response[80];
358
359 fputs((program == VIGR)
360 ? _("You are using shadow groups on this system.\n")
361 : _("You are using shadow passwords on this system.\n"), stdout);
362
363 /* TRANSLATORS: this program uses for y and n rpmatch(3),
364 * which means they can be translated. */
365 printf(_("Would you like to edit %s now [y/n]? "), orig_file);
366
367 if (fgets(response, sizeof(response), stdin) &&
368 rpmatch(response) == RPMATCH_YES)
369 edit_file(1);
370 }
371 exit(EXIT_SUCCESS);
372 }