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>
44 #define NOPAG 0xffffffff /* Also defined in afs/afs.h */
45 static afs_int32 hostAddr = 0;
46 static int hostAddrLookup = 0;
47 char *afs_server = 0, server_name[128];
48 static afs_int32 SetClientCreds();
50 /* Picks up the name of the remote afs client host where the rmtsys
51 * daemon resides. Since the clients may be diskless and/or readonly
52 * ones we felt it's better to rely on an shell environment
53 * (AFSSERVER) for the host name first. If that is not set, the
54 * $HOME/.AFSSERVER file is checked, otherwise the "/.AFSSERVER" is
58 GetAfsServerAddr(char *syscall)
60 register struct hostent *th;
64 /* Take advantage of caching and assume that the remote host
65 * address won't change during a single program's invocation.
71 if (!(afs_server = getenv("AFSSERVER"))) {
76 if (!(home_dir = getenv("HOME"))) {
77 /* Our last chance is the "/.AFSSERVER" file */
78 fp = fopen("/.AFSSERVER", "r");
82 fgets(server_name, 128, fp);
87 sprintf(pathname, "%s/%s", home_dir, ".AFSSERVER");
88 fp = fopen(pathname, "r");
90 /* Our last chance is the "/.AFSSERVER" file */
91 fp = fopen("/.AFSSERVER", "r");
96 fgets(server_name, 128, fp);
99 len = strlen(server_name);
103 if (server_name[len - 1] == '\n') {
104 server_name[len - 1] = 0;
106 afs_server = server_name;
108 th = gethostbyname(afs_server);
110 printf("host %s not found; %s call aborted\n", afs_server, syscall);
113 memcpy(&hostAddr, th->h_addr, sizeof(hostAddr));
118 /* Does the actual RX connection to the afs server */
119 struct rx_connection *
120 rx_connection(afs_int32 * errorcode, char *syscall)
122 struct rx_connection *conn;
123 struct rx_securityClass *null_securityObject;
126 if (!(host = GetAfsServerAddr(syscall))) {
128 return (struct rx_connection *)0;
130 *errorcode = rx_Init(0);
132 printf("Rx initialize failed \n");
133 return (struct rx_connection *)0;
135 null_securityObject = rxnull_NewClientSecurityObject();
137 rx_NewConnection(host, htons(AFSCONF_RMTSYSPORT), RMTSYS_SERVICEID,
138 null_securityObject, 0);
140 printf("Unable to make a new connection\n");
142 return (struct rx_connection *)0;
148 /* WARNING: The calling program (i.e. klog) MUST be suid-root since we need to
149 * do a setgroups(2) call with the new pag.... */
151 #pragma weak setpag = afs_setpag
159 struct rx_connection *conn;
161 afs_int32 errorcode, errornumber, newpag, ngroups, j, groups[NGROUPS_MAX];
163 if (!(conn = rx_connection(&errorcode, "setpag"))) {
164 /* Remote call can't be performed for some reason.
165 * Try the local 'setpag' system call ... */
166 errorcode = lsetpag();
169 ngroups = SetClientCreds(&creds, groups);
170 errorcode = RMTSYS_SetPag(conn, &creds, &newpag, &errornumber);
174 printf("Warning: Remote setpag to %s has failed (err=%d)...\n",
180 if (afs_get_pag_from_groups(groups[0], groups[1]) == NOPAG) {
181 /* we will have to shift grouplist to make room for pag */
182 if (ngroups + 2 > NGROUPS_MAX) {
183 /* this is what the real setpag returns */
187 for (j = ngroups - 1; j >= 0; j--) {
188 groups[j + 2] = groups[j];
192 afs_get_groups_from_pag(newpag, &groups[0], &groups[1]);
193 if (setgroups(ngroups, groups) == -1) {
197 errorcode = setuid(getuid());
199 errorcode = setreuid(-1, getuid());
200 #endif /* AFS_HPUX_ENV */
205 /* Remote pioctl(2) client routine */
207 #pragma weak pioctl = afs_pioctl
209 afs_pioctl(char *path, afs_int32 cmd, struct ViceIoctl *data,
213 pioctl(char *path, afs_int32 cmd, struct ViceIoctl *data, afs_int32 follow)
216 struct rx_connection *conn;
218 afs_int32 errorcode, errornumber, ins = data->in_size;
219 afs_uint32 groups[NGROUPS_MAX];
220 rmtbulk InData, OutData;
221 char pathname[256], *pathp = pathname, *inbuffer;
222 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
223 extern char *getwd();
224 #define getcwd(x,y) getwd(x)
226 if (!(conn = rx_connection(&errorcode, "pioctl"))) {
227 /* Remote call can't be performed for some reason.
228 * Try the local 'pioctl' system call ... */
229 errorcode = lpioctl(path, cmd, data, follow);
232 (void)SetClientCreds(&creds, groups);
237 if (!(inbuffer = (char *)malloc(ins)))
238 return (-1); /* helpless here */
240 memcpy(inbuffer, data->in, data->in_size);
241 InData.rmtbulk_len = data->in_size;
242 InData.rmtbulk_val = inbuffer;
243 inparam_conversion(cmd, InData.rmtbulk_val, 0);
244 OutData.rmtbulk_len = data->out_size;
245 OutData.rmtbulk_val = data->out;
246 /* We always need to pass absolute pathnames to the remote pioctl since we
247 * lose the current directory value when doing an rpc call. Below we
248 * prepend the current absolute path directory, if the name is relative */
251 /* assuming relative path name */
252 if (getcwd(pathname, 256) == NULL) {
254 printf("getwd failed; exiting\n");
257 strcpy(pathname + strlen(pathname), "/");
258 strcat(pathname, path);
260 strcpy(pathname, path);
263 /* Special meaning for a "NULL" pathname since xdr_string hates nil
264 * pointers, at least on non-RTS; of course the proper solution would
265 * be to change the interface declartion. */
266 strcpy(pathname, NIL_PATHP);
269 RMTSYS_Pioctl(conn, &creds, pathp, cmd, follow, &InData, &OutData,
273 errorcode = -1; /* Necessary since errorcode is 0 on
274 * standard remote pioctl errors */
275 if (errno != EDOM && errno != EACCES)
276 printf("Warning: Remote pioctl to %s has failed (err=%d)...\n",
280 /* Do the conversions back to the host order; store the results back
281 * on the same buffer */
282 outparam_conversion(cmd, OutData.rmtbulk_val, 1);
290 afs_get_pag_from_groups(afs_uint32 g0, afs_uint32 g1)
292 afs_uint32 h, l, result;
296 if (g0 < 0xc000 && g1 < 0xc000) {
297 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
299 h = (g1 >> 14) + h + h + h;
300 result = ((h << 28) | l);
301 /* Additional testing */
302 if (((result >> 24) & 0xff) == 'A')
311 afs_get_groups_from_pag(afs_uint32 pag, afs_uint32 * g0p, afs_uint32 * g1p)
313 unsigned short g0, g1;
316 g0 = 0x3fff & (pag >> 14);
318 g0 |= ((pag >> 28) / 3) << 14;
319 g1 |= ((pag >> 28) % 3) << 14;
326 SetClientCreds(struct clientcred *creds, afs_int32 * groups)
330 creds->uid = getuid();
331 groups[0] = groups[1] = 0;
332 ngroups = getgroups(NGROUPS_MAX, groups);
333 creds->group0 = groups[0];
334 creds->group1 = groups[1];