2 * Copyright 2000, International Business Machines Corporation and others.
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
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.
15 #include <afsconfig.h>
16 #include <afs/param.h>
23 #include <sys/types.h>
29 #include <netinet/in.h>
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();
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
56 GetAfsServerAddr(char *syscall)
58 register struct hostent *th;
62 /* Take advantage of caching and assume that the remote host
63 * address won't change during a single program's invocation.
69 if (!(afs_server = getenv("AFSSERVER"))) {
74 if (!(home_dir = getenv("HOME"))) {
75 /* Our last chance is the "/.AFSSERVER" file */
76 fp = fopen("/.AFSSERVER", "r");
80 fgets(server_name, 128, fp);
85 sprintf(pathname, "%s/%s", home_dir, ".AFSSERVER");
86 fp = fopen(pathname, "r");
88 /* Our last chance is the "/.AFSSERVER" file */
89 fp = fopen("/.AFSSERVER", "r");
94 fgets(server_name, 128, fp);
97 len = strlen(server_name);
101 if (server_name[len - 1] == '\n') {
102 server_name[len - 1] = 0;
104 afs_server = server_name;
106 th = gethostbyname(afs_server);
108 printf("host %s not found; %s call aborted\n", afs_server, syscall);
111 memcpy(&hostAddr, th->h_addr, sizeof(hostAddr));
116 /* Does the actual RX connection to the afs server */
117 struct rx_connection *
118 rx_connection(afs_int32 * errorcode, char *syscall)
120 struct rx_connection *conn;
121 struct rx_securityClass *null_securityObject;
124 if (!(host = GetAfsServerAddr(syscall))) {
126 return (struct rx_connection *)0;
128 *errorcode = rx_Init(0);
130 printf("Rx initialize failed \n");
131 return (struct rx_connection *)0;
133 null_securityObject = rxnull_NewClientSecurityObject();
135 rx_NewConnection(host, htons(AFSCONF_RMTSYSPORT), RMTSYS_SERVICEID,
136 null_securityObject, 0);
138 printf("Unable to make a new connection\n");
140 return (struct rx_connection *)0;
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.... */
149 #pragma weak setpag = afs_setpag
157 struct rx_connection *conn;
159 afs_int32 errorcode, errornumber, newpag, ngroups, j, groups[NGROUPS_MAX];
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();
167 ngroups = SetClientCreds(&creds, groups);
168 errorcode = RMTSYS_SetPag(conn, &creds, &newpag, &errornumber);
172 printf("Warning: Remote setpag to %s has failed (err=%d)...\n",
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 */
185 for (j = ngroups - 1; j >= 0; j--) {
186 groups[j + 2] = groups[j];
190 afs_get_groups_from_pag(newpag, &groups[0], &groups[1]);
191 if (setgroups(ngroups, groups) == -1) {
195 errorcode = setuid(getuid());
197 errorcode = setreuid(-1, getuid());
198 #endif /* AFS_HPUX_ENV */
203 /* Remote pioctl(2) client routine */
205 #pragma weak pioctl = afs_pioctl
207 afs_pioctl(char *path, afs_int32 cmd, struct ViceIoctl *data,
211 pioctl(char *path, afs_int32 cmd, struct ViceIoctl *data, afs_int32 follow)
214 struct rx_connection *conn;
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)
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);
230 (void)SetClientCreds(&creds, groups);
235 if (!(inbuffer = (char *)malloc(ins)))
236 return (-1); /* helpless here */
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 */
249 /* assuming relative path name */
250 if (getcwd(pathname, 256) == NULL) {
252 printf("getwd failed; exiting\n");
255 strcpy(pathname + strlen(pathname), "/");
256 strcat(pathname, path);
258 strcpy(pathname, path);
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);
267 RMTSYS_Pioctl(conn, &creds, pathp, cmd, follow, &InData, &OutData,
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",
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);
288 afs_get_pag_from_groups(afs_uint32 g0, afs_uint32 g1)
290 afs_uint32 h, l, result;
294 if (g0 < 0xc000 && g1 < 0xc000) {
295 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
297 h = (g1 >> 14) + h + h + h;
298 result = ((h << 28) | l);
299 /* Additional testing */
300 if (((result >> 24) & 0xff) == 'A')
309 afs_get_groups_from_pag(afs_uint32 pag, afs_uint32 * g0p, afs_uint32 * g1p)
311 unsigned short g0, g1;
314 g0 = 0x3fff & (pag >> 14);
316 g0 |= ((pag >> 28) / 3) << 14;
317 g1 |= ((pag >> 28) % 3) << 14;
324 SetClientCreds(struct clientcred *creds, afs_int32 * groups)
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];