curpag-via-pioctl-20090603
[openafs.git] / src / afsweb / apache_afs_utils.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 /*
11  * This file contains routines common to both the client and the server-
12  * primarily an interface routine to the pioctl call
13  * and a routine for setting the primary flag in the token structure in
14  * order to create a new PAG while doing the SET TOKEN. In addition there are
15  * debugging routines of interest to both the client and server processes
16  */
17
18 #include "apache_afs_utils.h"
19 #include "apache_afs_utils.h"
20
21 /* 
22  * do_pioctl does the actual call to pioctl (or equivalent for that platform)
23  * sets up the ViceIoctl buffer with all the parameters required
24  * NOTE: in_buffer and out_buffer may point to the same memory buffer
25  */
26 int
27 do_pioctl(char *in_buffer, int in_size, char *out_buffer, int out_size,
28           int opcode, char *path, int followSymLinks)
29 {
30     struct ViceIoctl iob;
31     iob.in = in_buffer;
32     iob.in_size = in_size;
33     iob.out = out_buffer;
34     iob.out_size = out_size;
35
36 #ifdef AFS_USR_SUN5_ENV
37     return syscall(AFS_SYSCALL, AFSCALL_PIOCTL, path, _VICEIOCTL(opcode),
38                    &iob, followSymLinks);
39 #else /* AFS_USR_SUN5_ENV */
40     return lpioctl(path, _VICEIOCTL(opcode), &iob, followSymLinks);
41 #endif /* AFS_USR_SUN5_ENV */
42 }
43
44 int
45 do_setpag()
46 {
47     return lsetpag();
48 }
49
50 /*
51  * Return 1 if we have a token, 0 if we don't
52  */
53 int
54 haveToken()
55 {
56     char temp[1024];
57     afs_int32 i = 0;
58     int code;
59
60     memcpy((void *)temp, (void *)&i, sizeof(afs_int32));
61     code =
62         do_pioctl(temp, sizeof(afs_int32), temp, sizeof(temp), VIOCGETTOK,
63                   NULL, 0);
64     if (code)
65         return 0;
66     else
67         return 1;
68 }
69
70
71 /* 
72  * flipPrimary sets the primary cell longword - 
73  * enabling a SETPAG if the same buffer is used with VICESETTOK 
74  */
75 flipPrimary(char *tokenBuf)
76 {
77     afs_int32 i;
78     char *temp = tokenBuf;
79
80     /* skip over the secret token */
81     memcpy(&i, temp, sizeof(afs_int32));
82     temp += (i + sizeof(afs_int32));
83
84     /* skip over the clear token */
85     memcpy(&i, temp, sizeof(afs_int32));
86     temp += (i + sizeof(afs_int32));
87
88     /* set the primary flag */
89     memcpy(&i, temp, sizeof(afs_int32));
90     i |= 0x8000;
91     memcpy(temp, &i, sizeof(afs_int32));
92     temp += sizeof(afs_int32);
93     return 0;
94 }
95
96 /* Returns the AFS pag number, if any, otherwise return -1 */
97 afs_int32
98 getPAG()
99 {
100     afs_int32 pag;
101
102     assert(sizeof(afs_uint32) == 4);
103     assert(sizeof(afs_int32) == 4);
104
105     pag = ktc_curpag();
106     if (pag == 0 || pag == -1)
107         return -1;
108
109     /* high order byte is always 'A'; actual pag value is low 24 bits */
110     return (pag & 0xFFFFFF);
111 }
112
113 /*
114  * Write out the formatted string to the error log if the specified level
115  * is greater than or equal to the global afsDebugLevel which is set by
116  * the directive SetAFSDebugLevel in the httpd.conf file
117  */
118 /* VARARGS1 */
119 void
120 afsLogError(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
121      char *a;
122      char *b;
123      char *c;
124      char *d;
125      char *e;
126      char *f;
127      char *g;
128      char *h;
129      char *i;
130      char *j;
131      char *k;
132      char *l;
133      char *m;
134      char *n;
135 {
136     char reason[1024];
137
138     sprintf(reason, a, b, c, d, e, f, g, h, i, j, k, l, m, n);
139     /*  LOG_REASON(reason,r->uri,r); */
140     strcat(reason, "\n");
141     fprintf(stderr, reason);
142 }
143
144
145 /* the following are debug utilities */
146
147 void
148 hexDump(char *tbuffer, int len)
149 {
150     int i;
151     int byte;
152     char *temp = tbuffer;
153
154     fprintf(stderr, "HEXDUMP:\n");
155     for (i = 0; i < len; i++) {
156         byte = *temp;
157         temp++;
158         fprintf(stderr, "%x", byte);
159     }
160     fprintf(stderr, "\n");
161 }
162
163 /* 
164  * debugging utility to see if the group id changes - if SETPAG worked 
165  * call this before and after the routine to dosetpag and verify results 
166  */
167
168 int
169 printGroups()
170 {
171     int numGroups, i;
172     gid_t grouplist[NGROUPS_MAX];
173
174     numGroups = getgroups(NGROUPS_MAX, &grouplist[0]);
175     if (numGroups == -1) {
176         perror("getgroups:");
177         return -1;
178     }
179     for (i = 0; i < numGroups; i++) {
180         fprintf(stderr, "grouplist[%d]=%d\n", i, grouplist[i]);
181     }
182     return 0;
183 }
184
185 /* 
186  * prints clear token fields, cell name and primary flag to stdout 
187  */
188
189 void
190 parseToken(char *buf)
191 {
192     afs_int32 len = 0;
193     char cellName[64];
194     register char *tp;
195
196     struct ClearToken {
197         afs_int32 AuthHandle;
198         char HandShakeKey[8];
199         afs_int32 ViceId;
200         afs_int32 BeginTimestamp;
201         afs_int32 EndTimestamp;
202     } clearToken;
203
204     assert(buf != NULL);
205
206     tp = buf;
207     memcpy(&len, tp, sizeof(afs_int32));        /* get size of secret token */
208     tp += (sizeof(afs_int32) + len);    /* skip secret token */
209
210     memcpy(&len, tp, sizeof(afs_int32));        /* get size of clear token */
211     if (len != sizeof(struct ClearToken)) {
212         fprintf(stderr,
213                 "weblog:parseToken: error getting length of ClearToken\n");
214         return;
215     }
216
217     tp += sizeof(afs_int32);    /* skip length of cleartoken */
218     memcpy(&clearToken, tp, sizeof(struct ClearToken)); /* copy cleartoken */
219
220     tp += len;                  /* skip clear token itself */
221
222     memcpy(&len, tp, sizeof(afs_int32));        /* copy the primary flag */
223     tp += sizeof(afs_int32);    /* skip primary flag */
224
225     /* tp now points to the cell name */
226     strcpy(cellName, tp);
227
228     fprintf(stderr, "CellName:%s\n", cellName);
229     fprintf(stderr, "Primary Flag (Hex):%x\n", len);
230     fprintf(stderr,
231             "ClearToken Fields:-\nAuthHandle=%d\n"
232             "ViceId=%d\nBeginTimestamp=%d\nEndTimestamp=%d\n",
233             clearToken.AuthHandle, clearToken.ViceId,
234             clearToken.BeginTimestamp, clearToken.EndTimestamp);
235 }