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(char *syscall)
56 register struct hostent *th;
60 /* Take advantage of caching and assume that the remote host
61 * address won't change during a single program's invocation.
67 if (!(afs_server = getenv("AFSSERVER"))) {
72 if (!(home_dir = getenv("HOME"))) {
73 /* Our last chance is the "/.AFSSERVER" file */
74 fp = fopen("/.AFSSERVER", "r");
78 fgets(server_name, 128, fp);
83 sprintf(pathname, "%s/%s", home_dir, ".AFSSERVER");
84 fp = fopen(pathname, "r");
86 /* Our last chance is the "/.AFSSERVER" file */
87 fp = fopen("/.AFSSERVER", "r");
92 fgets(server_name, 128, fp);
95 len = strlen(server_name);
99 if (server_name[len-1] == '\n') {
100 server_name[len-1] = 0;
102 afs_server = server_name;
104 th = gethostbyname(afs_server);
106 printf("host %s not found; %s call aborted\n", afs_server, syscall);
109 memcpy(&hostAddr, th->h_addr, sizeof(hostAddr));
114 /* Does the actual RX connection to the afs server */
115 struct rx_connection *rx_connection(afs_int32 *errorcode, char *syscall)
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(char *path, afs_int32 cmd, struct ViceIoctl *data, afs_int32 follow)
200 int pioctl(char *path, afs_int32 cmd, struct ViceIoctl *data, afs_int32 follow)
203 struct rx_connection *conn;
205 afs_int32 errorcode, errornumber, ins= data->in_size;
206 afs_uint32 groups[NGROUPS_MAX];
207 rmtbulk InData, OutData;
208 char pathname[256], *pathp = pathname, *inbuffer;
209 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
210 extern char *getwd();
211 #define getcwd(x,y) getwd(x)
213 if (!(conn = rx_connection(&errorcode, "pioctl"))) {
214 /* Remote call can't be performed for some reason.
215 * Try the local 'pioctl' system call ... */
216 errorcode = lpioctl(path, cmd, data, follow);
219 (void) SetClientCreds(&creds, groups);
223 if (!(inbuffer = (char *)malloc(ins)))
224 return (-1); /* helpless here */
226 memcpy(inbuffer, data->in, data->in_size);
227 InData.rmtbulk_len = data->in_size;
228 InData.rmtbulk_val = inbuffer;
229 inparam_conversion(cmd, InData.rmtbulk_val, 0);
230 OutData.rmtbulk_len = data->out_size;
231 OutData.rmtbulk_val = data->out;
232 /* We always need to pass absolute pathnames to the remote pioctl since we
233 * lose the current directory value when doing an rpc call. Below we
234 * prepend the current absolute path directory, if the name is relative */
237 /* assuming relative path name */
238 if (getcwd(pathname, 256) == NULL) {
240 printf("getwd failed; exiting\n");
243 strcpy(pathname+strlen(pathname), "/");
244 strcat(pathname, path);
246 strcpy(pathname, path);
249 /* Special meaning for a "NULL" pathname since xdr_string hates nil
250 * pointers, at least on non-RTS; of course the proper solution would
251 * be to change the interface declartion. */
252 strcpy(pathname, NIL_PATHP);
254 errorcode = RMTSYS_Pioctl(conn, &creds, pathp, cmd, follow, &InData,
255 &OutData, &errornumber);
258 errorcode = -1; /* Necessary since errorcode is 0 on
259 * standard remote pioctl errors */
260 if (errno != EDOM && errno != EACCES)
261 printf("Warning: Remote pioctl to %s has failed (err=%d)...\n",
265 /* Do the conversions back to the host order; store the results back
266 * on the same buffer */
267 outparam_conversion(cmd, OutData.rmtbulk_val, 1);
274 int afs_get_pag_from_groups(afs_uint32 g0, afs_uint32 g1)
276 afs_uint32 h, l, result;
280 if (g0 < 0xc000 && g1 < 0xc000) {
281 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
283 h = (g1 >> 14) + h + h + h;
284 result = ((h << 28) | l);
285 /* Additional testing */
286 if (((result >> 24) & 0xff) == 'A')
295 afs_get_groups_from_pag(afs_uint32 pag, afs_uint32 *g0p, afs_uint32 *g1p)
297 unsigned short g0, g1;
300 g0 = 0x3fff & (pag >> 14);
302 g0 |= ((pag >> 28) / 3) << 14;
303 g1 |= ((pag >> 28) % 3) << 14;
309 static afs_int32 SetClientCreds(struct clientcred *creds, afs_int32 *groups)
313 creds->uid = getuid();
314 groups[0] = groups[1] = 0;
315 ngroups = getgroups(NGROUPS_MAX, groups);
316 creds->group0 = groups[0];
317 creds->group1 = groups[1];