Upload Tizen:Base source
[framework/base/util-linux-ng.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 static char version_string[] = "vipw 1.4";
48
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <sys/param.h>
56 #include <sys/time.h>
57 #include <sys/wait.h>
58 #include <sys/resource.h>
59 #include <sys/file.h>
60 #include <signal.h>
61 #include <fcntl.h>
62 #include <errno.h>
63 #include <paths.h>
64 #include <unistd.h>
65
66 #include "setpwnam.h"
67 #include "xstrncpy.h"
68 #include "nls.h"
69
70 #ifdef HAVE_LIBSELINUX
71 #include <selinux/selinux.h>
72 #endif
73
74 #define FILENAMELEN 67
75
76 char *progname;
77 enum { VIPW, VIGR };
78 int program;
79 char orig_file[FILENAMELEN];   /* original file /etc/passwd or /etc/group */
80 char tmp_file[FILENAMELEN];    /* tmp file */
81 char tmptmp_file[FILENAMELEN]; /* very tmp file */
82
83 void pw_error __P((char *, int, int));
84
85 static void
86 copyfile(int from, int to) {
87         int nr, nw, off;
88         char buf[8*1024];
89         
90         while ((nr = read(from, buf, sizeof(buf))) > 0)
91                 for (off = 0; off < nr; nr -= nw, off += nw)
92                         if ((nw = write(to, buf + off, nr)) < 0)
93                           pw_error(tmp_file, 1, 1);
94
95         if (nr < 0)
96           pw_error(orig_file, 1, 1);
97 }
98
99
100 static void
101 pw_init(void) {
102         struct rlimit rlim;
103
104         /* Unlimited resource limits. */
105         rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
106         (void)setrlimit(RLIMIT_CPU, &rlim);
107         (void)setrlimit(RLIMIT_FSIZE, &rlim);
108         (void)setrlimit(RLIMIT_STACK, &rlim);
109         (void)setrlimit(RLIMIT_DATA, &rlim);
110         (void)setrlimit(RLIMIT_RSS, &rlim);
111
112         /* Don't drop core (not really necessary, but GP's). */
113         rlim.rlim_cur = rlim.rlim_max = 0;
114         (void)setrlimit(RLIMIT_CORE, &rlim);
115
116         /* Turn off signals. */
117         (void)signal(SIGALRM, SIG_IGN);
118         (void)signal(SIGHUP, SIG_IGN);
119         (void)signal(SIGINT, SIG_IGN);
120         (void)signal(SIGPIPE, SIG_IGN);
121         (void)signal(SIGQUIT, SIG_IGN);
122         (void)signal(SIGTERM, SIG_IGN);
123         (void)signal(SIGTSTP, SIG_IGN);
124         (void)signal(SIGTTOU, SIG_IGN);
125
126         /* Create with exact permissions. */
127         (void)umask(0);
128 }
129
130 static int
131 pw_lock(void) {
132         int lockfd, fd, ret;
133
134         /* 
135          * If the password file doesn't exist, the system is hosed.
136          * Might as well try to build one.  Set the close-on-exec bit so
137          * that users can't get at the encrypted passwords while editing.
138          * Open should allow flock'ing the file; see 4.4BSD.    XXX
139          */
140 #if 0 /* flock()ing is superfluous here, with the ptmp/ptmptmp system. */
141         if (flock(lockfd, LOCK_EX|LOCK_NB)) {
142                 if (program == VIPW)
143                         fprintf(stderr, _("%s: the password file is busy.\n"),
144                                 progname);
145                 else
146                         fprintf(stderr, _("%s: the group file is busy.\n"),
147                                 progname);
148                 exit(1);
149         }
150 #endif
151
152         if ((fd = open(tmptmp_file, O_WRONLY|O_CREAT, 0600)) == -1) {
153           (void)fprintf(stderr,
154                    "%s: %s: %s\n", progname, tmptmp_file, strerror(errno));
155           exit(1);
156         }
157         ret = link(tmptmp_file, tmp_file);
158         (void)unlink(tmptmp_file);
159         if (ret == -1) {
160             if (errno == EEXIST)
161                 (void)fprintf(stderr, 
162                               _("%s: the %s file is busy (%s present)\n"),
163                               progname,
164                               program == VIPW ? "password" : "group",
165                               tmp_file);
166             else {
167                 int errsv = errno;
168                 (void)fprintf(stderr, _("%s: can't link %s: %s\n"), progname,
169                               tmp_file, strerror(errsv));
170             }
171             exit(1);
172         }
173
174         lockfd = open(orig_file, O_RDONLY, 0);
175
176         if (lockfd < 0) {
177                 (void)fprintf(stderr, "%s: %s: %s\n",
178                     progname, orig_file, strerror(errno));
179                 unlink(tmp_file);
180                 exit(1);
181         }
182
183         copyfile(lockfd, fd);
184         (void)close(lockfd);
185         (void)close(fd);
186         return(1);
187 }
188
189 static void
190 pw_unlock(void) {
191         char tmp[FILENAMELEN+4];
192   
193         sprintf(tmp, "%s%s", orig_file, ".OLD");
194         unlink(tmp);
195         link(orig_file, tmp);
196
197 #ifdef HAVE_LIBSELINUX
198         if (is_selinux_enabled() > 0) {
199           security_context_t passwd_context=NULL;
200           int ret=0;
201           if (getfilecon(orig_file,&passwd_context) < 0) {
202             (void) fprintf(stderr,_("%s: Can't get context for %s"),progname,orig_file);
203             pw_error(orig_file, 1, 1);
204           }
205           ret=setfilecon(tmp_file,passwd_context);
206           freecon(passwd_context);
207           if (ret!=0) {
208             (void) fprintf(stderr,_("%s: Can't set context for %s"),progname,tmp_file);
209             pw_error(tmp_file, 1, 1);
210           }
211         }
212 #endif
213
214         if (rename(tmp_file, orig_file) == -1) {
215                 int errsv = errno;
216                 fprintf(stderr, 
217                         _("%s: can't unlock %s: %s (your changes are still in %s)\n"), 
218                         progname, orig_file, strerror(errsv), tmp_file);
219                 exit(1);
220         }
221         unlink(tmp_file);
222 }
223
224
225 static void
226 pw_edit(int notsetuid) {
227         int pstat;
228         pid_t pid;
229         char *p, *editor;
230
231         if (!(editor = getenv("EDITOR")))
232                 editor = strdup(_PATH_VI); /* adia@egnatia.ee.auth.gr */
233         if ((p = strrchr(strtok(editor," \t"), '/')) != NULL)
234                 ++p;
235         else 
236                 p = editor;
237
238         pid = fork();
239         if (pid < 0) {
240                 (void)fprintf(stderr, _("%s: Cannot fork\n"), progname);
241                 exit(1);
242         }
243         if (!pid) {
244                 if (notsetuid) {
245                         (void)setgid(getgid());
246                         (void)setuid(getuid());
247                 }
248                 execlp(editor, p, tmp_file, NULL);
249                 _exit(1);
250         }
251         for (;;) {
252             pid = waitpid(pid, &pstat, WUNTRACED);
253             if (WIFSTOPPED(pstat)) {
254                 /* the editor suspended, so suspend us as well */
255                 kill(getpid(), SIGSTOP);
256                 kill(pid, SIGCONT);
257             } else {
258                 break;
259             }
260         }
261         if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
262                 pw_error(editor, 1, 1);
263 }
264
265 void
266 pw_error(char *name, int err, int eval) {
267         if (err) {
268                 int sverrno = errno;
269
270                 fprintf(stderr, "%s: ", progname);
271                 if (name)
272                         (void)fprintf(stderr, "%s: ", name);
273                 fprintf(stderr, "%s\n", strerror(sverrno));
274         }
275         fprintf(stderr,
276             _("%s: %s unchanged\n"), progname, orig_file);
277         unlink(tmp_file);
278         exit(eval);
279 }
280
281 static void
282 edit_file(int is_shadow)
283 {
284         struct stat begin, end;
285
286         pw_init();
287         pw_lock();
288
289         if (stat(tmp_file, &begin))
290                 pw_error(tmp_file, 1, 1);
291
292         pw_edit(0);
293
294         if (stat(tmp_file, &end))
295                 pw_error(tmp_file, 1, 1);
296         if (begin.st_mtime == end.st_mtime) {
297                 (void)fprintf(stderr, _("%s: no changes made\n"), progname);
298                 pw_error((char *)NULL, 0, 0);
299         }
300         /* see pw_lock() where we create the file with mode 600 */
301         if (!is_shadow)
302                 chmod(tmp_file, 0644);
303         else
304                 chmod(tmp_file, 0400);
305         pw_unlock();
306 }
307
308 int main(int argc, char *argv[]) {
309
310         setlocale(LC_ALL, "");
311         bindtextdomain(PACKAGE, LOCALEDIR);
312         textdomain(PACKAGE);
313
314         memset(tmp_file, '\0', FILENAMELEN);
315         progname = (strrchr(argv[0], '/')) ? strrchr(argv[0], '/') + 1 : argv[0];
316         if (!strcmp(progname, "vigr")) {
317                 program = VIGR;
318                 xstrncpy(orig_file, GROUP_FILE, sizeof(orig_file));
319                 xstrncpy(tmp_file, GTMP_FILE, sizeof(tmp_file));
320                 xstrncpy(tmptmp_file, GTMPTMP_FILE, sizeof(tmptmp_file));
321         } else {
322                 program = VIPW;
323                 xstrncpy(orig_file, PASSWD_FILE, sizeof(orig_file));
324                 xstrncpy(tmp_file, PTMP_FILE, sizeof(tmp_file));
325                 xstrncpy(tmptmp_file, PTMPTMP_FILE, sizeof(tmptmp_file));
326         }
327
328         if ((argc > 1) && 
329             (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
330                 printf("%s\n", version_string);
331                 exit(0);
332         }
333
334         edit_file(0);
335
336         if (program == VIGR) {
337                 strncpy(orig_file, SGROUP_FILE, FILENAMELEN-1);
338                 strncpy(tmp_file, SGTMP_FILE, FILENAMELEN-1);
339                 strncpy(tmptmp_file, SGTMPTMP_FILE, FILENAMELEN-1);
340         } else {
341                 strncpy(orig_file, SHADOW_FILE, FILENAMELEN-1);
342                 strncpy(tmp_file, SPTMP_FILE, FILENAMELEN-1);
343                 strncpy(tmptmp_file, SPTMPTMP_FILE, FILENAMELEN-1);
344         }
345
346         if (access(orig_file, F_OK) == 0) {
347                 char response[80];
348
349                 printf((program == VIGR)
350                        ? _("You are using shadow groups on this system.\n")
351                        : _("You are using shadow passwords on this system.\n"));
352                 printf(_("Would you like to edit %s now [y/n]? "), orig_file);
353
354                 /* EOF means no */
355                 if (fgets(response, sizeof(response), stdin)) {
356                         if (response[0] == 'y' || response[0] == 'Y')
357                                 edit_file(1);
358                 }
359         }
360
361         exit(0);
362 }