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/>.
22 #include "lib/util_file.h"
23 #include "lib/util/smb_strtox.h"
26 #define DBGC_CLASS DBGC_QUOTA
28 #ifdef HAVE_SYS_QUOTAS
30 #if defined(HAVE_QUOTACTL_4A)
32 /*#endif HAVE_QUOTACTL_4A */
33 #elif defined(HAVE_QUOTACTL_4B)
35 /*#endif HAVE_QUOTACTL_4B */
36 #elif defined(HAVE_QUOTACTL_3)
38 #error HAVE_QUOTACTL_3 not implemented
40 /* #endif HAVE_QUOTACTL_3 */
41 #else /* NO_QUOTACTL_USED */
43 #endif /* NO_QUOTACTL_USED */
45 #if defined(HAVE_MNTENT) && defined(HAVE_REALPATH)
46 static int sys_path_to_bdev(const char *path
, char **mntpath
, char **bdev
, char **fs
)
51 struct mntent
*mnt
= NULL
;
53 char *stat_mntpath
= NULL
;
56 /* find the block device file */
61 if (sys_stat(path
, &S
, false) != 0) {
67 stat_mntpath
= sys_realpath(path
);
68 if (stat_mntpath
== NULL
) {
69 DBG_WARNING("realpath(%s) failed - %s\n", path
,
74 if (sys_stat(stat_mntpath
, &S
, false) != 0) {
75 DBG_WARNING("cannot stat real path %s - %s\n", stat_mntpath
,
80 if (S
.st_ex_dev
!= devno
) {
81 DBG_WARNING("device on real path has changed\n");
88 p
= strrchr(stat_mntpath
, '/');
90 DBG_ERR("realpath for %s does not begin with a '/'\n",
95 if (p
== stat_mntpath
) {
101 if (sys_stat(stat_mntpath
, &S
, false) != 0) {
102 DBG_WARNING("cannot stat real path component %s - %s\n",
103 stat_mntpath
, strerror(errno
));
106 if (S
.st_ex_dev
!= devno
) {
111 if (p
<= stat_mntpath
+ 1) {
116 fp
= setmntent(MOUNTED
,"r");
121 while ((mnt
= getmntent(fp
))) {
122 if (!strequal(mnt
->mnt_dir
, stat_mntpath
)) {
126 if ( sys_stat(mnt
->mnt_dir
, &S
, false) == -1 )
129 if (S
.st_ex_dev
== devno
) {
130 (*mntpath
) = SMB_STRDUP(mnt
->mnt_dir
);
131 (*bdev
) = SMB_STRDUP(mnt
->mnt_fsname
);
132 (*fs
) = SMB_STRDUP(mnt
->mnt_type
);
133 if ((*mntpath
)&&(*bdev
)&&(*fs
)) {
149 SAFE_FREE(stat_mntpath
);
152 /* #endif HAVE_MNTENT */
153 #elif defined(HAVE_DEVNM)
155 /* we have this on HPUX, ... */
156 static int sys_path_to_bdev(const char *path
, char **mntpath
, char **bdev
, char **fs
)
162 if (!path
||!mntpath
||!bdev
||!fs
)
163 smb_panic("sys_path_to_bdev: called with NULL pointer");
169 /* find the block device file */
171 if ((ret
=sys_stat(path
, &S
, false))!=0) {
175 if ((ret
=devnm(S_IFBLK
, S
.st_ex_dev
, dev_disk
, 256, 1))!=0) {
179 /* we should get the mntpath right...
180 * but I don't know how
183 (*mntpath
) = SMB_STRDUP(path
);
184 (*bdev
) = SMB_STRDUP(dev_disk
);
185 if ((*mntpath
)&&(*bdev
)) {
197 /* #endif HAVE_DEVNM */
199 /* we should fake this up...*/
200 static int sys_path_to_bdev(const char *path
, char **mntpath
, char **bdev
, char **fs
)
204 if (!path
||!mntpath
||!bdev
||!fs
)
205 smb_panic("sys_path_to_bdev: called with NULL pointer");
211 (*mntpath
) = SMB_STRDUP(path
);
223 /*********************************************************************
224 Now the list of all filesystem specific quota systems we have found
225 **********************************************************************/
228 int (*get_quota
)(const char *path
, const char *bdev
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
);
229 int (*set_quota
)(const char *path
, const char *bdev
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
);
230 } sys_quota_backends
[] = {
231 #ifdef HAVE_JFS_QUOTA_H
232 {"jfs2", sys_get_jfs2_quota
, sys_set_jfs2_quota
},
234 #if defined HAVE_XFS_QUOTAS
235 {"xfs", sys_get_xfs_quota
, sys_set_xfs_quota
},
236 {"gfs", sys_get_xfs_quota
, sys_set_xfs_quota
},
237 {"gfs2", sys_get_xfs_quota
, sys_set_xfs_quota
},
238 #endif /* HAVE_XFS_QUOTAS */
239 #ifdef HAVE_NFS_QUOTAS
240 {"nfs", sys_get_nfs_quota
, sys_set_nfs_quota
},
241 {"nfs4", sys_get_nfs_quota
, sys_set_nfs_quota
},
242 #endif /* HAVE_NFS_QUOTAS */
246 static int command_get_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
248 const struct loadparm_substitution
*lp_sub
=
249 loadparm_s3_global_substitution();
250 const char *get_quota_command
;
253 get_quota_command
= lp_get_quota_command(talloc_tos(), lp_sub
);
254 if (get_quota_command
&& *get_quota_command
) {
262 case SMB_USER_QUOTA_TYPE
:
263 case SMB_USER_FS_QUOTA_TYPE
:
266 case SMB_GROUP_QUOTA_TYPE
:
267 case SMB_GROUP_FS_QUOTA_TYPE
:
271 DEBUG(0,("invalid quota type.\n"));
275 argl
= talloc_zero_array(talloc_tos(), char *, 5);
279 argl
[0] = talloc_strdup(argl
, get_quota_command
);
280 if (argl
[0] == NULL
) {
284 argl
[1] = talloc_strdup(argl
, path
);
285 if (argl
[1] == NULL
) {
289 argl
[2] = talloc_asprintf(argl
, "%d", qtype
);
290 if (argl
[2] == NULL
) {
294 argl
[3] = talloc_asprintf(argl
, "%d", _id
);
295 if (argl
[3] == NULL
) {
301 DBG_NOTICE("Running command %s %s %d %d\n",
307 lines
= file_lines_ploadv(talloc_tos(), argl
, NULL
);
311 char *line
= lines
[0];
313 DEBUG (3, ("Read output from get_quota, \"%s\"\n", line
));
315 /* we need to deal with long long unsigned here, if supported */
317 dp
->qflags
= smb_strtoul(line
,
327 while (p
&& *p
&& isspace(*p
)) {
332 dp
->curblocks
= STR_TO_SMB_BIG_UINT(p
, &p
);
337 while (p
&& *p
&& isspace(*p
)) {
342 dp
->softlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
347 while (p
&& *p
&& isspace(*p
)) {
352 dp
->hardlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
357 while (p
&& *p
&& isspace(*p
)) {
362 dp
->curinodes
= STR_TO_SMB_BIG_UINT(p
, &p
);
367 while (p
&& *p
&& isspace(*p
)) {
372 dp
->isoftlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
377 while (p
&& *p
&& isspace(*p
)) {
382 dp
->ihardlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
387 while (p
&& *p
&& isspace(*p
)) {
392 dp
->bsize
= STR_TO_SMB_BIG_UINT(p
, NULL
);
400 DEBUG (3, ("Parsed output of get_quota, ...\n"));
403 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
404 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n",
405 dp
->qflags
,(long long unsigned)dp
->curblocks
,
406 (long long unsigned)dp
->softlimit
,(long long unsigned)dp
->hardlimit
,
407 (long long unsigned)dp
->curinodes
,
408 (long long unsigned)dp
->isoftlimit
,(long long unsigned)dp
->ihardlimit
,
409 (long long unsigned)dp
->bsize
));
413 DEBUG (0, ("get_quota_command failed!\n"));
423 DEBUG(0,("The output of get_quota_command is invalid!\n"));
427 static int command_set_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
429 const struct loadparm_substitution
*lp_sub
=
430 loadparm_s3_global_substitution();
431 const char *set_quota_command
;
433 set_quota_command
= lp_set_quota_command(talloc_tos(), lp_sub
);
434 if (set_quota_command
&& *set_quota_command
) {
440 case SMB_USER_QUOTA_TYPE
:
441 case SMB_USER_FS_QUOTA_TYPE
:
444 case SMB_GROUP_QUOTA_TYPE
:
445 case SMB_GROUP_FS_QUOTA_TYPE
:
452 argl
= talloc_zero_array(talloc_tos(), char *, 11);
456 argl
[0] = talloc_strdup(argl
, set_quota_command
);
457 if (argl
[0] == NULL
) {
461 argl
[1] = talloc_strdup(argl
, path
);
462 if (argl
[1] == NULL
) {
466 argl
[2] = talloc_asprintf(argl
, "%d", qtype
);
467 if (argl
[2] == NULL
) {
471 argl
[3] = talloc_asprintf(argl
, "%d", _id
);
472 if (argl
[3] == NULL
) {
476 argl
[4] = talloc_asprintf(argl
, "%u", dp
->qflags
);
477 if (argl
[4] == NULL
) {
481 argl
[5] = talloc_asprintf(argl
, "%llu",
482 (long long unsigned)dp
->softlimit
);
483 if (argl
[5] == NULL
) {
487 argl
[6] = talloc_asprintf(argl
, "%llu",
488 (long long unsigned)dp
->hardlimit
);
489 if (argl
[6] == NULL
) {
493 argl
[7] = talloc_asprintf(argl
, "%llu",
494 (long long unsigned)dp
->isoftlimit
);
495 if (argl
[7] == NULL
) {
499 argl
[8] = talloc_asprintf(argl
, "%llu",
500 (long long unsigned)dp
->ihardlimit
);
501 if (argl
[8] == NULL
) {
505 argl
[9] = talloc_asprintf(argl
, "%llu",
506 (long long unsigned)dp
->bsize
);
507 if (argl
[9] == NULL
) {
513 DBG_NOTICE("Running command "
522 (long long unsigned)dp
->softlimit
,
523 (long long unsigned)dp
->hardlimit
,
524 (long long unsigned)dp
->isoftlimit
,
525 (long long unsigned)dp
->ihardlimit
,
526 (long long unsigned)dp
->bsize
);
528 lines
= file_lines_ploadv(talloc_tos(), argl
, NULL
);
531 char *line
= lines
[0];
533 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line
));
539 DEBUG (0, ("set_quota_command failed!\n"));
547 int sys_get_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
552 char *mntpath
= NULL
;
557 smb_panic("sys_get_quota: called with NULL pointer");
559 if (command_get_quota(path
, qtype
, id
, dp
)==0) {
561 } else if (errno
!= ENOSYS
) {
565 if ((ret
=sys_path_to_bdev(path
,&mntpath
,&bdev
,&fs
))!=0) {
566 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path
));
571 DEBUG(10,("sys_get_quota() uid(%u, %u), fs(%s)\n", (unsigned)getuid(), (unsigned)geteuid(), fs
));
573 for (i
=0;(fs
&& sys_quota_backends
[i
].name
&& sys_quota_backends
[i
].get_quota
);i
++) {
574 if (strcmp(fs
,sys_quota_backends
[i
].name
)==0) {
575 ret
= sys_quota_backends
[i
].get_quota(mntpath
, bdev
, qtype
, id
, dp
);
577 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
578 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
580 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
581 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
589 /* use the default vfs quota functions */
590 ret
=sys_get_vfs_quota(mntpath
, bdev
, qtype
, id
, dp
);
592 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
593 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
595 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
596 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
607 int sys_set_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
612 char *mntpath
= NULL
;
616 /* find the block device file */
619 smb_panic("get_smb_quota: called with NULL pointer");
621 if (command_set_quota(path
, qtype
, id
, dp
)==0) {
623 } else if (errno
!= ENOSYS
) {
627 if ((ret
=sys_path_to_bdev(path
,&mntpath
,&bdev
,&fs
))!=0) {
628 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path
));
633 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
635 for (i
=0;(fs
&& sys_quota_backends
[i
].name
&& sys_quota_backends
[i
].set_quota
);i
++) {
636 if (strcmp(fs
,sys_quota_backends
[i
].name
)==0) {
637 ret
= sys_quota_backends
[i
].set_quota(mntpath
, bdev
, qtype
, id
, dp
);
639 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
640 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
642 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
643 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
651 /* use the default vfs quota functions */
652 ret
=sys_set_vfs_quota(mntpath
, bdev
, qtype
, id
, dp
);
654 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
655 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
657 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
658 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
669 #else /* HAVE_SYS_QUOTAS */
670 void dummy_sysquotas_c(void);
672 void dummy_sysquotas_c(void)
676 #endif /* HAVE_SYS_QUOTAS */