1 /* 2 * by Manuel Bouyer (bouyer@ensta.fr) 3 * 4 * There is no copyright, you can use it as you want. 5 */ 6 7 #ifndef lint 8 static const char rcsid[] = 9 "$FreeBSD$"; 10 #endif /* not lint */ 11 12 #include <sys/param.h> 13 #include <sys/types.h> 14 #include <sys/mount.h> 15 #include <sys/file.h> 16 #include <sys/stat.h> 17 #include <sys/socket.h> 18 #include <signal.h> 19 20 #include <ctype.h> 21 #include <errno.h> 22 #include <fstab.h> 23 #include <grp.h> 24 #include <pwd.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #include <syslog.h> 31 #include <varargs.h> 32 33 #include <ufs/ufs/quota.h> 34 #include <rpc/rpc.h> 35 #include <rpc/pmap_clnt.h> 36 #include <rpcsvc/rquota.h> 37 #include <arpa/inet.h> 38 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); 42 void initfs(void); 43 int getfsquota(long id, char *path, struct dqblk *dqblk); 44 int hasquota(struct fstab *fs, char **qfnamep); 45 46 /* 47 * structure containing informations about ufs filesystems 48 * initialised by initfs() 49 */ 50 struct fs_stat { 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 */ 55 } fs_stat; 56 struct fs_stat *fs_begin = NULL; 57 58 int from_inetd = 1; 59 60 void 61 cleanup(int sig) 62 { 63 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS); 64 exit(0); 65 } 66 67 int 68 main(int argc, char *argv[]) 69 { 70 SVCXPRT *transp; 71 int sock = 0; 72 int proto = 0; 73 struct sockaddr_in from; 74 int fromlen; 75 76 fromlen = sizeof(from); 77 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { 78 from_inetd = 0; 79 sock = RPC_ANYSOCK; 80 proto = IPPROTO_UDP; 81 } 82 83 if (!from_inetd) { 84 daemon(0, 0); 85 86 (void) pmap_unset(RQUOTAPROG, RQUOTAVERS); 87 88 (void) signal(SIGINT, cleanup); 89 (void) signal(SIGTERM, cleanup); 90 (void) signal(SIGHUP, cleanup); 91 } 92 93 openlog("rpc.rquotad", LOG_CONS|LOG_PID, LOG_DAEMON); 94 95 /* create and register the service */ 96 transp = svcudp_create(sock); 97 if (transp == NULL) { 98 syslog(LOG_ERR, "couldn't create udp service"); 99 exit(1); 100 } 101 if (!svc_register(transp, RQUOTAPROG, RQUOTAVERS, rquota_service, proto)) { 102 syslog(LOG_ERR, "unable to register (RQUOTAPROG, RQUOTAVERS, %s)", proto?"udp":"(inetd)"); 103 exit(1); 104 } 105 106 initfs(); /* init the fs_stat list */ 107 svc_run(); 108 syslog(LOG_ERR, "svc_run returned"); 109 exit(1); 110 } 111 112 void 113 rquota_service(struct svc_req *request, SVCXPRT *transp) 114 { 115 switch (request->rq_proc) { 116 case NULLPROC: 117 (void)svc_sendreply(transp, xdr_void, (char *)NULL); 118 break; 119 120 case RQUOTAPROC_GETQUOTA: 121 case RQUOTAPROC_GETACTIVEQUOTA: 122 sendquota(request, transp); 123 break; 124 125 default: 126 svcerr_noproc(transp); 127 break; 128 } 129 if (from_inetd) 130 exit(0); 131 } 132 133 /* read quota for the specified id, and send it */ 134 void 135 sendquota(struct svc_req *request, SVCXPRT *transp) 136 { 137 struct getquota_args getq_args; 138 struct getquota_rslt getq_rslt; 139 struct dqblk dqblk; 140 struct timeval timev; 141 142 bzero((char *)&getq_args, sizeof(getq_args)); 143 if (!svc_getargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) { 144 svcerr_decode(transp); 145 return; 146 } 147 if (request->rq_cred.oa_flavor != AUTH_UNIX) { 148 /* bad auth */ 149 getq_rslt.status = Q_EPERM; 150 } else if (!getfsquota(getq_args.gqa_uid, getq_args.gqa_pathp, &dqblk)) { 151 /* failed, return noquota */ 152 getq_rslt.status = Q_NOQUOTA; 153 } else { 154 gettimeofday(&timev, NULL); 155 getq_rslt.status = Q_OK; 156 getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE; 157 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE; 158 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit = 159 dqblk.dqb_bhardlimit; 160 getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit = 161 dqblk.dqb_bsoftlimit; 162 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks = 163 dqblk.dqb_curblocks; 164 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit = 165 dqblk.dqb_ihardlimit; 166 getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit = 167 dqblk.dqb_isoftlimit; 168 getq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles = 169 dqblk.dqb_curinodes; 170 getq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft = 171 dqblk.dqb_btime - timev.tv_sec; 172 getq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft = 173 dqblk.dqb_itime - timev.tv_sec; 174 } 175 if (!svc_sendreply(transp, xdr_getquota_rslt, (char *)&getq_rslt)) { 176 svcerr_systemerr(transp); 177 } 178 if (!svc_freeargs(transp, xdr_getquota_args, (caddr_t)&getq_args)) { 179 syslog(LOG_ERR, "unable to free arguments"); 180 exit(1); 181 } 182 } 183 184 void 185 printerr_reply(SVCXPRT *transp) /* when a reply to a request failed */ 186 { 187 char *name; 188 struct sockaddr_in *caller; 189 int save_errno; 190 191 save_errno = errno; 192 193 caller = svc_getcaller(transp); 194 name = (char *)inet_ntoa(caller->sin_addr); 195 errno = save_errno; 196 if (errno == 0) 197 syslog(LOG_ERR, "couldn't send reply to %s", name); 198 else 199 syslog(LOG_ERR, "couldn't send reply to %s: %m", name); 200 } 201 202 /* initialise the fs_tab list from entries in /etc/fstab */ 203 void 204 initfs(void) 205 { 206 struct fs_stat *fs_current = NULL; 207 struct fs_stat *fs_next = NULL; 208 char *qfpathname; 209 struct fstab *fs; 210 struct stat st; 211 212 setfsent(); 213 while ((fs = getfsent())) { 214 if (strcmp(fs->fs_vfstype, "ufs")) 215 continue; 216 if (!hasquota(fs, &qfpathname)) 217 continue; 218 219 fs_current = (struct fs_stat *) malloc(sizeof(struct fs_stat)); 220 fs_current->fs_next = fs_next; /* next element */ 221 222 fs_current->fs_file = malloc(sizeof(char) * (strlen(fs->fs_file) + 1)); 223 strcpy(fs_current->fs_file, fs->fs_file); 224 225 fs_current->qfpathname = malloc(sizeof(char) * (strlen(qfpathname) + 1)); 226 strcpy(fs_current->qfpathname, qfpathname); 227 228 stat(fs_current->fs_file, &st); 229 fs_current->st_dev = st.st_dev; 230 231 fs_next = fs_current; 232 } 233 endfsent(); 234 fs_begin = fs_current; 235 } 236 237 /* 238 * gets the quotas for id, filesystem path. 239 * Return 0 if fail, 1 otherwise 240 */ 241 int 242 getfsquota(long id, char *path, struct dqblk *dqblk) 243 { 244 struct stat st_path; 245 struct fs_stat *fs; 246 int qcmd, fd, ret = 0; 247 248 if (stat(path, &st_path) < 0) 249 return (0); 250 251 qcmd = QCMD(Q_GETQUOTA, USRQUOTA); 252 253 for (fs = fs_begin; fs != NULL; fs = fs->fs_next) { 254 /* where the devise is the same as path */ 255 if (fs->st_dev != st_path.st_dev) 256 continue; 257 258 /* find the specified filesystem. get and return quota */ 259 if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0) 260 return (1); 261 262 if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) { 263 syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname); 264 return (0); 265 } 266 if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) { 267 close(fd); 268 return (1); 269 } 270 switch (read(fd, dqblk, sizeof(struct dqblk))) { 271 case 0: 272 /* 273 * Convert implicit 0 quota (EOF) 274 * into an explicit one (zero'ed dqblk) 275 */ 276 bzero((caddr_t) dqblk, sizeof(struct dqblk)); 277 ret = 1; 278 break; 279 case sizeof(struct dqblk): /* OK */ 280 ret = 1; 281 break; 282 default: /* ERROR */ 283 syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname); 284 close(fd); 285 return (0); 286 } 287 close(fd); 288 } 289 return (ret); 290 } 291 292 /* 293 * Check to see if a particular quota is to be enabled. 294 * Comes from quota.c, NetBSD 0.9 295 */ 296 int 297 hasquota(struct fstab *fs, char **qfnamep) 298 { 299 static char initname, usrname[100]; 300 static char buf[BUFSIZ]; 301 char *opt, *cp; 302 char *qfextension[] = INITQFNAMES; 303 304 if (!initname) { 305 sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME); 306 initname = 1; 307 } 308 strcpy(buf, fs->fs_mntops); 309 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 310 if ((cp = index(opt, '='))) 311 *cp++ = '\0'; 312 if (strcmp(opt, usrname) == 0) 313 break; 314 } 315 if (!opt) 316 return (0); 317 if (cp) { 318 *qfnamep = cp; 319 return (1); 320 } 321 sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, qfextension[USRQUOTA]); 322 *qfnamep = buf; 323 return (1); 324 } 325