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 <afs/param.h>
18 #include <sys/types.h>
24 #include <netinet/in.h>
33 #define NOPAG 0xffffffff /* Also defined in afs/afs.h */
34 static afs_int32 hostAddr = 0;
35 static int hostAddrLookup = 0;
36 char *afs_server=0, server_name[128];
38 static afs_int32 SetClientCreds();
40 /* Picks up the name of the remote afs client host where the rmtsys
41 * daemon resides. Since the clients may be diskless and/or readonly
42 * ones we felt it's better to rely on an shell environment
43 * (AFSSERVER) for the host name first. If that is not set, the
44 * $HOME/.AFSSERVER file is checked, otherwise the "/.AFSSERVER" is
47 afs_int32 GetAfsServerAddr(syscall)
50 register struct hostent *th;
54 /* Take advantage of caching and assume that the remote host
55 * address won't change during a single program's invocation.
61 if (!(afs_server = getenv("AFSSERVER"))) {
66 if (!(home_dir = getenv("HOME"))) {
67 /* Our last chance is the "/.AFSSERVER" file */
68 fp = fopen("/.AFSSERVER", "r");
72 fgets(server_name, 128, fp);
77 sprintf(pathname, "%s/%s", home_dir, ".AFSSERVER");
78 fp = fopen(pathname, "r");
80 /* Our last chance is the "/.AFSSERVER" file */
81 fp = fopen("/.AFSSERVER", "r");
86 fgets(server_name, 128, fp);
89 len = strlen(server_name);
93 if (server_name[len-1] == '\n') {
94 server_name[len-1] = 0;
96 afs_server = server_name;
98 th = gethostbyname(afs_server);
100 printf("host %s not found; %s call aborted\n", afs_server, syscall);
103 bcopy(th->h_addr, &hostAddr, sizeof(hostAddr));
108 /* Does the actual RX connection to the afs server */
109 struct rx_connection *rx_connection(errorcode, syscall)
110 afs_int32 *errorcode;
113 struct rx_connection *conn;
114 struct rx_securityClass *null_securityObject;
116 if (!(host = GetAfsServerAddr(syscall))) {
118 return (struct rx_connection *)0;
120 *errorcode = rx_Init(0);
122 printf("Rx initialize failed \n");
123 return (struct rx_connection *)0;
125 null_securityObject = rxnull_NewClientSecurityObject();
126 conn = rx_NewConnection(host, htons(AFSCONF_RMTSYSPORT), RMTSYS_SERVICEID, null_securityObject, 0);
128 printf("Unable to make a new connection\n");
130 return (struct rx_connection *)0;
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.... */
139 #pragma weak setpag = afs_setpag
145 struct rx_connection *conn;
147 afs_int32 errorcode, errornumber, newpag, ngroups, j, groups[NGROUPS_MAX];
149 if (!(conn = rx_connection(&errorcode, "setpag"))) {
150 /* Remote call can't be performed for some reason.
151 * Try the local 'setpag' system call ... */
152 errorcode = lsetpag();
155 ngroups = SetClientCreds(&creds, groups);
156 errorcode = RMTSYS_SetPag(conn, &creds, &newpag, &errornumber);
160 printf("Warning: Remote setpag to %s has failed (err=%d)...\n",
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 */
173 for (j = ngroups - 1; j >= 0; j--) {
174 groups[j + 2] = groups[j];
178 afs_get_groups_from_pag(newpag, &groups[0], &groups[1]);
179 if (setgroups(ngroups, groups) == -1) {
183 errorcode = setuid(getuid());
185 errorcode = setreuid(-1, getuid());
186 #endif /* AFS_HPUX_ENV */
191 /* Remote pioctl(2) client routine */
193 #pragma weak pioctl = afs_pioctl
194 int afs_pioctl(path, cmd, data, follow)
196 int pioctl(path, cmd, data, follow)
199 afs_int32 cmd, follow;
200 struct ViceIoctl *data;
202 struct rx_connection *conn;
204 afs_int32 errorcode, groups[NGROUPS_MAX], errornumber, ins= data->in_size;
205 rmtbulk InData, OutData;
206 char pathname[256], *pathp = pathname, *inbuffer;
207 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
208 extern char *getwd();
209 #define getcwd(x,y) getwd(x)
211 if (!(conn = rx_connection(&errorcode, "pioctl"))) {
212 /* Remote call can't be performed for some reason.
213 * Try the local 'pioctl' system call ... */
214 errorcode = lpioctl(path, cmd, data, follow);
217 (void) SetClientCreds(&creds, groups);
221 if (!(inbuffer = (char *)malloc(ins)))
222 return (-1); /* helpless here */
224 bcopy(data->in, inbuffer, data->in_size);
225 InData.rmtbulk_len = data->in_size;
226 InData.rmtbulk_val = inbuffer;
227 inparam_conversion(cmd, InData.rmtbulk_val, 0);
228 OutData.rmtbulk_len = data->out_size;
229 OutData.rmtbulk_val = data->out;
230 /* We always need to pass absolute pathnames to the remote pioctl since we
231 * lose the current directory value when doing an rpc call. Below we
232 * prepend the current absolute path directory, if the name is relative */
235 /* assuming relative path name */
236 if (getcwd(pathname, 256) == NULL) {
238 printf("getwd failed; exiting\n");
241 strcpy(pathname+strlen(pathname), "/");
242 strcat(pathname, path);
244 strcpy(pathname, path);
247 /* Special meaning for a "NULL" pathname since xdr_string hates nil
248 * pointers, at least on non-RTS; of course the proper solution would
249 * be to change the interface declartion. */
250 strcpy(pathname, NIL_PATHP);
252 errorcode = RMTSYS_Pioctl(conn, &creds, pathp, cmd, follow, &InData,
253 &OutData, &errornumber);
256 errorcode = -1; /* Necessary since errorcode is 0 on
257 * standard remote pioctl errors */
258 if (errno != EDOM && errno != EACCES)
259 printf("Warning: Remote pioctl to %s has failed (err=%d)...\n",
263 /* Do the conversions back to the host order; store the results back
264 * on the same buffer */
265 outparam_conversion(cmd, OutData.rmtbulk_val, 1);
272 int afs_get_pag_from_groups(g0, g1)
275 afs_uint32 h, l, result;
279 if (g0 < 0xc000 && g1 < 0xc000) {
280 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
282 h = (g1 >> 14) + h + h + h;
283 result = ((h << 28) | l);
284 /* Additional testing */
285 if (((result >> 24) & 0xff) == 'A')
294 afs_get_groups_from_pag(pag, g0p, g1p)
296 afs_uint32 *g0p, *g1p;
298 unsigned short g0, g1;
301 g0 = 0x3fff & (pag >> 14);
303 g0 |= ((pag >> 28) / 3) << 14;
304 g1 |= ((pag >> 28) % 3) << 14;
310 static afs_int32 SetClientCreds(creds, groups)
311 struct clientcred *creds;
316 creds->uid = getuid();
317 groups[0] = groups[1] = 0;
318 ngroups = getgroups(NGROUPS_MAX, groups);
319 creds->group0 = groups[0];
320 creds->group1 = groups[1];