]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/vipw.c
2 * Copyright (c) 1987 Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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>
36 * Updated Thu Nov 9 21:58:53 1995 by Martin Schulze
37 * <joey@finlandia.infodrom.north.de>. Support for vigr.
39 * Martin Schulze's patches adapted to Util-Linux by Nicolai Langfeldt.
41 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
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
47 static char version_string
[] = "vipw 1.4";
49 #include <sys/types.h>
55 #include <sys/param.h>
58 #include <sys/resource.h>
69 #define FILENAMELEN 67
74 char orig_file
[FILENAMELEN
]; /* original file /etc/passwd or /etc/group */
75 char tmp_file
[FILENAMELEN
]; /* tmp file */
76 char tmptmp_file
[FILENAMELEN
]; /* very tmp file */
78 void pw_error
__P((char *, int, int));
81 copyfile(int from
, int to
) {
85 while ((nr
= read(from
, buf
, sizeof(buf
))) > 0)
86 for (off
= 0; off
< nr
; nr
-= nw
, off
+= nw
)
87 if ((nw
= write(to
, buf
+ off
, nr
)) < 0)
88 pw_error(tmp_file
, 1, 1);
91 pw_error(orig_file
, 1, 1);
99 /* Unlimited resource limits. */
100 rlim
.rlim_cur
= rlim
.rlim_max
= RLIM_INFINITY
;
101 (void)setrlimit(RLIMIT_CPU
, &rlim
);
102 (void)setrlimit(RLIMIT_FSIZE
, &rlim
);
103 (void)setrlimit(RLIMIT_STACK
, &rlim
);
104 (void)setrlimit(RLIMIT_DATA
, &rlim
);
105 (void)setrlimit(RLIMIT_RSS
, &rlim
);
107 /* Don't drop core (not really necessary, but GP's). */
108 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
109 (void)setrlimit(RLIMIT_CORE
, &rlim
);
111 /* Turn off signals. */
112 (void)signal(SIGALRM
, SIG_IGN
);
113 (void)signal(SIGHUP
, SIG_IGN
);
114 (void)signal(SIGINT
, SIG_IGN
);
115 (void)signal(SIGPIPE
, SIG_IGN
);
116 (void)signal(SIGQUIT
, SIG_IGN
);
117 (void)signal(SIGTERM
, SIG_IGN
);
118 (void)signal(SIGTSTP
, SIG_IGN
);
119 (void)signal(SIGTTOU
, SIG_IGN
);
121 /* Create with exact permissions. */
130 * If the password file doesn't exist, the system is hosed.
131 * Might as well try to build one. Set the close-on-exec bit so
132 * that users can't get at the encrypted passwords while editing.
133 * Open should allow flock'ing the file; see 4.4BSD. XXX
135 lockfd
= open(orig_file
, O_RDONLY
, 0);
138 (void)fprintf(stderr
, "%s: %s: %s\n",
139 progname
, orig_file
, strerror(errno
));
142 #if 0 /* flock()ing is superfluous here, with the ptmp/ptmptmp system. */
143 if (flock(lockfd
, LOCK_EX
|LOCK_NB
)) {
144 (void)fprintf(stderr
,
145 _("%s: the %s file is busy.\n"), progname
,
146 program
== VIPW
? "password" : "group" );
151 if ((fd
= open(tmptmp_file
, O_WRONLY
|O_CREAT
, 0644)) == -1) {
152 (void)fprintf(stderr
,
153 "%s: %s: %s\n", progname
, tmptmp_file
, strerror(errno
));
156 ret
= link(tmptmp_file
, tmp_file
);
157 (void)unlink(tmptmp_file
);
160 (void)fprintf(stderr
,
161 _("%s: the %s file is busy (%s present)\n"),
163 program
== VIPW
? "password" : "group",
167 (void)fprintf(stderr
, _("%s: can't link %s: %s\n"), progname
,
168 tmp_file
, strerror(errsv
));
172 copyfile(lockfd
, fd
);
180 char tmp
[FILENAMELEN
];
182 sprintf(tmp
, "%s%s", orig_file
, ".OLD");
184 link(orig_file
, tmp
);
185 if (rename(tmp_file
, orig_file
) == -1) {
187 (void)fprintf(stderr
,
188 _("%s: can't unlock %s: %s (your changes are still in %s)\n"),
189 progname
, orig_file
, strerror(errsv
), tmp_file
);
192 (void)unlink(tmp_file
);
197 pw_edit(int notsetuid
) {
202 if (!(editor
= getenv("EDITOR")))
203 editor
= strdup(_PATH_VI
); /* adia@egnatia.ee.auth.gr */
204 if ((p
= strrchr(strtok(editor
," \t"), '/')) != NULL
)
211 (void)fprintf(stderr
, _("%s: Cannot fork\n"), progname
);
216 (void)setgid(getgid());
217 (void)setuid(getuid());
219 execlp(editor
, p
, tmp_file
, NULL
);
223 pid
= waitpid(pid
, &pstat
, WUNTRACED
);
224 if (WIFSTOPPED(pstat
)) {
225 /* the editor suspended, so suspend us as well */
226 kill(getpid(), SIGSTOP
);
232 if (pid
== -1 || !WIFEXITED(pstat
) || WEXITSTATUS(pstat
) != 0)
233 pw_error(editor
, 1, 1);
237 pw_error(name
, err
, eval
)
245 (void)fprintf(stderr
, "%s: ", progname
);
247 (void)fprintf(stderr
, "%s: ", name
);
248 (void)fprintf(stderr
, "%s\n", strerror(sverrno
));
250 (void)fprintf(stderr
,
251 _("%s: %s unchanged\n"), progname
, orig_file
);
252 (void)unlink(tmp_file
);
256 int main(int argc
, char *argv
[])
258 struct stat begin
, end
;
260 setlocale(LC_ALL
, "");
261 bindtextdomain(PACKAGE
, LOCALEDIR
);
264 bzero(tmp_file
, FILENAMELEN
);
265 progname
= (rindex(argv
[0], '/')) ? rindex(argv
[0], '/') + 1 : argv
[0];
266 if (!strcmp(progname
, "vigr")) {
268 strncpy(orig_file
, GROUP_FILE
, FILENAMELEN
-1);
269 strncpy(tmp_file
, GTMP_FILE
, FILENAMELEN
-1);
270 strncpy(tmptmp_file
, GTMPTMP_FILE
, FILENAMELEN
-1);
274 strncpy(orig_file
, PASSWD_FILE
, FILENAMELEN
-1);
275 strncpy(tmp_file
, PTMP_FILE
, FILENAMELEN
-1);
276 strncpy(tmptmp_file
, PTMPTMP_FILE
, FILENAMELEN
-1);
280 (!strcmp(argv
[1], "-V") || !strcmp(argv
[1], "--version"))) {
281 printf("%s\n", version_string
);
288 if (stat(tmp_file
, &begin
))
289 pw_error(tmp_file
, 1, 1);
291 if (stat(tmp_file
, &end
))
292 pw_error(tmp_file
, 1, 1);
293 if (begin
.st_mtime
== end
.st_mtime
) {
294 (void)fprintf(stderr
, _("%s: no changes made\n"), progname
);
295 pw_error((char *)NULL
, 0, 0);