]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/rquotad/rquota_svc.c
d402f0b2f952f32a73c2e882977adac2418efc75
[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 #include <unistd.h>
24 #include <rpc/rpc.h>
25 #include "rquota.h"
26 #include <stdlib.h>
27 #include <rpc/pmap_clnt.h>
28 #include <string.h>
29 #include <memory.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <syslog.h>
33
34 #ifdef __STDC__
35 #define SIG_PF void(*)(int)
36 #endif
37
38 extern getquota_rslt *rquotaproc_getquota_1(getquota_args *argp,
39                                             struct svc_req *rqstp);
40 extern getquota_rslt *rquotaproc_getactivequota_1(getquota_args *argp,
41                                                   struct svc_req *rqstp);
42 extern getquota_rslt *rquotaproc_getquota_2(ext_getquota_args *argp,
43                                             struct svc_req *rqstp);
44 extern getquota_rslt *rquotaproc_getactivequota_2(ext_getquota_args *argp,
45                                                   struct svc_req *rqstp);
46
47 /*
48  * Global authentication credentials.
49  */
50 struct authunix_parms *unix_cred;
51
52 static void rquotaprog_1(struct svc_req *rqstp, register SVCXPRT *transp)
53 {
54    union {
55       getquota_args rquotaproc_getquota_1_arg;
56       getquota_args rquotaproc_getactivequota_1_arg;
57    } argument;
58    char *result;
59    xdrproc_t xdr_argument, xdr_result;
60    char *(*local)(char *, struct svc_req *);
61
62    /*
63     * Don't bother authentication for NULLPROC.
64     */
65    if (rqstp->rq_proc == NULLPROC) {
66       (void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *)NULL);
67       return;
68    }
69
70    /*
71     * First get authentication.
72     */
73    switch (rqstp->rq_cred.oa_flavor) {
74       case AUTH_UNIX:
75          unix_cred = (struct authunix_parms *)rqstp->rq_clntcred;
76          break;
77       case AUTH_NULL:
78       default:
79          svcerr_weakauth(transp);
80          return;
81    }
82
83    switch (rqstp->rq_proc) {
84       case RQUOTAPROC_GETQUOTA:
85          xdr_argument = (xdrproc_t) xdr_getquota_args;
86          xdr_result = (xdrproc_t) xdr_getquota_rslt;
87          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getquota_1;
88          break;
89
90       case RQUOTAPROC_GETACTIVEQUOTA:
91          xdr_argument = (xdrproc_t) xdr_getquota_args;
92          xdr_result = (xdrproc_t) xdr_getquota_rslt;
93          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getactivequota_1;
94          break;
95
96       default:
97          svcerr_noproc(transp);
98          return;
99    }
100
101    (void) memset((char *)&argument, 0, sizeof (argument));
102    if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
103       svcerr_decode(transp);
104       return;
105    }
106    result = (*local)((char *)&argument, rqstp);
107    if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
108       svcerr_systemerr(transp);
109    }
110
111    if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
112       syslog(LOG_ERR, "unable to free arguments");
113       exit(1);
114    }
115    return;
116 }
117
118 static void rquotaprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
119 {
120    union {
121       ext_getquota_args rquotaproc_getquota_2_arg;
122       ext_getquota_args rquotaproc_getactivequota_2_arg;
123    } argument;
124    char *result;
125    xdrproc_t xdr_argument, xdr_result;
126    char *(*local)(char *, struct svc_req *);
127
128    /*
129     * Don't bother authentication for NULLPROC.
130     */
131    if (rqstp->rq_proc == NULLPROC) {
132       (void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *)NULL);
133       return;
134    }
135
136    /*
137     * First get authentication.
138     */
139    switch (rqstp->rq_cred.oa_flavor) {
140       case AUTH_UNIX:
141          unix_cred = (struct authunix_parms *)rqstp->rq_clntcred;
142          break;
143       case AUTH_NULL:
144       default:
145          svcerr_weakauth(transp);
146          return;
147    }
148
149    switch (rqstp->rq_proc) {
150       case RQUOTAPROC_GETQUOTA:
151          xdr_argument = (xdrproc_t) xdr_ext_getquota_args;
152          xdr_result = (xdrproc_t) xdr_getquota_rslt;
153          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getquota_2;
154          break;
155
156       case RQUOTAPROC_GETACTIVEQUOTA:
157          xdr_argument = (xdrproc_t) xdr_ext_getquota_args;
158          xdr_result = (xdrproc_t) xdr_getquota_rslt;
159          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getactivequota_2;
160          break;
161
162       default:
163          svcerr_noproc(transp);
164          return;
165    }
166
167    (void) memset((char *)&argument, 0, sizeof (argument));
168    if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
169       svcerr_decode(transp);
170       return;
171    }
172    result = (*local)((char *)&argument, rqstp);
173    if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
174       svcerr_systemerr(transp);
175    }
176
177    if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
178       syslog(LOG_ERR, "unable to free arguments");
179       exit(1);
180    }
181    return;
182 }
183
184 int main(int argc, char **argv)
185 {
186    register SVCXPRT *transp;
187
188    (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
189    (void) pmap_unset(RQUOTAPROG, EXT_RQUOTAVERS);
190
191    openlog("rquota", LOG_PID, LOG_DAEMON);
192
193    transp = svcudp_create(RPC_ANYSOCK);
194    if (transp == NULL) {
195       syslog(LOG_ERR, "cannot create udp service.");
196       exit(1);
197    }
198    if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquotaprog_1, IPPROTO_UDP)) {
199       syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, udp).");
200       exit(1);
201    }
202    if (!svc_register(transp, RQUOTAPROG, EXT_RQUOTAVERS, rquotaprog_2, IPPROTO_UDP)) {
203       syslog(LOG_ERR, "unable to register (RQUOTAPROG, EXT_RQUOTAVERS, udp).");
204       exit(1);
205    }
206
207    daemon(1,1);
208    svc_run();
209
210    syslog(LOG_ERR, "svc_run returned");
211    exit(1);
212    /* NOTREACHED */
213 }