aa534ab92e91add7dc2cd1758ccd110e7b314f36
[openafs.git] / src / sys / rmtsysc.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 module (residing in lib/afs/librmtsys.a) implements the client side of
12  * the rpc version (via rx) of non-standard system calls. Currently only rpc
13  * calls of setpag, and pioctl are supported.
14  */
15 #include <afsconfig.h>
16 #include <afs/param.h>
17
18
19 #include <errno.h>
20 #include <limits.h>
21 #include <sys/types.h>
22 #include <afs/vice.h>
23 #ifdef AFS_NT40_ENV
24 #include <winsock2.h>
25 #else
26 #include <netdb.h>
27 #include <netinet/in.h>
28 #include <sys/file.h>
29 #endif
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>    
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #include <rx/xdr.h>
38 #include "rmtsys.h"
39 #include "sys_prototypes.h"
40
41
42 #define NOPAG       0xffffffff  /* Also defined in afs/afs.h */
43 static afs_int32 hostAddr = 0;
44 static int hostAddrLookup = 0;
45 char *afs_server = 0, server_name[128];
46 static afs_int32 SetClientCreds(struct clientcred *creds, afs_uint32 * groups);
47 int afs_get_pag_from_groups(afs_uint32 g0, afs_uint32 g1);
48 void afs_get_groups_from_pag(afs_uint32 pag, afs_uint32 * g0p, afs_uint32 * g1p);
49
50 /* Picks up the name of the remote afs client host where the rmtsys 
51  * daemon resides. Since the clients may be diskless and/or readonly
52  * ones we felt it's better to rely on an shell environment
53  * (AFSSERVER) for the host name first. If that is not set, the
54  * $HOME/.AFSSERVER file is checked, otherwise the "/.AFSSERVER" is
55  * used.
56  */
57 afs_int32
58 GetAfsServerAddr(char *syscall)
59 {
60     register struct hostent *th;
61
62     if (hostAddrLookup) {
63         /* Take advantage of caching and assume that the remote host
64          * address won't change during a single program's invocation.
65          */
66         return hostAddr;
67     }
68     hostAddrLookup = 1;
69
70     if (!(afs_server = getenv("AFSSERVER"))) {
71         char *home_dir;
72         FILE *fp;
73         int len;
74
75         if (!(home_dir = getenv("HOME"))) {
76             /* Our last chance is the "/.AFSSERVER" file */
77             fp = fopen("/.AFSSERVER", "r");
78             if (fp == 0) {
79                 return 0;
80             }
81             fgets(server_name, 128, fp);
82             fclose(fp);
83         } else {
84             char pathname[256];
85
86             sprintf(pathname, "%s/%s", home_dir, ".AFSSERVER");
87             fp = fopen(pathname, "r");
88             if (fp == 0) {
89                 /* Our last chance is the "/.AFSSERVER" file */
90                 fp = fopen("/.AFSSERVER", "r");
91                 if (fp == 0) {
92                     return 0;
93                 }
94             }
95             fgets(server_name, 128, fp);
96             fclose(fp);
97         }
98         len = strlen(server_name);
99         if (len == 0) {
100             return 0;
101         }
102         if (server_name[len - 1] == '\n') {
103             server_name[len - 1] = 0;
104         }
105         afs_server = server_name;
106     }
107     th = gethostbyname(afs_server);
108     if (!th) {
109         printf("host %s not found; %s call aborted\n", afs_server, syscall);
110         return 0;
111     }
112     memcpy(&hostAddr, th->h_addr, sizeof(hostAddr));
113     return hostAddr;
114 }
115
116
117 /* Does the actual RX connection to the afs server */
118 struct rx_connection *
119 rx_connection(afs_int32 * errorcode, char *syscall)
120 {
121     struct rx_connection *conn;
122     struct rx_securityClass *null_securityObject;
123     afs_int32 host;
124
125     if (!(host = GetAfsServerAddr(syscall))) {
126         *errorcode = -1;
127         return (struct rx_connection *)0;
128     }
129     *errorcode = rx_Init(0);
130     if (*errorcode) {
131         printf("Rx initialize failed \n");
132         return (struct rx_connection *)0;
133     }
134     null_securityObject = rxnull_NewClientSecurityObject();
135     conn =
136         rx_NewConnection(host, htons(AFSCONF_RMTSYSPORT), RMTSYS_SERVICEID,
137                          null_securityObject, 0);
138     if (!conn) {
139         printf("Unable to make a new connection\n");
140         *errorcode = -1;
141         return (struct rx_connection *)0;
142     }
143     return conn;
144 }
145
146
147 /* WARNING: The calling program (i.e. klog) MUST be suid-root since we need to
148  * do a setgroups(2) call with the new pag.... */
149 #ifdef AFS_DUX40_ENV
150 #pragma weak setpag = afs_setpag
151 int
152 afs_setpag(void)
153 #else
154 int
155 setpag(void)
156 #endif
157 {
158     struct rx_connection *conn;
159     clientcred creds;
160     afs_int32 errorcode, errornumber, newpag, ngroups, j;
161     afs_uint32 groups[NGROUPS_MAX];
162     
163     if (!(conn = rx_connection(&errorcode, "setpag"))) {
164         /* Remote call can't be performed for some reason.
165          * Try the local 'setpag' system call ... */
166         errorcode = lsetpag();
167         return errorcode;
168     }
169     ngroups = SetClientCreds(&creds, groups);
170     errorcode = RMTSYS_SetPag(conn, &creds, &newpag, &errornumber);
171     if (errornumber) {
172         errno = errornumber;
173         errorcode = -1;
174         printf("Warning: Remote setpag to %s has failed (err=%d)...\n",
175                afs_server, errno);
176     }
177     if (errorcode) {
178         return errorcode;
179     }
180     if (afs_get_pag_from_groups(groups[0], groups[1]) == NOPAG) {
181         /* we will have to shift grouplist to make room for pag */
182         if (ngroups + 2 > NGROUPS_MAX) {
183             /* this is what the real setpag returns */
184             errno = E2BIG;
185             return -1;
186         }
187         for (j = ngroups - 1; j >= 0; j--) {
188             groups[j + 2] = groups[j];
189         }
190         ngroups += 2;
191     }
192     afs_get_groups_from_pag(newpag, &groups[0], &groups[1]);
193     if (setgroups(ngroups, groups) == -1) {
194         return -1;
195     }
196 #ifdef  AFS_HPUX_ENV
197     errorcode = setuid(getuid());
198 #else
199     errorcode = setreuid(-1, getuid());
200 #endif /* AFS_HPUX_ENV */
201     return errorcode;
202 }
203
204
205 /* Remote pioctl(2) client routine */
206 #ifdef AFS_DUX40_ENV
207 #pragma weak pioctl = afs_pioctl
208 int
209 afs_pioctl(char *path, afs_int32 cmd, struct ViceIoctl *data,
210            afs_int32 follow)
211 #else
212 int
213 pioctl(char *path, afs_int32 cmd, struct ViceIoctl *data, afs_int32 follow)
214 #endif
215 {
216     struct rx_connection *conn;
217     clientcred creds;
218     afs_int32 errorcode, errornumber, ins = data->in_size;
219     afs_uint32 groups[NGROUPS_MAX];
220     rmtbulk InData, OutData;
221     char pathname[256], *pathp = pathname, *inbuffer;
222     if (!(conn = rx_connection(&errorcode, "pioctl"))) {
223         /* Remote call can't be performed for some reason.
224          * Try the local 'pioctl' system call ... */
225         errorcode = lpioctl(path, cmd, (char *)data, follow);
226         return errorcode;
227     }
228     (void)SetClientCreds(&creds, groups);
229 #ifdef  AFS_OSF_ENV
230     if (!ins)
231         ins = 1;
232 #endif
233     if (!(inbuffer = (char *)malloc(ins)))
234         return (-1);            /* helpless here */
235     if (data->in_size)
236         memcpy(inbuffer, data->in, data->in_size);
237     InData.rmtbulk_len = data->in_size;
238     InData.rmtbulk_val = inbuffer;
239     inparam_conversion(cmd, InData.rmtbulk_val, 0);
240
241     OutData.rmtbulk_len = MAXBUFFERLEN * sizeof(*OutData.rmtbulk_val);
242     OutData.rmtbulk_val = malloc(OutData.rmtbulk_len); 
243     if (!OutData.rmtbulk_val) {
244         free(inbuffer);
245         return -1;
246     }
247
248     /* We always need to pass absolute pathnames to the remote pioctl since we
249      * lose the current directory value when doing an rpc call. Below we
250      * prepend the current absolute path directory, if the name is relative */
251     if (path) {
252         if (*path != '/') {
253             /* assuming relative path name */
254             if (getcwd(pathname, 256) == NULL) {
255                 free(inbuffer);
256                 printf("getwd failed; exiting\n");
257                 exit(1);
258             }
259             strcpy(pathname + strlen(pathname), "/");
260             strcat(pathname, path);
261         } else {
262             strcpy(pathname, path);
263         }
264     } else {
265         /* Special meaning for a "NULL" pathname since xdr_string hates nil
266          * pointers, at least on non-RTS; of course the proper solution would
267          * be to change the interface declartion. */
268         strcpy(pathname, NIL_PATHP);
269     }
270     errorcode =
271         RMTSYS_Pioctl(conn, &creds, pathp, cmd, follow, &InData, &OutData,
272                       &errornumber);
273     if (errornumber) {
274         errno = errornumber;
275         errorcode = -1;         /* Necessary since errorcode is 0 on
276                                  * standard remote pioctl errors */
277         if (errno != EDOM && errno != EACCES)
278             printf("Warning: Remote pioctl to %s has failed (err=%d)...\n",
279                    afs_server, errno);
280     }
281     if (!errorcode) {
282         /* Do the conversions back to the host order; store the results back
283          * on the same buffer */
284         if (data->out_size < OutData.rmtbulk_len) {
285             errno = EINVAL;
286             errorcode = -1;
287         } else {
288             memcpy(data->out, OutData.rmtbulk_val, data->out_size);
289             outparam_conversion(cmd, data->out, 1);
290         }
291     }
292     free(OutData.rmtbulk_val);
293     free(inbuffer);
294     return errorcode;
295 }
296
297
298 int
299 afs_get_pag_from_groups(afs_uint32 g0, afs_uint32 g1)
300 {
301     afs_uint32 h, l, result;
302
303     g0 -= 0x3f00;
304     g1 -= 0x3f00;
305     if (g0 < 0xc000 && g1 < 0xc000) {
306         l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
307         h = (g0 >> 14);
308         h = (g1 >> 14) + h + h + h;
309         result = ((h << 28) | l);
310         /* Additional testing */
311         if (((result >> 24) & 0xff) == 'A')
312             return result;
313         else
314             return NOPAG;
315     }
316     return NOPAG;
317 }
318
319 void
320 afs_get_groups_from_pag(afs_uint32 pag, afs_uint32 * g0p, afs_uint32 * g1p)
321 {
322     unsigned short g0, g1;
323
324     pag &= 0x7fffffff;
325     g0 = 0x3fff & (pag >> 14);
326     g1 = 0x3fff & pag;
327     g0 |= ((pag >> 28) / 3) << 14;
328     g1 |= ((pag >> 28) % 3) << 14;
329     *g0p = g0 + 0x3f00;
330     *g1p = g1 + 0x3f00;
331 }
332
333
334 static afs_int32
335 SetClientCreds(struct clientcred *creds, afs_uint32 * groups)
336 {
337     afs_int32 ngroups;
338
339     creds->uid = getuid();
340     groups[0] = groups[1] = 0;
341     ngroups = getgroups(NGROUPS_MAX, groups);
342     creds->group0 = groups[0];
343     creds->group1 = groups[1];
344     return ngroups;
345 }