xref: /freebsd/crypto/heimdal/appl/afsutil/pagsh.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
1 /*
2  * Copyright (c) 1995, 1996, 1997, 1998, 1999 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.4 2000/10/02 05:05:53 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 
66 /*
67  * Run command with a new ticket file / credentials cache / token
68  */
69 
70 int
71 main(int argc, char **argv)
72 {
73   int f;
74   char tf[1024];
75   char *p;
76 
77   char *path;
78   char **args;
79   int i;
80 
81 #ifdef KRB5
82   snprintf (tf, sizeof(tf), "%sXXXXXX", KRB5_DEFAULT_CCROOT);
83   f = mkstemp (tf + 5);
84   close (f);
85   unlink (tf + 5);
86   esetenv("KRB5CCNAME", tf, 1);
87 #endif
88 
89 #ifdef KRB4
90   snprintf (tf, sizeof(tf), "%s_XXXXXX", TKT_ROOT);
91   f = mkstemp (tf);
92   close (f);
93   unlink (tf);
94   esetenv("KRBTKFILE", tf, 1);
95 #endif
96 
97   i = 0;
98 
99   args = (char **) malloc((argc + 10)*sizeof(char *));
100   if (args == NULL)
101       errx (1, "Out of memory allocating %lu bytes",
102 	    (unsigned long)((argc + 10)*sizeof(char *)));
103 
104   argv++;
105 
106   if(*argv == NULL) {
107     path = getenv("SHELL");
108     if(path == NULL){
109       struct passwd *pw = k_getpwuid(geteuid());
110       path = strdup(pw->pw_shell);
111     }
112   } else {
113     if(strcmp(*argv, "-c") ==  0) argv++;
114     path = strdup(*argv++);
115   }
116   if (path == NULL)
117       errx (1, "Out of memory copying path");
118 
119   p=strrchr(path, '/');
120   if(p)
121     args[i] = strdup(p+1);
122   else
123     args[i] = strdup(path);
124 
125   if (args[i++] == NULL)
126       errx (1, "Out of memory copying arguments");
127 
128   while(*argv)
129     args[i++] = *argv++;
130 
131   args[i++] = NULL;
132 
133   if(k_hasafs())
134     k_setpag();
135 
136   unsetenv("PAGPID");
137   execvp(path, args);
138   if (errno == ENOENT) {
139       char **sh_args = malloc ((i + 2) * sizeof(char *));
140       int j;
141 
142       if (sh_args == NULL)
143 	  errx (1, "Out of memory copying sh arguments");
144       for (j = 1; j < i; ++j)
145 	  sh_args[j + 2] = args[j];
146       sh_args[0] = "sh";
147       sh_args[1] = "-c";
148       sh_args[2] = path;
149       execv ("/bin/sh", sh_args);
150   }
151   err (1, "execvp");
152 }
153