makefiles-attempt-to-support-gmake-j-20011023
[openafs.git] / src / sys / pagsh.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #ifdef  AFS_AIX32_ENV
16 #include <signal.h>
17 #endif
18 #include <stdio.h>
19 #include <stdlib.h>
20 #ifndef AFS_NT40_ENV
21 #include <unistd.h>
22 #endif
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #endif
26 #include <pwd.h>
27 #ifdef AFS_KERBEROS_ENV
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #endif
31
32 #include "AFS_component_version_number.c"
33
34 extern afs_int32 setpag();
35
36 int main(argc, argv)
37 int argc;
38 char **argv;
39 {
40         struct passwd *pwe;
41         int uid, gid;
42         char *shell = "/bin/sh";
43
44 #ifdef  AFS_AIX32_ENV
45     /*
46      * The following signal action for AIX is necessary so that in case of a 
47      * crash (i.e. core is generated) we can include the user's data section 
48      * in the core dump. Unfortunately, by default, only a partial core is
49      * generated which, in many cases, isn't too useful.
50      */
51     struct sigaction nsa;
52     
53     sigemptyset(&nsa.sa_mask);
54     nsa.sa_handler = SIG_DFL;
55     nsa.sa_flags = SA_FULLDUMP;
56     sigaction(SIGSEGV, &nsa, NULL);
57 #endif
58         gid = getgid();
59         uid = getuid();
60         pwe = getpwuid(uid);
61         if (pwe == 0) {
62                 fprintf(stderr, "Intruder alert.\n");
63         } else {
64 /*              shell = pwe->pw_shell; */
65         }
66         if (setpag() == -1) {
67                 perror("setpag");
68         }
69 #ifdef AFS_KERBEROS_ENV
70         ktc_newpag();
71 #endif
72         (void) setuid(uid);
73         (void) setgid(gid);
74         argv[0] = shell;
75         execvp(shell, argv);
76         perror(shell);
77         fprintf(stderr, "No shell\n");
78         exit(1);
79 }
80
81
82 #ifdef AFS_KERBEROS_ENV
83 /* stolen from auth/ktc.c */
84
85 static afs_uint32 curpag()
86 {
87     afs_uint32 groups[30];
88     afs_uint32 g0, g1;
89     afs_uint32 h, l, ret;
90
91     if (getgroups(30, groups) < 2) return 0;
92
93     g0 = groups[0] & 0xffff;
94     g1 = groups[1] & 0xffff;
95     g0 -= 0x3f00;
96     g1 -= 0x3f00;
97     if ((g0 < 0xc000) && (g1 < 0xc000)) {
98         l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
99         h = (g0 >> 14);
100         h = (g1 >> 14) + h + h + h;
101         ret = ((h << 28) | l);
102         /* Additional testing */
103         if (((ret >> 24) & 0xff) == 'A')
104             return ret;
105         else
106             return -1;
107     }
108     return -1;
109 }
110
111 ktc_newpag()
112 {
113     extern char **environ;
114
115     afs_uint32 pag;
116     struct stat sbuf;
117     char fname[256], *prefix = "/ticket/";
118     int numenv;
119     char **newenv, **senv, **denv;
120
121     if (stat("/ticket", &sbuf) == -1) {
122         prefix = "/tmp/tkt";
123     }
124
125     pag = curpag() & 0xffffffff;
126     if (pag == -1) {
127         sprintf(fname, "%s%d", prefix, getuid());
128     }
129     else {
130         sprintf(fname, "%sp%ld", prefix, pag);
131     }
132 /*    ktc_set_tkt_string(fname); */
133
134     for (senv=environ, numenv=0; *senv; senv++) numenv++;
135     newenv = (char **)malloc((numenv+2) * sizeof(char *));
136
137     for (senv=environ, denv=newenv; *senv; *senv++) {
138         if (strncmp(*senv, "KRBTKFILE=", 10) != 0) *denv++ = *senv;
139     }
140
141     *denv = malloc(10 + strlen(fname) + 1);
142     strcpy(*denv, "KRBTKFILE=");
143     strcat(*denv, fname);
144     *++denv = 0;
145     environ = newenv;
146 }
147
148 #endif
149
150