Sync with FreeBSD (adding DragonFly 1.8.0).
[dragonfly.git] / libexec / rpc.rquotad / rquotad.c
1 /*
2  * by Manuel Bouyer (bouyer@ensta.fr)
3  * 
4  * There is no copyright, you can use it as you want.
5  *
6  * $FreeBSD: src/libexec/rpc.rquotad/rquotad.c,v 1.3.2.1 2001/07/02 23:46:27 mikeh Exp $
7  * $DragonFly: src/libexec/rpc.rquotad/rquotad.c,v 1.5 2006/04/03 01:59:27 dillon Exp $
8  */
9
10 #include <sys/param.h>
11 #include <sys/types.h>
12 #include <sys/mount.h>
13 #include <sys/file.h>
14 #include <sys/stat.h>
15 #include <sys/socket.h>
16 #include <signal.h>
17
18 #include <ctype.h>
19 #include <errno.h>
20 #include <fstab.h>
21 #include <grp.h>
22 #include <pwd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include <syslog.h>
29 #include <varargs.h>
30
31 #include <vfs/ufs/quota.h>
32 #include <rpc/rpc.h>
33 #include <rpc/pmap_clnt.h>
34 #include <rpcsvc/rquota.h>
35 #include <arpa/inet.h>
36
37 void rquota_service (struct svc_req *request, SVCXPRT *transp);
38 void sendquota (struct svc_req *request, SVCXPRT *transp);
39 void printerr_reply (SVCXPRT *transp);
40 void initfs (void);
41 int getfsquota (long id, char *path, struct ufs_dqblk *dqblk);
42 int hasquota (struct fstab *fs, char **qfnamep);
43
44 /*
45  * structure containing informations about ufs filesystems
46  * initialised by initfs()
47  */
48 struct fs_stat {
49         struct fs_stat *fs_next;        /* next element */
50         char   *fs_file;                /* mount point of the filesystem */
51         char   *qfpathname;             /* pathname of the quota file */
52         dev_t   st_dev;                 /* device of the filesystem */
53 } fs_stat;
54 struct fs_stat *fs_begin = NULL;
55
56 int from_inetd = 1;
57
58 void 
59 cleanup()
60 {
61         (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
62         exit(0);
63 }
64
65 int
66 main(argc, argv)
67         int     argc;
68         char   *argv[];
69 {
70         SVCXPRT *transp;
71         int sock = 0;
72         int proto = 0;
73         struct sockaddr_in from;
74         int fromlen;
75
76         fromlen = sizeof(from);
77         if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
78                 from_inetd = 0;
79                 sock = RPC_ANYSOCK;
80                 proto = IPPROTO_UDP;
81         }
82
83         if (!from_inetd) {
84                 daemon(0, 0);
85
86                 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
87
88                 (void) signal(SIGINT, cleanup);
89                 (void) signal(SIGTERM, cleanup);
90                 (void) signal(SIGHUP, cleanup);
91         }
92
93         openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON);
94
95         /* create and register the service */
96         transp = svcudp_create(sock);
97         if (transp == NULL) {
98                 syslog(LOG_ERR, "couldn't create udp service");
99                 exit(1);
100         }
101         if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquota_service, proto)) {
102                 syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s)", proto?"udp":"(inetd)");
103                 exit(1);
104         }
105
106         initfs();               /* init the fs_stat list */
107         svc_run();
108         syslog(LOG_ERR, "svc_run returned");
109         exit(1);
110 }
111
112 void 
113 rquota_service(request, transp)
114         struct svc_req *request;
115         SVCXPRT *transp;
116 {
117         switch (request->rq_proc) {
118         case NULLPROC:
119                 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
120                 break;
121
122         case RQUOTAPROC_GETQUOTA:
123         case RQUOTAPROC_GETACTIVEQUOTA:
124                 sendquota(request, transp);
125                 break;
126
127         default:
128                 svcerr_noproc(transp);
129                 break;
130         }
131         if (from_inetd)
132                 exit(0);
133 }
134
135 /* read quota for the specified id, and send it */
136 void 
137 sendquota(request, transp)
138         struct svc_req *request;
139         SVCXPRT *transp;
140 {
141         struct getquota_args getq_args;
142         struct getquota_rslt getq_rslt;
143         struct ufs_dqblk dqblk;
144         struct timeval timev;
145
146         bzero((char *)&getq_args, sizeof(getq_args));
147         if (!svc_getargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
148                 svcerr_decode(transp);
149                 return;
150         }
151         if (request->rq_cred.oa_flavor != AUTH_UNIX) {
152                 /* bad auth */
153                 getq_rslt.status = Q_EPERM;
154         } else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp, &dqblk)) {
155                 /* failed, return noquota */
156                 getq_rslt.status = Q_NOQUOTA;
157         } else {
158                 gettimeofday(&timev, NULL);
159                 getq_rslt.status = Q_OK;
160                 getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE;
161                 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE;
162                 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit =
163                     dqblk.dqb_bhardlimit;
164                 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit =
165                     dqblk.dqb_bsoftlimit;
166                 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks =
167                     dqblk.dqb_curblocks;
168                 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit =
169                     dqblk.dqb_ihardlimit;
170                 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit =
171                     dqblk.dqb_isoftlimit;
172                 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles =
173                     dqblk.dqb_curinodes;
174                 getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft =
175                     dqblk.dqb_btime - timev.tv_sec;
176                 getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft =
177                     dqblk.dqb_itime - timev.tv_sec;
178         }
179         if (!svc_sendreply(transp, xdr_getquota_rslt, (char *)&getq_rslt)) {
180                 svcerr_systemerr(transp);
181         }
182         if (!svc_freeargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
183                 syslog(LOG_ERR, "unable to free arguments");
184                 exit(1);
185         }
186 }
187
188 void 
189 printerr_reply(transp)  /* when a reply to a request failed */
190         SVCXPRT *transp;
191 {
192         char   *name;
193         struct sockaddr_in *caller;
194         int     save_errno;
195
196         save_errno = errno;
197
198         caller = svc_getcaller(transp);
199         name = (char *)inet_ntoa(caller->sin_addr);
200         errno = save_errno;
201         if (errno == 0)
202                 syslog(LOG_ERR, "couldn't send reply to %s", name);
203         else
204                 syslog(LOG_ERR, "couldn't send reply to %s: %m", name);
205 }
206
207 /* initialise the fs_tab list from entries in /etc/fstab */
208 void 
209 initfs()
210 {
211         struct fs_stat *fs_current = NULL;
212         struct fs_stat *fs_next = NULL;
213         char *qfpathname;
214         struct fstab *fs;
215         struct stat st;
216
217         setfsent();
218         while ((fs = getfsent())) {
219                 if (strcmp(fs->fs_vfstype, "ufs"))
220                         continue;
221                 if (!hasquota(fs, &qfpathname))
222                         continue;
223
224                 fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
225                 fs_current->fs_next = fs_next;  /* next element */
226
227                 fs_current->fs_file = malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
228                 strcpy(fs_current->fs_file, fs->fs_file);
229
230                 fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1));
231                 strcpy(fs_current->qfpathname, qfpathname);
232
233                 stat(fs_current->fs_file, &st);
234                 fs_current->st_dev = st.st_dev;
235
236                 fs_next = fs_current;
237         }
238         endfsent();
239         fs_begin = fs_current;
240 }
241
242 /*
243  * gets the quotas for id, filesystem path.
244  * Return 0 if fail, 1 otherwise
245  */
246 int
247 getfsquota(id, path, dqblk)
248         long id;
249         char   *path;
250         struct ufs_dqblk *dqblk;
251 {
252         struct stat st_path;
253         struct fs_stat *fs;
254         int     qcmd, fd, ret = 0;
255
256         if (stat(path, &st_path) < 0)
257                 return (0);
258
259         qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
260
261         for (fs = fs_begin; fs != NULL; fs = fs->fs_next) {
262                 /* where the devise is the same as path */
263                 if (fs->st_dev != st_path.st_dev)
264                         continue;
265
266                 /* find the specified filesystem. get and return quota */
267                 if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
268                         return (1);
269
270                 if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
271                         syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
272                         return (0);
273                 }
274                 if (lseek(fd, (off_t)(id * sizeof(struct ufs_dqblk)), L_SET) == (off_t)-1) {
275                         close(fd);
276                         return (1);
277                 }
278                 switch (read(fd, dqblk, sizeof(struct ufs_dqblk))) {
279                 case 0:
280                         /*
281                          * Convert implicit 0 quota (EOF)
282                          * into an explicit one (zero'ed dqblk)
283                          */
284                         bzero((caddr_t) dqblk, sizeof(struct ufs_dqblk));
285                         ret = 1;
286                         break;
287                 case sizeof(struct ufs_dqblk):  /* OK */
288                         ret = 1;
289                         break;
290                 default:        /* ERROR */
291                         syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
292                         close(fd);
293                         return (0);
294                 }
295                 close(fd);
296         }
297         return (ret);
298 }
299
300 /*
301  * Check to see if a particular quota is to be enabled.
302  * Comes from quota.c, NetBSD 0.9
303  */
304 int
305 hasquota(fs, qfnamep)
306         struct fstab *fs;
307         char  **qfnamep;
308 {
309         static char initname, usrname[100];
310         static char buf[BUFSIZ];
311         char    *opt, *cp;
312         char    *qfextension[] = INITQFNAMES;
313
314         if (!initname) {
315                 sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME);
316                 initname = 1;
317         }
318         strcpy(buf, fs->fs_mntops);
319         for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
320                 if ((cp = index(opt, '=')))
321                         *cp++ = '\0';
322                 if (strcmp(opt, usrname) == 0)
323                         break;
324         }
325         if (!opt)
326                 return (0);
327         if (cp) {
328                 *qfnamep = cp;
329                 return (1);
330         }
331         sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]);
332         *qfnamep = buf;
333         return (1);
334 }