sysquota: we need to list nfs4 as a separate fs name for the sys_get_nfs_quota backend
[gd/samba-autobuild/.git] / source3 / lib / sysquotas.c
1 /* 
2    Unix SMB/CIFS implementation.
3    System QUOTA function wrappers
4    Copyright (C) Stefan (metze) Metzmacher      2003
5    
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.
10    
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.
15    
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/>.
18 */
19
20
21 #include "includes.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_QUOTA
25
26 #ifdef HAVE_SYS_QUOTAS
27
28 #if defined(HAVE_QUOTACTL_4A) 
29
30 /*#endif HAVE_QUOTACTL_4A */
31 #elif defined(HAVE_QUOTACTL_4B)
32
33 /*#endif HAVE_QUOTACTL_4B */
34 #elif defined(HAVE_QUOTACTL_3)
35
36 #error HAVE_QUOTACTL_3 not implemented
37
38 /* #endif  HAVE_QUOTACTL_3 */
39 #else /* NO_QUOTACTL_USED */
40
41 #endif /* NO_QUOTACTL_USED */
42
43 #ifdef HAVE_MNTENT
44 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
45 {
46         int ret = -1;
47         SMB_STRUCT_STAT S;
48         FILE *fp;
49         struct mntent *mnt;
50         SMB_DEV_T devno;
51
52         /* find the block device file */
53
54         if (!path||!mntpath||!bdev||!fs)
55                 smb_panic("sys_path_to_bdev: called with NULL pointer");
56
57         (*mntpath) = NULL;
58         (*bdev) = NULL;
59         (*fs) = NULL;
60         
61         if ( sys_stat(path, &S, false) == -1 )
62                 return (-1);
63
64         devno = S.st_ex_dev ;
65
66         fp = setmntent(MOUNTED,"r");
67         if (fp == NULL) {
68                 return -1;
69         }
70   
71         while ((mnt = getmntent(fp))) {
72                 if ( sys_stat(mnt->mnt_dir, &S, false) == -1 )
73                         continue ;
74
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;
86                         }
87
88                         break;
89                 }
90         }
91
92         endmntent(fp) ;
93
94         return ret;
95 }
96 /* #endif HAVE_MNTENT */
97 #elif defined(HAVE_DEVNM)
98
99 /* we have this on HPUX, ... */
100 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
101 {
102         int ret = -1;
103         char dev_disk[256];
104         SMB_STRUCT_STAT S;
105
106         if (!path||!mntpath||!bdev||!fs)
107                 smb_panic("sys_path_to_bdev: called with NULL pointer");
108
109         (*mntpath) = NULL;
110         (*bdev) = NULL;
111         (*fs) = NULL;
112         
113         /* find the block device file */
114
115         if ((ret=sys_stat(path, &S, false))!=0) {
116                 return ret;
117         }
118         
119         if ((ret=devnm(S_IFBLK, S.st_ex_dev, dev_disk, 256, 1))!=0) {
120                 return ret;     
121         }
122
123         /* we should get the mntpath right...
124          * but I don't know how
125          * --metze
126          */
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;
135         }       
136         
137         
138         return ret;     
139 }
140
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)
145 {
146         int ret = -1;
147
148         if (!path||!mntpath||!bdev||!fs)
149                 smb_panic("sys_path_to_bdev: called with NULL pointer");
150
151         (*mntpath) = NULL;
152         (*bdev) = NULL;
153         (*fs) = NULL;
154         
155         (*mntpath) = SMB_STRDUP(path);
156         if (*mntpath) {
157                 ret = 0;
158         } else {
159                 SAFE_FREE(*mntpath);
160                 ret = -1;
161         }
162
163         return ret;
164 }
165 #endif
166
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         {"nfs4", sys_get_nfs_quota,     sys_set_nfs_quota},
181 #endif /* HAVE_NFS_QUOTAS */
182         {NULL,  NULL,                   NULL}
183 };
184
185 static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
186 {
187         const char *get_quota_command;
188         char **lines = NULL;
189
190         get_quota_command = lp_get_quota_command(talloc_tos());
191         if (get_quota_command && *get_quota_command) {
192                 const char *p;
193                 char *p2;
194                 char *syscmd = NULL;
195                 int _id = -1;
196
197                 switch(qtype) {
198                         case SMB_USER_QUOTA_TYPE:
199                         case SMB_USER_FS_QUOTA_TYPE:
200                                 _id = id.uid;
201                                 break;
202                         case SMB_GROUP_QUOTA_TYPE:
203                         case SMB_GROUP_FS_QUOTA_TYPE:
204                                 _id = id.gid;
205                                 break;
206                         default:
207                                 DEBUG(0,("invalid quota type.\n"));
208                                 return -1;
209                 }
210
211                 if (asprintf(&syscmd, "%s \"%s\" %d %d",
212                         get_quota_command, path, qtype, _id) < 0) {
213                         return -1;
214                 }
215
216                 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
217
218                 lines = file_lines_pload(syscmd, NULL);
219                 SAFE_FREE(syscmd);
220
221                 if (lines) {
222                         char *line = lines[0];
223
224                         DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
225
226                         /* we need to deal with long long unsigned here, if supported */
227
228                         dp->qflags = (enum SMB_QUOTA_TYPE)strtoul(line, &p2, 10);
229                         p = p2;
230                         while (p && *p && isspace(*p)) {
231                                 p++;
232                         }
233
234                         if (p && *p) {
235                                 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
236                         } else {
237                                 goto invalid_param;
238                         }
239
240                         while (p && *p && isspace(*p)) {
241                                 p++;
242                         }
243
244                         if (p && *p) {
245                                 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
246                         } else {
247                                 goto invalid_param;
248                         }
249
250                         while (p && *p && isspace(*p)) {
251                                 p++;
252                         }
253
254                         if (p && *p) {
255                                 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
256                         } else {
257                                 goto invalid_param;
258                         }
259
260                         while (p && *p && isspace(*p)) {
261                                 p++;
262                         }
263
264                         if (p && *p) {
265                                 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
266                         } else {
267                                 goto invalid_param;
268                         }
269
270                         while (p && *p && isspace(*p)) {
271                                 p++;
272                         }
273
274                         if (p && *p) {
275                                 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
276                         } else {
277                                 goto invalid_param;
278                         }
279
280                         while (p && *p && isspace(*p)) {
281                                 p++;
282                         }
283
284                         if (p && *p) {
285                                 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
286                         } else {
287                                 goto invalid_param;     
288                         }
289
290                         while (p && *p && isspace(*p)) {
291                                 p++;
292                         }
293
294                         if (p && *p) {
295                                 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
296                         } else {
297                                 dp->bsize = 1024;
298                         }
299
300                         TALLOC_FREE(lines);
301                         lines = NULL;
302
303                         DEBUG (3, ("Parsed output of get_quota, ...\n"));
304
305                         DEBUGADD (5,( 
306                                 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
307                                 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n", 
308                                 dp->qflags,(long long unsigned)dp->curblocks,
309                                 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
310                                 (long long unsigned)dp->curinodes,
311                                 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
312                                 (long long unsigned)dp->bsize));
313                         return 0;
314                 }
315
316                 DEBUG (0, ("get_quota_command failed!\n"));
317                 return -1;
318         }
319
320         errno = ENOSYS;
321         return -1;
322
323 invalid_param:
324
325         TALLOC_FREE(lines);
326         DEBUG(0,("The output of get_quota_command is invalid!\n"));
327         return -1;
328 }
329
330 static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
331 {
332         const char *set_quota_command;
333
334         set_quota_command = lp_set_quota_command(talloc_tos());
335         if (set_quota_command && *set_quota_command) {
336                 char **lines = NULL;
337                 char *syscmd = NULL;
338                 int _id = -1;
339
340                 switch(qtype) {
341                         case SMB_USER_QUOTA_TYPE:
342                         case SMB_USER_FS_QUOTA_TYPE:
343                                 _id = id.uid;
344                                 break;
345                         case SMB_GROUP_QUOTA_TYPE:
346                         case SMB_GROUP_FS_QUOTA_TYPE:
347                                 _id = id.gid;
348                                 break;
349                         default:
350                                 return -1;
351                 }
352
353                 if (asprintf(&syscmd,
354                         "%s \"%s\" %d %d "
355                         "%u %llu %llu "
356                         "%llu %llu %llu ",
357                         set_quota_command, path, qtype, _id, dp->qflags,
358                         (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
359                         (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
360                         (long long unsigned)dp->bsize) < 0) {
361                         return -1;
362                 }
363
364                 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
365
366                 lines = file_lines_pload(syscmd, NULL);
367                 SAFE_FREE(syscmd);
368                 if (lines) {
369                         char *line = lines[0];
370
371                         DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
372
373                         TALLOC_FREE(lines);
374
375                         return 0;
376                 }
377                 DEBUG (0, ("set_quota_command failed!\n"));
378                 return -1;
379         }
380
381         errno = ENOSYS;
382         return -1;
383 }
384
385 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
386 {
387         int ret = -1;
388         int i;
389         bool ready = False;
390         char *mntpath = NULL;
391         char *bdev = NULL;
392         char *fs = NULL;
393
394         if (!path||!dp)
395                 smb_panic("sys_get_quota: called with NULL pointer");
396
397         if (command_get_quota(path, qtype, id, dp)==0) {        
398                 return 0;
399         } else if (errno != ENOSYS) {
400                 return -1;
401         }
402
403         if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
404                 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
405                 return ret;
406         }
407
408         errno = 0;
409         DEBUG(10,("sys_get_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
410
411         for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
412                 if (strcmp(fs,sys_quota_backends[i].name)==0) {
413                         ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
414                         if (ret!=0) {
415                                 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
416                                         fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
417                         } else {
418                                 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
419                                         fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
420                         }
421                         ready = True;
422                         break;  
423                 }               
424         }
425
426         if (!ready) {
427                 /* use the default vfs quota functions */
428                 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
429                 if (ret!=0) {
430                         DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
431                                 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
432                 } else {
433                         DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
434                                 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
435                 }
436         }
437
438         SAFE_FREE(mntpath);
439         SAFE_FREE(bdev);
440         SAFE_FREE(fs);
441
442         if ((ret!=0)&& (errno == EDQUOT)) {
443                 DEBUG(10,("sys_get_quota() warning over quota!\n"));
444                 return 0;
445         }
446
447         return ret;
448 }
449
450 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
451 {
452         int ret = -1;
453         int i;
454         bool ready = False;
455         char *mntpath = NULL;
456         char *bdev = NULL;
457         char *fs = NULL;
458
459         /* find the block device file */
460
461         if (!path||!dp)
462                 smb_panic("get_smb_quota: called with NULL pointer");
463
464         if (command_set_quota(path, qtype, id, dp)==0) {        
465                 return 0;
466         } else if (errno != ENOSYS) {
467                 return -1;
468         }
469
470         if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
471                 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
472                 return ret;
473         }
474
475         errno = 0;
476         DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid())); 
477
478         for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
479                 if (strcmp(fs,sys_quota_backends[i].name)==0) {
480                         ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
481                         if (ret!=0) {
482                                 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
483                                         fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
484                         } else {
485                                 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
486                                         fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
487                         }
488                         ready = True;
489                         break;
490                 }               
491         }
492
493         if (!ready) {
494                 /* use the default vfs quota functions */
495                 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
496                 if (ret!=0) {
497                         DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
498                                 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
499                 } else {
500                         DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
501                                 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
502                 }
503         }
504
505         SAFE_FREE(mntpath);
506         SAFE_FREE(bdev);
507         SAFE_FREE(fs);
508
509         if ((ret!=0)&& (errno == EDQUOT)) {
510                 DEBUG(10,("sys_set_quota() warning over quota!\n"));
511                 return 0;
512         }
513
514         return ret;             
515 }
516
517 #else /* HAVE_SYS_QUOTAS */
518  void dummy_sysquotas_c(void);
519
520  void dummy_sysquotas_c(void)
521 {
522         return;
523 }
524 #endif /* HAVE_SYS_QUOTAS */
525