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>
32 #include <ufs/ufs/quota.h>
34 #include <rpcsvc/rquota.h>
35 #include <arpa/inet.h>
38 void rquota_service(struct svc_req *request, SVCXPRT *transp);
39 void sendquota(struct svc_req *request, SVCXPRT *transp);
40 void printerr_reply(SVCXPRT *transp);
42 int getfsquota(long id, char *path, struct dqblk *dqblk);
43 int hasquota(struct fstab *fs, char **qfnamep);
46 * structure containing informations about ufs filesystems
47 * initialised by initfs()
50 struct fs_stat *fs_next; /* next element */
51 char *fs_file; /* mount point of the filesystem */
52 char *qfpathname; /* pathname of the quota file */
53 dev_t st_dev; /* device of the filesystem */
55 struct fs_stat *fs_begin = NULL;
62 (void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
67 main(int argc, char *argv[])
71 struct sockaddr_storage from;
74 fromlen = sizeof(from);
75 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
82 (void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
84 (void) signal(SIGINT, cleanup);
85 (void) signal(SIGTERM, cleanup);
86 (void) signal(SIGHUP, cleanup);
89 openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON);
91 /* create and register the service */
93 transp = svc_tli_create(0, NULL, NULL, 0, 0);
95 syslog(LOG_ERR, "couldn't create udp service.");
98 ok = svc_reg(transp, RQUOTAPROG, RQUOTAVERS,
99 rquota_service, NULL);
101 ok = svc_create(rquota_service,
102 RQUOTAPROG, RQUOTAVERS, "udp");
104 syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s)", (!from_inetd)?"udp":"(inetd)");
108 initfs(); /* init the fs_stat list */
110 syslog(LOG_ERR, "svc_run returned");
115 rquota_service(struct svc_req *request, SVCXPRT *transp)
117 switch (request->rq_proc) {
119 (void)svc_sendreply(transp, (xdrproc_t)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(struct svc_req *request, SVCXPRT *transp)
139 struct getquota_args getq_args;
140 struct getquota_rslt getq_rslt;
142 struct timeval timev;
144 bzero((char *)&getq_args, sizeof(getq_args));
145 if (!svc_getargs(transp, (xdrproc_t)xdr_getquota_args, &getq_args)) {
146 svcerr_decode(transp);
149 if (request->rq_cred.oa_flavor != AUTH_UNIX) {
151 getq_rslt.status = Q_EPERM;
152 } else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp, &dqblk)) {
153 /* failed, return noquota */
154 getq_rslt.status = Q_NOQUOTA;
156 gettimeofday(&timev, NULL);
157 getq_rslt.status = Q_OK;
158 getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE;
159 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE;
160 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit =
161 dqblk.dqb_bhardlimit;
162 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit =
163 dqblk.dqb_bsoftlimit;
164 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks =
166 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit =
167 dqblk.dqb_ihardlimit;
168 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit =
169 dqblk.dqb_isoftlimit;
170 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles =
172 getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft =
173 dqblk.dqb_btime - timev.tv_sec;
174 getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft =
175 dqblk.dqb_itime - timev.tv_sec;
177 if (!svc_sendreply(transp, (xdrproc_t)xdr_getquota_rslt, &getq_rslt)) {
178 svcerr_systemerr(transp);
180 if (!svc_freeargs(transp, (xdrproc_t)xdr_getquota_args, &getq_args)) {
181 syslog(LOG_ERR, "unable to free arguments");
187 printerr_reply(SVCXPRT *transp) /* when a reply to a request failed */
189 char name[INET6_ADDRSTRLEN];
190 struct sockaddr *caller;
195 caller = (struct sockaddr *)svc_getrpccaller(transp)->buf;
196 getnameinfo(caller, caller->sa_len, name, sizeof (name),
197 NULL, 0, NI_NUMERICHOST);
200 syslog(LOG_ERR, "couldn't send reply to %s", name);
202 syslog(LOG_ERR, "couldn't send reply to %s: %m", name);
205 /* initialise the fs_tab list from entries in /etc/fstab */
209 struct fs_stat *fs_current = NULL;
210 struct fs_stat *fs_next = NULL;
216 while ((fs = getfsent())) {
217 if (strcmp(fs->fs_vfstype, "ufs"))
219 if (!hasquota(fs, &qfpathname))
222 fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
223 fs_current->fs_next = fs_next; /* next element */
225 fs_current->fs_file = malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
226 strcpy(fs_current->fs_file, fs->fs_file);
228 fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1));
229 strcpy(fs_current->qfpathname, qfpathname);
231 stat(fs_current->fs_file, &st);
232 fs_current->st_dev = st.st_dev;
234 fs_next = fs_current;
237 fs_begin = fs_current;
241 * gets the quotas for id, filesystem path.
242 * Return 0 if fail, 1 otherwise
245 getfsquota(long id, char *path, struct dqblk *dqblk)
249 int qcmd, fd, ret = 0;
251 if (stat(path, &st_path) < 0)
254 qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
256 for (fs = fs_begin; fs != NULL; fs = fs->fs_next) {
257 /* where the devise is the same as path */
258 if (fs->st_dev != st_path.st_dev)
261 /* find the specified filesystem. get and return quota */
262 if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
265 if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
266 syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
269 if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) {
273 switch (read(fd, dqblk, sizeof(struct dqblk))) {
276 * Convert implicit 0 quota (EOF)
277 * into an explicit one (zero'ed dqblk)
279 bzero(dqblk, sizeof(struct dqblk));
282 case sizeof(struct dqblk): /* OK */
286 syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
296 * Check to see if a particular quota is to be enabled.
297 * Comes from quota.c, NetBSD 0.9
300 hasquota(struct fstab *fs, char **qfnamep)
302 static char initname, usrname[100];
303 static char buf[BUFSIZ];
305 char *qfextension[] = INITQFNAMES;
308 sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME);
311 strcpy(buf, fs->fs_mntops);
312 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
313 if ((cp = index(opt, '=')))
315 if (strcmp(opt, usrname) == 0)
324 sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]);