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