sys-prototypes-20090315
[openafs.git] / src / sys / rmtsyss.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 /* Daemon that implements remote procedure call service for non-vendor system
11  * calls (currently setpag and pioctl). The AFS cache manager daemon, afsd,
12  * currently fires up this module, when the "-rmtsys" flag is given.
13  * This module resides in the lib/afs/librmtsys.a library.
14  */
15 #include <afsconfig.h>
16 #include <afs/param.h>
17
18 RCSID
19     ("$Header$");
20
21 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <afs/vice.h>
24 #include <netdb.h>
25 #include <netinet/in.h>
26 #include <sys/stat.h>
27 #include <sys/file.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <rx/xdr.h>
31 #include <errno.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <stdarg.h>
35 /*#include <afs/cellconfig.h>*/
36 #include "rmtsys.h"
37 #include "sys_prototypes.h"
38
39 #define NFS_EXPORTER        1   /* To probably handle more later */
40 #define PSETPAG             110 /* Also defined in afs/afs_pioctl.c */
41 #define PIOCTL_HEADER       6   /* # of words prepended to special pioctl */
42 #define PSetClientContext   99  /* Sets up caller's creds */
43 #define N_SECURITY_OBJECTS  1   /* No real security yet */
44
45 #define SETCLIENTCONTEXT(BLOB, host, uid, group0, group1, cmd, exportertype) { \
46            (BLOB)[0] = (host); \
47            (BLOB)[1] = (uid); \
48            (BLOB)[2] = (group0); \
49            (BLOB)[3] = (group1); \
50            (BLOB)[4] = (cmd); \
51            (BLOB)[5] = (exportertype); \
52 }
53
54
55 /* Main routine of the remote AFS system call server. The calling process will
56  * never return; this is currently called from afsd (when "-rmtsys" is passed
57  * as a parameter) */
58 void
59 rmtsysd(void)
60 {
61 /*  void catchsig(int); */
62     struct rx_securityClass *(securityObjects[N_SECURITY_OBJECTS]);
63     struct rx_service *service;
64
65     /* 
66      * Ignore SIGHUP signal since apparently is sent to the processes that
67      * start up from /etc/rc for some systems like hpux and aix3.1... 
68      */
69     signal(SIGHUP, SIG_IGN);
70
71     /* Initialize the rx-based RMTSYS server */
72     if (rx_Init(htons(AFSCONF_RMTSYSPORT)) < 0)
73         rmt_Quit("rx_init");
74     securityObjects[0] = rxnull_NewServerSecurityObject();
75     if (securityObjects[0] == (struct rx_securityClass *)0)
76         rmt_Quit("rxnull_NewServerSecurityObject");
77     service =
78         rx_NewService(0, RMTSYS_SERVICEID, AFSCONF_RMTSYSSERVICE,
79                       securityObjects, N_SECURITY_OBJECTS,
80                       RMTSYS_ExecuteRequest);
81     if (service == NULL)
82         rmt_Quit("rx_NewService");
83     /* One may wish to tune some default RX params for better performance
84      * at some point... */
85     rx_SetMaxProcs(service, 2);
86     rx_StartServer(1);          /* Donate this process to the server process pool */
87 }
88
89
90 /* Implements the remote setpag(2) call. Note that unlike the standard call,
91  * here we also get back the new pag value; we need this so that the caller
92  * can add it to its group list via setgroups() */
93 afs_int32
94 SRMTSYS_SetPag(struct rx_call *call, clientcred *creds, afs_int32 *newpag, 
95                afs_int32 *errornumber)
96 {
97     afs_uint32 blob[PIOCTL_HEADER];
98     struct ViceIoctl data;
99     register afs_int32 error;
100
101     *errornumber = 0;
102     SETCLIENTCONTEXT(blob, rx_HostOf(call->conn->peer), creds->uid,
103                      creds->group0, creds->group1, PSETPAG, NFS_EXPORTER);
104     data.in = (caddr_t) blob;
105     data.in_size = sizeof(blob);
106     data.out = (caddr_t) blob;
107     data.out_size = sizeof(blob);
108     /* force local pioctl call */
109     error = lpioctl(0, _VICEIOCTL(PSetClientContext), (char *)&data, 1);
110     if (error) {
111         if (errno == PSETPAG) {
112             *newpag = blob[0];  /* new pag value */
113         } else
114             *errornumber = errno;
115     }
116     return 0;
117 }
118
119
120 /* Implements the remote pioctl(2) call */
121 afs_int32
122 SRMTSYS_Pioctl(struct rx_call *call, clientcred *creds, char *path, 
123                afs_int32 cmd, afs_int32 follow, rmtbulk *InData, 
124                rmtbulk *OutData, afs_int32 *errornumber)
125 {
126     register afs_int32 error;
127     struct ViceIoctl data;
128     char *pathp = path;
129     afs_uint32 blob[PIOCTL_HEADER];
130
131     *errornumber = 0;
132     SETCLIENTCONTEXT(blob, rx_HostOf(call->conn->peer), creds->uid,
133                      creds->group0, creds->group1, cmd, NFS_EXPORTER);
134     data.in =
135         (char *)malloc(InData->rmtbulk_len +
136                        PIOCTL_HEADER * sizeof(afs_int32));
137     if (!data.in)
138         return (-1);            /* helpless here */
139     if (!strcmp(path, NIL_PATHP))
140         pathp = (char *)0;      /* It meant to be NIL */
141     memcpy(data.in, blob, sizeof(blob));
142     inparam_conversion(cmd, InData->rmtbulk_val, 1);
143     memcpy(data.in + sizeof(blob), InData->rmtbulk_val, InData->rmtbulk_len);
144     data.in_size = InData->rmtbulk_len + PIOCTL_HEADER * sizeof(afs_int32);
145     data.out = OutData->rmtbulk_val;
146     data.out_size = OutData->rmtbulk_len;
147     /* force local pioctl call */
148     error = lpioctl(pathp, _VICEIOCTL(PSetClientContext), (char *)&data, follow);
149     if (error) {
150         *errornumber = errno;
151     } else {
152         /* Send the results back in network order */
153         outparam_conversion(cmd, data.out, 0);
154     }
155     free(data.in);
156     /* Note that we return success (i.e. 0) even when pioctl fails; that's
157      * because the actual errno is passed back via 'errornumber' and this call
158      * MUST return success error in order to get that OUT params back (YUCK!)
159      */
160     return (0);
161 }
162
163 void
164 rmt_Quit(char *msg, ...)
165 {
166     va_list ap;
167     
168     va_start(ap, msg);
169     vfprintf(stderr, msg, ap);
170     va_end(ap);
171     exit(1);
172 }