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 $
10 #include <sys/types.h>
11 #include <sys/mount.h>
13 #include <sys/socket.h>
29 #include <vfs/ufs/quota.h>
31 #include <rpcsvc/rquota.h>
32 #include <arpa/inet.h>
35 static void cleanup(int);
36 static void rquota_service(struct svc_req *request, SVCXPRT *transp);
37 static void sendquota(struct svc_req *request, SVCXPRT *transp);
38 static void initfs(void);
39 static int getfsquota(long id, char *path, struct ufs_dqblk *dqblk);
40 static int hasquota(struct fstab *fs, char **qfnamep);
43 * structure containing informations about ufs filesystems
44 * initialised by initfs()
47 struct fs_stat *fs_next; /* next element */
48 char *fs_file; /* mount point of the filesystem */
49 char *qfpathname; /* pathname of the quota file */
50 dev_t st_dev; /* device of the filesystem */
52 struct fs_stat *fs_begin = NULL;
57 cleanup(int signo __unused)
59 (void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
68 struct sockaddr_storage from;
71 fromlen = sizeof(from);
72 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
79 (void) rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL);
81 (void) signal(SIGINT, cleanup);
82 (void) signal(SIGTERM, cleanup);
83 (void) signal(SIGHUP, cleanup);
86 openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON);
88 /* create and register the service */
90 transp = svc_tli_create(0, NULL, NULL, 0, 0);
92 syslog(LOG_ERR, "couldn't create udp service.");
95 ok = svc_reg(transp, RQUOTAPROG, RQUOTAVERS,
96 rquota_service, NULL);
98 ok = svc_create(rquota_service,
99 RQUOTAPROG, RQUOTAVERS, "udp");
101 syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s)", (!from_inetd)?"udp":"(inetd)");
105 initfs(); /* init the fs_stat list */
107 syslog(LOG_ERR, "svc_run returned");
112 rquota_service(struct svc_req *request, SVCXPRT *transp)
114 switch (request->rq_proc) {
116 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
119 case RQUOTAPROC_GETQUOTA:
120 case RQUOTAPROC_GETACTIVEQUOTA:
121 sendquota(request, transp);
125 svcerr_noproc(transp);
132 /* read quota for the specified id, and send it */
134 sendquota(struct svc_req *request, SVCXPRT *transp)
136 struct getquota_args getq_args;
137 struct getquota_rslt getq_rslt;
138 struct ufs_dqblk dqblk;
139 struct timeval timev;
141 bzero((char *)&getq_args, sizeof(getq_args));
142 if (!svc_getargs(transp, (xdrproc_t)xdr_getquota_args, (caddr_t)&getq_args)) {
143 svcerr_decode(transp);
146 if (request->rq_cred.oa_flavor != AUTH_UNIX) {
148 getq_rslt.status = Q_EPERM;
149 } else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp, &dqblk)) {
150 /* failed, return noquota */
151 getq_rslt.status = Q_NOQUOTA;
153 gettimeofday(&timev, NULL);
154 getq_rslt.status = Q_OK;
155 getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE;
156 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE;
157 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit =
158 dqblk.dqb_bhardlimit;
159 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit =
160 dqblk.dqb_bsoftlimit;
161 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks =
163 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit =
164 dqblk.dqb_ihardlimit;
165 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit =
166 dqblk.dqb_isoftlimit;
167 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles =
169 getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft =
170 dqblk.dqb_btime - timev.tv_sec;
171 getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft =
172 dqblk.dqb_itime - timev.tv_sec;
174 if (!svc_sendreply(transp, (xdrproc_t)xdr_getquota_rslt, (char *)&getq_rslt)) {
175 svcerr_systemerr(transp);
177 if (!svc_freeargs(transp, (xdrproc_t)xdr_getquota_args, (caddr_t)&getq_args)) {
178 syslog(LOG_ERR, "unable to free arguments");
183 /* initialise the fs_tab list from entries in /etc/fstab */
187 struct fs_stat *fs_current = NULL;
188 struct fs_stat *fs_next = NULL;
194 while ((fs = getfsent())) {
195 if (strcmp(fs->fs_vfstype, "ufs"))
197 if (!hasquota(fs, &qfpathname))
200 fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat));
201 fs_current->fs_next = fs_next; /* next element */
203 fs_current->fs_file = malloc(sizeof(char) * (strlen(fs->fs_file) + 1));
204 strcpy(fs_current->fs_file, fs->fs_file);
206 fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1));
207 strcpy(fs_current->qfpathname, qfpathname);
209 stat(fs_current->fs_file, &st);
210 fs_current->st_dev = st.st_dev;
212 fs_next = fs_current;
215 fs_begin = fs_current;
219 * gets the quotas for id, filesystem path.
220 * Return 0 if fail, 1 otherwise
223 getfsquota(long id, char *path, struct ufs_dqblk *dqblk)
227 int qcmd, fd, ret = 0;
229 if (stat(path, &st_path) < 0)
232 qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
234 for (fs = fs_begin; fs != NULL; fs = fs->fs_next) {
235 /* where the devise is the same as path */
236 if (fs->st_dev != st_path.st_dev)
239 /* find the specified filesystem. get and return quota */
240 if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
243 if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
244 syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
247 if (lseek(fd, (off_t)(id * sizeof(struct ufs_dqblk)), L_SET) == (off_t)-1) {
251 switch (read(fd, dqblk, sizeof(struct ufs_dqblk))) {
254 * Convert implicit 0 quota (EOF)
255 * into an explicit one (zero'ed dqblk)
257 bzero((caddr_t) dqblk, sizeof(struct ufs_dqblk));
260 case sizeof(struct ufs_dqblk): /* OK */
264 syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
274 * Check to see if a particular quota is to be enabled.
275 * Comes from quota.c, NetBSD 0.9
278 hasquota(struct fstab *fs, char **qfnamep)
280 static char initname, usrname[100];
281 static char buf[BUFSIZ];
283 const char *qfextension[] = INITQFNAMES;
286 sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME);
289 strcpy(buf, fs->fs_mntops);
290 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
291 if ((cp = index(opt, '=')))
293 if (strcmp(opt, usrname) == 0)
302 sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]);