2 Unix SMB/CIFS implementation.
3 System QUOTA function wrappers
4 Copyright (C) Stefan (metze) Metzmacher 2003
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #define DBGC_CLASS DBGC_QUOTA
26 #ifdef HAVE_SYS_QUOTAS
28 #if defined(HAVE_QUOTACTL_4A)
30 /*#endif HAVE_QUOTACTL_4A */
31 #elif defined(HAVE_QUOTACTL_4B)
33 /*#endif HAVE_QUOTACTL_4B */
34 #elif defined(HAVE_QUOTACTL_3)
36 #error HAVE_QUOTACTL_3 not implemented
38 /* #endif HAVE_QUOTACTL_3 */
39 #else /* NO_QUOTACTL_USED */
41 #endif /* NO_QUOTACTL_USED */
44 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
52 /* find the block device file */
54 if (!path||!mntpath||!bdev||!fs)
55 smb_panic("sys_path_to_bdev: called with NULL pointer");
61 if ( sys_stat(path, &S, false) == -1 )
66 fp = setmntent(MOUNTED,"r");
71 while ((mnt = getmntent(fp))) {
72 if ( sys_stat(mnt->mnt_dir, &S, false) == -1 )
75 if (S.st_ex_dev == devno) {
76 (*mntpath) = SMB_STRDUP(mnt->mnt_dir);
77 (*bdev) = SMB_STRDUP(mnt->mnt_fsname);
78 (*fs) = SMB_STRDUP(mnt->mnt_type);
79 if ((*mntpath)&&(*bdev)&&(*fs)) {
96 /* #endif HAVE_MNTENT */
97 #elif defined(HAVE_DEVNM)
99 /* we have this on HPUX, ... */
100 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
106 if (!path||!mntpath||!bdev||!fs)
107 smb_panic("sys_path_to_bdev: called with NULL pointer");
113 /* find the block device file */
115 if ((ret=sys_stat(path, &S, false))!=0) {
119 if ((ret=devnm(S_IFBLK, S.st_ex_dev, dev_disk, 256, 1))!=0) {
123 /* we should get the mntpath right...
124 * but I don't know how
127 (*mntpath) = SMB_STRDUP(path);
128 (*bdev) = SMB_STRDUP(dev_disk);
129 if ((*mntpath)&&(*bdev)) {
141 /* #endif HAVE_DEVNM */
143 /* we should fake this up...*/
144 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
148 if (!path||!mntpath||!bdev||!fs)
149 smb_panic("sys_path_to_bdev: called with NULL pointer");
155 (*mntpath) = SMB_STRDUP(path);
167 /*********************************************************************
168 Now the list of all filesystem specific quota systems we have found
169 **********************************************************************/
172 int (*get_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
173 int (*set_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
174 } sys_quota_backends[] = {
175 #ifdef HAVE_XFS_QUOTAS
176 {"xfs", sys_get_xfs_quota, sys_set_xfs_quota},
177 #endif /* HAVE_XFS_QUOTAS */
178 #ifdef HAVE_NFS_QUOTAS
179 {"nfs", sys_get_nfs_quota, sys_set_nfs_quota},
180 #endif /* HAVE_NFS_QUOTAS */
184 static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
186 const char *get_quota_command;
189 get_quota_command = lp_get_quota_command(talloc_tos());
190 if (get_quota_command && *get_quota_command) {
197 case SMB_USER_QUOTA_TYPE:
198 case SMB_USER_FS_QUOTA_TYPE:
201 case SMB_GROUP_QUOTA_TYPE:
202 case SMB_GROUP_FS_QUOTA_TYPE:
206 DEBUG(0,("invalid quota type.\n"));
210 if (asprintf(&syscmd, "%s \"%s\" %d %d",
211 get_quota_command, path, qtype, _id) < 0) {
215 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
217 lines = file_lines_pload(syscmd, NULL);
221 char *line = lines[0];
223 DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
225 /* we need to deal with long long unsigned here, if supported */
227 dp->qflags = (enum SMB_QUOTA_TYPE)strtoul(line, &p2, 10);
229 while (p && *p && isspace(*p)) {
234 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
239 while (p && *p && isspace(*p)) {
244 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
249 while (p && *p && isspace(*p)) {
254 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
259 while (p && *p && isspace(*p)) {
264 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
269 while (p && *p && isspace(*p)) {
274 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
279 while (p && *p && isspace(*p)) {
284 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
289 while (p && *p && isspace(*p)) {
294 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
302 DEBUG (3, ("Parsed output of get_quota, ...\n"));
305 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
306 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n",
307 dp->qflags,(long long unsigned)dp->curblocks,
308 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
309 (long long unsigned)dp->curinodes,
310 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
311 (long long unsigned)dp->bsize));
315 DEBUG (0, ("get_quota_command failed!\n"));
325 DEBUG(0,("The output of get_quota_command is invalid!\n"));
329 static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
331 const char *set_quota_command;
333 set_quota_command = lp_set_quota_command(talloc_tos());
334 if (set_quota_command && *set_quota_command) {
340 case SMB_USER_QUOTA_TYPE:
341 case SMB_USER_FS_QUOTA_TYPE:
344 case SMB_GROUP_QUOTA_TYPE:
345 case SMB_GROUP_FS_QUOTA_TYPE:
352 if (asprintf(&syscmd,
356 set_quota_command, path, qtype, _id, dp->qflags,
357 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
358 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
359 (long long unsigned)dp->bsize) < 0) {
363 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
365 lines = file_lines_pload(syscmd, NULL);
368 char *line = lines[0];
370 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
376 DEBUG (0, ("set_quota_command failed!\n"));
384 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
389 char *mntpath = NULL;
394 smb_panic("sys_get_quota: called with NULL pointer");
396 if (command_get_quota(path, qtype, id, dp)==0) {
398 } else if (errno != ENOSYS) {
402 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
403 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
408 DEBUG(10,("sys_get_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
410 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
411 if (strcmp(fs,sys_quota_backends[i].name)==0) {
412 ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
414 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
415 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
417 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
418 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
426 /* use the default vfs quota functions */
427 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
429 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
430 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
432 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
433 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
441 if ((ret!=0)&& (errno == EDQUOT)) {
442 DEBUG(10,("sys_get_quota() warning over quota!\n"));
449 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
454 char *mntpath = NULL;
458 /* find the block device file */
461 smb_panic("get_smb_quota: called with NULL pointer");
463 if (command_set_quota(path, qtype, id, dp)==0) {
465 } else if (errno != ENOSYS) {
469 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
470 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
475 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
477 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
478 if (strcmp(fs,sys_quota_backends[i].name)==0) {
479 ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
481 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
482 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
484 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
485 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
493 /* use the default vfs quota functions */
494 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
496 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
497 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
499 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
500 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
508 if ((ret!=0)&& (errno == EDQUOT)) {
509 DEBUG(10,("sys_set_quota() warning over quota!\n"));
516 #else /* HAVE_SYS_QUOTAS */
517 void dummy_sysquotas_c(void);
519 void dummy_sysquotas_c(void)
523 #endif /* HAVE_SYS_QUOTAS */