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>
16 #include <afsconfig.h>
19 #include <sys/types.h>
25 #include <netinet/in.h>
37 #define NOPAG 0xffffffff /* Also defined in afs/afs.h */
38 static afs_int32 hostAddr = 0;
39 static int hostAddrLookup = 0;
40 char *afs_server=0, server_name[128];
42 static afs_int32 SetClientCreds();
44 /* Picks up the name of the remote afs client host where the rmtsys
45 * daemon resides. Since the clients may be diskless and/or readonly
46 * ones we felt it's better to rely on an shell environment
47 * (AFSSERVER) for the host name first. If that is not set, the
48 * $HOME/.AFSSERVER file is checked, otherwise the "/.AFSSERVER" is
51 afs_int32 GetAfsServerAddr(syscall)
54 register struct hostent *th;
58 /* Take advantage of caching and assume that the remote host
59 * address won't change during a single program's invocation.
65 if (!(afs_server = getenv("AFSSERVER"))) {
70 if (!(home_dir = getenv("HOME"))) {
71 /* Our last chance is the "/.AFSSERVER" file */
72 fp = fopen("/.AFSSERVER", "r");
76 fgets(server_name, 128, fp);
81 sprintf(pathname, "%s/%s", home_dir, ".AFSSERVER");
82 fp = fopen(pathname, "r");
84 /* Our last chance is the "/.AFSSERVER" file */
85 fp = fopen("/.AFSSERVER", "r");
90 fgets(server_name, 128, fp);
93 len = strlen(server_name);
97 if (server_name[len-1] == '\n') {
98 server_name[len-1] = 0;
100 afs_server = server_name;
102 th = gethostbyname(afs_server);
104 printf("host %s not found; %s call aborted\n", afs_server, syscall);
107 bcopy(th->h_addr, &hostAddr, sizeof(hostAddr));
112 /* Does the actual RX connection to the afs server */
113 struct rx_connection *rx_connection(errorcode, syscall)
114 afs_int32 *errorcode;
117 struct rx_connection *conn;
118 struct rx_securityClass *null_securityObject;
120 if (!(host = GetAfsServerAddr(syscall))) {
122 return (struct rx_connection *)0;
124 *errorcode = rx_Init(0);
126 printf("Rx initialize failed \n");
127 return (struct rx_connection *)0;
129 null_securityObject = rxnull_NewClientSecurityObject();
130 conn = rx_NewConnection(host, htons(AFSCONF_RMTSYSPORT), RMTSYS_SERVICEID, null_securityObject, 0);
132 printf("Unable to make a new connection\n");
134 return (struct rx_connection *)0;
140 /* WARNING: The calling program (i.e. klog) MUST be suid-root since we need to
141 * do a setgroups(2) call with the new pag.... */
143 #pragma weak setpag = afs_setpag
149 struct rx_connection *conn;
151 afs_int32 errorcode, errornumber, newpag, ngroups, j, groups[NGROUPS_MAX];
153 if (!(conn = rx_connection(&errorcode, "setpag"))) {
154 /* Remote call can't be performed for some reason.
155 * Try the local 'setpag' system call ... */
156 errorcode = lsetpag();
159 ngroups = SetClientCreds(&creds, groups);
160 errorcode = RMTSYS_SetPag(conn, &creds, &newpag, &errornumber);
164 printf("Warning: Remote setpag to %s has failed (err=%d)...\n",
170 if (afs_get_pag_from_groups(groups[0], groups[1]) == NOPAG) {
171 /* we will have to shift grouplist to make room for pag */
172 if (ngroups + 2 > NGROUPS_MAX) {
173 /* this is what the real setpag returns */
177 for (j = ngroups - 1; j >= 0; j--) {
178 groups[j + 2] = groups[j];
182 afs_get_groups_from_pag(newpag, &groups[0], &groups[1]);
183 if (setgroups(ngroups, groups) == -1) {
187 errorcode = setuid(getuid());
189 errorcode = setreuid(-1, getuid());
190 #endif /* AFS_HPUX_ENV */
195 /* Remote pioctl(2) client routine */
197 #pragma weak pioctl = afs_pioctl
198 int afs_pioctl(path, cmd, data, follow)
200 int pioctl(path, cmd, data, follow)
203 afs_int32 cmd, follow;
204 struct ViceIoctl *data;
206 struct rx_connection *conn;
208 afs_int32 errorcode, errornumber, ins= data->in_size;
209 afs_uint32 groups[NGROUPS_MAX];
210 rmtbulk InData, OutData;
211 char pathname[256], *pathp = pathname, *inbuffer;
212 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
213 extern char *getwd();
214 #define getcwd(x,y) getwd(x)
216 if (!(conn = rx_connection(&errorcode, "pioctl"))) {
217 /* Remote call can't be performed for some reason.
218 * Try the local 'pioctl' system call ... */
219 errorcode = lpioctl(path, cmd, data, follow);
222 (void) SetClientCreds(&creds, groups);
226 if (!(inbuffer = (char *)malloc(ins)))
227 return (-1); /* helpless here */
229 bcopy(data->in, inbuffer, data->in_size);
230 InData.rmtbulk_len = data->in_size;
231 InData.rmtbulk_val = inbuffer;
232 inparam_conversion(cmd, InData.rmtbulk_val, 0);
233 OutData.rmtbulk_len = data->out_size;
234 OutData.rmtbulk_val = data->out;
235 /* We always need to pass absolute pathnames to the remote pioctl since we
236 * lose the current directory value when doing an rpc call. Below we
237 * prepend the current absolute path directory, if the name is relative */
240 /* assuming relative path name */
241 if (getcwd(pathname, 256) == NULL) {
243 printf("getwd failed; exiting\n");
246 strcpy(pathname+strlen(pathname), "/");
247 strcat(pathname, path);
249 strcpy(pathname, path);
252 /* Special meaning for a "NULL" pathname since xdr_string hates nil
253 * pointers, at least on non-RTS; of course the proper solution would
254 * be to change the interface declartion. */
255 strcpy(pathname, NIL_PATHP);
257 errorcode = RMTSYS_Pioctl(conn, &creds, pathp, cmd, follow, &InData,
258 &OutData, &errornumber);
261 errorcode = -1; /* Necessary since errorcode is 0 on
262 * standard remote pioctl errors */
263 if (errno != EDOM && errno != EACCES)
264 printf("Warning: Remote pioctl to %s has failed (err=%d)...\n",
268 /* Do the conversions back to the host order; store the results back
269 * on the same buffer */
270 outparam_conversion(cmd, OutData.rmtbulk_val, 1);
277 int afs_get_pag_from_groups(g0, g1)
280 afs_uint32 h, l, result;
284 if (g0 < 0xc000 && g1 < 0xc000) {
285 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
287 h = (g1 >> 14) + h + h + h;
288 result = ((h << 28) | l);
289 /* Additional testing */
290 if (((result >> 24) & 0xff) == 'A')
299 afs_get_groups_from_pag(pag, g0p, g1p)
301 afs_uint32 *g0p, *g1p;
303 unsigned short g0, g1;
306 g0 = 0x3fff & (pag >> 14);
308 g0 |= ((pag >> 28) / 3) << 14;
309 g1 |= ((pag >> 28) % 3) << 14;
315 static afs_int32 SetClientCreds(creds, groups)
316 struct clientcred *creds;
321 creds->uid = getuid();
322 groups[0] = groups[1] = 0;
323 ngroups = getgroups(NGROUPS_MAX, groups);
324 creds->group0 = groups[0];
325 creds->group1 = groups[1];