version 1.19, 2007/07/26 16:10:16 |
version 1.20, 2007/09/05 23:55:57 |
|
|
#include "version.h" |
#include "version.h" |
|
|
#ifndef lint |
#ifndef lint |
__unused static const char rcsid[] = "$Sudo: visudo.c,v 1.166.2.9 2007/07/22 19:21:01 millert Exp $"; |
__unused static const char rcsid[] = "$Sudo: visudo.c,v 1.166.2.10 2007/09/01 13:39:13 millert Exp $"; |
#endif /* lint */ |
#endif /* lint */ |
|
|
struct sudoersfile { |
struct sudoersfile { |
|
|
static void edit_sudoers __P((struct sudoersfile *, char *, char *, int)); |
static void edit_sudoers __P((struct sudoersfile *, char *, char *, int)); |
static void visudo __P((struct sudoersfile *, char *, char *)); |
static void visudo __P((struct sudoersfile *, char *, char *)); |
static void setup_signals __P((void)); |
static void setup_signals __P((void)); |
static void install_sudoers __P((struct sudoersfile *)); |
static void install_sudoers __P((struct sudoersfile *, int)); |
static int check_syntax __P(()); |
static int check_syntax __P(()); |
static int run_command __P((char *, char **)); |
static int run_command __P((char *, char **)); |
static char *get_args __P((char *)); |
static char *get_args __P((char *)); |
|
|
char **argv; |
char **argv; |
{ |
{ |
char *args, *editor; |
char *args, *editor; |
int ch, checkonly, n; |
int ch, checkonly, n, oldperms; |
|
|
/* Initialize sudoers struct. */ |
/* Initialize sudoers struct. */ |
sudoers.path = _PATH_SUDOERS; |
sudoers.path = _PATH_SUDOERS; |
|
|
/* |
/* |
* Arg handling. |
* Arg handling. |
*/ |
*/ |
checkonly = 0; |
checkonly = oldperms = FALSE; |
while ((ch = getopt(argc, argv, "Vcf:sq")) != -1) { |
while ((ch = getopt(argc, argv, "Vcf:sq")) != -1) { |
switch (ch) { |
switch (ch) { |
case 'V': |
case 'V': |
|
|
case 'f': /* sudoers file path */ |
case 'f': /* sudoers file path */ |
sudoers.path = optarg; |
sudoers.path = optarg; |
easprintf(&sudoers.tpath, "%s.tmp", optarg); |
easprintf(&sudoers.tpath, "%s.tmp", optarg); |
|
oldperms = TRUE; |
break; |
break; |
case 's': |
case 's': |
pedantic++; /* strict mode */ |
pedantic++; /* strict mode */ |
|
|
visudo(&sudoers, editor, args); |
visudo(&sudoers, editor, args); |
|
|
/* Install the new sudoers file. */ |
/* Install the new sudoers file. */ |
install_sudoers(&sudoers); |
install_sudoers(&sudoers, oldperms); |
|
|
exit(0); |
exit(0); |
} |
} |
|
|
* move it into place. Returns TRUE on success, else FALSE. |
* move it into place. Returns TRUE on success, else FALSE. |
*/ |
*/ |
static void |
static void |
install_sudoers(sp) |
install_sudoers(sp, oldperms) |
struct sudoersfile *sp; |
struct sudoersfile *sp; |
|
int oldperms; |
{ |
{ |
|
struct stat sb; |
|
|
/* |
/* |
* Change mode and ownership of temp file so when |
* Change mode and ownership of temp file so when |
* we move it to sp->path things are kosher. |
* we move it to sp->path things are kosher. |
*/ |
*/ |
if (chown(sp->tpath, SUDOERS_UID, SUDOERS_GID) != 0) { |
if (oldperms) { |
warn("unable to set (uid, gid) of %s to (%d, %d)", |
/* Use perms of the existing file. */ |
sp->tpath, SUDOERS_UID, SUDOERS_GID); |
#ifdef HAVE_FSTAT |
Exit(-1); |
if (fstat(sp->fd, &sb) == -1) |
} |
#else |
if (chmod(sp->tpath, SUDOERS_MODE) != 0) { |
if (stat(sp->path, &sb) == -1) |
warn("unable to change mode of %s to 0%o", sp->tpath, SUDOERS_MODE); |
#endif |
Exit(-1); |
err(1, "can't stat %s", sp->path); |
|
(void) chown(sp->tpath, sb.st_uid, sb.st_gid); |
|
(void) chmod(sp->tpath, sb.st_mode & 0777); |
|
} else { |
|
if (chown(sp->tpath, SUDOERS_UID, SUDOERS_GID) != 0) { |
|
warn("unable to set (uid, gid) of %s to (%d, %d)", |
|
sp->tpath, SUDOERS_UID, SUDOERS_GID); |
|
Exit(-1); |
|
} |
|
if (chmod(sp->tpath, SUDOERS_MODE) != 0) { |
|
warn("unable to change mode of %s to 0%o", sp->tpath, SUDOERS_MODE); |
|
Exit(-1); |
|
} |
} |
} |
|
|
/* |
/* |