include updates
[tomato.git] / release / src-rt-6.x.4708 / router / samba-3.0.25b / source / lib / sysquotas.c
blob62714cf4d51579cc442dfdf07cbae0059502cc42
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_QUOTA
27 #ifdef HAVE_SYS_QUOTAS
29 #if defined(HAVE_QUOTACTL_4A)
31 /*#endif HAVE_QUOTACTL_4A */
32 #elif defined(HAVE_QUOTACTL_4B)
34 #error HAVE_QUOTACTL_4B not implemeted
36 /*#endif HAVE_QUOTACTL_4B */
37 #elif defined(HAVE_QUOTACTL_3)
39 #error HAVE_QUOTACTL_3 not implemented
41 /* #endif HAVE_QUOTACTL_3 */
42 #else /* NO_QUOTACTL_USED */
44 #endif /* NO_QUOTACTL_USED */
46 #ifdef HAVE_MNTENT
47 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
49 int ret = -1;
50 SMB_STRUCT_STAT S;
51 FILE *fp;
52 struct mntent *mnt;
53 SMB_DEV_T devno;
55 /* find the block device file */
57 if (!path||!mntpath||!bdev||!fs)
58 smb_panic("sys_path_to_bdev: called with NULL pointer");
60 (*mntpath) = NULL;
61 (*bdev) = NULL;
62 (*fs) = NULL;
64 if ( sys_stat(path, &S) == -1 )
65 return (-1);
67 devno = S.st_dev ;
69 fp = setmntent(MOUNTED,"r");
70 if (fp == NULL) {
71 return -1;
74 while ((mnt = getmntent(fp))) {
75 if ( sys_stat(mnt->mnt_dir,&S) == -1 )
76 continue ;
78 if (S.st_dev == devno) {
79 (*mntpath) = SMB_STRDUP(mnt->mnt_dir);
80 (*bdev) = SMB_STRDUP(mnt->mnt_fsname);
81 (*fs) = SMB_STRDUP(mnt->mnt_type);
82 if ((*mntpath)&&(*bdev)&&(*fs)) {
83 ret = 0;
84 } else {
85 SAFE_FREE(*mntpath);
86 SAFE_FREE(*bdev);
87 SAFE_FREE(*fs);
88 ret = -1;
91 break;
95 endmntent(fp) ;
97 return ret;
99 /* #endif HAVE_MNTENT */
100 #elif defined(HAVE_DEVNM)
102 /* we have this on HPUX, ... */
103 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
105 int ret = -1;
106 char dev_disk[256];
107 SMB_STRUCT_STAT S;
109 if (!path||!mntpath||!bdev||!fs)
110 smb_panic("sys_path_to_bdev: called with NULL pointer");
112 (*mntpath) = NULL;
113 (*bdev) = NULL;
114 (*fs) = NULL;
116 /* find the block device file */
118 if ((ret=sys_stat(path, &S))!=0) {
119 return ret;
122 if ((ret=devnm(S_IFBLK, S.st_dev, dev_disk, 256, 1))!=0) {
123 return ret;
126 /* we should get the mntpath right...
127 * but I don't know how
128 * --metze
130 (*mntpath) = SMB_STRDUP(path);
131 (*bdev) = SMB_STRDUP(dev_disk);
132 if ((*mntpath)&&(*bdev)) {
133 ret = 0;
134 } else {
135 SAFE_FREE(*mntpath);
136 SAFE_FREE(*bdev);
137 ret = -1;
141 return ret;
144 /* #endif HAVE_DEVNM */
145 #else
146 /* we should fake this up...*/
147 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
149 int ret = -1;
151 if (!path||!mntpath||!bdev||!fs)
152 smb_panic("sys_path_to_bdev: called with NULL pointer");
154 (*mntpath) = NULL;
155 (*bdev) = NULL;
156 (*fs) = NULL;
158 (*mntpath) = SMB_STRDUP(path);
159 if (*mntpath) {
160 ret = 0;
161 } else {
162 SAFE_FREE(*mntpath);
163 ret = -1;
166 return ret;
168 #endif
170 /*********************************************************************
171 Now the list of all filesystem specific quota systems we have found
172 **********************************************************************/
173 static struct {
174 const char *name;
175 int (*get_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
176 int (*set_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
177 } sys_quota_backends[] = {
178 #ifdef HAVE_XFS_QUOTAS
179 {"xfs", sys_get_xfs_quota, sys_set_xfs_quota},
180 #endif /* HAVE_XFS_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();
190 if (get_quota_command && *get_quota_command) {
191 const char *p;
192 char *p2;
193 pstring syscmd;
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 slprintf(syscmd, sizeof(syscmd)-1,
211 "%s \"%s\" %d %d",
212 get_quota_command, path, qtype, _id);
214 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
216 lines = file_lines_pload(syscmd, NULL);
217 if (lines) {
218 char *line = lines[0];
220 DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
222 /* we need to deal with long long unsigned here, if supported */
224 dp->qflags = (enum SMB_QUOTA_TYPE)strtoul(line, &p2, 10);
225 p = p2;
226 while (p && *p && isspace(*p)) {
227 p++;
230 if (p && *p) {
231 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
232 } else {
233 goto invalid_param;
236 while (p && *p && isspace(*p)) {
237 p++;
240 if (p && *p) {
241 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
242 } else {
243 goto invalid_param;
246 while (p && *p && isspace(*p)) {
247 p++;
250 if (p && *p) {
251 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
252 } else {
253 goto invalid_param;
256 while (p && *p && isspace(*p)) {
257 p++;
260 if (p && *p) {
261 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
262 } else {
263 goto invalid_param;
266 while (p && *p && isspace(*p)) {
267 p++;
270 if (p && *p) {
271 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
272 } else {
273 goto invalid_param;
276 while (p && *p && isspace(*p)) {
277 p++;
280 if (p && *p) {
281 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
282 } else {
283 goto invalid_param;
286 while (p && *p && isspace(*p)) {
287 p++;
290 if (p && *p) {
291 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
292 } else {
293 dp->bsize = 1024;
296 file_lines_free(lines);
297 lines = NULL;
299 DEBUG (3, ("Parsed output of get_quota, ...\n"));
301 #ifdef LARGE_SMB_OFF_T
302 DEBUGADD (5,(
303 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
304 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n",
305 dp->qflags,(long long unsigned)dp->curblocks,
306 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
307 (long long unsigned)dp->curinodes,
308 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
309 (long long unsigned)dp->bsize));
310 #else /* LARGE_SMB_OFF_T */
311 DEBUGADD (5,(
312 "qflags:%u curblocks:%lu softlimit:%lu hardlimit:%lu\n"
313 "curinodes:%lu isoftlimit:%lu ihardlimit:%lu bsize:%lu\n",
314 dp->qflags,(long unsigned)dp->curblocks,
315 (long unsigned)dp->softlimit,(long unsigned)dp->hardlimit,
316 (long unsigned)dp->curinodes,
317 (long unsigned)dp->isoftlimit,(long unsigned)dp->ihardlimit,
318 (long unsigned)dp->bsize));
319 #endif /* LARGE_SMB_OFF_T */
320 return 0;
323 DEBUG (0, ("get_quota_command failed!\n"));
324 return -1;
327 errno = ENOSYS;
328 return -1;
330 invalid_param:
332 file_lines_free(lines);
333 DEBUG(0,("The output of get_quota_command is invalid!\n"));
334 return -1;
337 static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
339 const char *set_quota_command;
341 set_quota_command = lp_set_quota_command();
342 if (set_quota_command && *set_quota_command) {
343 char **lines;
344 pstring syscmd;
345 int _id = -1;
347 switch(qtype) {
348 case SMB_USER_QUOTA_TYPE:
349 case SMB_USER_FS_QUOTA_TYPE:
350 _id = id.uid;
351 break;
352 case SMB_GROUP_QUOTA_TYPE:
353 case SMB_GROUP_FS_QUOTA_TYPE:
354 _id = id.gid;
355 break;
356 default:
357 return -1;
360 #ifdef LARGE_SMB_OFF_T
361 slprintf(syscmd, sizeof(syscmd)-1,
362 "%s \"%s\" %d %d "
363 "%u %llu %llu "
364 "%llu %llu %llu ",
365 set_quota_command, path, qtype, _id, dp->qflags,
366 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
367 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
368 (long long unsigned)dp->bsize);
369 #else /* LARGE_SMB_OFF_T */
370 slprintf(syscmd, sizeof(syscmd)-1,
371 "%s \"%s\" %d %d "
372 "%u %lu %lu "
373 "%lu %lu %lu ",
374 set_quota_command, path, qtype, _id, dp->qflags,
375 (long unsigned)dp->softlimit,(long unsigned)dp->hardlimit,
376 (long unsigned)dp->isoftlimit,(long unsigned)dp->ihardlimit,
377 (long unsigned)dp->bsize);
378 #endif /* LARGE_SMB_OFF_T */
382 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
384 lines = file_lines_pload(syscmd, NULL);
385 if (lines) {
386 char *line = lines[0];
388 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
390 file_lines_free(lines);
392 return 0;
394 DEBUG (0, ("set_quota_command failed!\n"));
395 return -1;
398 errno = ENOSYS;
399 return -1;
402 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
404 int ret = -1;
405 int i;
406 BOOL ready = False;
407 char *mntpath = NULL;
408 char *bdev = NULL;
409 char *fs = NULL;
411 if (!path||!dp)
412 smb_panic("sys_get_quota: called with NULL pointer");
414 if (command_get_quota(path, qtype, id, dp)==0) {
415 return 0;
416 } else if (errno != ENOSYS) {
417 return -1;
420 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
421 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
422 return ret;
425 errno = 0;
426 DEBUG(10,("sys_get_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
428 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
429 if (strcmp(fs,sys_quota_backends[i].name)==0) {
430 ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
431 if (ret!=0) {
432 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
433 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
434 } else {
435 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
436 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
438 ready = True;
439 break;
443 if (!ready) {
444 /* use the default vfs quota functions */
445 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
446 if (ret!=0) {
447 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
448 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
449 } else {
450 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
451 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
455 SAFE_FREE(mntpath);
456 SAFE_FREE(bdev);
457 SAFE_FREE(fs);
459 if ((ret!=0)&& (errno == EDQUOT)) {
460 DEBUG(10,("sys_get_quota() warning over quota!\n"));
461 return 0;
464 return ret;
467 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
469 int ret = -1;
470 int i;
471 BOOL ready = False;
472 char *mntpath = NULL;
473 char *bdev = NULL;
474 char *fs = NULL;
476 /* find the block device file */
478 if (!path||!dp)
479 smb_panic("get_smb_quota: called with NULL pointer");
481 if (command_set_quota(path, qtype, id, dp)==0) {
482 return 0;
483 } else if (errno != ENOSYS) {
484 return -1;
487 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
488 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
489 return ret;
492 errno = 0;
493 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
495 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
496 if (strcmp(fs,sys_quota_backends[i].name)==0) {
497 ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
498 if (ret!=0) {
499 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
500 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
501 } else {
502 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
503 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
505 ready = True;
506 break;
510 if (!ready) {
511 /* use the default vfs quota functions */
512 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
513 if (ret!=0) {
514 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
515 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
516 } else {
517 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
518 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
522 SAFE_FREE(mntpath);
523 SAFE_FREE(bdev);
524 SAFE_FREE(fs);
526 if ((ret!=0)&& (errno == EDQUOT)) {
527 DEBUG(10,("sys_set_quota() warning over quota!\n"));
528 return 0;
531 return ret;
534 #else /* HAVE_SYS_QUOTAS */
535 void dummy_sysquotas_c(void);
537 void dummy_sysquotas_c(void)
539 return;
541 #endif /* HAVE_SYS_QUOTAS */