00d467439906eb0c88492167599dcf29dcc556d4
3 * $Id: become.c,v 1.7 1997/09/08 13:43:20 mdw Exp $
5 * Main code for `become'
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.7 1997/09/08 13:43:20 mdw
33 * Change userid when creating tracefiles rather than fiddling with
34 * `access': it works rather better. Also, insert some stdio buffer
35 * flushing to ensure tracedumps are completely written.
37 * Revision 1.6 1997/09/05 13:47:44 mdw
38 * Make the `-L' (trace-level) option's argument optional, like the long
41 * Revision 1.5 1997/09/05 11:45:19 mdw
42 * Add support for different login styles, and environment variable
43 * manipulation in a safe and useful way.
45 * Revision 1.4 1997/08/20 16:15:13 mdw
46 * Overhaul of environment handling. Fix daft bug in path search code.
48 * Revision 1.3 1997/08/07 16:28:59 mdw
49 * Do something useful when users attempt to become themselves.
51 * Revision 1.2 1997/08/04 10:24:20 mdw
52 * Sources placed under CVS control.
54 * Revision 1.1 1997/07/21 13:47:54 mdw
59 /*----- Header files ------------------------------------------------------*/
61 /* --- ANSI headers --- */
70 /* --- Unix headers --- */
72 #include <sys/types.h>
74 #include <sys/socket.h>
75 #include <sys/utsname.h>
77 #include <netinet/in.h>
79 #include <arpa/inet.h>
86 extern char **environ
;
88 /* --- Local headers --- */
103 /*----- Type definitions --------------------------------------------------*/
105 /* --- Symbol table entry for an environment variable --- */
107 typedef struct sym_env
{
108 sym_base _base
; /* Symbol table information */
109 unsigned f
; /* Flags word (see below) */
110 char *val
; /* Pointer to variable value */
113 /* --- Environment variable flags --- */
119 /* --- Login behaviour types --- */
122 l_preserve
, /* Preserve the environment */
123 l_user
, /* Update who I am */
124 l_login
/* Do a full login */
127 /*----- Static variables --------------------------------------------------*/
129 static sym_table bc__env
;
131 /*----- Main code ---------------------------------------------------------*/
133 /* --- @bc__write@ --- *
135 * Arguments: @FILE *fp@ = pointer to a stream to write on
136 * @const char *p@ = pointer to a string
140 * Use: Writes the string to the stream, substituting the program
141 * name (as returned by @quis@) for each occurrence of the
145 static void bc__write(FILE *fp
, const char *p
)
147 const char *n
= quis();
149 size_t nl
= strlen(n
);
151 /* --- Try to be a little efficient --- *
153 * Gather up non-`$' characters using @strcspn@ and spew them out really
164 fwrite(n
, nl
, 1, fp
);
169 /* --- @bc__setenv@ --- *
171 * Arguments: @sym_env *e@ = pointer to environment variable block
172 * @const char *val@ = value to set
176 * Use: Sets an environment variable block to the right value.
179 static void bc__setenv(sym_env
*e
, const char *val
)
181 e
->val
= xmalloc(strlen(e
->_base
.name
) + 1 + strlen(val
) + 1);
182 sprintf(e
->val
, "%s=%s", e
->_base
.name
, val
);
185 /* --- @bc__putenv@ --- *
187 * Arguments: @const char *var@ = name of the variable to set, or 0 if
188 * this is embedded in the value string
189 * @const char *val@ = value to set, or 0 if the variable must
191 * @unsigned int fl@ = flags to set
192 * @unsigned int force@ = force overwrite of preserved variables
194 * Returns: Pointer to symbol block, or zero if it was deleted.
196 * Use: Puts an item into the environment.
199 static sym_env
*bc__putenv(const char *var
, const char *val
,
200 unsigned int fl
, unsigned int force
)
207 /* --- Sort out embedded variable names --- */
210 const char *p
= strchr(val
, '=');
225 /* --- Find the variable block --- */
228 e
= sym_find(&bc__env
, var
, -1, sizeof(*e
), &f
);
229 if (~e
->f
& envFlag_preserve
|| force
) {
235 e
= sym_find(&bc__env
, var
, -1, 0, 0);
236 if (e
&& (force
|| ~e
->f
& envFlag_preserve
))
237 sym_remove(&bc__env
, e
);
241 /* --- Tidy up and return --- */
250 /* --- @bc__banner@ --- *
252 * Arguments: @FILE *fp@ = stream to write on
256 * Use: Writes a banner containing copyright information.
259 static void bc__banner(FILE *fp
)
261 bc__write(fp
, "$ version " VERSION
"\n");
264 /* --- @bc__usage@ --- *
266 * Arguments: @FILE *fp@ = stream to write on
270 * Use: Writes a terse reminder of command line syntax.
273 static void bc__usage(FILE *fp
)
277 " $ -c <shell-command> <user>\n"
278 " $ [<env-var>] <user> [<command> [<arguments>]...]\n"
279 " $ -d [-p <port>] [-f <config-file>]\n");
282 /* --- @bc__help@ --- *
284 * Arguments: @FILE *fp@ = stream to write on
285 * @int suid@ = whether we're running set-uid
289 * Use: Displays a help message for this excellent piece of software.
292 static void bc__help(FILE *fp
, int suid
)
299 "The `$' program allows you to run a process as another user.\n"
300 "If a command name is given, this is the process executed. If the `-c'\n"
301 "option is used, the process is assumed to be `/bin/sh'. If no command is\n"
302 "given, your default login shell is used.\n"
304 "Your user id, the user id you wish to become, the name of the process\n"
305 "you wish to run, and the identity of the current host are looked up to\n"
306 "ensure that you have permission to do this.\n"
308 "Note that logs are kept of all uses of this program.\n"
310 "Options available are:\n"
312 "-h, --help Display this help text\n"
313 "-u, --usage Display a short usage summary\n"
314 "-v, --version Display $'s version number\n"
316 "-e, --preserve-environment Try to preserve the current environment\n"
317 "-s, --su, --set-user Set environment variables to reflect USER\n"
318 "-l, --login Really log in as USER\n"
320 "-c CMD, --command=CMD Run the (Bourne) shell command CMD\n"
322 "-d, --daemon Start a daemon\n"
323 "-p PORT, --port=PORT In daemon mode, listen on PORT\n"
324 "-f FILE, --config-file=FILE In daemon mode, read config from FILE\n");
329 "-I USER, --impersonate=USER Claim to be USER when asking the server\n");
332 "-T FILE, --trace=FILE Dump trace information to FILE (boring)\n"
333 "-L OPTS, --trace-level=OPTS Set level of tracing information\n");
339 * Arguments: @int argc@ = number of command line arguments
340 * @char *argv[]@ = pointer to the various arguments
342 * Returns: Zero if successful.
344 * Use: Allows a user to change UID.
347 int main(int argc
, char *argv
[])
349 /* --- Request block setup parameters --- */
351 request rq
; /* Request buffer to build */
352 char *cmd
= 0; /* Shell command to execute */
353 char *binary
= "/bin/sh"; /* Default binary to execute */
354 char **env
= environ
; /* Default environment to pass */
355 char **todo
= 0; /* Pointer to argument list */
356 char *who
= 0; /* Who we're meant to become */
357 struct passwd
*from_pw
= 0; /* User we are right now */
358 struct passwd
*to_pw
= 0; /* User we want to become */
360 /* --- Become server setup parameters --- */
362 char *conffile
= file_RULES
; /* Default config file for daemon */
363 int port
= -1; /* Default port for daemon */
365 /* --- Miscellanous shared variables --- */
367 unsigned flags
= 0; /* Various useful flags */
368 int style
= DEFAULT_LOGIN_STYLE
; /* Login style */
370 /* --- Default argument list executes a shell command --- */
372 static char *shell
[] = {
373 "/bin/sh", /* Bourne shell */
374 "-c", /* Read from command line */
375 0, /* Pointer to shell command */
379 /* --- Definitions for the various flags --- */
382 f_daemon
= 1, /* Start up in daemon mode */
383 f_duff
= 2, /* Fault in arguments */
384 f_login
= 4, /* Execute as a login shell */
385 f_dummy
= 8, /* Don't actually do anything */
386 f_setuid
= 16 /* We're running setuid */
389 /* --- Set up the program name --- */
393 if (getuid() != geteuid())
396 /* --- Read the environment into a hashtable --- */
401 sym_createTable(&bc__env
);
402 for (p
= environ
; *p
; p
++)
403 bc__putenv(0, *p
, 0, 0);
406 /* --- Parse some command line arguments --- */
410 static struct option opts
[] = {
412 /* --- Asking for help --- */
414 { "help", 0, 0, 'h' },
415 { "usage", 0, 0, 'u' },
416 { "version", 0, 0, 'v' },
418 /* --- Login style options --- */
420 { "preserve-environment", 0, 0, 'e' },
422 { "set-user", 0, 0, 's' },
423 { "login", 0, 0, 'l' },
425 /* --- Command to run options --- */
427 { "command", gFlag_argReq
, 0, 'c' },
429 /* --- Server options --- */
431 { "daemon", 0, 0, 'd' },
432 { "port", gFlag_argReq
, 0, 'p' },
433 { "config-file", gFlag_argReq
, 0, 'f' },
435 /* --- Tracing options --- */
438 { "impersonate", gFlag_argReq
, 0, 'I' },
439 { "trace", gFlag_argOpt
, 0, 'T' },
440 { "trace-level", gFlag_argOpt
, 0, 'L' },
446 i
= mdwopt(argc
, argv
,
447 "-" "huv" "esl" "c:" "dp:f:" T("I:T::L::"),
448 opts
, 0, 0, gFlag_envVar
);
454 /* --- Asking for help --- */
457 bc__help(stdout
, flags
& f_setuid
);
469 /* --- Login style options --- */
481 /* --- Command to run options --- */
487 /* --- Server options --- */
499 /* --- Pretend to be a different user --- *
501 * There are all sorts of nasty implications for this option. Don't
502 * allow it if we're running setuid. Disable the actual login anyway.
508 if (flags
& f_setuid
)
509 moan("shan't allow impersonation while running setuid");
512 if (isdigit((unsigned char)optarg
[0]))
513 pw
= getpwuid(atoi(optarg
));
515 pw
= getpwnam(optarg
);
517 die("can't impersonate unknown user `%s'", optarg
);
518 from_pw
= userdb_copyUser(pw
);
519 rq
.from
= from_pw
->pw_uid
;
526 /* --- Tracing support --- *
528 * Be careful not to zap a file I wouldn't normally be allowed to write
537 if (optarg
== 0 || strcmp(optarg
, "-") == 0)
540 uid_t eu
= geteuid(), ru
= getuid();
543 if (setreuid(eu
, ru
))
548 die("couldn't temporarily give up privileges: %s",
552 if ((fp
= fopen(optarg
, "w")) == 0) {
553 die("couldn't open trace file `%s' for writing: %s",
554 optarg
, strerror(errno
));
558 if (setreuid(ru
, eu
))
562 die("couldn't regain privileges: %s", strerror(errno
));
564 traceon(fp
, TRACE_DFL
);
565 trace(TRACE_MISC
, "become: tracing enabled");
570 /* --- Setting trace levels --- */
576 unsigned int lvl
= 0, l
;
577 const char *p
= optarg
;
579 /* --- Table of tracing facilities --- */
587 static tr lvltbl
[] = {
588 { 'm', TRACE_MISC
, "miscellaneous messages" },
589 { 's', TRACE_SETUP
, "building the request block" },
590 { 'd', TRACE_DAEMON
, "server process" },
591 { 'r', TRACE_RULE
, "ruleset scanning" },
592 { 'c', TRACE_CHECK
, "request checking" },
593 { 'l', TRACE_CLIENT
, "client process" },
594 { 'R', TRACE_RAND
, "random number generator" },
595 { 'C', TRACE_CRYPTO
, "cryptographic processing of requests" },
596 { 'y', TRACE_YACC
, "parsing configuration file" },
597 { 'D', TRACE_DFL
, "default tracing options" },
598 { 'A', TRACE_ALL
, "all tracing options" },
603 /* --- Output some help if there's no arguemnt --- */
611 for (tp
= lvltbl
; tp
->l
; tp
++) {
612 if ((flags
& f_setuid
) == 0 || tp
->l
& ~TRACE_PRIV
)
613 printf("%c -- %s\n", tp
->ch
, tp
->help
);
617 "Also, `+' and `-' options are recognised to turn on and off vavrious\n"
618 "tracing options. For example, `A-r' enables everything except ruleset\n"
619 "tracing, and `A-D+c' is everything except the defaults, but with request\n"
631 for (tp
= lvltbl
; tp
->l
&& *p
!= tp
->ch
; tp
++)
634 if (flags
& f_setuid
)
637 lvl
= sense ?
(lvl
| l
) : (lvl
& ~l
);
639 moan("unknown trace option `%c'", *p
);
645 yydebug
= ((lvl
& TRACE_YACC
) != 0);
650 /* --- Something that wasn't an option --- *
652 * The following nasties are supported:
654 * * NAME=VALUE -- preserve NAME, and give it a VALUE
655 * * NAME= -- preserve NAME, and give it an empty value
656 * * NAME- -- delete NAME
657 * * NAME! -- preserve NAME with existing value
659 * Anything else is either the user name (which is OK) or the start of
660 * the command (in which case I stop and read the rest of the command).
664 size_t sz
= strcspn(optarg
, "=-!");
667 /* --- None of the above --- */
669 if (optarg
[sz
] == 0 || (optarg
[sz
] != '=' && optarg
[sz
+ 1] != 0)) {
678 /* --- Do the appropriate thing --- */
680 switch (optarg
[sz
]) {
682 bc__putenv(0, optarg
, envFlag_preserve
, 1);
686 bc__putenv(optarg
, 0, 0, 1);
690 if ((e
= sym_find(&bc__env
, optarg
, -1, 0, 0)) != 0)
691 e
->f
|= envFlag_preserve
;
696 /* --- Something I didn't understand has occurred --- */
705 if (flags
& f_duff
) {
710 /* --- Switch to daemon mode if requested --- */
712 if (flags
& f_daemon
) {
713 T( trace(TRACE_MISC
, "become: daemon mode requested"); )
714 daemon_init(conffile
, port
);
718 /* --- Open a syslog --- */
720 openlog(quis(), 0, LOG_AUTH
);
722 /* --- Pick out the uid --- */
732 if (isdigit((unsigned char)who
[0]))
733 pw
= getpwuid(atoi(who
));
737 die("unknown user `%s'", who
);
738 to_pw
= userdb_copyUser(pw
);
742 /* --- Fill in the easy bits of the request --- */
748 pw
= getpwuid(rq
.from
);
750 die("who are you? (can't find user %li)", (long)rq
.from
);
751 from_pw
= userdb_copyUser(pw
);
754 /* --- Find the local host address --- */
760 if ((he
= gethostbyname(u
.nodename
)) == 0)
761 die("who am I? (can't resolve `%s')", u
.nodename
);
762 memcpy(&rq
.host
, he
->h_addr
, sizeof(struct in_addr
));
765 /* --- Shell commands are easy --- */
772 /* --- A command given on the command line isn't too hard --- */
774 else if (optind
< argc
) {
775 todo
= argv
+ optind
;
779 else switch (style
) {
781 /* --- An unadorned becoming requires little work --- */
784 shell
[0] = getenv("SHELL");
786 shell
[0] = from_pw
->pw_shell
;
792 /* --- An su-like login needs slightly less effort --- */
795 shell
[0] = to_pw
->pw_shell
;
801 /* --- A login request needs a little bit of work --- */
804 const char *p
= strrchr(to_pw
->pw_shell
, '/');
810 shell
[0] = xmalloc(strlen(p
) + 2);
812 strcpy(shell
[0] + 1, p
);
815 binary
= to_pw
->pw_shell
;
816 chdir(to_pw
->pw_dir
);
820 /* --- Mangle the environment --- *
822 * This keeps getting more complicated all the time. (How true. Now I've
823 * got all sorts of nasty environment mangling to do.)
825 * The environment stuff now happens in seven phases:
827 * 1. Mark very special variables to be preserved. Currently only TERM
828 * and DISPLAY are treated in this way.
830 * 2. Set and preserve Become's own environment variables.
832 * 3. Set and preserve the user identity variables (USER, LOGNAME, HOME,
833 * SHELL and MAIL) if we're being `su'-like or `login'-like.
835 * 4. If we're preserving the environment or being `su'-like, process the
836 * PATH variable a little. Otherwise reset it to something
839 * 5. If we're being `login'-like, expunge all unpreserved variables.
841 * 6. Expunge any security-critical variables.
843 * 7. Build a new environment table to pass to child processes.
847 /* --- Variables to be preserved always --- *
849 * A user can explicitly expunge a variable in this list, in which case
850 * we never get to see it here.
853 static char *preserve
[] = {
857 /* --- Variables to be expunged --- *
859 * Any environment string which has one of the following as a prefix will
860 * be expunged from the environment passed to the called process. The
861 * first line lists variables which have been used to list search paths
862 * for shared libraries: by manipulating these, an attacker could replace
863 * a standard library with one of his own. The second line lists other
864 * well-known dangerous environment variables.
867 static char *banned
[] = {
868 "-LD_", "SHLIB_PATH", "LIBPATH", "-_RLD_",
869 "IFS", "ENV", "BASH_ENV", "KRB_CONF",
873 /* --- Other useful variables --- */
882 /* --- Stage one. Preserve display-specific variables --- */
884 for (pp
= preserve
; *pp
; pp
++) {
885 if ((e
= sym_find(&bc__env
, *pp
, -1, 0, 0)) != 0)
886 e
->f
|= envFlag_preserve
;
889 /* --- Stage two. Set Become's own variables --- */
891 e
= sym_find(&bc__env
, "BECOME_ORIGINAL_USER", -1, sizeof(*e
), &f
);
893 bc__setenv(e
, from_pw
->pw_name
);
894 e
->f
|= envFlag_preserve
;
896 e
= sym_find(&bc__env
, "BECOME_ORIGINAL_HOME", -1, sizeof(*e
), &f
);
898 bc__setenv(e
, from_pw
->pw_dir
);
899 e
->f
|= envFlag_preserve
;
901 bc__putenv("BECOME_OLD_USER", from_pw
->pw_name
, envFlag_preserve
, 0);
902 bc__putenv("BECOME_OLD_HOME", from_pw
->pw_dir
, envFlag_preserve
, 0);
903 bc__putenv("BECOME_USER", to_pw
->pw_name
, envFlag_preserve
, 0);
904 bc__putenv("BECOME_HOME", to_pw
->pw_dir
, envFlag_preserve
, 0);
906 /* --- Stage three. Set user identity --- */
910 static char *maildirs
[] = {
911 "/var/spool/mail", "/var/mail",
912 "/usr/spool/mail", "/usr/mail",
918 for (pp
= maildirs
; *pp
; pp
++) {
919 if (stat(*pp
, &s
) == 0 && S_ISDIR(s
.st_mode
)) {
920 sprintf(b
, "%s/%s", *pp
, to_pw
->pw_name
);
921 bc__putenv("MAIL", b
, envFlag_preserve
, 0);
928 bc__putenv("USER", to_pw
->pw_name
, envFlag_preserve
, 0);
929 bc__putenv("LOGNAME", to_pw
->pw_name
, envFlag_preserve
, 0);
930 bc__putenv("HOME", to_pw
->pw_dir
, envFlag_preserve
, 0);
931 bc__putenv("SHELL", to_pw
->pw_shell
, envFlag_preserve
, 0);
935 /* --- Stage four. Set the user's PATH properly --- */
938 /* --- Find an existing path --- *
940 * If there's no path, or this is a login, then set a default path,
941 * unless we're meant to preserve the existing one. Whew!
944 e
= sym_find(&bc__env
, "PATH", -1, sizeof(*e
), &f
);
946 if (!f
|| (style
== l_login
&& ~e
->f
& envFlag_preserve
)) {
948 rq
.to ?
"/usr/bin:/bin" : "/usr/bin:/usr/sbin:/bin:/sbin",
949 envFlag_preserve
, 0);
952 /* --- Find the string --- */
954 e
->f
= envFlag_preserve
;
955 p
= strchr(e
->val
, '=') + 1;
958 /* --- Write the new version to a dynamically allocated buffer --- */
960 e
->val
= xmalloc(4 + 1 + strlen(p
) + 1);
961 strcpy(e
->val
, "PATH=");
964 for (p
= strtok(p
, ":"); p
; p
= strtok(0, ":")) {
979 /* --- Stages five and six. Expunge variables and count numbers --- *
981 * Folded together, so I only need one pass through the table. Also
982 * count the number of variables needed at this time.
987 for (sym_createIter(&i
, &bc__env
); (e
= sym_next(&i
)) != 0; ) {
989 /* --- Login style expunges all unpreserved variables --- */
991 if (style
== l_login
&& ~e
->f
& envFlag_preserve
)
994 /* --- Otherwise just check the name against the list --- */
996 for (pp
= banned
; *pp
; pp
++) {
999 if (memcmp(e
->_base
.name
, p
, strlen(p
)) == 0)
1001 } else if (strcmp(e
->_base
.name
, p
) == 0)
1009 sym_remove(&bc__env
, e
);
1012 /* --- Stage seven. Build the new environment block --- */
1014 env
= qq
= xmalloc((sz
+ 1) * sizeof(*qq
));
1016 for (sym_createIter(&i
, &bc__env
); (e
= sym_next(&i
)) != 0; )
1021 /* --- Trace the command --- */
1023 IF_TRACING(TRACE_SETUP
, {
1026 trace(TRACE_SETUP
, "setup: from user %s to user %s",
1027 from_pw
->pw_name
, to_pw
->pw_name
);
1028 trace(TRACE_SETUP
, "setup: binary == `%s'", binary
);
1029 for (i
= 0; todo
[i
]; i
++)
1030 trace(TRACE_SETUP
, "setup: arg %i == `%s'", i
, todo
[i
]);
1031 for (i
= 0; env
[i
]; i
++)
1032 trace(TRACE_SETUP
, "setup: env %i == `%s'", i
, env
[i
]);
1035 /* --- If necessary, resolve the path to the command --- */
1037 if (!strchr(binary
, '/')) {
1041 if ((p
= getenv("PATH")) == 0)
1042 p
= "/bin:/usr/bin";
1045 for (p
= strtok(path
, ":"); p
; p
= strtok(0, ":")) {
1047 /* --- Check length of string before copying --- */
1049 if (strlen(p
) + strlen(binary
) + 2 > sizeof(rq
.cmd
))
1052 /* --- Now build the pathname and check it --- */
1054 sprintf(rq
.cmd
, "%s/%s", p
, todo
[0]);
1055 if (stat(rq
.cmd
, &st
) == 0 && /* Check it exists */
1056 st
.st_mode
& 0111 && /* Check it's executable */
1057 S_ISREG(st
.st_mode
)) /* Check it's a file */
1062 die("couldn't find `%s' in path", todo
[0]);
1066 T( trace(TRACE_SETUP
, "setup: resolve binary to `%s'", binary
); )
1068 /* --- Canonicalise the path string, if necessary --- */
1075 /* --- Insert current directory name if path not absolute --- */
1080 if (!getcwd(b
, sizeof(b
)))
1081 die("couldn't read current directory: %s", strerror(errno
));
1086 /* --- Now copy over characters from the path string --- */
1090 /* --- Check for buffer overflows here --- *
1092 * I write at most one byte per iteration so this is OK. Remember to
1093 * allow one for the null byte.
1096 if (p
>= b
+ sizeof(b
) - 1)
1097 die("internal error: buffer overflow while canonifying path");
1099 /* --- Reduce multiple slashes to just one --- */
1107 /* --- Handle dots in filenames --- *
1109 * @p[-1]@ is valid here, because if @*q@ is not a `/' then either
1110 * we've just stuck the current directory on the end of the buffer,
1111 * or we've just put something else on the end.
1114 else if (*q
== '.' && p
[-1] == '/') {
1116 /* --- A simple `./' just gets removed --- */
1118 if (q
[1] == 0 || q
[1] == '/') {
1124 /* --- A `../' needs to be peeled back to the previous `/' --- */
1126 if (q
[1] == '.' && (q
[2] == 0 || q
[2] == '/')) {
1129 while (p
> b
&& p
[-1] != '/')
1142 T( trace(TRACE_SETUP
, "setup: canonify binary to `%s'", rq
.cmd
); )
1144 /* --- Run the check --- *
1146 * If the user is already what she wants to be, then print a warning.
1147 * Then, if I was just going to spawn a shell, quit, to reduce user
1148 * confusion. Otherwise, do what was wanted anyway.
1151 if (rq
.from
== rq
.to
) {
1152 moan("you already are `%s'!", to_pw
->pw_name
);
1153 if (!cmd
&& todo
== shell
) {
1154 moan("(to prevent confusion, I'm not spawning a shell)");
1161 "permission %s for %s to become %s to run `%s'",
1162 a ?
"granted" : "denied", from_pw
->pw_name
, to_pw
->pw_name
,
1166 die("permission denied");
1169 /* --- Now do the job --- */
1171 T( trace(TRACE_MISC
, "become: permission granted"); )
1173 if (flags
& f_dummy
) {
1174 puts("permission granted");
1177 if (setuid(rq
.to
) == -1)
1178 die("couldn't set uid: %s", strerror(errno
));
1180 execve(rq
.cmd
, todo
, env
);
1181 die("couldn't exec `%s': %s", rq
.cmd
, strerror(errno
));
1186 /*----- That's all, folks -------------------------------------------------*/