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