forked from smbecker/samba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysquotas.c
619 lines (509 loc) · 14 KB
/
sysquotas.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
Unix SMB/CIFS implementation.
System QUOTA function wrappers
Copyright (C) Stefan (metze) Metzmacher 2003
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "lib/util_file.h"
#include "lib/util/smb_strtox.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_QUOTA
#ifdef HAVE_SYS_QUOTAS
#if defined(HAVE_QUOTACTL_4A)
/*#endif HAVE_QUOTACTL_4A */
#elif defined(HAVE_QUOTACTL_4B)
/*#endif HAVE_QUOTACTL_4B */
#elif defined(HAVE_QUOTACTL_3)
#error HAVE_QUOTACTL_3 not implemented
/* #endif HAVE_QUOTACTL_3 */
#else /* NO_QUOTACTL_USED */
#endif /* NO_QUOTACTL_USED */
#if defined(HAVE_MNTENT) && defined(HAVE_REALPATH)
static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
{
int ret = -1;
SMB_STRUCT_STAT S;
FILE *fp;
struct mntent *mnt = NULL;
SMB_DEV_T devno;
char *stat_mntpath = NULL;
char *p;
/* find the block device file */
(*mntpath) = NULL;
(*bdev) = NULL;
(*fs) = NULL;
if (sys_stat(path, &S, false) != 0) {
return -1;
}
devno = S.st_ex_dev ;
stat_mntpath = sys_realpath(path);
if (stat_mntpath == NULL) {
DBG_WARNING("realpath(%s) failed - %s\n", path,
strerror(errno));
goto out;
}
if (sys_stat(stat_mntpath, &S, false) != 0) {
DBG_WARNING("cannot stat real path %s - %s\n", stat_mntpath,
strerror(errno));
goto out;
}
if (S.st_ex_dev != devno) {
DBG_WARNING("device on real path has changed\n");
goto out;
}
while (true) {
char save_ch;
p = strrchr(stat_mntpath, '/');
if (p == NULL) {
DBG_ERR("realpath for %s does not begin with a '/'\n",
path);
goto out;
}
if (p == stat_mntpath) {
++p;
}
save_ch = *p;
*p = 0;
if (sys_stat(stat_mntpath, &S, false) != 0) {
DBG_WARNING("cannot stat real path component %s - %s\n",
stat_mntpath, strerror(errno));
goto out;
}
if (S.st_ex_dev != devno) {
*p = save_ch;
break;
}
if (p <= stat_mntpath + 1) {
break;
}
}
fp = setmntent(MOUNTED,"r");
if (fp == NULL) {
goto out;
}
while ((mnt = getmntent(fp))) {
if (!strequal(mnt->mnt_dir, stat_mntpath)) {
continue;
}
if ( sys_stat(mnt->mnt_dir, &S, false) == -1 )
continue ;
if (S.st_ex_dev == devno) {
(*mntpath) = SMB_STRDUP(mnt->mnt_dir);
(*bdev) = SMB_STRDUP(mnt->mnt_fsname);
(*fs) = SMB_STRDUP(mnt->mnt_type);
if ((*mntpath)&&(*bdev)&&(*fs)) {
ret = 0;
} else {
SAFE_FREE(*mntpath);
SAFE_FREE(*bdev);
SAFE_FREE(*fs);
ret = -1;
}
break;
}
}
endmntent(fp) ;
out:
SAFE_FREE(stat_mntpath);
return ret;
}
/* #endif HAVE_MNTENT */
#elif defined(HAVE_DEVNM)
/* we have this on HPUX, ... */
static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
{
int ret = -1;
char dev_disk[256];
SMB_STRUCT_STAT S;
if (!path||!mntpath||!bdev||!fs)
smb_panic("sys_path_to_bdev: called with NULL pointer");
(*mntpath) = NULL;
(*bdev) = NULL;
(*fs) = NULL;
/* find the block device file */
if ((ret=sys_stat(path, &S, false))!=0) {
return ret;
}
if ((ret=devnm(S_IFBLK, S.st_ex_dev, dev_disk, 256, 1))!=0) {
return ret;
}
/* we should get the mntpath right...
* but I don't know how
* --metze
*/
(*mntpath) = SMB_STRDUP(path);
(*bdev) = SMB_STRDUP(dev_disk);
if ((*mntpath)&&(*bdev)) {
ret = 0;
} else {
SAFE_FREE(*mntpath);
SAFE_FREE(*bdev);
ret = -1;
}
return ret;
}
/* #endif HAVE_DEVNM */
#else
/* we should fake this up...*/
static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
{
int ret = -1;
if (!path||!mntpath||!bdev||!fs)
smb_panic("sys_path_to_bdev: called with NULL pointer");
(*mntpath) = NULL;
(*bdev) = NULL;
(*fs) = NULL;
(*mntpath) = SMB_STRDUP(path);
if (*mntpath) {
ret = 0;
} else {
SAFE_FREE(*mntpath);
ret = -1;
}
return ret;
}
#endif
/*********************************************************************
Now the list of all filesystem specific quota systems we have found
**********************************************************************/
static struct {
const char *name;
int (*get_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
int (*set_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
} sys_quota_backends[] = {
#ifdef HAVE_JFS_QUOTA_H
{"jfs2", sys_get_jfs2_quota, sys_set_jfs2_quota},
#endif
#if defined HAVE_XFS_QUOTAS
{"xfs", sys_get_xfs_quota, sys_set_xfs_quota},
{"gfs", sys_get_xfs_quota, sys_set_xfs_quota},
{"gfs2", sys_get_xfs_quota, sys_set_xfs_quota},
#endif /* HAVE_XFS_QUOTAS */
#ifdef HAVE_NFS_QUOTAS
{"nfs", sys_get_nfs_quota, sys_set_nfs_quota},
{"nfs4", sys_get_nfs_quota, sys_set_nfs_quota},
#endif /* HAVE_NFS_QUOTAS */
{NULL, NULL, NULL}
};
static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
{
const struct loadparm_substitution *lp_sub =
loadparm_s3_global_substitution();
const char *get_quota_command;
char **lines = NULL;
get_quota_command = lp_get_quota_command(talloc_tos(), lp_sub);
if (get_quota_command && *get_quota_command) {
const char *p;
char *p2;
int _id = -1;
int error = 0;
char **argl = NULL;
switch(qtype) {
case SMB_USER_QUOTA_TYPE:
case SMB_USER_FS_QUOTA_TYPE:
_id = id.uid;
break;
case SMB_GROUP_QUOTA_TYPE:
case SMB_GROUP_FS_QUOTA_TYPE:
_id = id.gid;
break;
default:
DEBUG(0,("invalid quota type.\n"));
return -1;
}
argl = str_list_make_empty(talloc_tos());
str_list_add_printf(&argl, "%s", get_quota_command);
str_list_add_printf(&argl, "%s", path);
str_list_add_printf(&argl, "%d", qtype);
str_list_add_printf(&argl, "%d", _id);
if (argl == NULL) {
return -1;
}
DBG_NOTICE("Running command %s %s %d %d\n",
get_quota_command,
path,
qtype,
_id);
lines = file_lines_ploadv(talloc_tos(), argl, NULL);
TALLOC_FREE(argl);
if (lines) {
char *line = lines[0];
DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
/* we need to deal with long long unsigned here, if supported */
dp->qflags = smb_strtoul(line,
&p2,
10,
&error,
SMB_STR_STANDARD);
if (error != 0) {
goto invalid_param;
}
p = p2;
while (p && *p && isspace(*p)) {
p++;
}
if (p && *p) {
dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
} else {
goto invalid_param;
}
while (p && *p && isspace(*p)) {
p++;
}
if (p && *p) {
dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
} else {
goto invalid_param;
}
while (p && *p && isspace(*p)) {
p++;
}
if (p && *p) {
dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
} else {
goto invalid_param;
}
while (p && *p && isspace(*p)) {
p++;
}
if (p && *p) {
dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
} else {
goto invalid_param;
}
while (p && *p && isspace(*p)) {
p++;
}
if (p && *p) {
dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
} else {
goto invalid_param;
}
while (p && *p && isspace(*p)) {
p++;
}
if (p && *p) {
dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
} else {
goto invalid_param;
}
while (p && *p && isspace(*p)) {
p++;
}
if (p && *p) {
dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
} else {
dp->bsize = 1024;
}
TALLOC_FREE(lines);
lines = NULL;
DEBUG (3, ("Parsed output of get_quota, ...\n"));
DEBUGADD (5,(
"qflags:%"PRIu32" curblocks:%"PRIu64" softlimit:%"PRIu64" hardlimit:%"PRIu64"\n"
"curinodes:%"PRIu64" isoftlimit:%"PRIu64" ihardlimit:%"PRIu64" bsize:%"PRIu64"\n",
dp->qflags,dp->curblocks,
dp->softlimit,dp->hardlimit,
dp->curinodes,
dp->isoftlimit,dp->ihardlimit,
dp->bsize));
return 0;
}
DEBUG (0, ("get_quota_command failed!\n"));
return -1;
}
errno = ENOSYS;
return -1;
invalid_param:
TALLOC_FREE(lines);
DEBUG(0,("The output of get_quota_command is invalid!\n"));
return -1;
}
static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
{
const struct loadparm_substitution *lp_sub =
loadparm_s3_global_substitution();
const char *set_quota_command;
set_quota_command = lp_set_quota_command(talloc_tos(), lp_sub);
if (set_quota_command && *set_quota_command) {
char **lines = NULL;
int _id = -1;
char **argl = NULL;
switch(qtype) {
case SMB_USER_QUOTA_TYPE:
case SMB_USER_FS_QUOTA_TYPE:
_id = id.uid;
break;
case SMB_GROUP_QUOTA_TYPE:
case SMB_GROUP_FS_QUOTA_TYPE:
_id = id.gid;
break;
default:
return -1;
}
argl = str_list_make_empty(talloc_tos());
str_list_add_printf(&argl, "%s", set_quota_command);
str_list_add_printf(&argl, "%s", path);
str_list_add_printf(&argl, "%d", qtype);
str_list_add_printf(&argl, "%d", _id);
str_list_add_printf(&argl, "%u", dp->qflags);
str_list_add_printf(
&argl, "%"PRIu64, dp->softlimit);
str_list_add_printf(
&argl, "%"PRIu64, dp->hardlimit);
str_list_add_printf(
&argl, "%"PRIu64, dp->isoftlimit);
str_list_add_printf(
&argl, "%"PRIu64, dp->ihardlimit);
str_list_add_printf(
&argl, "%"PRIu64, dp->bsize);
if (argl == NULL) {
return -1;
}
DBG_NOTICE("Running command "
"%s %s %d %d "
"%"PRIu32" %"PRIu64" %"PRIu64" "
"%"PRIu64" %"PRIu64" %"PRIu64"\n",
set_quota_command,
path,
qtype,
_id,
dp->qflags,
dp->softlimit,
dp->hardlimit,
dp->isoftlimit,
dp->ihardlimit,
dp->bsize);
lines = file_lines_ploadv(talloc_tos(), argl, NULL);
TALLOC_FREE(argl);
if (lines) {
char *line = lines[0];
DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
TALLOC_FREE(lines);
return 0;
}
DEBUG (0, ("set_quota_command failed!\n"));
return -1;
}
errno = ENOSYS;
return -1;
}
int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
{
int ret = -1;
int i;
bool ready = False;
char *mntpath = NULL;
char *bdev = NULL;
char *fs = NULL;
if (!path||!dp)
smb_panic("sys_get_quota: called with NULL pointer");
if (command_get_quota(path, qtype, id, dp)==0) {
return 0;
} else if (errno != ENOSYS) {
return -1;
}
if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
return ret;
}
errno = 0;
DEBUG(10,("sys_get_quota() uid(%u, %u), fs(%s)\n", (unsigned)getuid(), (unsigned)geteuid(), fs));
for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
if (strcmp(fs,sys_quota_backends[i].name)==0) {
ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
if (ret!=0) {
DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
} else {
DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
}
ready = True;
break;
}
}
if (!ready) {
/* use the default vfs quota functions */
ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
if (ret!=0) {
DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
"vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
} else {
DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
"vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
}
}
SAFE_FREE(mntpath);
SAFE_FREE(bdev);
SAFE_FREE(fs);
return ret;
}
int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
{
int ret = -1;
int i;
bool ready = False;
char *mntpath = NULL;
char *bdev = NULL;
char *fs = NULL;
/* find the block device file */
if (!path||!dp)
smb_panic("get_smb_quota: called with NULL pointer");
if (command_set_quota(path, qtype, id, dp)==0) {
return 0;
} else if (errno != ENOSYS) {
return -1;
}
if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
return ret;
}
errno = 0;
DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
if (strcmp(fs,sys_quota_backends[i].name)==0) {
ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
if (ret!=0) {
DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
} else {
DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
}
ready = True;
break;
}
}
if (!ready) {
/* use the default vfs quota functions */
ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
if (ret!=0) {
DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
"vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
} else {
DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
"vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
}
}
SAFE_FREE(mntpath);
SAFE_FREE(bdev);
SAFE_FREE(fs);
return ret;
}
#else /* HAVE_SYS_QUOTAS */
void dummy_sysquotas_c(void);
void dummy_sysquotas_c(void)
{
return;
}
#endif /* HAVE_SYS_QUOTAS */