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