]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/rquotad/rquota_svc.c
e1149664ced37639518c042b22fe890bc1ff97ea
[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 <errno.h>
29 #include <rpc/rpc.h>
30 #include "rquota.h"
31 #include <stdlib.h>
32 #include <rpc/pmap_clnt.h>
33 #include <string.h>
34 #include <memory.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <syslog.h>
38 #include <signal.h>
39 #include <getopt.h>
40 #include <rpcmisc.h>
41
42 #ifdef __STDC__
43 #define SIG_PF void(*)(int)
44 #endif
45
46 extern getquota_rslt *rquotaproc_getquota_1(getquota_args *argp,
47                                             struct svc_req *rqstp);
48 extern getquota_rslt *rquotaproc_getactivequota_1(getquota_args *argp,
49                                                   struct svc_req *rqstp);
50 extern getquota_rslt *rquotaproc_getquota_2(ext_getquota_args *argp,
51                                             struct svc_req *rqstp);
52 extern getquota_rslt *rquotaproc_getactivequota_2(ext_getquota_args *argp,
53                                                   struct svc_req *rqstp);
54
55 static struct option longopts[] =
56 {
57         { "help", 0, 0, 'h' },
58         { "version", 0, 0, 'v' },
59         { "port", 1, 0, 'p' },
60         { NULL, 0, 0, 0 }
61 };
62
63 /*
64  * Global authentication credentials.
65  */
66 struct authunix_parms *unix_cred;
67
68 static void rquotaprog_1(struct svc_req *rqstp, register SVCXPRT *transp)
69 {
70    union {
71       getquota_args rquotaproc_getquota_1_arg;
72       getquota_args rquotaproc_getactivequota_1_arg;
73    } argument;
74    char *result;
75    xdrproc_t xdr_argument, xdr_result;
76    char *(*local)(char *, struct svc_req *);
77
78 #ifdef HAVE_TCP_WRAPPER
79    /* remote host authorization check */
80    if (!check_default("rquotad", svc_getcaller(transp),
81                       rqstp->rq_proc, RQUOTAPROG)) {
82          svcerr_auth (transp, AUTH_FAILED);
83          return;
84    }
85 #endif
86
87    /*
88     * Don't bother authentication for NULLPROC.
89     */
90    if (rqstp->rq_proc == NULLPROC) {
91       (void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *)NULL);
92       return;
93    }
94
95    /*
96     * First get authentication.
97     */
98    switch (rqstp->rq_cred.oa_flavor) {
99       case AUTH_UNIX:
100          unix_cred = (struct authunix_parms *)rqstp->rq_clntcred;
101          break;
102       case AUTH_NULL:
103       default:
104          svcerr_weakauth(transp);
105          return;
106    }
107
108    switch (rqstp->rq_proc) {
109       case RQUOTAPROC_GETQUOTA:
110          xdr_argument = (xdrproc_t) xdr_getquota_args;
111          xdr_result = (xdrproc_t) xdr_getquota_rslt;
112          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getquota_1;
113          break;
114
115       case RQUOTAPROC_GETACTIVEQUOTA:
116          xdr_argument = (xdrproc_t) xdr_getquota_args;
117          xdr_result = (xdrproc_t) xdr_getquota_rslt;
118          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getactivequota_1;
119          break;
120
121       default:
122          svcerr_noproc(transp);
123          return;
124    }
125
126    (void) memset((char *)&argument, 0, sizeof (argument));
127    if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
128       svcerr_decode(transp);
129       return;
130    }
131    result = (*local)((char *)&argument, rqstp);
132    if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
133       svcerr_systemerr(transp);
134    }
135
136    if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
137       syslog(LOG_ERR, "unable to free arguments");
138       exit(1);
139    }
140    return;
141 }
142
143 static void rquotaprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
144 {
145    union {
146       ext_getquota_args rquotaproc_getquota_2_arg;
147       ext_getquota_args rquotaproc_getactivequota_2_arg;
148    } argument;
149    char *result;
150    xdrproc_t xdr_argument, xdr_result;
151    char *(*local)(char *, struct svc_req *);
152
153 #ifdef HAVE_TCP_WRAPPER
154    /* remote host authorization check */
155    if (!check_default("rquotad", svc_getcaller(transp),
156                       rqstp->rq_proc, RQUOTAPROG)) {
157          svcerr_auth (transp, AUTH_FAILED);
158          return;
159    }
160 #endif
161
162    /*
163     * Don't bother authentication for NULLPROC.
164     */
165    if (rqstp->rq_proc == NULLPROC) {
166       (void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *)NULL);
167       return;
168    }
169
170    /*
171     * First get authentication.
172     */
173    switch (rqstp->rq_cred.oa_flavor) {
174       case AUTH_UNIX:
175          unix_cred = (struct authunix_parms *)rqstp->rq_clntcred;
176          break;
177       case AUTH_NULL:
178       default:
179          svcerr_weakauth(transp);
180          return;
181    }
182
183    switch (rqstp->rq_proc) {
184       case RQUOTAPROC_GETQUOTA:
185          xdr_argument = (xdrproc_t) xdr_ext_getquota_args;
186          xdr_result = (xdrproc_t) xdr_getquota_rslt;
187          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getquota_2;
188          break;
189
190       case RQUOTAPROC_GETACTIVEQUOTA:
191          xdr_argument = (xdrproc_t) xdr_ext_getquota_args;
192          xdr_result = (xdrproc_t) xdr_getquota_rslt;
193          local = (char *(*)(char *, struct svc_req *)) rquotaproc_getactivequota_2;
194          break;
195
196       default:
197          svcerr_noproc(transp);
198          return;
199    }
200
201    (void) memset((char *)&argument, 0, sizeof (argument));
202    if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
203       svcerr_decode(transp);
204       return;
205    }
206    result = (*local)((char *)&argument, rqstp);
207    if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
208       svcerr_systemerr(transp);
209    }
210
211    if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
212       syslog(LOG_ERR, "unable to free arguments");
213       exit(1);
214    }
215    return;
216 }
217
218 static void
219 usage(const char *prog, int n)
220 {
221   fprintf(stderr, "Usage: %s [-p|--port port] [-h|-?|--help] [-v|--version]\n", prog);
222   exit(n);
223 }
224
225 int main(int argc, char **argv)
226 {
227    register SVCXPRT *transp;
228    char c;
229    int port = 0;
230
231    (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
232    (void) pmap_unset(RQUOTAPROG, EXT_RQUOTAVERS);
233
234    openlog("rquota", LOG_PID, LOG_DAEMON);
235
236    while ((c = getopt_long(argc, argv, "hp:v", longopts, NULL)) != EOF) {
237      switch (c) {
238      case '?':
239      case 'h':
240        usage(argv[0], 0);
241        break;
242      case 'p':
243        port = atoi(optarg);
244        if (port < 1 || port > 65535) {
245          fprintf(stderr, "%s: bad port number: %s\n",
246                  argv[0], optarg);
247          usage(argv[0], 1);
248        }
249        break;
250      case 'v':
251        printf("rquotad %s\n", VERSION);
252        exit(0);
253      default:
254        usage(argv[0], 1);
255      }
256    }
257
258    if (chdir(NFS_STATEDIR)) {
259      fprintf(stderr, "%s: chdir(%s) failed: %s\n",
260              argv [0], NFS_STATEDIR, strerror(errno));
261
262      exit(1);
263    }
264
265    /* WARNING: the following works on Linux and SysV, but not BSD! */
266    signal(SIGCHLD, SIG_IGN);
267
268    if (port)
269      transp = svcudp_create(makesock(port, IPPROTO_UDP));
270    else
271      transp = svcudp_create(RPC_ANYSOCK);
272    if (transp == NULL) {
273       syslog(LOG_ERR, "cannot create udp service.");
274       exit(1);
275    }
276    if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquotaprog_1, IPPROTO_UDP)) {
277       syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, udp).");
278       exit(1);
279    }
280    if (!svc_register(transp, RQUOTAPROG, EXT_RQUOTAVERS, rquotaprog_2, IPPROTO_UDP)) {
281       syslog(LOG_ERR, "unable to register (RQUOTAPROG, EXT_RQUOTAVERS, udp).");
282       exit(1);
283    }
284
285    daemon(1,1);
286    svc_run();
287
288    syslog(LOG_ERR, "svc_run returned");
289    exit(1);
290    /* NOTREACHED */
291 }