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