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