e38cf93cd846a945cf05895d36ac4dcfbe4bff28
[openafs.git] / src / afs / afs_pag_call.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include "afs/sysincludes.h"    /* Standard vendor system headers */
15 #include "afsincludes.h"        /* Afs-based standard headers */
16 #include "afs/afs_stats.h"
17 #include "rx/rx_globals.h"
18 #include "rx/rxstat.h"
19 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
20 #include "net/if.h"
21 #ifdef AFS_SGI62_ENV
22 #include "h/hashing.h"
23 #endif
24 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_DARWIN_ENV)
25 #include "netinet/in_var.h"
26 #endif
27 #endif /* !defined(UKERNEL) */
28 #include "rmtsys.h"
29 #include "pagcb.h"
30
31
32 afs_int32 afs_termState = 0;
33 afs_int32 afs_gcpags = AFS_GCPAGS;
34 int afs_shuttingdown = 0;
35 int afs_cold_shutdown = 0;
36 int afs_resourceinit_flag = 0;
37 afs_int32 afs_nfs_server_addr;
38 struct interfaceAddr afs_cb_interface;
39 struct afs_osi_WaitHandle AFS_WaitHandler;
40 static struct rx_securityClass *srv_secobj;
41 static struct rx_securityClass *clt_secobj;
42 static struct rx_service *stats_svc;
43 static struct rx_service *pagcb_svc;
44 static struct rx_connection *rmtsys_conn;
45 char *afs_sysname = 0;
46 char *afs_sysnamelist[MAXNUMSYSNAMES];
47 int afs_sysnamecount = 0;
48 int afs_sysnamegen = 0;
49 afs_int32 afs_showflags = GAGUSER | GAGCONSOLE; /* show all messages */
50
51
52 void
53 afs_Daemon(void)
54 {
55     afs_int32 now, last10MinCheck, last60MinCheck;
56
57     last10MinCheck = 0;
58     last60MinCheck = 0;
59     while (1) {
60         rx_CheckPackets();
61         now = osi_Time();
62
63         if (last10MinCheck + 600 < now) {
64             afs_GCUserData();
65         }
66
67         if (last60MinCheck + 3600 < now) {
68             afs_int32 didany;
69             afs_GCPAGs(&didany);
70         }
71
72         now = 20000 - (osi_Time() - now);
73         afs_osi_Wait(now, &AFS_WaitHandler, 0);
74
75         if (afs_termState == AFSOP_STOP_AFS) {
76 #if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
77             afs_termState = AFSOP_STOP_RXEVENT;
78 #else
79             afs_termState = AFSOP_STOP_COMPLETE;
80 #endif
81             afs_osi_Wakeup(&afs_termState);
82             return;
83         }
84     }
85 }
86
87
88 void
89 afspag_Init(afs_int32 nfs_server_addr)
90 {
91     struct clientcred ccred;
92     struct rmtbulk idata, odata;
93     afs_int32 code, err, addr, obuf;
94     int i;
95
96     afs_uuid_create(&afs_cb_interface.uuid);
97
98     AFS_GLOCK();
99
100     afs_InitStats();
101     rx_Init(htons(7001));
102
103     AFS_STATCNT(afs_ResourceInit);
104     AFS_RWLOCK_INIT(&afs_xuser, "afs_xuser");
105     AFS_RWLOCK_INIT(&afs_xpagcell, "afs_xpagcell");
106     AFS_RWLOCK_INIT(&afs_xpagsys, "afs_xpagsys");
107     AFS_RWLOCK_INIT(&afs_icl_lock, "afs_icl_lock");
108
109     afs_resourceinit_flag = 1;
110     afs_nfs_server_addr = nfs_server_addr;
111     for (i = 0; i < MAXNUMSYSNAMES; i++) {
112         afs_sysnamelist[i] = afs_osi_Alloc(MAXSYSNAME);
113         osi_Assert(afs_sysnamelist[i] != NULL);
114     }
115     afs_sysname = afs_sysnamelist[0];
116     strcpy(afs_sysname, SYS_NAME);
117     afs_sysnamecount = 1;
118     afs_sysnamegen++;
119
120     srv_secobj = rxnull_NewServerSecurityObject();
121     stats_svc = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &srv_secobj,
122                               1, RXSTATS_ExecuteRequest);
123     pagcb_svc = rx_NewService(0, PAGCB_SERVICEID, "pagcb", &srv_secobj,
124                               1, PAGCB_ExecuteRequest);
125     rx_StartServer(0);
126
127     clt_secobj = rxnull_NewClientSecurityObject();
128     rmtsys_conn = rx_NewConnection(nfs_server_addr, htons(7009),
129                                    RMTSYS_SERVICEID, clt_secobj, 0);
130
131 #ifdef RXK_LISTENER_ENV
132     afs_start_thread(rxk_Listener,       "Rx Listener");
133 #endif
134     afs_start_thread((void *)(void *)rx_ServerProc,      "Rx Server Thread");
135     afs_start_thread(afs_rxevent_daemon, "Rx Event Daemon");
136     afs_start_thread(afs_Daemon,         "AFS PAG Daemon");
137
138     afs_icl_InitLogs();
139
140     AFS_GUNLOCK();
141
142     /* If it's reachable, tell the translator to nuke our creds.
143      * We should be more agressive about making sure this gets done,
144      * even if the translator is unreachable when we boot.
145      */
146     addr = obuf = err = 0;
147     idata.rmtbulk_len = sizeof(addr);
148     idata.rmtbulk_val = (char *)&addr;
149     odata.rmtbulk_len = sizeof(obuf);
150     odata.rmtbulk_val = (char *)&obuf;
151     memset(&ccred, 0, sizeof(ccred));
152     code = RMTSYS_Pioctl(rmtsys_conn, &ccred, NIL_PATHP, 0x4F01, 0,
153                          &idata, &odata, &err);
154 }                               /*afs_ResourceInit */
155
156
157 /* called with the GLOCK held */
158 void
159 afspag_Shutdown(void)
160 {
161     if (afs_shuttingdown)
162         return;
163     afs_shuttingdown = 1;
164     afs_termState = AFSOP_STOP_RXCALLBACK;
165     rx_WakeupServerProcs();
166     while (afs_termState == AFSOP_STOP_RXCALLBACK)
167         afs_osi_Sleep(&afs_termState);
168     /* rx_ServerProc sets AFS_STOP_AFS */
169
170     while (afs_termState == AFSOP_STOP_AFS) {
171         afs_osi_CancelWait(&AFS_WaitHandler);
172         afs_osi_Sleep(&afs_termState);
173     }
174     /* afs_Daemon sets AFS_STOP_RXEVENT */
175
176 #if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
177     while (afs_termState == AFSOP_STOP_RXEVENT)
178         afs_osi_Sleep(&afs_termState);
179     /* afs_rxevent_daemon sets AFSOP_STOP_RXK_LISTENER */
180
181 #if defined(RXK_LISTENER_ENV)
182     afs_osi_UnmaskRxkSignals();
183     osi_StopListener();
184     while (afs_termState == AFSOP_STOP_RXK_LISTENER)
185         afs_osi_Sleep(&afs_termState);
186     /* rxk_Listener sets AFSOP_STOP_COMPLETE */
187 #endif
188 #endif
189 }
190
191 static void
192 token_conversion(char *buffer, int buf_size, int in)
193 {
194     struct ClearToken *ticket;
195     afs_int32 *lptr, n;
196
197     /* secret ticket */
198     if (buf_size < 4) return;
199     lptr = (afs_int32 *)buffer;
200     buffer += 4; buf_size -= 4;
201     if (in) {
202         *lptr = ntohl(*lptr);
203         n = *lptr;
204     } else {
205         n = *lptr;
206         *lptr = htonl(*lptr);
207     }
208     if (n < 0 || buf_size < n) return;
209     buffer += n; buf_size -= n;
210
211     /* clear token */
212     if (buf_size < 4) return;
213     lptr = (afs_int32 *)buffer;
214     buffer += 4; buf_size -= 4;
215     if (in) {
216         *lptr = ntohl(*lptr);
217         n = *lptr;
218     } else {
219         n = *lptr;
220         *lptr = htonl(*lptr);
221     }
222     if (n < 0 || buf_size < n) return;
223     if (n >= sizeof(struct ClearToken)) {
224         ticket = (struct ClearToken *)buffer;
225         if (in) {
226             ticket->AuthHandle     = ntohl(ticket->AuthHandle);
227             ticket->ViceId         = ntohl(ticket->ViceId);
228             ticket->BeginTimestamp = ntohl(ticket->BeginTimestamp);
229             ticket->EndTimestamp   = ntohl(ticket->EndTimestamp);
230         } else {
231             ticket->AuthHandle     = htonl(ticket->AuthHandle);
232             ticket->ViceId         = htonl(ticket->ViceId);
233             ticket->BeginTimestamp = htonl(ticket->BeginTimestamp);
234             ticket->EndTimestamp   = htonl(ticket->EndTimestamp);
235         }
236     }
237     buffer += n; buf_size -= n;
238
239     /* primary flag */
240     if (buf_size < 4) return;
241     lptr = (afs_int32 *)buffer;
242     if (in) {
243         *lptr = ntohl(*lptr);
244     } else {
245         *lptr = htonl((*lptr) & ~0x8000);
246     }
247     return;
248 }
249
250 static void
251 FetchVolumeStatus_conversion(char *buffer, int buf_size, int in)
252 {
253     AFSFetchVolumeStatus *status = (AFSFetchVolumeStatus *)buffer;
254
255     if (buf_size < sizeof(AFSFetchVolumeStatus))
256         return;
257     if (in) {
258         status->Vid              = ntohl(status->Vid);
259         status->ParentId         = ntohl(status->ParentId);
260         status->Type             = ntohl(status->Type);
261         status->MinQuota         = ntohl(status->MinQuota);
262         status->MaxQuota         = ntohl(status->MaxQuota);
263         status->BlocksInUse      = ntohl(status->BlocksInUse);
264         status->PartBlocksAvail  = ntohl(status->PartBlocksAvail);
265         status->PartMaxBlocks    = ntohl(status->PartMaxBlocks);
266     } else {
267         status->Vid              = htonl(status->Vid);
268         status->ParentId         = htonl(status->ParentId);
269         status->Type             = htonl(status->Type);
270         status->MinQuota         = htonl(status->MinQuota);
271         status->MaxQuota         = htonl(status->MaxQuota);
272         status->BlocksInUse      = htonl(status->BlocksInUse);
273         status->PartBlocksAvail  = htonl(status->PartBlocksAvail);
274         status->PartMaxBlocks    = htonl(status->PartMaxBlocks);
275     }
276 }
277
278 static void
279 inparam_conversion(int cmd, char *buffer, int buf_size, int in)
280 {
281     afs_int32 *lptr = (afs_int32 *)buffer;
282
283     switch (cmd & 0xffff) {
284         case (0x5600 |  3): /* VIOCSETTOK */
285             token_conversion(buffer, buf_size, in);
286             return;
287
288         case (0x5600 |  5): /* VIOCSETVOLSTAT */
289             FetchVolumeStatus_conversion(buffer, buf_size, in);
290             return;
291
292         case (0x5600 |  8): /* VIOCGETTOK */
293         case (0x5600 | 10): /* VIOCCKSERV */
294         case (0x5600 | 20): /* VIOCACCESS */
295         case (0x5600 | 24): /* VIOCSETCACHESIZE */
296         case (0x5600 | 27): /* VIOCGETCELL */
297         case (0x5600 | 32): /* VIOC_AFS_MARINER_HOST */
298         case (0x5600 | 34): /* VIOC_VENUSLOG */
299         case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
300         case (0x5600 | 39): /* VIOC_EXPORTAFS */
301             /* one 32-bit integer */
302             if (buf_size >= 4) {
303                 if (in) lptr[0] = ntohl(lptr[0]);
304                 else    lptr[0] = htonl(lptr[0]);
305             }
306             return;
307
308         case (0x5600 | 36): /* VIOCSETCELLSTATUS */
309             /* two 32-bit integers */
310             if (buf_size >= 4) {
311                 if (in) lptr[0] = ntohl(lptr[0]);
312                 else    lptr[0] = htonl(lptr[0]);
313             }
314             if (buf_size >= 8) {
315                 if (in) lptr[1] = ntohl(lptr[1]);
316                 else    lptr[1] = htonl(lptr[1]);
317             }
318             return;
319     }
320 }
321
322 static void
323 outparam_conversion(int cmd, char *buffer, int buf_size, int in)
324 {
325     afs_int32 *lptr = (afs_int32 *)buffer;
326     int i;
327
328     switch (cmd & 0xffff) {
329         case (0x5600 |  4): /* VIOCGETVOLSTAT */
330         case (0x5600 |  5): /* VIOCSETVOLSTAT */
331             FetchVolumeStatus_conversion(buffer, buf_size, in);
332             return;
333
334         case (0x5600 |  8): /* VIOCGETTOK */
335             token_conversion(buffer, buf_size, in);
336             return;
337
338         case (0x5600 | 12): /* VIOCCKCONN */
339         case (0x5600 | 32): /* VIOC_AFS_MARINER_HOST */
340         case (0x5600 | 34): /* VIOC_VENUSLOG */
341         case (0x5600 | 35): /* VIOC_GETCELLSTATUS */
342         case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
343         case (0x5600 | 39): /* VIOC_EXPORTAFS */
344             /* one 32-bit integer */
345             if (buf_size >= 4) {
346                 if (in) lptr[0] = ntohl(lptr[0]);
347                 else    lptr[0] = htonl(lptr[0]);
348             }
349             return;
350
351         case (0x5600 | 40): /* VIOCGETCACHEPARMS */
352             /* sixteen 32-bit integers */
353             for (i = 0; i < 16 && buf_size >= 4; i++) {
354                 if (in) lptr[i] = ntohl(lptr[i]);
355                 else    lptr[i] = htonl(lptr[i]);
356                 buf_size -= 4;
357             }
358             return;
359     }
360 }
361
362
363 /* called with the GLOCK held */
364 int
365 #ifdef  AFS_SUN5_ENV
366 afs_syscall_pioctl(path, com, cmarg, follow, rvp, credp)
367      rval_t *rvp;
368      afs_ucred_t *credp;
369 #else
370 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
371 afs_syscall_pioctl(path, com, cmarg, follow, credp)
372      afs_ucred_t *credp;
373 #else
374 afs_syscall_pioctl(path, com, cmarg, follow)
375 #endif
376 #endif
377      char *path;
378      unsigned int com;
379      caddr_t cmarg;
380      int follow;
381 {
382 #ifdef  AFS_AIX41_ENV
383     struct ucred *credp = crref();      /* don't free until done! */
384 #endif
385 #ifdef AFS_LINUX22_ENV
386     cred_t *credp = crref();    /* don't free until done! */
387 #endif
388     struct afs_ioctl data;
389     struct clientcred ccred;
390     struct rmtbulk idata, odata;
391     short in_size, out_size;
392     afs_int32 code = 0, pag, err;
393     gid_t g0, g1;
394     char *abspath, *pathbuf = 0;
395
396     AFS_STATCNT(afs_syscall_pioctl);
397     if (follow)
398         follow = 1;             /* compat. with old venus */
399     code = copyin_afs_ioctl(cmarg, &data);
400     if (code) goto out;
401
402     if ((com & 0xff) == 90) {
403         /* PSetClientContext, in any space */
404         code = EINVAL;
405         goto out;
406     }
407
408     /* Special handling for a few pioctls */
409     switch (com & 0xffff) {
410         case (0x5600 |  3): /* VIOCSETTOK */
411             code = afspag_PSetTokens(data.in, data.in_size, &credp);
412             if (code) goto out;
413             break;
414
415         case (0x5600 |  9): /* VIOCUNLOG */
416         case (0x5600 | 21): /* VIOCUNPAG */
417             code = afspag_PUnlog(data.in, data.in_size, &credp);
418             if (code) goto out;
419             break;
420
421         case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
422             code = afspag_PSetSysName(data.in, data.in_size, &credp);
423             if (code) goto out;
424             break;
425     }
426
427     /* Set up credentials */
428     memset(&ccred, 0, sizeof(ccred));
429     pag = PagInCred(credp);
430     ccred.uid = afs_cr_uid(credp);
431     if (pag != NOPAG) {
432          afs_get_groups_from_pag(pag, &g0, &g1);
433          ccred.group0 = g0;
434          ccred.group1 = g1;
435     }
436
437     /*
438      * Copy the path and convert to absolute, if one was given.
439      * NB: We can only use osI_AllocLargeSpace here as long as
440      * RMTSYS_MAXPATHLEN is less than AFS_LRALLOCSIZ.
441      */
442     if (path) {
443         pathbuf = osi_AllocLargeSpace(RMTSYS_MAXPATHLEN);
444         if (!pathbuf) {
445             code = ENOMEM;
446             goto out;
447         }
448         code = osi_abspath(path, pathbuf, RMTSYS_MAXPATHLEN, 0, &abspath);
449         if (code)
450             goto out_path;
451     } else {
452         abspath = NIL_PATHP;
453     }
454
455     /* Allocate, copy, and convert incoming data */
456     idata.rmtbulk_len = in_size = data.in_size;
457     if (in_size  < 0 || in_size  > MAXBUFFERLEN) {
458         code = EINVAL;
459         goto out_path;
460     }
461     if (in_size > AFS_LRALLOCSIZ)
462          idata.rmtbulk_val = osi_Alloc(in_size);
463     else
464          idata.rmtbulk_val = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
465     if (!idata.rmtbulk_val) {
466         code = ENOMEM;
467         goto out_path;
468     }
469     if (in_size) {
470         AFS_COPYIN(data.in, idata.rmtbulk_val, in_size, code);
471         if (code)
472             goto out_idata;
473         inparam_conversion(com, idata.rmtbulk_val, in_size, 0);
474     }
475
476     /* Allocate space for outgoing data */
477     odata.rmtbulk_len = out_size = data.out_size;
478     if (out_size < 0 || out_size > MAXBUFFERLEN) {
479         code = EINVAL;
480         goto out_idata;
481     }
482     if (out_size > AFS_LRALLOCSIZ)
483          odata.rmtbulk_val = osi_Alloc(out_size);
484     else
485          odata.rmtbulk_val = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
486     if (!odata.rmtbulk_val) {
487         code = ENOMEM;
488         goto out_idata;
489     }
490
491     AFS_GUNLOCK();
492     code = RMTSYS_Pioctl(rmtsys_conn, &ccred, abspath, com, follow,
493                          &idata, &odata, &err);
494     AFS_GLOCK();
495     if (code)
496         goto out_odata;
497
498     /* Convert and copy out the result */
499     if (odata.rmtbulk_len > out_size) {
500         code = E2BIG;
501         goto out_odata;
502     }
503     if (odata.rmtbulk_len) {
504         outparam_conversion(com, odata.rmtbulk_val, odata.rmtbulk_len, 1);
505         AFS_COPYOUT(odata.rmtbulk_val, data.out, odata.rmtbulk_len, code);
506     }
507     if (!code)
508         code = err;
509
510 out_odata:
511     if (out_size > AFS_LRALLOCSIZ)
512         osi_Free(odata.rmtbulk_val, out_size);
513     else
514         osi_FreeLargeSpace(odata.rmtbulk_val);
515
516 out_idata:
517     if (in_size > AFS_LRALLOCSIZ)
518         osi_Free(idata.rmtbulk_val, in_size);
519     else
520         osi_FreeLargeSpace(idata.rmtbulk_val);
521
522 out_path:
523     if (path)
524         osi_FreeLargeSpace(pathbuf);
525
526 out:
527 #if defined(AFS_LINUX22_ENV) || defined(AFS_AIX41_ENV)
528     crfree(credp);
529 #endif
530 #if defined(KERNEL_HAVE_UERROR)
531     if (!getuerror())
532         setuerror(code);
533     return (getuerror());
534 #else
535     return (code);
536 #endif
537 }
538
539
540 int
541 afs_syscall_call(parm, parm2, parm3, parm4, parm5, parm6)
542      long parm, parm2, parm3, parm4, parm5, parm6;
543 {
544     /* superusers may shut us down, as with afsd --shutdown */
545 #ifdef AFS_SUN5_ENV
546     if (parm == AFSOP_SHUTDOWN && afs_suser(CRED()))
547 #else
548     if (parm == AFSOP_SHUTDOWN && afs_suser(NULL))
549 #endif
550     {
551         AFS_GLOCK();
552         afspag_Shutdown();
553         AFS_GUNLOCK();
554         return 0;
555     }
556
557     /* otherwise, we don't support afs_syscall_call, period */
558 #if defined(KERNEL_HAVE_UERROR)
559     setuerror(EPERM);
560 #endif
561     return EPERM;
562 }