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>
22 #include <sys/types.h>
28 #include <netinet/in.h>
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];
45 static afs_int32 SetClientCreds();
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
54 afs_int32 GetAfsServerAddr(syscall)
57 register struct hostent *th;
61 /* Take advantage of caching and assume that the remote host
62 * address won't change during a single program's invocation.
68 if (!(afs_server = getenv("AFSSERVER"))) {
73 if (!(home_dir = getenv("HOME"))) {
74 /* Our last chance is the "/.AFSSERVER" file */
75 fp = fopen("/.AFSSERVER", "r");
79 fgets(server_name, 128, fp);
84 sprintf(pathname, "%s/%s", home_dir, ".AFSSERVER");
85 fp = fopen(pathname, "r");
87 /* Our last chance is the "/.AFSSERVER" file */
88 fp = fopen("/.AFSSERVER", "r");
93 fgets(server_name, 128, fp);
96 len = strlen(server_name);
100 if (server_name[len-1] == '\n') {
101 server_name[len-1] = 0;
103 afs_server = server_name;
105 th = gethostbyname(afs_server);
107 printf("host %s not found; %s call aborted\n", afs_server, syscall);
110 memcpy(&hostAddr, th->h_addr, sizeof(hostAddr));
115 /* Does the actual RX connection to the afs server */
116 struct rx_connection *rx_connection(errorcode, syscall)
117 afs_int32 *errorcode;
120 struct rx_connection *conn;
121 struct rx_securityClass *null_securityObject;
123 if (!(host = GetAfsServerAddr(syscall))) {
125 return (struct rx_connection *)0;
127 *errorcode = rx_Init(0);
129 printf("Rx initialize failed \n");
130 return (struct rx_connection *)0;
132 null_securityObject = rxnull_NewClientSecurityObject();
133 conn = rx_NewConnection(host, htons(AFSCONF_RMTSYSPORT), RMTSYS_SERVICEID, null_securityObject, 0);
135 printf("Unable to make a new connection\n");
137 return (struct rx_connection *)0;
143 /* WARNING: The calling program (i.e. klog) MUST be suid-root since we need to
144 * do a setgroups(2) call with the new pag.... */
146 #pragma weak setpag = afs_setpag
152 struct rx_connection *conn;
154 afs_int32 errorcode, errornumber, newpag, ngroups, j, groups[NGROUPS_MAX];
156 if (!(conn = rx_connection(&errorcode, "setpag"))) {
157 /* Remote call can't be performed for some reason.
158 * Try the local 'setpag' system call ... */
159 errorcode = lsetpag();
162 ngroups = SetClientCreds(&creds, groups);
163 errorcode = RMTSYS_SetPag(conn, &creds, &newpag, &errornumber);
167 printf("Warning: Remote setpag to %s has failed (err=%d)...\n",
173 if (afs_get_pag_from_groups(groups[0], groups[1]) == NOPAG) {
174 /* we will have to shift grouplist to make room for pag */
175 if (ngroups + 2 > NGROUPS_MAX) {
176 /* this is what the real setpag returns */
180 for (j = ngroups - 1; j >= 0; j--) {
181 groups[j + 2] = groups[j];
185 afs_get_groups_from_pag(newpag, &groups[0], &groups[1]);
186 if (setgroups(ngroups, groups) == -1) {
190 errorcode = setuid(getuid());
192 errorcode = setreuid(-1, getuid());
193 #endif /* AFS_HPUX_ENV */
198 /* Remote pioctl(2) client routine */
200 #pragma weak pioctl = afs_pioctl
201 int afs_pioctl(path, cmd, data, follow)
203 int pioctl(path, cmd, data, follow)
206 afs_int32 cmd, follow;
207 struct ViceIoctl *data;
209 struct rx_connection *conn;
211 afs_int32 errorcode, errornumber, ins= data->in_size;
212 afs_uint32 groups[NGROUPS_MAX];
213 rmtbulk InData, OutData;
214 char pathname[256], *pathp = pathname, *inbuffer;
215 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
216 extern char *getwd();
217 #define getcwd(x,y) getwd(x)
219 if (!(conn = rx_connection(&errorcode, "pioctl"))) {
220 /* Remote call can't be performed for some reason.
221 * Try the local 'pioctl' system call ... */
222 errorcode = lpioctl(path, cmd, data, follow);
225 (void) SetClientCreds(&creds, groups);
229 if (!(inbuffer = (char *)malloc(ins)))
230 return (-1); /* helpless here */
232 memcpy(inbuffer, data->in, data->in_size);
233 InData.rmtbulk_len = data->in_size;
234 InData.rmtbulk_val = inbuffer;
235 inparam_conversion(cmd, InData.rmtbulk_val, 0);
236 OutData.rmtbulk_len = data->out_size;
237 OutData.rmtbulk_val = data->out;
238 /* We always need to pass absolute pathnames to the remote pioctl since we
239 * lose the current directory value when doing an rpc call. Below we
240 * prepend the current absolute path directory, if the name is relative */
243 /* assuming relative path name */
244 if (getcwd(pathname, 256) == NULL) {
246 printf("getwd failed; exiting\n");
249 strcpy(pathname+strlen(pathname), "/");
250 strcat(pathname, path);
252 strcpy(pathname, path);
255 /* Special meaning for a "NULL" pathname since xdr_string hates nil
256 * pointers, at least on non-RTS; of course the proper solution would
257 * be to change the interface declartion. */
258 strcpy(pathname, NIL_PATHP);
260 errorcode = RMTSYS_Pioctl(conn, &creds, pathp, cmd, follow, &InData,
261 &OutData, &errornumber);
264 errorcode = -1; /* Necessary since errorcode is 0 on
265 * standard remote pioctl errors */
266 if (errno != EDOM && errno != EACCES)
267 printf("Warning: Remote pioctl to %s has failed (err=%d)...\n",
271 /* Do the conversions back to the host order; store the results back
272 * on the same buffer */
273 outparam_conversion(cmd, OutData.rmtbulk_val, 1);
280 int afs_get_pag_from_groups(g0, g1)
283 afs_uint32 h, l, result;
287 if (g0 < 0xc000 && g1 < 0xc000) {
288 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
290 h = (g1 >> 14) + h + h + h;
291 result = ((h << 28) | l);
292 /* Additional testing */
293 if (((result >> 24) & 0xff) == 'A')
302 afs_get_groups_from_pag(pag, g0p, g1p)
304 afs_uint32 *g0p, *g1p;
306 unsigned short g0, g1;
309 g0 = 0x3fff & (pag >> 14);
311 g0 |= ((pag >> 28) / 3) << 14;
312 g1 |= ((pag >> 28) % 3) << 14;
318 static afs_int32 SetClientCreds(creds, groups)
319 struct clientcred *creds;
324 creds->uid = getuid();
325 groups[0] = groups[1] = 0;
326 ngroups = getgroups(NGROUPS_MAX, groups);
327 creds->group0 = groups[0];
328 creds->group1 = groups[1];