]> granicus.if.org Git - sudo/commitdiff
Use warn/err and getprogname() throughout. The main exception is
authorTodd C. Miller <Todd.Miller@courtesan.com>
Wed, 2 Apr 2003 18:25:30 +0000 (18:25 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Wed, 2 Apr 2003 18:25:30 +0000 (18:25 +0000)
openlog().  Since the admin may be filtering logs based on the
program name in the log files, hard code this to "sudo".

18 files changed:
alloc.c
auth/fwtk.c
auth/rfc1938.c
auth/securid.c
auth/securid5.c
auth/sia.c
check.c
compat.h
defaults.c
env.c
find_path.c
interfaces.c
logging.c
parse.c
sudo.c
sudo.h
testsudoers.c
visudo.c

diff --git a/alloc.c b/alloc.c
index c58fd169f72047af1503880d8afbac63f887381a..1a87e14a3a63f577863a1f6d01f6308cfe822a0b 100644 (file)
--- a/alloc.c
+++ b/alloc.c
 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
 # include <malloc.h>
 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <limits.h>
 
 #include "sudo.h"
@@ -81,8 +86,6 @@ static const char rcsid[] = "$Sudo$";
 # endif /* SIZE_T_MAX */
 #endif /* SIZE_MAX */
 
-extern char **Argv;            /* from sudo.c */
-
 /*
  * emalloc() calls the system malloc(3) and exits with an error if
  * malloc(3) fails.
@@ -93,15 +96,11 @@ emalloc(size)
 {
     VOID *ptr;
 
-    if (size == 0) {
-       (void) fprintf(stderr, "%s: internal error, tried to emalloc(0)\n",
-           Argv[0]);
-       exit(1);
-    }
-    if ((ptr = (VOID *) malloc(size)) == NULL) {
-       (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
-       exit(1);
-    }
+    if (size == 0)
+       errx(1, "internal error, tried to emalloc(0)");
+
+    if ((ptr = (VOID *) malloc(size)) == NULL)
+       errx(1, "unable to allocate memory");
     return(ptr);
 }
 
@@ -116,21 +115,14 @@ emalloc2(nmemb, size)
 {
     VOID *ptr;
 
-    if (nmemb == 0 || size == 0) {
-       (void) fprintf(stderr, "%s: internal error, tried to emalloc2(0)\n",
-           Argv[0]);
-       exit(1);
-    }
-    if (nmemb > SIZE_MAX / size) {
-       (void) fprintf(stderr, "%s: internal error, emalloc2() overflow\n",
-           Argv[0]);
-       exit(1);
-    }
+    if (nmemb == 0 || size == 0)
+       errx(1, "internal error, tried to emalloc2(0)");
+    if (nmemb > SIZE_MAX / size)
+       errx(1, "internal error, emalloc2() overflow");
+
     size *= nmemb;
-    if ((ptr = (VOID *) malloc(size)) == NULL) {
-       (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
-       exit(1);
-    }
+    if ((ptr = (VOID *) malloc(size)) == NULL)
+       errx(1, "unable to allocate memory");
     return(ptr);
 }
 
@@ -145,16 +137,12 @@ erealloc(ptr, size)
     size_t size;
 {
 
-    if (size == 0) {
-       (void) fprintf(stderr, "%s: internal error, tried to erealloc(0)\n",
-           Argv[0]);
-       exit(1);
-    }
+    if (size == 0)
+       errx(1, "internal error, tried to erealloc(0)");
+
     ptr = ptr ? (VOID *) realloc(ptr, size) : (VOID *) malloc(size);
-    if (ptr == NULL) {
-       (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
-       exit(1);
-    }
+    if (ptr == NULL)
+       errx(1, "unable to allocate memory");
     return(ptr);
 }
 
@@ -171,22 +159,15 @@ erealloc3(ptr, nmemb, size)
     size_t size;
 {
 
-    if (nmemb == 0 || size == 0) {
-       (void) fprintf(stderr, "%s: internal error, tried to erealloc3(0)\n",
-           Argv[0]);
-       exit(1);
-    }
-    if (nmemb > SIZE_MAX / size) {
-       (void) fprintf(stderr, "%s: internal error, erealloc3() overflow\n",
-           Argv[0]);
-       exit(1);
-    }
+    if (nmemb == 0 || size == 0)
+       errx(1, "internal error, tried to erealloc3(0)");
+    if (nmemb > SIZE_MAX / size)
+       errx(1, "internal error, erealloc3() overflow");
+
     size *= nmemb;
     ptr = ptr ? (VOID *) realloc(ptr, size) : (VOID *) malloc(size);
-    if (ptr == NULL) {
-       (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
-       exit(1);
-    }
+    if (ptr == NULL)
+       errx(1, "unable to allocate memory");
     return(ptr);
 }
 
@@ -236,10 +217,8 @@ easprintf(va_alist)
     len = vasprintf(ret, fmt, ap);
     va_end(ap);
 
-    if (len == -1) {
-       (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
-       exit(1);
-    }
+    if (len == -1)
+       errx(1, "unable to allocate memory");
     return(len);
 }
 
@@ -255,9 +234,7 @@ evasprintf(ret, format, args)
 {
     int len;
 
-    if ((len = vasprintf(ret, format, args)) == -1) {
-       (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
-       exit(1);
-    }
+    if ((len = vasprintf(ret, format, args)) == -1)
+       errx(1, "unable to allocate memory");
     return(len);
 }
index 94c1807792c1021d4cb498b95159438947142bfb..72449c86295d72d917619363e685a5ac1d67cb82 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999-2002 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1999-2003 Todd C. Miller <Todd.Miller@courtesan.com>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <pwd.h>
 
 #include <auth.h>
@@ -77,25 +82,22 @@ fwtk_init(pw, promptp, auth)
     char resp[128];                    /* Response from the server */
 
     if ((confp = cfg_read("sudo")) == (Cfg *)-1) {
-       (void) fprintf(stderr, "%s: cannot read fwtk config.\n", Argv[0]);
+       warnx("cannot read fwtk config");
        return(AUTH_FATAL);
     }
 
     if (auth_open(confp)) {
-       (void) fprintf(stderr, "%s: cannot connect to authentication server.\n",
-           Argv[0]);
+       warnx("cannot connect to authentication server");
        return(AUTH_FATAL);
     }
 
     /* Get welcome message from auth server */
     if (auth_recv(resp, sizeof(resp))) {
-       (void) fprintf(stderr,
-           "%s: lost connection to authentication server.\n", Argv[0]);
+       warnx("lost connection to authentication server");
        return(AUTH_FATAL);
     }
     if (strncmp(resp, "Authsrv ready", 13) != 0) {
-       (void) fprintf(stderr,
-           "%s: authentication server error.\n%s\n", Argv[0], resp);
+       warnx("authentication server error:\n%s", resp);
        return(AUTH_FATAL);
     }
 
@@ -117,8 +119,7 @@ fwtk_verify(pw, prompt, auth)
     /* Send username to authentication server. */
     (void) snprintf(buf, sizeof(buf), "authorize %s 'sudo'", pw->pw_name);
     if (auth_send(buf) || auth_recv(resp, sizeof(resp))) {
-       (void) fprintf(stderr,
-           "%s: lost connection to authentication server.\n", Argv[0]);
+       warnx("lost connection to authentication server");
        return(AUTH_FATAL);
     }
 
@@ -134,7 +135,7 @@ fwtk_verify(pw, prompt, auth)
        pass = tgetpass(prompt, def_ival(I_PASSWD_TIMEOUT) * 60,
            tgetpass_flags);
     } else {
-       (void) fprintf(stderr, "%s: %s\n", Argv[0], resp);
+       warnx("%s", resp);
        return(AUTH_FATAL);
     }
     if (!pass) {                       /* ^C or error */
@@ -146,8 +147,7 @@ fwtk_verify(pw, prompt, auth)
     /* Send the user's response to the server */
     (void) snprintf(buf, sizeof(buf), "response '%s'", pass);
     if (auth_send(buf) || auth_recv(resp, sizeof(resp))) {
-       (void) fprintf(stderr,
-           "%s: lost connection to authentication server.\n", Argv[0]);
+       warnx("lost connection to authentication server");
        error = AUTH_FATAL;
        goto done;
     }
@@ -159,7 +159,7 @@ fwtk_verify(pw, prompt, auth)
 
     /* Main loop prints "Permission Denied" or insult. */
     if (strcmp(resp, "Permission Denied.") != 0)
-       fprintf(stderr, "%s: %s\n", Argv[0], resp);
+       warnx("%s", resp);
     error = AUTH_FAILURE;
 done:
     memset(pass, 0, strlen(pass));
index eaf58f04e22e7bdcb843edd4f7e51b03683f3e07..9b74a67ce28eb402cda85e78092909ea70d5ef46 100644 (file)
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <pwd.h>
 
 #if defined(HAVE_SKEY)
@@ -121,9 +126,7 @@ rfc1938_setup(pw, promptp, auth)
      */
     if (rfc1938challenge(&rfc1938, pw->pw_name, challenge, sizeof(challenge))) {
        if (IS_ONEANDONLY(auth)) {
-           (void) fprintf(stderr,
-                          "%s: You do not exist in the %s database.\n",
-                          Argv[0], auth->name);
+           warnx("you do not exist in the %s database", auth->name);
            return(AUTH_FATAL);
        } else {
            return(AUTH_FAILURE);
index 74c58f0146de7ab9a96838557e2360eb7dbc8205..8a9c3dd95c844ef79d577f1a13b81701053e7630 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999-2001 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1999-2001, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
  * All rights reserved.
  *
  * This code is derived from software contributed by Giles Todd
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <pwd.h>
 
 #include <sdi_athd.h>
@@ -103,7 +108,7 @@ securid_setup(pw, promptp, auth)
        strlcpy(sd->username, pw->pw_name, 32);
        return(AUTH_SUCCESS);
     } else {
-       (void) fprintf(stderr, "%s: Cannot contact SecurID server\n", Argv[0]);
+       warnx("unable to contact the SecurID server");
        return(AUTH_FATAL);
     }
 }
index a489760e802984d5304a22e8cfb74736e11304b5..f9d7fdd1ce6e3dfed48ff1d989f66c03a9e8e30d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999-2002 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1999-2003 Todd C. Miller <Todd.Miller@courtesan.com>
  * Copyright (c) 2002 Michael Stroucken <michael@stroucken.org>
  * All rights reserved.
  *
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <pwd.h>
 
 /* Needed for SecurID v5.0 Authentication on UNIX */
@@ -99,7 +104,7 @@ securid_init(pw, promptp, auth)
     if (AceInitialize() != SD_FALSE)
        return(AUTH_SUCCESS);
 
-    fprintf(stderr, "Failed to initialise ACE API library.\n");
+    warnx("failed to initialise the ACE API library");
     return(AUTH_FATAL);
 }
 
@@ -127,7 +132,7 @@ securid_setup(pw, promptp, auth)
 
     /* Re-initialize SecurID every time. */
     if (SD_Init(sd) != ACM_OK) {
-       (void) fprintf(stderr, "%s: Cannot contact SecurID server\n", Argv[0]);
+       warnx("unable to contact the SecurID server");
        return(AUTH_FATAL);
     }
 
@@ -136,19 +141,19 @@ securid_setup(pw, promptp, auth)
 
     switch (retval) {
         case ACE_UNDEFINED_USERNAME:
-               fprintf(stderr, "Invalid username length for SecurID\n");
+               warnx("invalid username length for SecurID");
                return(AUTH_FATAL);
 
        case ACE_ERR_INVALID_HANDLE:
-               fprintf(stderr, "Invalid Authentication Handle for SecurID\n");
+               warnx("invalid Authentication Handle for SecurID");
                return(AUTH_FATAL);
 
        case ACM_ACCESS_DENIED:
-               fprintf(stderr, "SecurID communication has failed\n");
+               warnx("SecurID communication failed");
                return(AUTH_FATAL);
 
        case ACM_OK:
-               fprintf(stderr, "User ID locked for SecurID Authentication\n");
+               warnx("User ID locked for SecurID Authentication");
                return(AUTH_SUCCESS);
        }
 }
@@ -180,17 +185,17 @@ securid_verify(pw, pass, auth)
     /* Have ACE verify password */
     switch (SD_Check(*sd, pass, pw->pw_name)) {
        case ACE_UNDEFINED_PASSCODE:
-               fprintf(stderr, "Invalid passcode length for SecurID\n");
+               warnx("invalid passcode length for SecurID");
                rval = AUTH_FATAL;
                break;
 
        case ACE_UNDEFINED_USERNAME:
-               fprintf(stderr, "Invalid username length for SecurID\n");
+               warnx("invalid username length for SecurID");
                rval = AUTH_FATAL;
                break;
 
        case ACE_ERR_INVALID_HANDLE:
-               fprintf(stderr, "Invalid Authentication Handle for SecurID\n");
+               warnx("invalid Authentication Handle for SecurID");
                rval = AUTH_FATAL;
 
        case ACM_ACCESS_DENIED:
index 15a0eac6137ae1fd5b80124a2f5d364921968c5b..45abe640ea4b882aa31ce6765a13f85278cc9ac4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999-2001 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1999-2001, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
  * All rights reserved.
  *
  * This code is derived from software contributed by Spider Boardman
@@ -111,6 +111,8 @@ sia_setup(pw, promptp, auth)
     sudo_auth *auth;
 {
     SIAENTITY *siah = NULL;
+    extern int Argc;
+    extern char **Argv;
 
     if (sia_ses_init(&siah, Argc, Argv, NULL, pw->pw_name, ttyname(0), 1, NULL)
        != SIASUCCESS) {
diff --git a/check.c b/check.c
index e2eb7b20d69b56f6e9f7f75abdc00e0a527317c9..ddf9ee05c54e8e4ca4ad2d6176bce5dddf3e5521 100644 (file)
--- a/check.c
+++ b/check.c
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -269,9 +274,7 @@ expand_prompt(old_prompt, user, host)
 
 oflow:
     /* We pre-allocate enough space, so this should never happen. */
-    (void) fprintf(stderr, "%s: internal error, expand_prompt() overflow\n",
-       Argv[0]);
-    exit(1);
+    errx(1, "internal error, expand_prompt() overflow");
 }
 
 /*
@@ -543,15 +546,13 @@ remove_timestamp(remove)
            else
                status = rmdir(timestampdir);
            if (status == -1 && errno != ENOENT) {
-               log_error(NO_EXIT, "can't remove %s (%s), will reset to epoch",
+               log_error(NO_EXIT, "can't remove %s (%s), will reset to Epoch",
                    ts, strerror(errno));
                remove = FALSE;
            }
        }
-       if (!remove && touch(ts, 0) == -1) {
-           (void) fprintf(stderr, "%s: can't reset %s to epoch: %s\n",
-               Argv[0], ts, strerror(errno));
-       }
+       if (!remove && touch(ts, 0) == -1)
+           err(1, "can't reset %s to Epoch", ts);
     }
 
     free(timestampdir);
index 5304cac4a5ffa8e8833f949f3702b306e6d1fd4a..994f692eef3f765d603d20a289176adfd7e3875e 100644 (file)
--- a/compat.h
+++ b/compat.h
@@ -227,4 +227,17 @@ typedef struct sigaction sigaction_t;
 # define RLIM_INFINITY (-1)
 #endif
 
+/*
+ * If we lack getprogname(), emulate with __progname if possible.
+ * Otherwise, add a prototype for use with our own getprogname.c.
+ */
+#ifndef HAVE_GETPROGNAME
+# ifdef HAVE___PROGNAME
+extern const char *__progname;
+#  define getprogname()          (__progname)
+# else
+const char *getprogname __P((void));
+#endif /* HAVE___PROGNAME */
+#endif /* !HAVE_GETPROGNAME */
+
 #endif /* _SUDO_COMPAT_H */
index 628bd7fb0a2975d5987d949ab473d65e817a26c9..8d901e311c6884f509cea6f4b20f8792a8fc0d4e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999-2001 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1999-2001, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 # ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <ctype.h>
 
 #include "sudo.h"
@@ -219,8 +224,7 @@ set_default(var, val, op)
            break;
     }
     if (!cur->name) {
-       (void) fprintf(stderr,
-           "%s: unknown defaults entry `%s' referenced near line %d\n", Argv[0],
+       warnx("unknown defaults entry `%s' referenced near line %d",
            var, sudolineno);
        return(FALSE);
     }
@@ -229,12 +233,9 @@ set_default(var, val, op)
        case T_LOGFAC:
            if (!store_syslogfac(val, cur, op)) {
                if (val)
-                   (void) fprintf(stderr,
-                       "%s: value '%s' is invalid for option '%s'\n", Argv[0],
-                       val, var);
+                   warnx("value `%s' is invalid for option `%s'", val, var);
                else
-                   (void) fprintf(stderr,
-                       "%s: no value specified for `%s' on line %d\n", Argv[0],
+                   warnx("no value specified for `%s' on line %d",
                        var, sudolineno);
                return(FALSE);
            }
@@ -242,12 +243,9 @@ set_default(var, val, op)
        case T_LOGPRI:
            if (!store_syslogpri(val, cur, op)) {
                if (val)
-                   (void) fprintf(stderr,
-                       "%s: value '%s' is invalid for option '%s'\n", Argv[0],
-                       val, var);
+                   warnx("value `%s' is invalid for option `%s'", val, var);
                else
-                   (void) fprintf(stderr,
-                       "%s: no value specified for `%s' on line %d\n", Argv[0],
+                   warnx("no value specified for `%s' on line %d",
                        var, sudolineno);
                return(FALSE);
            }
@@ -255,12 +253,9 @@ set_default(var, val, op)
        case T_PWFLAG:
            if (!store_pwflag(val, cur, op)) {
                if (val)
-                   (void) fprintf(stderr,
-                       "%s: value '%s' is invalid for option '%s'\n", Argv[0],
-                       val, var);
+                   warnx("value `%s' is invalid for option `%s'", val, var);
                else
-                   (void) fprintf(stderr,
-                       "%s: no value specified for `%s' on line %d\n", Argv[0],
+                   warnx("no value specified for `%s' on line %d",
                        var, sudolineno);
                return(FALSE);
            }
@@ -269,22 +264,17 @@ set_default(var, val, op)
            if (!val) {
                /* Check for bogus boolean usage or lack of a value. */
                if (!(cur->type & T_BOOL) || op != FALSE) {
-                   (void) fprintf(stderr,
-                       "%s: no value specified for `%s' on line %d\n", Argv[0],
+                   warnx("no value specified for `%s' on line %d",
                        var, sudolineno);
                    return(FALSE);
                }
            }
            if ((cur->type & T_PATH) && val && *val != '/') {
-               (void) fprintf(stderr,
-                   "%s: values for `%s' must start with a '/'\n", Argv[0],
-                   var);
+               warnx("values for `%s' must start with a '/'", var);
                return(FALSE);
            }
            if (!store_str(val, cur, op)) {
-               (void) fprintf(stderr,
-                   "%s: value '%s' is invalid for option '%s'\n", Argv[0],
-                   val, var);
+               warnx("value `%s' is invalid for option `%s'", val, var);
                return(FALSE);
            }
            break;
@@ -292,16 +282,13 @@ set_default(var, val, op)
            if (!val) {
                /* Check for bogus boolean usage or lack of a value. */
                if (!(cur->type & T_BOOL) || op != FALSE) {
-                   (void) fprintf(stderr,
-                       "%s: no value specified for `%s' on line %d\n", Argv[0],
+                   warnx("no value specified for `%s' on line %d",
                        var, sudolineno);
                    return(FALSE);
                }
            }
            if (!store_int(val, cur, op)) {
-               (void) fprintf(stderr,
-                   "%s: value '%s' is invalid for option '%s'\n", Argv[0],
-                   val, var);
+               warnx("value `%s' is invalid for option `%s'", val, var);
                return(FALSE);
            }
            break;
@@ -309,16 +296,13 @@ set_default(var, val, op)
            if (!val) {
                /* Check for bogus boolean usage or lack of a value. */
                if (!(cur->type & T_BOOL) || op != FALSE) {
-                   (void) fprintf(stderr,
-                       "%s: no value specified for `%s' on line %d\n", Argv[0],
+                   warnx("no value specified for `%s' on line %d",
                        var, sudolineno);
                    return(FALSE);
                }
            }
            if (!store_uint(val, cur, op)) {
-               (void) fprintf(stderr,
-                   "%s: value '%s' is invalid for option '%s'\n", Argv[0],
-                   val, var);
+               warnx("value `%s' is invalid for option `%s'", val, var);
                return(FALSE);
            }
            break;
@@ -326,24 +310,20 @@ set_default(var, val, op)
            if (!val) {
                /* Check for bogus boolean usage or lack of a value. */
                if (!(cur->type & T_BOOL) || op != FALSE) {
-                   (void) fprintf(stderr,
-                       "%s: no value specified for `%s' on line %d\n", Argv[0],
+                   warnx("no value specified for `%s' on line %d",
                        var, sudolineno);
                    return(FALSE);
                }
            }
            if (!store_mode(val, cur, op)) {
-               (void) fprintf(stderr,
-                   "%s: value '%s' is invalid for option '%s'\n", Argv[0],
-                   val, var);
+               warnx("value `%s' is invalid for option `%s'", val, var);
                return(FALSE);
            }
            break;
        case T_FLAG:
            if (val) {
-               (void) fprintf(stderr,
-                   "%s: option `%s' does not take a value on line %d\n",
-                   Argv[0], var, sudolineno);
+               warnx("option `%s' does not take a value on line %d",
+                   var, sudolineno);
                return(FALSE);
            }
            cur->sd_un.flag = op;
@@ -356,16 +336,13 @@ set_default(var, val, op)
            if (!val) {
                /* Check for bogus boolean usage or lack of a value. */
                if (!(cur->type & T_BOOL) || op != FALSE) {
-                   (void) fprintf(stderr,
-                       "%s: no value specified for `%s' on line %d\n", Argv[0],
+                   warnx("no value specified for `%s' on line %d",
                        var, sudolineno);
                    return(FALSE);
                }
            }
            if (!store_list(val, cur, op)) {
-               (void) fprintf(stderr,
-                   "%s: value '%s' is invalid for option '%s'\n", Argv[0],
-                   val, var);
+               warnx("value `%s' is invalid for option `%s'", val, var);
                return(FALSE);
            }
     }
diff --git a/env.c b/env.c
index 542189c29b639a1fc811adb026a8c07b800ea09c..2130c22523a6663419f9603fa39d1ddfa1017f00 100644 (file)
--- a/env.c
+++ b/env.c
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <pwd.h>
-#include <errno.h>
 
 #include "sudo.h"
 
@@ -113,7 +117,7 @@ static const char *initial_badenv_table[] = {
 #endif
 #ifdef HAVE_KERB4
     "KRB_CONF*",
-    "KRBCONFDIR"
+    "KRBCONFDIR",
     "KRBTKFILE",
 #endif /* HAVE_KERB4 */
 #ifdef HAVE_KERB5
@@ -223,9 +227,8 @@ format_env(var, val)
     if (strlcpy(estring, var, esize) >= esize ||
        strlcat(estring, "=", esize) >= esize ||
        strlcat(estring, val, esize) >= esize) {
-       (void) fprintf(stderr, "%s: internal error, format_env() overflow\n",
-           Argv[0]);
-       exit(1);
+
+       errx(1, "internal error, format_env() overflow");
     }
 
     return(estring);
index ae34e23905c400cb89e9fa53e4686b8563ef7f37..d6ec7cd9fdfecbd5a0254cf726df7a0bcbf47b07 100644 (file)
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
-#include <errno.h>
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 
 #include "sudo.h"
 
@@ -84,10 +88,8 @@ find_path(infile, outfile, path)
     int checkdot = 0;          /* check current dir? */
     int len;                   /* length parameter */
 
-    if (strlen(infile) >= MAXPATHLEN) {
-       (void) fprintf(stderr, "%s: path too long: %s\n", Argv[0], infile);
-       exit(1);
-    }
+    if (strlen(infile) >= MAXPATHLEN)
+       errx(1, "%s: File name too long", infile);
 
     /*
      * If we were given a fully qualified or relative path
@@ -130,10 +132,8 @@ find_path(infile, outfile, path)
         * Resolve the path and exit the loop if found.
         */
        len = snprintf(command, sizeof(command), "%s/%s", path, infile);
-       if (len <= 0 || len >= sizeof(command)) {
-           (void) fprintf(stderr, "%s: path too long: %s\n", Argv[0], infile);
-           exit(1);
-       }
+       if (len <= 0 || len >= sizeof(command))
+           errx(1, "%s: File name too long", infile);
        if ((result = sudo_goodpath(command)))
            break;
 
index 7b3d1de002ca458163b7683d6567aab271b3a648..8d2cafd8d742bd3e0bc4f89b9bea831e9ec4b82b 100644 (file)
@@ -73,13 +73,16 @@ struct rtentry;
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <netdb.h>
-#include <errno.h>
 #ifdef _ISC
 # include <sys/stream.h>
 # include <sys/sioctl.h>
 # include <sys/stropts.h>
-# include <net/errno.h>
 # define STRSET(cmd, param, len) {strioctl.ic_cmd=(cmd);\
                                 strioctl.ic_dp=(param);\
                                 strioctl.ic_timout=0;\
@@ -186,11 +189,8 @@ load_interfaces()
 #endif /* _ISC */
 
     sock = socket(AF_INET, SOCK_DGRAM, 0);
-    if (sock < 0) {
-       (void) fprintf(stderr, "%s: cannot open socket: %s\n",
-           Argv[0], strerror(errno));
-       exit(1);
-    }
+    if (sock < 0)
+       err(1, "cannot open socket");
 
     /*
      * Get interface configuration or return (leaving num_interfaces == 0)
@@ -324,5 +324,5 @@ dump_interfaces()
     puts("Local IP address and netmask pairs:");
     for (i = 0; i < num_interfaces; i++)
        printf("\t%s / 0x%x\n", inet_ntoa(interfaces[i].addr),
-           ntohl(interfaces[i].netmask.s_addr));
+           (unsigned int)ntohl(interfaces[i].netmask.s_addr));
 }
index f52b8d1e9ea7ad3da5b294b692d6691c1b0e1525..708027c296b1fada82ea07b189b6f15fb66d55ad 100644 (file)
--- a/logging.c
+++ b/logging.c
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <pwd.h>
 #include <signal.h>
 #include <time.h>
@@ -107,9 +112,9 @@ mysyslog(pri, fmt, va_alist)
     va_start(ap);
 #endif
 #ifdef LOG_NFACILITIES
-    openlog(Argv[0], 0, def_ival(I_LOGFAC));
+    openlog("sudo", 0, def_ival(I_LOGFAC));
 #else
-    openlog(Argv[0], 0);
+    openlog("sudo", 0);
 #endif
     vsnprintf(buf, sizeof(buf), fmt, ap);
 #ifdef BROKEN_SYSLOG
@@ -400,10 +405,10 @@ log_error(va_alist)
     /*
      * Tell the user.
      */
-    (void) fprintf(stderr, "%s: %s", Argv[0], message);
     if (flags & USE_ERRNO)
-       (void) fprintf(stderr, ": %s", strerror(serrno));
-    (void) fputc('\n', stderr);
+       warn("%s", message);
+    else
+       warnx("%s", message);
 
     /*
      * Send a copy of the error via mail.
@@ -459,18 +464,13 @@ send_mail(line)
     (void) sigaddset(&set, SIGCHLD);
     (void) sigprocmask(SIG_BLOCK, &set, &oset);
 
-    if (pipe(pfd) == -1) {
-       (void) fprintf(stderr, "%s: cannot open pipe: %s\n",
-           Argv[0], strerror(errno));
-       exit(1);
-    }
+    if (pipe(pfd) == -1)
+       err(1, "cannot open pipe");
 
     switch (pid = fork()) {
        case -1:
            /* Error. */
-           (void) fprintf(stderr, "%s: cannot fork: %s\n",
-               Argv[0], strerror(errno));
-           exit(1);
+           err(1, "cannot fork");
            break;
        case 0:
            {
diff --git a/parse.c b/parse.c
index 4bf6ba57a80475ea47627a5bab7e0e27cc37a007..625fa75a6f7c886072f160165d9fca6bdba3adbe 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -60,7 +60,7 @@
 #endif /* HAVE_UNISTD_H */
 #ifdef HAVE_FNMATCH
 # include <fnmatch.h>
-#endif /* HAVE_FNMATCH_H */
+#endif /* HAVE_FNMATCH */
 #ifdef HAVE_NETGROUP_H
 # include <netgroup.h>
 #endif /* HAVE_NETGROUP_H */
diff --git a/sudo.c b/sudo.c
index 1c56960c820c6cfe498238a2cec38c8c182d0342..315a24589c0bbd50dbfc1c378cef9ec3ddbf8b18 100644 (file)
--- a/sudo.c
+++ b/sudo.c
@@ -35,7 +35,7 @@
  * with this distribution.
  */
 
-#define _SUDO_SUDO_C
+#define _SUDO_MAIN
 
 #include "config.h"
 
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <pwd.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -105,7 +110,7 @@ static const char rcsid[] = "$Sudo$";
  * Prototypes
  */
 static int init_vars                   __P((int));
-static int parse_args                  __P((void));
+static int parse_args                  __P((int, char **));
 static void check_sudoers              __P((void));
 static void initial_setup              __P((void));
 static void set_loginclass             __P((struct passwd *));
@@ -121,13 +126,11 @@ extern struct passwd *sudo_getpwuid       __P((uid_t));
 /*
  * Globals
  */
-int Argc;
-char **Argv;
-int NewArgc = 0;
-char **NewArgv = NULL;
+int Argc, NewArgc;
+char **Argv, **NewArgv;
 struct sudo_user sudo_user;
 struct passwd *auth_pw;
-FILE *sudoers_fp = NULL;
+FILE *sudoers_fp;
 struct interface *interfaces;
 int num_interfaces;
 int tgetpass_flags;
@@ -161,9 +164,12 @@ main(argc, argv, envp)
     extern int printmatches;
     extern char **environ;
 
+    Argc = argc;
+    Argv = argv;
+
     /* Must be done as the first thing... */
 #if defined(HAVE_GETPRPWNAM) && defined(HAVE_SET_AUTH_PARAMETERS)
-    (void) set_auth_parameters(argc, argv);
+    (void) set_auth_parameters(Argc, Argv);
 # ifdef HAVE_INITPRIVS
     initprivs();
 # endif
@@ -172,13 +178,8 @@ main(argc, argv, envp)
     /* Zero out the environment. */
     environ = zero_env(envp);
 
-    Argv = argv;
-    Argc = argc;
-
-    if (geteuid() != 0) {
-       (void) fprintf(stderr, "Sorry, %s must be setuid root.\n", Argv[0]);
-       exit(1);
-    }
+    if (geteuid() != 0)
+       errx(1, "must be setuid root");
 
     /*
      * Signal setup:
@@ -202,7 +203,7 @@ main(argc, argv, envp)
     setpwent();
 
     /* Parse our arguments. */
-    sudo_mode = parse_args();
+    sudo_mode = parse_args(Argc, Argv);
 
     /* Setup defaults data structures. */
     init_defaults();
@@ -322,7 +323,7 @@ main(argc, argv, envp)
     if (user_uid == 0 && !def_flag(I_ROOT_SUDO)) {
        (void) fprintf(stderr,
            "Sorry, %s has been configured to not allow root to run it.\n",
-           Argv[0]);
+           getprogname());
        exit(1);
     }
 
@@ -360,11 +361,10 @@ main(argc, argv, envp)
     if (validated & VALIDATE_OK) {
        /* Finally tell the user if the command did not exist. */
        if (cmnd_status == NOT_FOUND_DOT) {
-           (void) fprintf(stderr, "%s: ignoring `%s' found in '.'\nUse `sudo ./%s' if this is the `%s' you wish to run.\n", Argv[0], user_cmnd, user_cmnd, user_cmnd);
+           warnx("ignoring `%s' found in '.'\nUse `sudo ./%s' if this is the `%s' you wish to run.", user_cmnd, user_cmnd, user_cmnd);
            exit(1);
        } else if (cmnd_status == NOT_FOUND) {
-           (void) fprintf(stderr, "%s: %s: command not found\n", Argv[0],
-               user_cmnd);
+           warnx("%s: command not found", user_cmnd);
            exit(1);
        }
 
@@ -420,8 +420,7 @@ main(argc, argv, envp)
        /*
         * If we got here then the exec() failed...
         */
-       (void) fprintf(stderr, "%s: unable to exec %s: %s\n",
-           Argv[0], safe_cmnd, strerror(errno));
+       warn("unable to execute %s", safe_cmnd);
        exit(127);
     } else if ((validated & FLAG_NO_USER) || (validated & FLAG_NO_HOST)) {
        log_auth(validated, 1);
@@ -438,10 +437,9 @@ main(argc, argv, envp)
            log_auth(validated,
                !(cmnd_status == NOT_FOUND_DOT || cmnd_status == NOT_FOUND));
            if (cmnd_status == NOT_FOUND)
-               (void) fprintf(stderr, "%s: %s: command not found\n", Argv[0],
-                   user_cmnd);
+               warnx("%s: command not found", user_cmnd);
            else if (cmnd_status == NOT_FOUND_DOT)
-               (void) fprintf(stderr, "%s: ignoring `%s' found in '.'\nUse `sudo ./%s' if this is the `%s' you wish to run.\n", Argv[0], user_cmnd, user_cmnd, user_cmnd);
+               warnx("ignoring `%s' found in '.'\nUse `sudo ./%s' if this is the `%s' you wish to run.", user_cmnd, user_cmnd, user_cmnd);
        } else {
            /* Just tell the user they are not allowed to run foo. */
            log_auth(validated, 1);
@@ -467,11 +465,8 @@ init_vars(sudo_mode)
     int nohostname, rval;
 
     /* Sanity check command from user. */
-    if (user_cmnd == NULL && strlen(NewArgv[0]) >= MAXPATHLEN) {
-       (void) fprintf(stderr, "%s: %s: Pathname too long\n", Argv[0],
-           NewArgv[0]);
-       exit(1);
-    }
+    if (user_cmnd == NULL && strlen(NewArgv[0]) >= MAXPATHLEN)
+       errx(1, "%s: File name too long", NewArgv[0]);
 
 #ifdef HAVE_TZSET
     (void) tzset();            /* set the timezone if applicable */
@@ -554,8 +549,7 @@ init_vars(sudo_mode)
     if (!getcwd(user_cwd, sizeof(user_cwd))) {
        set_perms(PERM_ROOT);
        if (!getcwd(user_cwd, sizeof(user_cwd))) {
-           (void) fprintf(stderr, "%s: Can't get working directory!\n",
-                          Argv[0]);
+           warnx("cannot get working directory");
            (void) strlcpy(user_cwd, "unknown", sizeof(user_cwd));
        }
     } else
@@ -571,12 +565,10 @@ init_vars(sudo_mode)
        NewArgv = (char **) emalloc2((++NewArgc + 1), sizeof(char *));
        if (user_shell && *user_shell) {
            NewArgv[0] = user_shell;
-       } else {
-           (void) fprintf(stderr, "%s: Unable to determine shell.", Argv[0]);
-           exit(1);
-       }
+       } else
+           errx(1, "unable to determine shell");
 
-       /* copy the args from Argv */
+       /* copy the args from NewArgv */
        for (dst = NewArgv + 1; (*dst = *src) != NULL; ++src, ++dst)
            ;
     }
@@ -613,11 +605,8 @@ init_vars(sudo_mode)
            user_args = (char *) emalloc(size);
            for (to = user_args, from = NewArgv + 1; *from; from++) {
                n = strlcpy(to, *from, size - (to - user_args));
-               if (n >= size - (to - user_args)) {
-                   (void) fprintf(stderr,
-                       "%s: internal error, init_vars() overflow\n", Argv[0]);
-                   exit(1);
-               }
+               if (n >= size - (to - user_args))
+                   errx(1, "internal error, init_vars() overflow");
                to += n;
                *to++ = ' ';
            }
@@ -633,13 +622,15 @@ init_vars(sudo_mode)
  * Command line argument parsing, can't use getopt(3).
  */
 static int
-parse_args()
+parse_args(argc, argv)
+    int argc;
+    char **argv;
 {
     int rval = MODE_RUN;               /* what mode is sudo to be run in? */
     int excl = 0;                      /* exclusive arg, no others allowed */
 
-    NewArgv = Argv + 1;
-    NewArgc = Argc - 1;
+    NewArgv = argv + 1;
+    NewArgc = argc - 1;
 
     if (NewArgc == 0) {                        /* no options and no command */
        rval |= (MODE_IMPLIED_SHELL | MODE_SHELL);
@@ -647,11 +638,8 @@ parse_args()
     }
 
     while (NewArgc > 0 && NewArgv[0][0] == '-') {
-       if (NewArgv[0][1] != '\0' && NewArgv[0][2] != '\0') {
-           (void) fprintf(stderr, "%s: Please use single character options\n",
-               Argv[0]);
-           usage(1);
-       }
+       if (NewArgv[0][1] != '\0' && NewArgv[0][2] != '\0')
+           warnx("please use single character options");
 
        switch (NewArgv[0][1]) {
            case 'p':
@@ -661,7 +649,6 @@ parse_args()
 
                user_prompt = NewArgv[1];
 
-               /* Shift Argv over and adjust Argc. */
                NewArgc--;
                NewArgv++;
                break;
@@ -672,7 +659,6 @@ parse_args()
 
                user_runas = &NewArgv[1];
 
-               /* Shift Argv over and adjust Argc. */
                NewArgc--;
                NewArgv++;
                break;
@@ -684,7 +670,6 @@ parse_args()
 
                login_style = NewArgv[1];
 
-               /* Shift Argv over and adjust Argc. */
                NewArgc--;
                NewArgv++;
                break;
@@ -698,7 +683,6 @@ parse_args()
                login_class = NewArgv[1];
                def_flag(I_USE_LOGINCLASS) = TRUE;
 
-               /* Shift Argv over and adjust Argc. */
                NewArgc--;
                NewArgv++;
                break;
@@ -770,12 +754,10 @@ parse_args()
                    rval |= (MODE_IMPLIED_SHELL | MODE_SHELL);
                return(rval);
            case '\0':
-               (void) fprintf(stderr, "%s: '-' requires an argument\n",
-                   Argv[0]);
+               warnx("'-' requires an argument");
                usage(1);
            default:
-               (void) fprintf(stderr, "%s: Illegal option %s\n", Argv[0],
-                   NewArgv[0]);
+               warnx("illegal option `%s'", NewArgv[0]);
                usage(1);
        }
        NewArgc--;
@@ -808,23 +790,17 @@ check_sudoers()
        (statbuf.st_mode & 0007777) == 0400) {
 
        if (chmod(_PATH_SUDOERS, SUDOERS_MODE) == 0) {
-           (void) fprintf(stderr, "%s: fixed mode on %s\n",
-               Argv[0], _PATH_SUDOERS);
+           warnx("fixed mode on %s", _PATH_SUDOERS);
            statbuf.st_mode |= SUDOERS_MODE;
            if (statbuf.st_gid != SUDOERS_GID) {
                if (!chown(_PATH_SUDOERS,(uid_t) -1,SUDOERS_GID)) {
-                   (void) fprintf(stderr, "%s: set group on %s\n",
-                       Argv[0], _PATH_SUDOERS);
+                   warnx("set group on %s", _PATH_SUDOERS);
                    statbuf.st_gid = SUDOERS_GID;
-               } else {
-                   (void) fprintf(stderr,"%s: Unable to set group on %s: %s\n",
-                       Argv[0], _PATH_SUDOERS, strerror(errno));
-               }
+               } else
+                   warn("unable to set group on %s", _PATH_SUDOERS);
            }
-       } else {
-           (void) fprintf(stderr, "%s: Unable to fix mode on %s: %s\n",
-               Argv[0], _PATH_SUDOERS, strerror(errno));
-       }
+       } else
+           warn("unable to fix mode on %s", _PATH_SUDOERS);
     }
 
     /*
@@ -945,11 +921,8 @@ set_loginclass(pw)
        errflags = NO_MAIL|MSG_ONLY|NO_EXIT;
 
     if (login_class && strcmp(login_class, "-") != 0) {
-       if (strcmp(*user_runas, "root") != 0 && user_uid != 0) {
-           (void) fprintf(stderr, "%s: only root can use -c %s\n",
-               Argv[0], login_class);
-           exit(1);
-       }
+       if (strcmp(*user_runas, "root") != 0 && user_uid != 0)
+           errx(1, "only root can use -c %s", login_class);
     } else {
        login_class = pw->pw_class;
        if (!login_class || !*login_class)
diff --git a/sudo.h b/sudo.h
index 09d23f30a598c8278ada43d5cc2538c3b246410a..edb10dd38b7739cfe389e56cc44235b70a6db848 100644 (file)
--- a/sudo.h
+++ b/sudo.h
@@ -236,12 +236,10 @@ int pam_prep_user __P((struct passwd *));
 YY_DECL;
 
 /* Only provide extern declarations outside of sudo.c. */
-#ifndef _SUDO_SUDO_C
+#ifndef _SUDO_MAIN
 extern struct sudo_user sudo_user;
 extern struct passwd *auth_pw;
 
-extern int Argc;
-extern char **Argv;
 extern FILE *sudoers_fp;
 extern int tgetpass_flags;
 extern uid_t timestamp_uid;
index 79cfa12cd437a216e2fc7d743a0d9cd4f766c1eb..be5ff929976c310891f27d6b54da04e302cd54eb 100644 (file)
@@ -34,6 +34,8 @@
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#define _SUDO_MAIN
+
 #include "config.h"
 
 #include <sys/param.h>
 #endif /* HAVE_UNISTD_H */
 #ifdef HAVE_FNMATCH
 # include <fnmatch.h>
-#endif /* HAVE_FNMATCH_H */
+#endif /* HAVE_FNMATCH */
 #ifdef HAVE_NETGROUP_H
 # include <netgroup.h>
 #endif /* HAVE_NETGROUP_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <ctype.h>
 #include <pwd.h>
 #include <grp.h>
@@ -96,15 +103,15 @@ void set_perms_dummy       __P((int));
 /*
  * Globals
  */
-char **Argv, **NewArgv;
 int  Argc, NewArgc;
+char **Argv, **NewArgv;
 int parse_error = FALSE;
 int num_interfaces;
 struct interface *interfaces;
 struct sudo_user sudo_user;
-void (*set_perms) __P((int)) = set_perms_dummy;
 extern int clearaliases;
 extern int pedantic;
+void (*set_perms) __P((int)) = set_perms_dummy;
 
 /*
  * Returns TRUE if "s" has shell meta characters in it,
@@ -361,7 +368,7 @@ main(argc, argv)
        NewArgc = Argc - 3;
     } else {
        (void) fprintf(stderr,
-           "usage: %s [-u user] <user> <host> <command> [args]\n", Argv[0]);
+           "usage: sudo [-u user] <user> <host> <command> [args]\n");
        exit(1);
     }
 
@@ -385,11 +392,8 @@ main(argc, argv)
        user_args = (char *) emalloc(size);
        for (to = user_args, from = NewArgv + 1; *from; from++) {
            n = strlcpy(to, *from, size - (to - user_args));
-           if (n >= size - (to - user_args)) {
-               (void) fprintf(stderr,
-                   "%s: internal error, init_vars() overflow\n", Argv[0]);
-               exit(1);
-           }
+           if (n >= size - (to - user_args))
+                   errx(1, "internal error, init_vars() overflow");
            to += n;
            *to++ = ' ';
        }
index 9193b83f5d53f53657b764a925d6826448634149..c4f616392ee3045d95f0a02bc71c478477379302 100644 (file)
--- a/visudo.c
+++ b/visudo.c
@@ -36,6 +36,8 @@
  * Lock the sudoers file for safe editing (ala vipw) and check for parse errors.
  */
 
+#define _SUDO_MAIN
+
 #include "config.h"
 
 #include <sys/types.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_ERR_H
+# include <err.h>
+#else
+# include "emul/err.h"
+#endif /* HAVE_ERR_H */
 #include <ctype.h>
 #include <pwd.h>
 #include <time.h>
@@ -135,10 +142,7 @@ main(argc, argv)
     /* Warn about aliases that are used before being defined. */
     pedantic = 1;
 
-    /*
-     * Parse command line options
-     */
-    Argv = argv;
+    Argv = argv;                       /* for warn/err */
 
     /*
      * Arg handling.
@@ -147,7 +151,7 @@ main(argc, argv)
     while ((ch = getopt(argc, argv, "Vcf:sq")) != -1) {
        switch (ch) {
            case 'V':
-               (void) printf("visudo version %s\n", version);
+               (void) printf("%s version %s\n", getprogname(), version);
                exit(0);
            case 'c':
                checkonly++;            /* check mode */
@@ -173,11 +177,8 @@ main(argc, argv)
 
     /* Mock up a fake sudo_user struct. */
     user_host = user_shost = user_cmnd = "";
-    if ((sudo_user.pw = getpwuid(getuid())) == NULL) {
-       (void) fprintf(stderr, "%s: Can't find you in the passwd database.\n",
-           Argv[0]);
-       exit(1);
-    }
+    if ((sudo_user.pw = getpwuid(getuid())) == NULL)
+       errx(1, "you don't exist in the passwd database");
 
     /* Setup defaults data structures. */
     init_defaults();
@@ -190,34 +191,23 @@ main(argc, argv)
      * sudoers_fd must remain open throughout in order to hold the lock.
      */
     sudoers_fd = open(sudoers, O_RDWR | O_CREAT, SUDOERS_MODE);
-    if (sudoers_fd == -1) {
-       (void) fprintf(stderr, "%s: %s: %s\n", Argv[0], sudoers,
-           strerror(errno));
-       exit(1);
-    }
-    if (!lock_file(sudoers_fd, SUDO_TLOCK)) {
-       (void) fprintf(stderr, "%s: sudoers file busy, try again later.\n",
-           Argv[0]);
-       exit(1);
-    }
+    if (sudoers_fd == -1)
+       err(1, "%s", sudoers);
+    if (!lock_file(sudoers_fd, SUDO_TLOCK))
+       errx(1, "sudoers file busy, try again later");
 #ifdef HAVE_FSTAT
-    if (fstat(sudoers_fd, &sudoers_sb) == -1) {
+    if (fstat(sudoers_fd, &sudoers_sb) == -1)
 #else
-    if (stat(sudoers, &sudoers_sb) == -1) {
+    if (stat(sudoers, &sudoers_sb) == -1)
 #endif
-       (void) fprintf(stderr, "%s: can't stat %s: %s\n",
-           Argv[0], sudoers, strerror(errno));
-       exit(1);
-    }
+       err(1, "can't stat %s", sudoers);
 
     /*
      * Open sudoers temp file.
      */
     stmp_fd = open(stmp, O_WRONLY | O_CREAT | O_TRUNC, 0600);
-    if (stmp_fd < 0) {
-       (void) fprintf(stderr, "%s: %s: %s\n", Argv[0], stmp, strerror(errno));
-       exit(1);
-    }
+    if (stmp_fd < 0)
+       err(1, "%s", stmp);
 
     /* Install signal handlers to clean up stmp if we are killed. */
     setup_signals();
@@ -225,11 +215,8 @@ main(argc, argv)
     /* Copy sudoers -> stmp and reset the mtime */
     if (sudoers_sb.st_size) {
        while ((n = read(sudoers_fd, buf, sizeof(buf))) > 0)
-           if (write(stmp_fd, buf, n) != n) {
-               (void) fprintf(stderr, "%s: Write failed: %s\n", Argv[0],
-               strerror(errno));
-               Exit(-1);
-           }
+           if (write(stmp_fd, buf, n) != n)
+               err(1, "write error");
 
        /* Add missing newline at EOF if needed. */
        if (n > 0 && buf[n - 1] != '\n') {
@@ -270,9 +257,7 @@ main(argc, argv)
        } else {
            if (def_flag(I_ENV_EDITOR)) {
                /* If we are honoring $EDITOR this is a fatal error. */
-               (void) fprintf(stderr,
-                   "%s: specified editor (%s) doesn't exist!\n",
-                   Argv[0], UserEditor);
+               warnx("specified editor (%s) doesn't exist!", UserEditor);
                Exit(-1);
            } else {
                /* Otherwise, just ignore $EDITOR. */
@@ -295,8 +280,7 @@ main(argc, argv)
 
        if (stat(UserEditor, &user_editor_sb) != 0) {
            /* Should never happen since we already checked above. */
-           (void) fprintf(stderr, "%s: unable to stat editor (%s): %s\n",
-               Argv[0], UserEditor, strerror(errno));
+           warn("unable to stat editor (%s)", UserEditor);
            Exit(-1);
        }
        EditorPath = estrdup(def_str(I_EDITOR));
@@ -344,8 +328,7 @@ main(argc, argv)
 
        /* Bleah, none of the editors existed! */
        if (Editor == NULL || *Editor == '\0') {
-           (void) fprintf(stderr, "%s: no editor found (editor path = %s)\n",
-               Argv[0], def_str(I_EDITOR));
+           warnx("no editor found (editor path = %s)", def_str(I_EDITOR));
            Exit(-1);
        }
     }
@@ -381,15 +364,13 @@ main(argc, argv)
             * Sanity checks.
             */
            if (stat(stmp, &stmp_sb) < 0) {
-               (void) fprintf(stderr,
-                   "%s: Can't stat temporary file (%s), %s unchanged.\n",
-                   Argv[0], stmp, sudoers);
+               warnx("cannot stat temporary file (%s), %s unchanged",
+                   stmp, sudoers);
                Exit(-1);
            }
            if (stmp_sb.st_size == 0) {
-               (void) fprintf(stderr,
-                   "%s: Zero length temporary file (%s), %s unchanged.\n",
-                   Argv[0], stmp, sudoers);
+               warnx("zero length temporary file (%s), %s unchanged",
+                   stmp, sudoers);
                Exit(-1);
            }
 
@@ -400,9 +381,8 @@ main(argc, argv)
            yyout = stdout;
            yyin = fopen(stmp, "r+");
            if (yyin == NULL) {
-               (void) fprintf(stderr,
-                   "%s: Can't re-open temporary file (%s), %s unchanged.\n",
-                   Argv[0], stmp, sudoers);
+               warnx("can't re-open temporary file (%s), %s unchanged.",
+                   stmp, sudoers);
                Exit(-1);
            }
 
@@ -419,16 +399,13 @@ main(argc, argv)
            /* Parse the sudoers temp file */
            yyrestart(yyin);
            if (yyparse() && parse_error != TRUE) {
-               (void) fprintf(stderr,
-                   "%s: Failed to parse temporary file (%s), unknown error.\n",
-                   Argv[0], stmp);
+               warnx("unabled to parse temporary file (%s), unknown error",
+                   stmp);
                parse_error = TRUE;
            }
            fclose(yyin);
        } else {
-           (void) fprintf(stderr,
-               "%s: Editor (%s) failed, %s unchanged.\n", Argv[0],
-                   Editor, sudoers);
+           warnx("editor (%s) failed, %s unchanged", Editor, sudoers);
            Exit(-1);
        }
 
@@ -452,7 +429,7 @@ main(argc, argv)
      */
     if (sudoers_sb.st_mtime != now && sudoers_sb.st_mtime == stmp_sb.st_mtime &&
        sudoers_sb.st_size == stmp_sb.st_size) {
-       (void) fprintf(stderr, "%s: sudoers file unchanged.\n", Argv[0]);
+       warnx("sudoers file unchanged");
        Exit(0);
     }
 
@@ -461,15 +438,12 @@ main(argc, argv)
      * we move it to sudoers things are kosher.
      */
     if (chown(stmp, SUDOERS_UID, SUDOERS_GID)) {
-       (void) fprintf(stderr,
-           "%s: Unable to set (uid, gid) of %s to (%d, %d): %s\n",
-           Argv[0], stmp, SUDOERS_UID, SUDOERS_GID, strerror(errno));
+       warn("unable to set (uid, gid) of %s to (%d, %d)",
+           stmp, SUDOERS_UID, SUDOERS_GID);
        Exit(-1);
     }
     if (chmod(stmp, SUDOERS_MODE)) {
-       (void) fprintf(stderr,
-           "%s: Unable to change mode of %s to %o: %s\n",
-           Argv[0], stmp, SUDOERS_MODE, strerror(errno));
+       warn("unable to change mode of %s to 0%o", stmp, SUDOERS_MODE);
        Exit(-1);
     }
 
@@ -480,9 +454,8 @@ main(argc, argv)
      */
     if (rename(stmp, sudoers)) {
        if (errno == EXDEV) {
-           (void) fprintf(stderr,
-             "%s: %s and %s not on the same filesystem, using mv to rename.\n",
-             Argv[0], stmp, sudoers);
+           warnx("%s and %s not on the same filesystem, using mv to rename",
+             stmp, sudoers);
 
            /* Build up argument vector for the command */
            if ((av[0] = strrchr(_PATH_MV, '/')) != NULL)
@@ -495,14 +468,12 @@ main(argc, argv)
 
            /* And run it... */
            if (run_command(_PATH_MV, av)) {
-               (void) fprintf(stderr,
-                              "%s: Command failed: '%s %s %s', %s unchanged.\n",
-                              Argv[0], _PATH_MV, stmp, sudoers, sudoers);
+               warnx("command failed: '%s %s %s', %s unchanged",
+                   _PATH_MV, stmp, sudoers, sudoers);
                Exit(-1);
            }
        } else {
-           (void) fprintf(stderr, "%s: Error renaming %s, %s unchanged: %s\n",
-                                  Argv[0], stmp, sudoers, strerror(errno));
+           warn("error renaming %s, %s unchanged", stmp, sudoers);
            Exit(-1);
        }
     }
@@ -637,15 +608,13 @@ run_command(path, argv)
 
     switch (pid = fork()) {
        case -1:
-           (void) fprintf(stderr,
-               "%s: unable to run %s: %s\n", Argv[0], path, strerror(errno));
+           warn("unable to run %s", path);
            Exit(-1);
            break;      /* NOTREACHED */
        case 0:
            (void) sigprocmask(SIG_SETMASK, &oset, NULL);
            execv(path, argv);
-           (void) fprintf(stderr,
-               "%s: unable to run %s: %s\n", Argv[0], path, strerror(errno));
+           warn("unable to run %s", path);
            _exit(127);
            break;      /* NOTREACHED */
     }
@@ -669,17 +638,14 @@ check_syntax(quiet)
 
     if ((yyin = fopen(sudoers, "r")) == NULL) {
        if (!quiet)
-           (void) fprintf(stderr, "%s: unable to open %s: %s\n", Argv[0],
-               sudoers, strerror(errno));
+           warn("unable to open %s", sudoers);
        exit(1);
     }
     yyout = stdout;
     init_parser();
     if (yyparse() && parse_error != TRUE) {
        if (!quiet)
-           (void) fprintf(stderr,
-               "%s: failed to parse %s file, unknown error.\n",
-               Argv[0], sudoers);
+           warnx("failed to parse %s file, unknown error", sudoers);
        parse_error = TRUE;
     }
     if (!quiet){
@@ -707,7 +673,7 @@ Exit(sig)
     (void) unlink(stmp);
 
     if (sig > 0) {
-       write(STDERR_FILENO, Argv[0], strlen(Argv[0]));
+       write(STDERR_FILENO, getprogname(), strlen(getprogname()));
        write(STDERR_FILENO, emsg, sizeof(emsg) - 1);
        _exit(-sig);
     }
@@ -718,6 +684,6 @@ static void
 usage()
 {
     (void) fprintf(stderr, "usage: %s [-c] [-f sudoers] [-q] [-s] [-V]\n",
-       Argv[0]);
+       getprogname());
     exit(1);
 }