]> git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/vipw.c
textual: add a docstring to most of the utilities
[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
65 #include "c.h"
66 #include "fileutils.h"
67 #include "closestream.h"
68 #include "nls.h"
69 #include "setpwnam.h"
70 #include "strutils.h"
71 #include "xalloc.h"
72 #include "rpmatch.h"
73
74 #ifdef HAVE_LIBSELINUX
75 # include <selinux/selinux.h>
76 #endif
77
78 #define FILENAMELEN 67
79
80 enum {
81 VIPW,
82 VIGR
83 };
84 int program;
85 char orig_file[FILENAMELEN]; /* original file /etc/passwd or /etc/group */
86 char *tmp_file; /* tmp file */
87
88 void pw_error __P((char *, int, int));
89
90 static void copyfile(int from, int to)
91 {
92 int nr, nw, off;
93 char buf[8 * 1024];
94
95 while ((nr = read(from, buf, sizeof(buf))) > 0)
96 for (off = 0; off < nr; nr -= nw, off += nw)
97 if ((nw = write(to, buf + off, nr)) < 0)
98 pw_error(tmp_file, 1, 1);
99
100 if (nr < 0)
101 pw_error(orig_file, 1, 1);
102 }
103
104 static void pw_init(void)
105 {
106 struct rlimit rlim;
107
108 /* Unlimited resource limits. */
109 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
110 (void)setrlimit(RLIMIT_CPU, &rlim);
111 (void)setrlimit(RLIMIT_FSIZE, &rlim);
112 (void)setrlimit(RLIMIT_STACK, &rlim);
113 (void)setrlimit(RLIMIT_DATA, &rlim);
114 (void)setrlimit(RLIMIT_RSS, &rlim);
115
116 /* Don't drop core (not really necessary, but GP's). */
117 rlim.rlim_cur = rlim.rlim_max = 0;
118 (void)setrlimit(RLIMIT_CORE, &rlim);
119
120 /* Turn off signals. */
121 (void)signal(SIGALRM, SIG_IGN);
122 (void)signal(SIGHUP, SIG_IGN);
123 (void)signal(SIGINT, SIG_IGN);
124 (void)signal(SIGPIPE, SIG_IGN);
125 (void)signal(SIGQUIT, SIG_IGN);
126 (void)signal(SIGTERM, SIG_IGN);
127 (void)signal(SIGTSTP, SIG_IGN);
128 (void)signal(SIGTTOU, SIG_IGN);
129
130 /* Create with exact permissions. */
131 (void)umask(0);
132 }
133
134 static FILE * pw_tmpfile(int lockfd)
135 {
136 FILE *fd;
137 char *tmpname = NULL;
138 char *dir = "/etc";
139
140 if ((fd = xfmkstemp(&tmpname, dir)) == NULL) {
141 ulckpwdf();
142 err(EXIT_FAILURE, _("can't open temporary file"));
143 }
144
145 copyfile(lockfd, fileno(fd));
146 tmp_file = tmpname;
147 return fd;
148 }
149
150 static void pw_write(void)
151 {
152 char tmp[FILENAMELEN + 4];
153
154 sprintf(tmp, "%s%s", orig_file, ".OLD");
155 unlink(tmp);
156
157 if (link(orig_file, tmp))
158 warn(_("%s: create a link to %s failed"), orig_file, tmp);
159
160 #ifdef HAVE_LIBSELINUX
161 if (is_selinux_enabled() > 0) {
162 security_context_t passwd_context = NULL;
163 int ret = 0;
164 if (getfilecon(orig_file, &passwd_context) < 0) {
165 warnx(_("Can't get context for %s"), orig_file);
166 pw_error(orig_file, 1, 1);
167 }
168 ret = setfilecon(tmp_file, passwd_context);
169 freecon(passwd_context);
170 if (ret != 0) {
171 warnx(_("Can't set context for %s"), tmp_file);
172 pw_error(tmp_file, 1, 1);
173 }
174 }
175 #endif
176
177 if (rename(tmp_file, orig_file) == -1) {
178 int errsv = errno;
179 errx(EXIT_FAILURE,
180 ("cannot write %s: %s (your changes are still in %s)"),
181 orig_file, strerror(errsv), tmp_file);
182 }
183 unlink(tmp_file);
184 free(tmp_file);
185 }
186
187 static void pw_edit(void)
188 {
189 int pstat;
190 pid_t pid;
191 char *p, *editor, *tk;
192
193 editor = getenv("EDITOR");
194 editor = xstrdup(editor ? editor : _PATH_VI);
195
196 tk = strtok(editor, " \t");
197 if (tk && (p = strrchr(tk, '/')) != NULL)
198 ++p;
199 else
200 p = editor;
201
202 pid = fork();
203 if (pid < 0)
204 err(EXIT_FAILURE, _("fork failed"));
205
206 if (!pid) {
207 execlp(editor, p, tmp_file, NULL);
208 /* Shouldn't get here */
209 _exit(EXIT_FAILURE);
210 }
211 for (;;) {
212 pid = waitpid(pid, &pstat, WUNTRACED);
213 if (WIFSTOPPED(pstat)) {
214 /* the editor suspended, so suspend us as well */
215 kill(getpid(), SIGSTOP);
216 kill(pid, SIGCONT);
217 } else {
218 break;
219 }
220 }
221 if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
222 pw_error(editor, 1, 1);
223
224 free(editor);
225 }
226
227 void __attribute__((__noreturn__))
228 pw_error(char *name, int err, int eval)
229 {
230 if (err) {
231 if (name)
232 warn("%s: ", name);
233 else
234 warn(NULL);
235 }
236 warnx(_("%s unchanged"), orig_file);
237 unlink(tmp_file);
238 ulckpwdf();
239 exit(eval);
240 }
241
242 static void edit_file(int is_shadow)
243 {
244 struct stat begin, end;
245 int passwd_file, ch_ret;
246 FILE *tmp_fd;
247
248 pw_init();
249
250 /* acquire exclusive lock */
251 if (lckpwdf() < 0)
252 err(EXIT_FAILURE, _("cannot get lock"));
253
254 passwd_file = open(orig_file, O_RDONLY, 0);
255 if (passwd_file < 0)
256 err(EXIT_FAILURE, _("cannot open %s"), orig_file);
257 tmp_fd = pw_tmpfile(passwd_file);
258
259 if (fstat(fileno(tmp_fd), &begin))
260 pw_error(tmp_file, 1, 1);
261
262 pw_edit();
263
264 if (fstat(fileno(tmp_fd), &end))
265 pw_error(tmp_file, 1, 1);
266 /* Some editors, such as Vim with 'writebackup' mode enabled,
267 * use "atomic save" in which the old file is deleted and a new
268 * one with the same name created in its place. */
269 if (end.st_nlink == 0) {
270 if (close_stream(tmp_fd) != 0)
271 err(EXIT_FAILURE, _("write error"));
272 tmp_fd = fopen(tmp_file, "r");
273 if (!tmp_file)
274 err(EXIT_FAILURE, _("cannot open %s"), tmp_file);
275 if (fstat(fileno(tmp_fd), &end))
276 pw_error(tmp_file, 1, 1);
277 }
278 if (begin.st_mtime == end.st_mtime) {
279 warnx(_("no changes made"));
280 pw_error((char *)NULL, 0, 0);
281 }
282 /* pw_tmpfile() will create the file with mode 600 */
283 if (!is_shadow)
284 ch_ret = fchmod(fileno(tmp_fd), 0644);
285 else
286 ch_ret = fchmod(fileno(tmp_fd), 0400);
287 if (ch_ret < 0)
288 err(EXIT_FAILURE, "%s: %s", _("cannot chmod file"), orig_file);
289 if (close_stream(tmp_fd) != 0)
290 err(EXIT_FAILURE, _("write error"));
291 pw_write();
292 close(passwd_file);
293 ulckpwdf();
294 }
295
296 static void __attribute__((__noreturn__)) usage(FILE *out)
297 {
298 fputs(USAGE_HEADER, out);
299 fprintf(out, " %s\n", program_invocation_short_name);
300
301 fputs(USAGE_SEPARATOR, out);
302 fputs(_("Edit the password or group file.\n"), out);
303
304 fputs(USAGE_OPTIONS, out);
305 fputs(USAGE_HELP, out);
306 fputs(USAGE_VERSION, out);
307 fprintf(out, USAGE_MAN_TAIL("vipw(8)"));
308 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
309 }
310
311 int main(int argc, char *argv[])
312 {
313 setlocale(LC_ALL, "");
314 bindtextdomain(PACKAGE, LOCALEDIR);
315 textdomain(PACKAGE);
316 atexit(close_stdout);
317
318 if (!strcmp(program_invocation_short_name, "vigr")) {
319 program = VIGR;
320 xstrncpy(orig_file, GROUP_FILE, sizeof(orig_file));
321 } else {
322 program = VIPW;
323 xstrncpy(orig_file, PASSWD_FILE, sizeof(orig_file));
324 }
325
326 if (1 < argc) {
327 if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
328 printf(UTIL_LINUX_VERSION);
329 exit(EXIT_SUCCESS);
330 }
331 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
332 usage(stdout);
333 usage(stderr);
334 }
335
336 edit_file(0);
337
338 if (program == VIGR) {
339 strncpy(orig_file, SGROUP_FILE, FILENAMELEN - 1);
340 } else {
341 strncpy(orig_file, SHADOW_FILE, FILENAMELEN - 1);
342 }
343
344 if (access(orig_file, F_OK) == 0) {
345 char response[80];
346
347 printf((program == VIGR)
348 ? _("You are using shadow groups on this system.\n")
349 : _("You are using shadow passwords on this system.\n"));
350 /* TRANSLATORS: this program uses for y and n rpmatch(3),
351 * which means they can be translated. */
352 printf(_("Would you like to edit %s now [y/n]? "), orig_file);
353
354 if (fgets(response, sizeof(response), stdin)) {
355 if (rpmatch(response) == 1)
356 edit_file(1);
357 }
358 }
359 exit(EXIT_SUCCESS);
360 }