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