2 * by Manuel Bouyer (bouyer@ensta.fr)
4 * There is no copyright, you can use it as you want.
8 static const char rcsid[] =
12 #include <sys/param.h>
13 #include <sys/types.h>
14 #include <sys/mount.h>
17 #include <sys/socket.h>
33 #include <ufs/ufs/quota.h>
35 #include <rpcsvc/rquota.h>
36 #include <arpa/inet.h>
39 void rquota_service(struct svc_req *request, SVCXPRT *transp);
40 void sendquota(struct svc_req *request, SVCXPRT *transp);
41 void printerr_reply(SVCXPRT *transp);
43 int getfsquota(long id, char *path, struct dqblk *dqblk);
44 int hasquota(struct fstab *fs, char **qfnamep);
47 * structure containing informations about ufs filesystems
48 * initialised by initfs()
51 struct fs_stat *fs_next; /* next element */
52 char *fs_file; /* mount point of the filesystem */
53 char *qfpathname; /* pathname of the quota file */
54 dev_t st_dev; /* device of the filesystem */
56 struct fs_stat *fs_begin = NULL;
63 (void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
68 main(int argc, char *argv[])
72 struct sockaddr_storage from;
75 fromlen = sizeof(from);
76 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
83 (void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
85 (void) signal(SIGINT, cleanup);
86 (void) signal(SIGTERM, cleanup);
87 (void) signal(SIGHUP, cleanup);
90 openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON);
92 /* create and register the service */
94 transp = svc_tli_create(0, NULL, NULL, 0, 0);
96 syslog(LOG_ERR, "couldn't create udp service.");
99 ok = svc_reg(transp, RQUOTAPROG, RQUOTAVERS,
100 rquota_service, NULL);
102 ok = svc_create(rquota_service,
103 RQUOTAPROG, RQUOTAVERS, "udp");
105 syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s)", (!from_inetd)?"udp":"(inetd)");
109 initfs(); /* init the fs_stat list */
111 syslog(LOG_ERR, "svc_run returned");
116 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 = malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
227 strcpy(fs_current->fs_file, fs->fs_file);
229 fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1));
230 strcpy(fs_current->qfpathname, qfpathname);
232 stat(fs_current->fs_file, &st);
233 fs_current->st_dev = st.st_dev;
235 fs_next = fs_current;
238 fs_begin = fs_current;
242 * gets the quotas for id, filesystem path.
243 * Return 0 if fail, 1 otherwise
246 getfsquota(long id, char *path, struct dqblk *dqblk)
250 int qcmd, fd, ret = 0;
252 if (stat(path, &st_path) < 0)
255 qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
257 for (fs = fs_begin; fs != NULL; fs = fs->fs_next) {
258 /* where the devise is the same as path */
259 if (fs->st_dev != st_path.st_dev)
262 /* find the specified filesystem. get and return quota */
263 if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
266 if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
267 syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
270 if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) {
274 switch (read(fd, dqblk, sizeof(struct dqblk))) {
277 * Convert implicit 0 quota (EOF)
278 * into an explicit one (zero'ed dqblk)
280 bzero(dqblk, sizeof(struct dqblk));
283 case sizeof(struct dqblk): /* OK */
287 syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
297 * Check to see if a particular quota is to be enabled.
298 * Comes from quota.c, NetBSD 0.9
301 hasquota(struct fstab *fs, char **qfnamep)
303 static char initname, usrname[100];
304 static char buf[BUFSIZ];
306 char *qfextension[] = INITQFNAMES;
309 sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME);
312 strcpy(buf, fs->fs_mntops);
313 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
314 if ((cp = index(opt, '=')))
316 if (strcmp(opt, usrname) == 0)
325 sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]);