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