1 /* 2 * Copyright (c) 1995 - 2002 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifdef HAVE_CONFIG_H 35 #include <config.h> 36 #endif 37 38 RCSID("$Id: pagsh.c,v 1.6 2002/08/23 17:54:20 assar Exp $"); 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 #ifdef HAVE_SYS_TYPES_H 45 #include <sys/types.h> 46 #endif 47 #include <time.h> 48 #ifdef HAVE_FCNTL_H 49 #include <fcntl.h> 50 #endif 51 #ifdef HAVE_PWD_H 52 #include <pwd.h> 53 #endif 54 55 #ifdef KRB5 56 #include <krb5.h> 57 #endif 58 #ifdef KRB4 59 #include <krb.h> 60 #endif 61 #include <kafs.h> 62 63 #include <err.h> 64 #include <roken.h> 65 #include <getarg.h> 66 67 static int help_flag; 68 static int version_flag; 69 static int c_flag; 70 71 struct getargs getargs[] = { 72 { NULL, 'c', arg_flag, &c_flag }, 73 { "version", 0, arg_flag, &version_flag }, 74 { "help", 'h', arg_flag, &help_flag }, 75 }; 76 77 static int num_args = sizeof(getargs) / sizeof(getargs[0]); 78 79 static void 80 usage(int ecode) 81 { 82 arg_printusage(getargs, num_args, NULL, "command [args...]"); 83 exit(ecode); 84 } 85 86 /* 87 * Run command with a new ticket file / credentials cache / token 88 */ 89 90 int 91 main(int argc, char **argv) 92 { 93 int f; 94 char tf[1024]; 95 char *p; 96 97 char *path; 98 char **args; 99 int i; 100 int optind = 0; 101 102 set_progname(argv[0]); 103 if(getarg(getargs, num_args, argc, argv, &optind)) 104 usage(1); 105 if(help_flag) 106 usage(0); 107 if(version_flag) { 108 print_version(NULL); 109 exit(0); 110 } 111 112 argc -= optind; 113 argv += optind; 114 115 #ifdef KRB5 116 snprintf (tf, sizeof(tf), "%sXXXXXX", KRB5_DEFAULT_CCROOT); 117 f = mkstemp (tf + 5); 118 close (f); 119 unlink (tf + 5); 120 esetenv("KRB5CCNAME", tf, 1); 121 #endif 122 123 #ifdef KRB4 124 snprintf (tf, sizeof(tf), "%s_XXXXXX", TKT_ROOT); 125 f = mkstemp (tf); 126 close (f); 127 unlink (tf); 128 esetenv("KRBTKFILE", tf, 1); 129 #endif 130 131 i = 0; 132 133 args = (char **) malloc((argc + 10)*sizeof(char *)); 134 if (args == NULL) 135 errx (1, "Out of memory allocating %lu bytes", 136 (unsigned long)((argc + 10)*sizeof(char *))); 137 138 if(*argv == NULL) { 139 path = getenv("SHELL"); 140 if(path == NULL){ 141 struct passwd *pw = k_getpwuid(geteuid()); 142 path = strdup(pw->pw_shell); 143 } 144 } else { 145 path = strdup(*argv++); 146 } 147 if (path == NULL) 148 errx (1, "Out of memory copying path"); 149 150 p=strrchr(path, '/'); 151 if(p) 152 args[i] = strdup(p+1); 153 else 154 args[i] = strdup(path); 155 156 if (args[i++] == NULL) 157 errx (1, "Out of memory copying arguments"); 158 159 while(*argv) 160 args[i++] = *argv++; 161 162 args[i++] = NULL; 163 164 if(k_hasafs()) 165 k_setpag(); 166 167 unsetenv("PAGPID"); 168 execvp(path, args); 169 if (errno == ENOENT) { 170 char **sh_args = malloc ((i + 2) * sizeof(char *)); 171 int j; 172 173 if (sh_args == NULL) 174 errx (1, "Out of memory copying sh arguments"); 175 for (j = 1; j < i; ++j) 176 sh_args[j + 2] = args[j]; 177 sh_args[0] = "sh"; 178 sh_args[1] = "-c"; 179 sh_args[2] = path; 180 execv ("/bin/sh", sh_args); 181 } 182 err (1, "execvp"); 183 } 184