2 * by Manuel Bouyer (bouyer@ensta.fr)
4 * There is no copyright, you can use it as you want.
10 #include <sys/param.h>
11 #include <sys/mount.h>
14 #include <sys/socket.h>
16 #include <ufs/ufs/quota.h>
18 #include <rpcsvc/rquota.h>
19 #include <arpa/inet.h>
34 void rquota_service(struct svc_req *request, SVCXPRT *transp);
35 void sendquota(struct svc_req *request, SVCXPRT *transp);
36 void printerr_reply(SVCXPRT *transp);
38 int getfsquota(long id, char *path, struct dqblk *dqblk);
39 int hasquota(struct fstab *fs, char **qfnamep);
42 * structure containing informations about ufs filesystems
43 * initialised by initfs()
46 struct fs_stat *fs_next; /* next element */
47 char *fs_file; /* mount point of the filesystem */
48 char *qfpathname; /* pathname of the quota file */
49 dev_t st_dev; /* device of the filesystem */
51 struct fs_stat *fs_begin = NULL;
60 (void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
69 struct sockaddr_storage from;
72 fromlen = sizeof(from);
73 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
80 (void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
82 (void) signal(SIGINT, cleanup);
83 (void) signal(SIGTERM, cleanup);
84 (void) signal(SIGHUP, cleanup);
87 openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON);
89 /* create and register the service */
91 transp = svc_tli_create(0, NULL, NULL, 0, 0);
93 syslog(LOG_ERR, "couldn't create udp service.");
96 ok = svc_reg(transp, RQUOTAPROG, RQUOTAVERS,
97 rquota_service, NULL);
99 ok = svc_create(rquota_service,
100 RQUOTAPROG, RQUOTAVERS, "udp");
103 "unable to register (RQUOTAPROG, RQUOTAVERS, %s)",
104 from_inetd ? "(inetd)" : "udp");
110 syslog(LOG_ERR, "svc_run returned");
115 rquota_service(struct svc_req *request, SVCXPRT *transp)
118 switch (request->rq_proc) {
120 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL);
123 case RQUOTAPROC_GETQUOTA:
124 case RQUOTAPROC_GETACTIVEQUOTA:
125 sendquota(request, transp);
129 svcerr_noproc(transp);
136 /* read quota for the specified id, and send it */
138 sendquota(struct svc_req *request, SVCXPRT *transp)
140 struct getquota_args getq_args;
141 struct getquota_rslt getq_rslt;
143 struct timeval timev;
145 bzero((char *)&getq_args, sizeof(getq_args));
146 if (!svc_getargs(transp, (xdrproc_t)xdr_getquota_args, &getq_args)) {
147 svcerr_decode(transp);
150 if (request->rq_cred.oa_flavor != AUTH_UNIX) {
152 getq_rslt.status = Q_EPERM;
153 } else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp, &dqblk)) {
154 /* failed, return noquota */
155 getq_rslt.status = Q_NOQUOTA;
157 gettimeofday(&timev, NULL);
158 getq_rslt.status = Q_OK;
159 getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE;
160 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE;
161 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit =
162 dqblk.dqb_bhardlimit;
163 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit =
164 dqblk.dqb_bsoftlimit;
165 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks =
167 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit =
168 dqblk.dqb_ihardlimit;
169 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit =
170 dqblk.dqb_isoftlimit;
171 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles =
173 getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft =
174 dqblk.dqb_btime - timev.tv_sec;
175 getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft =
176 dqblk.dqb_itime - timev.tv_sec;
178 if (!svc_sendreply(transp, (xdrproc_t)xdr_getquota_rslt, &getq_rslt)) {
179 svcerr_systemerr(transp);
181 if (!svc_freeargs(transp, (xdrproc_t)xdr_getquota_args, &getq_args)) {
182 syslog(LOG_ERR, "unable to free arguments");
188 printerr_reply(SVCXPRT *transp) /* when a reply to a request failed */
190 char name[INET6_ADDRSTRLEN];
191 struct sockaddr *caller;
196 caller = (struct sockaddr *)svc_getrpccaller(transp)->buf;
197 getnameinfo(caller, caller->sa_len, name, sizeof (name),
198 NULL, 0, NI_NUMERICHOST);
201 syslog(LOG_ERR, "couldn't send reply to %s", name);
203 syslog(LOG_ERR, "couldn't send reply to %s: %m", name);
206 /* initialise the fs_tab list from entries in /etc/fstab */
210 struct fs_stat *fs_current = NULL;
211 struct fs_stat *fs_next = NULL;
217 while ((fs = getfsent())) {
218 if (strcmp(fs->fs_vfstype, "ufs"))
220 if (!hasquota(fs, &qfpathname))
223 fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
224 fs_current->fs_next = fs_next; /* next element */
226 fs_current->fs_file =
227 malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
228 strcpy(fs_current->fs_file, fs->fs_file);
230 fs_current->qfpathname =
231 malloc(sizeof(char) * (strlen(qfpathname) + 1));
232 strcpy(fs_current->qfpathname, qfpathname);
234 stat(fs_current->fs_file, &st);
235 fs_current->st_dev = st.st_dev;
237 fs_next = fs_current;
240 fs_begin = fs_current;
244 * gets the quotas for id, filesystem path.
245 * Return 0 if fail, 1 otherwise
248 getfsquota(long id, char *path, struct dqblk *dqblk)
252 int qcmd, fd, ret = 0;
254 if (stat(path, &st_path) < 0)
257 qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
259 for (fs = fs_begin; fs != NULL; fs = fs->fs_next) {
260 /* where the devise is the same as path */
261 if (fs->st_dev != st_path.st_dev)
264 /* find the specified filesystem. get and return quota */
265 if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
268 if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
269 syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
272 if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) {
276 switch (read(fd, dqblk, sizeof(struct dqblk))) {
279 * Convert implicit 0 quota (EOF)
280 * into an explicit one (zero'ed dqblk)
282 bzero(dqblk, sizeof(struct dqblk));
285 case sizeof(struct dqblk): /* OK */
289 syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
299 * Check to see if a particular quota is to be enabled.
300 * Comes from quota.c, NetBSD 0.9
303 hasquota(struct fstab *fs, char **qfnamep)
305 static char initname, usrname[100];
306 static char buf[BUFSIZ];
308 const char *qfextension[] = INITQFNAMES;
311 sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME);
314 strcpy(buf, fs->fs_mntops);
315 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
316 if ((cp = index(opt, '=')))
318 if (strcmp(opt, usrname) == 0)
327 sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME,
328 qfextension[USRQUOTA]);