s3: add sysquotas_4B support
[Samba/gebeck_regimport.git] / source3 / lib / sysquotas.c
blobecd5147d86550b905e9a90e1b3ea3b7aec67b084
1 /*
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/>.
21 #include "includes.h"
23 #undef DBGC_CLASS
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 */
43 #ifdef HAVE_MNTENT
44 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
46 int ret = -1;
47 SMB_STRUCT_STAT S;
48 FILE *fp;
49 struct mntent *mnt;
50 SMB_DEV_T devno;
52 /* find the block device file */
54 if (!path||!mntpath||!bdev||!fs)
55 smb_panic("sys_path_to_bdev: called with NULL pointer");
57 (*mntpath) = NULL;
58 (*bdev) = NULL;
59 (*fs) = NULL;
61 if ( sys_stat(path, &S, false) == -1 )
62 return (-1);
64 devno = S.st_ex_dev ;
66 fp = setmntent(MOUNTED,"r");
67 if (fp == NULL) {
68 return -1;
71 while ((mnt = getmntent(fp))) {
72 if ( sys_stat(mnt->mnt_dir, &S, false) == -1 )
73 continue ;
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)) {
80 ret = 0;
81 } else {
82 SAFE_FREE(*mntpath);
83 SAFE_FREE(*bdev);
84 SAFE_FREE(*fs);
85 ret = -1;
88 break;
92 endmntent(fp) ;
94 return ret;
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)
102 int ret = -1;
103 char dev_disk[256];
104 SMB_STRUCT_STAT S;
106 if (!path||!mntpath||!bdev||!fs)
107 smb_panic("sys_path_to_bdev: called with NULL pointer");
109 (*mntpath) = NULL;
110 (*bdev) = NULL;
111 (*fs) = NULL;
113 /* find the block device file */
115 if ((ret=sys_stat(path, &S, false))!=0) {
116 return ret;
119 if ((ret=devnm(S_IFBLK, S.st_ex_dev, dev_disk, 256, 1))!=0) {
120 return ret;
123 /* we should get the mntpath right...
124 * but I don't know how
125 * --metze
127 (*mntpath) = SMB_STRDUP(path);
128 (*bdev) = SMB_STRDUP(dev_disk);
129 if ((*mntpath)&&(*bdev)) {
130 ret = 0;
131 } else {
132 SAFE_FREE(*mntpath);
133 SAFE_FREE(*bdev);
134 ret = -1;
138 return ret;
141 /* #endif HAVE_DEVNM */
142 #else
143 /* we should fake this up...*/
144 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
146 int ret = -1;
148 if (!path||!mntpath||!bdev||!fs)
149 smb_panic("sys_path_to_bdev: called with NULL pointer");
151 (*mntpath) = NULL;
152 (*bdev) = NULL;
153 (*fs) = NULL;
155 (*mntpath) = SMB_STRDUP(path);
156 if (*mntpath) {
157 ret = 0;
158 } else {
159 SAFE_FREE(*mntpath);
160 ret = -1;
163 return ret;
165 #endif
167 /*********************************************************************
168 Now the list of all filesystem specific quota systems we have found
169 **********************************************************************/
170 static struct {
171 const char *name;
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 */
181 {NULL, NULL, NULL}
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;
187 char **lines = NULL;
189 get_quota_command = lp_get_quota_command(talloc_tos());
190 if (get_quota_command && *get_quota_command) {
191 const char *p;
192 char *p2;
193 char *syscmd = NULL;
194 int _id = -1;
196 switch(qtype) {
197 case SMB_USER_QUOTA_TYPE:
198 case SMB_USER_FS_QUOTA_TYPE:
199 _id = id.uid;
200 break;
201 case SMB_GROUP_QUOTA_TYPE:
202 case SMB_GROUP_FS_QUOTA_TYPE:
203 _id = id.gid;
204 break;
205 default:
206 DEBUG(0,("invalid quota type.\n"));
207 return -1;
210 if (asprintf(&syscmd, "%s \"%s\" %d %d",
211 get_quota_command, path, qtype, _id) < 0) {
212 return -1;
215 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
217 lines = file_lines_pload(syscmd, NULL);
218 SAFE_FREE(syscmd);
220 if (lines) {
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);
228 p = p2;
229 while (p && *p && isspace(*p)) {
230 p++;
233 if (p && *p) {
234 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
235 } else {
236 goto invalid_param;
239 while (p && *p && isspace(*p)) {
240 p++;
243 if (p && *p) {
244 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
245 } else {
246 goto invalid_param;
249 while (p && *p && isspace(*p)) {
250 p++;
253 if (p && *p) {
254 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
255 } else {
256 goto invalid_param;
259 while (p && *p && isspace(*p)) {
260 p++;
263 if (p && *p) {
264 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
265 } else {
266 goto invalid_param;
269 while (p && *p && isspace(*p)) {
270 p++;
273 if (p && *p) {
274 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
275 } else {
276 goto invalid_param;
279 while (p && *p && isspace(*p)) {
280 p++;
283 if (p && *p) {
284 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
285 } else {
286 goto invalid_param;
289 while (p && *p && isspace(*p)) {
290 p++;
293 if (p && *p) {
294 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
295 } else {
296 dp->bsize = 1024;
299 TALLOC_FREE(lines);
300 lines = NULL;
302 DEBUG (3, ("Parsed output of get_quota, ...\n"));
304 DEBUGADD (5,(
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));
312 return 0;
315 DEBUG (0, ("get_quota_command failed!\n"));
316 return -1;
319 errno = ENOSYS;
320 return -1;
322 invalid_param:
324 TALLOC_FREE(lines);
325 DEBUG(0,("The output of get_quota_command is invalid!\n"));
326 return -1;
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) {
335 char **lines = NULL;
336 char *syscmd = NULL;
337 int _id = -1;
339 switch(qtype) {
340 case SMB_USER_QUOTA_TYPE:
341 case SMB_USER_FS_QUOTA_TYPE:
342 _id = id.uid;
343 break;
344 case SMB_GROUP_QUOTA_TYPE:
345 case SMB_GROUP_FS_QUOTA_TYPE:
346 _id = id.gid;
347 break;
348 default:
349 return -1;
352 if (asprintf(&syscmd,
353 "%s \"%s\" %d %d "
354 "%u %llu %llu "
355 "%llu %llu %llu ",
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) {
360 return -1;
363 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
365 lines = file_lines_pload(syscmd, NULL);
366 SAFE_FREE(syscmd);
367 if (lines) {
368 char *line = lines[0];
370 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
372 TALLOC_FREE(lines);
374 return 0;
376 DEBUG (0, ("set_quota_command failed!\n"));
377 return -1;
380 errno = ENOSYS;
381 return -1;
384 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
386 int ret = -1;
387 int i;
388 bool ready = False;
389 char *mntpath = NULL;
390 char *bdev = NULL;
391 char *fs = NULL;
393 if (!path||!dp)
394 smb_panic("sys_get_quota: called with NULL pointer");
396 if (command_get_quota(path, qtype, id, dp)==0) {
397 return 0;
398 } else if (errno != ENOSYS) {
399 return -1;
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));
404 return ret;
407 errno = 0;
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);
413 if (ret!=0) {
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)));
416 } else {
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)));
420 ready = True;
421 break;
425 if (!ready) {
426 /* use the default vfs quota functions */
427 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
428 if (ret!=0) {
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)));
431 } else {
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)));
437 SAFE_FREE(mntpath);
438 SAFE_FREE(bdev);
439 SAFE_FREE(fs);
441 if ((ret!=0)&& (errno == EDQUOT)) {
442 DEBUG(10,("sys_get_quota() warning over quota!\n"));
443 return 0;
446 return ret;
449 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
451 int ret = -1;
452 int i;
453 bool ready = False;
454 char *mntpath = NULL;
455 char *bdev = NULL;
456 char *fs = NULL;
458 /* find the block device file */
460 if (!path||!dp)
461 smb_panic("get_smb_quota: called with NULL pointer");
463 if (command_set_quota(path, qtype, id, dp)==0) {
464 return 0;
465 } else if (errno != ENOSYS) {
466 return -1;
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));
471 return ret;
474 errno = 0;
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);
480 if (ret!=0) {
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)));
483 } else {
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)));
487 ready = True;
488 break;
492 if (!ready) {
493 /* use the default vfs quota functions */
494 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
495 if (ret!=0) {
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)));
498 } else {
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)));
504 SAFE_FREE(mntpath);
505 SAFE_FREE(bdev);
506 SAFE_FREE(fs);
508 if ((ret!=0)&& (errno == EDQUOT)) {
509 DEBUG(10,("sys_set_quota() warning over quota!\n"));
510 return 0;
513 return ret;
516 #else /* HAVE_SYS_QUOTAS */
517 void dummy_sysquotas_c(void);
519 void dummy_sysquotas_c(void)
521 return;
523 #endif /* HAVE_SYS_QUOTAS */