]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - login-utils/vipw.c
docs: update year in libs docs
[thirdparty/util-linux.git] / login-utils / vipw.c
index 6eff4f3b352ca5798c1f16cdb6aad069c339b322..a071b639dd03e1926c07405941de64479bd62a8e 100644 (file)
  * Updated Thu Oct 12 09:56:55 1995 by faith@cs.unc.edu with security
  * patches from Zefram <A.Main@dcs.warwick.ac.uk>
  *
- * Updated Thu Nov  9 21:58:53 1995 by Martin Schulze 
+ * Updated Thu Nov  9 21:58:53 1995 by Martin Schulze
  * <joey@finlandia.infodrom.north.de>.  Support for vigr.
  *
  * Martin Schulze's patches adapted to Util-Linux by Nicolai Langfeldt.
+ *
+ * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
+ * - added Native Language Support
+ * Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
+ * - fixed strerr(errno) in gettext calls
  */
 
-#ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1987 Regents of the University of California.\n\
- All rights reserved.\n";
-#endif /* not lint */
-
-#ifndef lint
-/*static char sccsid[] = "from: @(#)vipw.c     5.16 (Berkeley) 3/3/91";*/
-static char rcsid[] = "$Id: vipw.c,v 1.6 1997/07/06 00:12:23 aebr Exp $";
-#endif /* not lint */
-
-static char version_string[] = "vipw 1.4";
-
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <paths.h>
 #include <pwd.h>
+#include <shadow.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/file.h>
 #include <sys/param.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
 #include <sys/time.h>
+#include <sys/types.h>
 #include <sys/wait.h>
-#include <sys/resource.h>
-#include <sys/file.h>
-#include <signal.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <paths.h>
 #include <unistd.h>
+#include <getopt.h>
 
+#include "c.h"
+#include "fileutils.h"
+#include "closestream.h"
+#include "nls.h"
 #include "setpwnam.h"
+#include "strutils.h"
+#include "xalloc.h"
+#include "rpmatch.h"
+
+#ifdef HAVE_LIBSELINUX
+# include <selinux/selinux.h>
+#endif
 
 #define FILENAMELEN 67
 
-char *progname;
-enum { VIPW, VIGR };
-int program;
-char orig_file[FILENAMELEN];   /* original file /etc/passwd or /etc/group */
-char tmp_file[FILENAMELEN];    /* tmp file */
-char tmptmp_file[FILENAMELEN]; /* very tmp file */
+enum {
+       VIPW,
+       VIGR
+};
+static int program;
+static char orig_file[FILENAMELEN];    /* original file /etc/passwd or /etc/group */
+static char *tmp_file;                 /* tmp file */
 
-void pw_error __P((char *, int, int));
+void pw_error (char *, int, int);
 
-void
-copyfile(from, to)
-       register int from, to;
+static void copyfile(int from, int to)
 {
-       register int nr, nw, off;
-       char buf[8*1024];
-       
+       int nr, nw, off;
+       char buf[8 * 1024];
+
        while ((nr = read(from, buf, sizeof(buf))) > 0)
                for (off = 0; off < nr; nr -= nw, off += nw)
                        if ((nw = write(to, buf + off, nr)) < 0)
-                         pw_error(tmp_file, 1, 1);
+                               pw_error(tmp_file, 1, 1);
 
        if (nr < 0)
-         pw_error(orig_file, 1, 1);
+               pw_error(orig_file, 1, 1);
+#ifdef HAVE_EXPLICIT_BZERO
+       explicit_bzero(buf, sizeof(buf));
+#endif
 }
 
-
-void
-pw_init()
+static void pw_init(void)
 {
        struct rlimit rlim;
 
@@ -130,168 +135,238 @@ pw_init()
        (void)umask(0);
 }
 
-int
-pw_lock()
+static FILE * pw_tmpfile(int lockfd)
 {
-       int lockfd, fd, ret;
-
-       /* 
-        * If the password file doesn't exist, the system is hosed.
-        * Might as well try to build one.  Set the close-on-exec bit so
-        * that users can't get at the encrypted passwords while editing.
-        * Open should allow flock'ing the file; see 4.4BSD.    XXX
-        */
-       lockfd = open(orig_file, O_RDONLY, 0);
-
-       if (lockfd < 0) {
-               (void)fprintf(stderr, "%s: %s: %s\n",
-                   progname, orig_file, strerror(errno));
-               exit(1);
-       }
-#if 0 /* flock()ing is superfluous here, with the ptmp/ptmptmp system. */
-       if (flock(lockfd, LOCK_EX|LOCK_NB)) {
-               (void)fprintf(stderr,
-                   "%s: the %s file is busy.\n", progname, 
-                             program == VIPW ? "password" : "group" );
-               exit(1);
-       }
-#endif
+       FILE *fd;
+       char *tmpname = NULL;
 
-       if ((fd = open(tmptmp_file, O_WRONLY|O_CREAT, 0644)) == -1) {
-         (void)fprintf(stderr,
-                  "%s: %s: %s\n", progname, tmptmp_file, strerror(errno));
-         exit(1);
+       if ((fd = xfmkstemp(&tmpname, "/etc", ".vipw")) == NULL) {
+               ulckpwdf();
+               err(EXIT_FAILURE, _("can't open temporary file"));
        }
-       ret = link(tmptmp_file, tmp_file);
-       (void)unlink(tmptmp_file);
-       if (ret == -1) {
-           if (errno == EEXIST)
-               (void)fprintf(stderr, 
-                             "%s: the %s file is busy\n", progname,
-                             program == VIPW ? "password" : "group" );
-           else
-               (void)fprintf(stderr, "%s: can't link %s: %s\n", progname,
-                             tmp_file, strerror(errno));
-           exit(1);
-       }
-       copyfile(lockfd, fd);
-       (void)close(lockfd);
-       (void)close(fd);
-       return(1);
+
+       copyfile(lockfd, fileno(fd));
+       tmp_file = tmpname;
+       return fd;
 }
 
-void
-pw_unlock()
+static void pw_write(void)
 {
-  char tmp[FILENAMELEN];
-  
-  sprintf(tmp, "%s%s", orig_file, ".OLD");
-  unlink(tmp);
-  link(orig_file, tmp);
-  if (rename(tmp_file, orig_file) == -1) {
-    (void)fprintf(stderr, 
-                 "%s: can't unlock %s: %s (your changes are still in %s)\n", 
-                 progname, orig_file, strerror(errno), tmp_file);
-    exit(1);
-  }
-  (void)unlink(tmp_file);
-}
+       char tmp[FILENAMELEN + 4];
+
+       sprintf(tmp, "%s%s", orig_file, ".OLD");
+       unlink(tmp);
+
+       if (link(orig_file, tmp))
+               warn(_("%s: create a link to %s failed"), orig_file, tmp);
+
+#ifdef HAVE_LIBSELINUX
+       if (is_selinux_enabled() > 0) {
+               security_context_t passwd_context = NULL;
+               int ret = 0;
+               if (getfilecon(orig_file, &passwd_context) < 0) {
+                       warnx(_("Can't get context for %s"), orig_file);
+                       pw_error(orig_file, 1, 1);
+               }
+               ret = setfilecon(tmp_file, passwd_context);
+               freecon(passwd_context);
+               if (ret != 0) {
+                       warnx(_("Can't set context for %s"), tmp_file);
+                       pw_error(tmp_file, 1, 1);
+               }
+       }
+#endif
 
+       if (rename(tmp_file, orig_file) == -1) {
+               int errsv = errno;
+               errx(EXIT_FAILURE,
+                    ("cannot write %s: %s (your changes are still in %s)"),
+                    orig_file, strerror(errsv), tmp_file);
+       }
+       unlink(tmp_file);
+       free(tmp_file);
+       tmp_file = NULL;
+}
 
-void
-pw_edit(notsetuid)
-       int notsetuid;
+static void pw_edit(void)
 {
        int pstat;
        pid_t pid;
-       char *p, *editor;
+       char *p, *editor, *tk;
+
+       editor = getenv("EDITOR");
+       editor = xstrdup(editor ? editor : _PATH_VI);
 
-       if (!(editor = getenv("EDITOR")))
-               editor = _PATH_VI;
-       if ((p = strrchr(strtok(editor," \t"), '/')) != NULL)
+       tk = strtok(editor, " \t");
+       if (tk && (p = strrchr(tk, '/')) != NULL)
                ++p;
-       else 
+       else
                p = editor;
 
-       if (!(pid = vfork())) {
-               if (notsetuid) {
-                       (void)setgid(getgid());
-                       (void)setuid(getuid());
-               }
+       pid = fork();
+       if (pid < 0)
+               err(EXIT_FAILURE, _("fork failed"));
+
+       if (!pid) {
                execlp(editor, p, tmp_file, NULL);
-               _exit(1);
+               errexec(editor);
        }
        for (;;) {
-           pid = waitpid(pid, &pstat, WUNTRACED);
-           if (WIFSTOPPED(pstat)) {
-               /* the editor suspended, so suspend us as well */
-               kill(getpid(), SIGSTOP);
-               kill(pid, SIGCONT);
-           } else {
-               break;
-           }
+               pid = waitpid(pid, &pstat, WUNTRACED);
+               if (WIFSTOPPED(pstat)) {
+                       /* the editor suspended, so suspend us as well */
+                       kill(getpid(), SIGSTOP);
+                       kill(pid, SIGCONT);
+               } else {
+                       break;
+               }
        }
        if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
                pw_error(editor, 1, 1);
+
+       free(editor);
 }
 
-void
-pw_error(name, err, eval)
-       char *name;
-       int err, eval;
+void __attribute__((__noreturn__))
+pw_error(char *name, int err, int eval)
 {
-       int sverrno;
-
        if (err) {
-               sverrno = errno;
-               (void)fprintf(stderr, "%s: ", progname);
                if (name)
-                       (void)fprintf(stderr, "%s: ", name);
-               (void)fprintf(stderr, "%s\n", strerror(sverrno));
+                       warn("%s: ", name);
+               else
+                       warn(NULL);
        }
-       (void)fprintf(stderr,
-           "%s: %s unchanged\n", progname, orig_file);
-       (void)unlink(tmp_file);
+       warnx(_("%s unchanged"), orig_file);
+
+       if (tmp_file)
+               unlink(tmp_file);
+       ulckpwdf();
        exit(eval);
 }
 
+static void edit_file(int is_shadow)
+{
+       struct stat begin, end;
+       int passwd_file, ch_ret;
+       FILE *tmp_fd;
+
+       pw_init();
+
+       /* acquire exclusive lock */
+       if (lckpwdf() < 0)
+               err(EXIT_FAILURE, _("cannot get lock"));
+
+       passwd_file = open(orig_file, O_RDONLY | O_CLOEXEC, 0);
+       if (passwd_file < 0)
+               err(EXIT_FAILURE, _("cannot open %s"), orig_file);
+       tmp_fd = pw_tmpfile(passwd_file);
+
+       if (fstat(fileno(tmp_fd), &begin))
+               pw_error(tmp_file, 1, 1);
+
+       pw_edit();
+
+       if (fstat(fileno(tmp_fd), &end))
+               pw_error(tmp_file, 1, 1);
+       /* Some editors, such as Vim with 'writebackup' mode enabled,
+        * use "atomic save" in which the old file is deleted and a new
+        * one with the same name created in its place.  */
+       if (end.st_nlink == 0) {
+               if (close_stream(tmp_fd) != 0)
+                       err(EXIT_FAILURE, _("write error"));
+               tmp_fd = fopen(tmp_file, "r" UL_CLOEXECSTR);
+               if (!tmp_fd)
+                       err(EXIT_FAILURE, _("cannot open %s"), tmp_file);
+               if (fstat(fileno(tmp_fd), &end))
+                       pw_error(tmp_file, 1, 1);
+       }
+       if (begin.st_mtime == end.st_mtime) {
+               warnx(_("no changes made"));
+               pw_error((char *)NULL, 0, 0);
+       }
+       /* pw_tmpfile() will create the file with mode 600 */
+       if (!is_shadow)
+               ch_ret = fchmod(fileno(tmp_fd), 0644);
+       else
+               ch_ret = fchmod(fileno(tmp_fd), 0400);
+       if (ch_ret < 0)
+               err(EXIT_FAILURE, "%s: %s", _("cannot chmod file"), orig_file);
+       if (close_stream(tmp_fd) != 0)
+               err(EXIT_FAILURE, _("write error"));
+       pw_write();
+       close(passwd_file);
+       ulckpwdf();
+}
+
+static void __attribute__((__noreturn__)) usage(void)
+{
+       FILE *out = stdout;
+       fputs(USAGE_HEADER, out);
+       fprintf(out, " %s\n", program_invocation_short_name);
+
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Edit the password or group file.\n"), out);
+
+       fputs(USAGE_OPTIONS, out);
+       printf(USAGE_HELP_OPTIONS(16));
+       printf(USAGE_MAN_TAIL("vipw(8)"));
+       exit(EXIT_SUCCESS);
+}
+
 int main(int argc, char *argv[])
 {
-  struct stat begin, end;
-
-  bzero(tmp_file, FILENAMELEN);
-  progname = (rindex(argv[0], '/')) ? rindex(argv[0], '/') + 1 : argv[0];
-  if (!strcmp(progname, "vigr")) {
-    program = VIGR;
-    strncpy(orig_file, GROUP_FILE, FILENAMELEN-1);
-    strncpy(tmp_file, GTMP_FILE, FILENAMELEN-1);
-    strncpy(tmptmp_file, GTMPTMP_FILE, FILENAMELEN-1);
-  }
-  else {
-    program = VIPW;
-    strncpy(orig_file, PASSWD_FILE, FILENAMELEN-1);
-    strncpy(tmp_file, PTMP_FILE, FILENAMELEN-1);
-    strncpy(tmptmp_file, PTMPTMP_FILE, FILENAMELEN-1);
-  }
-
-  if ((argc > 1) && 
-      (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
-    printf("%s\n", version_string);
-    exit(0);
-  }
-
-  pw_init();
-  pw_lock();
-
-  if (stat(tmp_file, &begin))
-    pw_error(tmp_file, 1, 1);
-  pw_edit(0);
-  if (stat(tmp_file, &end))
-    pw_error(tmp_file, 1, 1);
-  if (begin.st_mtime == end.st_mtime) {
-    (void)fprintf(stderr, "%s: no changes made\n", progname);
-    pw_error((char *)NULL, 0, 0);
-  }
-  pw_unlock();
-  exit(0);
+       int c;
+       static const struct option longopts[] = {
+               {"version", no_argument, NULL, 'V'},
+               {"help", no_argument, NULL, 'h'},
+               {NULL, 0, NULL, 0}
+       };
+
+       setlocale(LC_ALL, "");
+       bindtextdomain(PACKAGE, LOCALEDIR);
+       textdomain(PACKAGE);
+       close_stdout_atexit();
+
+       if (!strcmp(program_invocation_short_name, "vigr")) {
+               program = VIGR;
+               xstrncpy(orig_file, GROUP_FILE, sizeof(orig_file));
+       } else {
+               program = VIPW;
+               xstrncpy(orig_file, PASSWD_FILE, sizeof(orig_file));
+       }
+
+       while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) {
+               switch (c) {
+               case 'V':
+                       print_version(EXIT_SUCCESS);
+               case 'h':
+                       usage();
+               default:
+                       errtryhelp(EXIT_FAILURE);
+               }
+       }
+
+       edit_file(0);
+
+       if (program == VIGR)
+               xstrncpy(orig_file, SGROUP_FILE, sizeof(orig_file));
+       else
+               xstrncpy(orig_file, SHADOW_FILE, sizeof(orig_file));
+
+       if (access(orig_file, F_OK) == 0) {
+               char response[80];
+
+               fputs((program == VIGR)
+                      ? _("You are using shadow groups on this system.\n")
+                      : _("You are using shadow passwords on this system.\n"), stdout);
+
+               /* TRANSLATORS: this program uses for y and n rpmatch(3),
+                * which means they can be translated. */
+               printf(_("Would you like to edit %s now [y/n]? "), orig_file);
+
+               if (fgets(response, sizeof(response), stdin) &&
+                   rpmatch(response) == RPMATCH_YES)
+                       edit_file(1);
+       }
+       exit(EXIT_SUCCESS);
 }