sys: retry lsetpag if errno is EINTR
[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 = 0;
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         } else {
69             char *pathname;
70
71             len = asprintf(&pathname, "%s/%s", home_dir, ".AFSSERVER");
72             if (len < 0 || pathname == NULL)
73                 return 0;
74             fp = fopen(pathname, "r");
75             free(pathname);
76
77             if (fp == 0) {
78                 /* Our last chance is the "/.AFSSERVER" file */
79                 fp = fopen("/.AFSSERVER", "r");
80                 if (fp == 0) {
81                     return 0;
82                 }
83             }
84         }
85         if (fgets(server_name, 128, fp) != NULL)
86             len = strlen(server_name);
87         fclose(fp);
88         if (len == 0) {
89             return 0;
90         }
91         if (server_name[len - 1] == '\n') {
92             server_name[len - 1] = 0;
93         }
94         afs_server = server_name;
95     }
96     th = gethostbyname(afs_server);
97     if (!th) {
98         printf("host %s not found; %s call aborted\n", afs_server, syscall);
99         return 0;
100     }
101     memcpy(&hostAddr, th->h_addr, sizeof(hostAddr));
102     return hostAddr;
103 }
104
105
106 /* Does the actual RX connection to the afs server */
107 struct rx_connection *
108 rx_connection(afs_int32 * errorcode, char *syscall)
109 {
110     struct rx_connection *conn;
111     struct rx_securityClass *null_securityObject;
112     afs_int32 host;
113
114     if (!(host = GetAfsServerAddr(syscall))) {
115         *errorcode = -1;
116         return (struct rx_connection *)0;
117     }
118     *errorcode = rx_Init(0);
119     if (*errorcode) {
120         printf("Rx initialize failed \n");
121         return (struct rx_connection *)0;
122     }
123     null_securityObject = rxnull_NewClientSecurityObject();
124     conn =
125         rx_NewConnection(host, htons(AFSCONF_RMTSYSPORT), RMTSYS_SERVICEID,
126                          null_securityObject, RX_SECIDX_NULL);
127     if (!conn) {
128         printf("Unable to make a new connection\n");
129         *errorcode = -1;
130         return (struct rx_connection *)0;
131     }
132     return conn;
133 }
134
135
136 /* WARNING: The calling program (i.e. klog) MUST be suid-root since we need to
137  * do a setgroups(2) call with the new pag.... */
138 int
139 setpag(void)
140 {
141     struct rx_connection *conn;
142     clientcred creds;
143     afs_int32 errorcode, errornumber, newpag, ngroups, j;
144     afs_uint32 groups[NGROUPS_MAX];
145
146     if (!(conn = rx_connection(&errorcode, "setpag"))) {
147         /* Remote call can't be performed for some reason.
148          * Try the local 'setpag' system call ... */
149         do {
150             errorcode = lsetpag();
151         } while (errorcode && errno == EINTR);
152
153         return errorcode;
154     }
155     ngroups = SetClientCreds(&creds, groups);
156     errorcode = RMTSYS_SetPag(conn, &creds, &newpag, &errornumber);
157     if (errornumber) {
158         errno = errornumber;
159         errorcode = -1;
160         printf("Warning: Remote setpag to %s has failed (err=%d)...\n",
161                afs_server, errno);
162     }
163     if (errorcode) {
164         return errorcode;
165     }
166     if (afs_get_pag_from_groups(groups[0], groups[1]) == NOPAG) {
167         /* we will have to shift grouplist to make room for pag */
168         if (ngroups + 2 > NGROUPS_MAX) {
169             /* this is what the real setpag returns */
170             errno = E2BIG;
171             return -1;
172         }
173         for (j = ngroups - 1; j >= 0; j--) {
174             groups[j + 2] = groups[j];
175         }
176         ngroups += 2;
177     }
178     afs_get_groups_from_pag(newpag, &groups[0], &groups[1]);
179     if (setgroups(ngroups, groups) == -1) {
180         return -1;
181     }
182 #ifdef  AFS_HPUX_ENV
183     errorcode = setuid(getuid());
184 #else
185     errorcode = setreuid(-1, getuid());
186 #endif /* AFS_HPUX_ENV */
187     return errorcode;
188 }
189
190
191 /* Remote pioctl(2) client routine */
192 int
193 pioctl(char *path, afs_int32 cmd, struct ViceIoctl *data, afs_int32 follow)
194 {
195     struct rx_connection *conn;
196     clientcred creds;
197     afs_int32 errorcode, errornumber, ins = data->in_size;
198     afs_uint32 groups[NGROUPS_MAX];
199     rmtbulk InData, OutData;
200     char pathname[256], *pathp = pathname, *inbuffer;
201     if (!(conn = rx_connection(&errorcode, "pioctl"))) {
202         /* Remote call can't be performed for some reason.
203          * Try the local 'pioctl' system call ... */
204         errorcode = lpioctl(path, cmd, data, follow);
205         return errorcode;
206     }
207     (void)SetClientCreds(&creds, groups);
208     if (!(inbuffer = malloc(ins)))
209         return (-1);            /* helpless here */
210     if (data->in_size)
211         memcpy(inbuffer, data->in, data->in_size);
212     InData.rmtbulk_len = data->in_size;
213     InData.rmtbulk_val = inbuffer;
214     inparam_conversion(cmd, InData.rmtbulk_val, 0);
215
216     OutData.rmtbulk_len = MAXBUFFERLEN * sizeof(*OutData.rmtbulk_val);
217     OutData.rmtbulk_val = malloc(OutData.rmtbulk_len);
218     if (!OutData.rmtbulk_val) {
219         free(inbuffer);
220         return -1;
221     }
222
223     /* We always need to pass absolute pathnames to the remote pioctl since we
224      * lose the current directory value when doing an rpc call. Below we
225      * prepend the current absolute path directory, if the name is relative */
226     if (path) {
227         if (*path != '/') {
228             /* assuming relative path name */
229             if (getcwd(pathname, 256) == NULL) {
230                 free(inbuffer);
231                 printf("getwd failed\n");
232                 return -1;
233             }
234             strcpy(pathname + strlen(pathname), "/");
235             strcat(pathname, path);
236         } else {
237             strcpy(pathname, path);
238         }
239     } else {
240         /* Special meaning for a "NULL" pathname since xdr_string hates nil
241          * pointers, at least on non-RTS; of course the proper solution would
242          * be to change the interface declartion. */
243         strcpy(pathname, NIL_PATHP);
244     }
245     errorcode =
246         RMTSYS_Pioctl(conn, &creds, pathp, cmd, follow, &InData, &OutData,
247                       &errornumber);
248     if (errornumber) {
249         errno = errornumber;
250         errorcode = -1;         /* Necessary since errorcode is 0 on
251                                  * standard remote pioctl errors */
252         if (errno != EDOM && errno != EACCES)
253             printf("Warning: Remote pioctl to %s has failed (err=%d)...\n",
254                    afs_server, errno);
255     }
256     if (!errorcode) {
257         /* Do the conversions back to the host order; store the results back
258          * on the same buffer */
259         if (data->out_size < OutData.rmtbulk_len) {
260             errno = EINVAL;
261             errorcode = -1;
262         } else {
263             memcpy(data->out, OutData.rmtbulk_val, data->out_size);
264             outparam_conversion(cmd, data->out, 1);
265         }
266     }
267     free(OutData.rmtbulk_val);
268     free(inbuffer);
269     return errorcode;
270 }
271
272
273 int
274 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 void
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
310 SetClientCreds(struct clientcred *creds, afs_uint32 * groups)
311 {
312     afs_int32 ngroups;
313
314     creds->uid = getuid();
315     groups[0] = groups[1] = 0;
316     ngroups = getgroups(NGROUPS_MAX, groups);
317     creds->group0 = groups[0];
318     creds->group1 = groups[1];
319     return ngroups;
320 }