]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/rquotad/rquota_svc.c
2000-08-25 Ion Badulescu <ionut@cs.columbia.edu>
[nfs-utils.git] / utils / rquotad / rquota_svc.c
1 /*
2  * QUOTA    An implementation of the diskquota system for the LINUX
3  *          operating system. QUOTA is implemented using the BSD systemcall
4  *          interface as the means of communication with the user level.
5  *          Should work for all filesystems because of integration into the
6  *          VFS layer of the operating system.
7  *          This is based on the Melbourne quota system wich uses both user and
8  *          group quota files.
9  *
10  *          This part accepts the rquota rpc-requests.
11  *
12  * Version: $Id: rquota_svc.c,v 2.6 1996/11/17 16:59:46 mvw Exp mvw $
13  *
14  * Author:  Marco van Wieringen <mvw@planets.elm.net>
15  *
16  *          This program is free software; you can redistribute it and/or
17  *          modify it under the terms of the GNU General Public License
18  *          as published by the Free Software Foundation; either version
19  *          2 of the License, or (at your option) any later version.
20  */
21 #include "config.h"
22
23 #ifdef HAVE_TCP_WRAPPER
24 #include "tcpwrapper.h"
25 #endif
26
27 #include <unistd.h>
28 #include <rpc/rpc.h>
29 #include "rquota.h"
30 #include <stdlib.h>
31 #include <rpc/pmap_clnt.h>
32 #include <string.h>
33 #include <memory.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <syslog.h>
37 #include <signal.h>
38
39 #ifdef __STDC__
40 #define SIG_PF void(*)(int)
41 #endif
42
43 extern getquota_rslt *rquotaproc_getquota_1(getquota_args *argp,
44                                             struct svc_req *rqstp);
45 extern getquota_rslt *rquotaproc_getactivequota_1(getquota_args *argp,
46                                                   struct svc_req *rqstp);
47 extern getquota_rslt *rquotaproc_getquota_2(ext_getquota_args *argp,
48                                             struct svc_req *rqstp);
49 extern getquota_rslt *rquotaproc_getactivequota_2(ext_getquota_args *argp,
50                                                   struct svc_req *rqstp);
51
52 /*
53  * Global authentication credentials.
54  */
55 struct authunix_parms *unix_cred;
56
57 static void rquotaprog_1(struct svc_req *rqstp, register SVCXPRT *transp)
58 {
59    union {
60       getquota_args rquotaproc_getquota_1_arg;
61       getquota_args rquotaproc_getactivequota_1_arg;
62    } argument;
63    char *result;
64    xdrproc_t xdr_argument, xdr_result;
65    char *(*local)(char *, struct svc_req *);
66
67 #ifdef HAVE_TCP_WRAPPER
68    /* remote host authorization check */
69    if (!check_default("rquotad", svc_getcaller(transp),
70                       rqstp->rq_proc, RQUOTAPROG)) {
71          svcerr_auth (transp, AUTH_FAILED);
72          return;
73    }
74 #endif
75
76    /*
77     * Don't bother authentication for NULLPROC.
78     */
79    if (rqstp->rq_proc == NULLPROC) {
80       (void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *)NULL);
81       return;
82    }
83
84    /*
85     * First get authentication.
86     */
87    switch (rqstp->rq_cred.oa_flavor) {
88       case AUTH_UNIX:
89          unix_cred = (struct authunix_parms *)rqstp->rq_clntcred;
90          break;
91       case AUTH_NULL:
92       default:
93          svcerr_weakauth(transp);
94          return;
95    }
96
97    switch (rqstp->rq_proc) {
98       case RQUOTAPROC_GETQUOTA:
99          xdr_argument = (xdrproc_t) xdr_getquota_args;
100          xdr_result = (xdrproc_t) xdr_getquota_rslt;
101          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getquota_1;
102          break;
103
104       case RQUOTAPROC_GETACTIVEQUOTA:
105          xdr_argument = (xdrproc_t) xdr_getquota_args;
106          xdr_result = (xdrproc_t) xdr_getquota_rslt;
107          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getactivequota_1;
108          break;
109
110       default:
111          svcerr_noproc(transp);
112          return;
113    }
114
115    (void) memset((char *)&argument, 0, sizeof (argument));
116    if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
117       svcerr_decode(transp);
118       return;
119    }
120    result = (*local)((char *)&argument, rqstp);
121    if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
122       svcerr_systemerr(transp);
123    }
124
125    if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
126       syslog(LOG_ERR, "unable to free arguments");
127       exit(1);
128    }
129    return;
130 }
131
132 static void rquotaprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
133 {
134    union {
135       ext_getquota_args rquotaproc_getquota_2_arg;
136       ext_getquota_args rquotaproc_getactivequota_2_arg;
137    } argument;
138    char *result;
139    xdrproc_t xdr_argument, xdr_result;
140    char *(*local)(char *, struct svc_req *);
141
142 #ifdef HAVE_TCP_WRAPPER
143    /* remote host authorization check */
144    if (!check_default("rquotad", svc_getcaller(transp),
145                       rqstp->rq_proc, RQUOTAPROG)) {
146          svcerr_auth (transp, AUTH_FAILED);
147          return;
148    }
149 #endif
150
151    /*
152     * Don't bother authentication for NULLPROC.
153     */
154    if (rqstp->rq_proc == NULLPROC) {
155       (void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *)NULL);
156       return;
157    }
158
159    /*
160     * First get authentication.
161     */
162    switch (rqstp->rq_cred.oa_flavor) {
163       case AUTH_UNIX:
164          unix_cred = (struct authunix_parms *)rqstp->rq_clntcred;
165          break;
166       case AUTH_NULL:
167       default:
168          svcerr_weakauth(transp);
169          return;
170    }
171
172    switch (rqstp->rq_proc) {
173       case RQUOTAPROC_GETQUOTA:
174          xdr_argument = (xdrproc_t) xdr_ext_getquota_args;
175          xdr_result = (xdrproc_t) xdr_getquota_rslt;
176          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getquota_2;
177          break;
178
179       case RQUOTAPROC_GETACTIVEQUOTA:
180          xdr_argument = (xdrproc_t) xdr_ext_getquota_args;
181          xdr_result = (xdrproc_t) xdr_getquota_rslt;
182          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getactivequota_2;
183          break;
184
185       default:
186          svcerr_noproc(transp);
187          return;
188    }
189
190    (void) memset((char *)&argument, 0, sizeof (argument));
191    if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
192       svcerr_decode(transp);
193       return;
194    }
195    result = (*local)((char *)&argument, rqstp);
196    if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
197       svcerr_systemerr(transp);
198    }
199
200    if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
201       syslog(LOG_ERR, "unable to free arguments");
202       exit(1);
203    }
204    return;
205 }
206
207 int main(int argc, char **argv)
208 {
209    register SVCXPRT *transp;
210
211    (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
212    (void) pmap_unset(RQUOTAPROG, EXT_RQUOTAVERS);
213
214    openlog("rquota", LOG_PID, LOG_DAEMON);
215
216    /* WARNING: the following works on Linux and SysV, but not BSD! */
217    signal(SIGCHLD, SIG_IGN);
218
219    transp = svcudp_create(RPC_ANYSOCK);
220    if (transp == NULL) {
221       syslog(LOG_ERR, "cannot create udp service.");
222       exit(1);
223    }
224    if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquotaprog_1, IPPROTO_UDP)) {
225       syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, udp).");
226       exit(1);
227    }
228    if (!svc_register(transp, RQUOTAPROG, EXT_RQUOTAVERS, rquotaprog_2, IPPROTO_UDP)) {
229       syslog(LOG_ERR, "unable to register (RQUOTAPROG, EXT_RQUOTAVERS, udp).");
230       exit(1);
231    }
232
233    daemon(1,1);
234    svc_run();
235
236    syslog(LOG_ERR, "svc_run returned");
237    exit(1);
238    /* NOTREACHED */
239 }