2 * by Manuel Bouyer (bouyer@ensta.fr)
4 * There is no copyright, you can use it as you want.
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.4 2003/11/14 03:54:31 dillon Exp $
10 #include <sys/param.h>
11 #include <sys/types.h>
12 #include <sys/mount.h>
15 #include <sys/socket.h>
31 #include <vfs/ufs/quota.h>
33 #include <rpc/pmap_clnt.h>
34 #include <rpcsvc/rquota.h>
35 #include <arpa/inet.h>
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);
41 int getfsquota (long id, char *path, struct dqblk *dqblk);
42 int hasquota (struct fstab *fs, char **qfnamep);
45 * structure containing informations about ufs filesystems
46 * initialised by initfs()
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 */
54 struct fs_stat *fs_begin = NULL;
61 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
73 struct sockaddr_in from;
76 fromlen = sizeof(from);
77 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
86 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS);
88 (void) signal(SIGINT, cleanup);
89 (void) signal(SIGTERM, cleanup);
90 (void) signal(SIGHUP, cleanup);
93 openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON);
95 /* create and register the service */
96 transp = svcudp_create(sock);
98 syslog(LOG_ERR, "couldn't create udp service");
101 if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquota_service, proto)) {
102 syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s)", proto?"udp":"(inetd)");
106 initfs(); /* init the fs_stat list */
108 syslog(LOG_ERR, "svc_run returned");
113 rquota_service(request, transp)
114 struct svc_req *request;
117 switch (request->rq_proc) {
119 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
122 case RQUOTAPROC_GETQUOTA:
123 case RQUOTAPROC_GETACTIVEQUOTA:
124 sendquota(request, transp);
128 svcerr_noproc(transp);
135 /* read quota for the specified id, and send it */
137 sendquota(request, transp)
138 struct svc_req *request;
141 struct getquota_args getq_args;
142 struct getquota_rslt getq_rslt;
144 struct timeval timev;
146 bzero((char *)&getq_args, sizeof(getq_args));
147 if (!svc_getargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
148 svcerr_decode(transp);
151 if (request->rq_cred.oa_flavor != AUTH_UNIX) {
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;
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 =
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 =
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;
179 if (!svc_sendreply(transp, xdr_getquota_rslt, (char *)&getq_rslt)) {
180 svcerr_systemerr(transp);
182 if (!svc_freeargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) {
183 syslog(LOG_ERR, "unable to free arguments");
189 printerr_reply(transp) /* when a reply to a request failed */
193 struct sockaddr_in *caller;
198 caller = svc_getcaller(transp);
199 name = (char *)inet_ntoa(caller->sin_addr);
202 syslog(LOG_ERR, "couldn't send reply to %s", name);
204 syslog(LOG_ERR, "couldn't send reply to %s: %m", name);
207 /* initialise the fs_tab list from entries in /etc/fstab */
211 struct fs_stat *fs_current = NULL;
212 struct fs_stat *fs_next = NULL;
218 while ((fs = getfsent())) {
219 if (strcmp(fs->fs_vfstype, "ufs"))
221 if (!hasquota(fs, &qfpathname))
224 fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
225 fs_current->fs_next = fs_next; /* next element */
227 fs_current->fs_file = malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
228 strcpy(fs_current->fs_file, fs->fs_file);
230 fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1));
231 strcpy(fs_current->qfpathname, qfpathname);
233 stat(fs_current->fs_file, &st);
234 fs_current->st_dev = st.st_dev;
236 fs_next = fs_current;
239 fs_begin = fs_current;
243 * gets the quotas for id, filesystem path.
244 * Return 0 if fail, 1 otherwise
247 getfsquota(id, path, dqblk)
254 int qcmd, fd, ret = 0;
256 if (stat(path, &st_path) < 0)
259 qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
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)
266 /* find the specified filesystem. get and return quota */
267 if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
270 if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
271 syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
274 if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) {
278 switch (read(fd, dqblk, sizeof(struct dqblk))) {
281 * Convert implicit 0 quota (EOF)
282 * into an explicit one (zero'ed dqblk)
284 bzero((caddr_t) dqblk, sizeof(struct dqblk));
287 case sizeof(struct dqblk): /* OK */
291 syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
301 * Check to see if a particular quota is to be enabled.
302 * Comes from quota.c, NetBSD 0.9
305 hasquota(fs, qfnamep)
309 static char initname, usrname[100];
310 static char buf[BUFSIZ];
312 char *qfextension[] = INITQFNAMES;
315 sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME);
318 strcpy(buf, fs->fs_mntops);
319 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
320 if ((cp = index(opt, '=')))
322 if (strcmp(opt, usrname) == 0)
331 sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]);