Fix a segv in winbindd caused by trying to free an fstring. Make a copy of the machin...
[tprouty/samba.git] / source / 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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_QUOTA
26
27 #ifdef HAVE_SYS_QUOTAS
28
29 #if defined(HAVE_QUOTACTL_4A) 
30
31 /*#endif HAVE_QUOTACTL_4A */
32 #elif defined(HAVE_QUOTACTL_4B)
33
34 #error HAVE_QUOTACTL_4B not implemeted
35
36 /*#endif HAVE_QUOTACTL_4B */
37 #elif defined(HAVE_QUOTACTL_3)
38
39 #error HAVE_QUOTACTL_3 not implemented
40
41 /* #endif  HAVE_QUOTACTL_3 */
42 #else /* NO_QUOTACTL_USED */
43
44 #endif /* NO_QUOTACTL_USED */
45
46 #ifdef HAVE_MNTENT
47 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
48 {
49         int ret = -1;
50         SMB_STRUCT_STAT S;
51         FILE *fp;
52         struct mntent *mnt;
53         SMB_DEV_T devno;
54
55         /* find the block device file */
56
57         if (!path||!mntpath||!bdev||!fs)
58                 smb_panic("sys_path_to_bdev: called with NULL pointer");
59
60         (*mntpath) = NULL;
61         (*bdev) = NULL;
62         (*fs) = NULL;
63         
64         if ( sys_stat(path, &S) == -1 )
65                 return (-1);
66
67         devno = S.st_dev ;
68
69         fp = setmntent(MOUNTED,"r");
70         if (fp == NULL) {
71                 return -1;
72         }
73   
74         while ((mnt = getmntent(fp))) {
75                 if ( sys_stat(mnt->mnt_dir,&S) == -1 )
76                         continue ;
77
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;
89                         }
90
91                         break;
92                 }
93         }
94
95         endmntent(fp) ;
96
97         return ret;
98 }
99 /* #endif HAVE_MNTENT */
100 #elif defined(HAVE_DEVNM)
101
102 /* we have this on HPUX, ... */
103 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
104 {
105         int ret = -1;
106         char dev_disk[256];
107         SMB_STRUCT_STAT S;
108
109         if (!path||!mntpath||!bdev||!fs)
110                 smb_panic("sys_path_to_bdev: called with NULL pointer");
111
112         (*mntpath) = NULL;
113         (*bdev) = NULL;
114         (*fs) = NULL;
115         
116         /* find the block device file */
117
118         if ((ret=sys_stat(path, &S))!=0) {
119                 return ret;
120         }
121         
122         if ((ret=devnm(S_IFBLK, S.st_dev, dev_disk, 256, 1))!=0) {
123                 return ret;     
124         }
125
126         /* we should get the mntpath right...
127          * but I don't know how
128          * --metze
129          */
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;
138         }       
139         
140         
141         return ret;     
142 }
143
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)
148 {
149         int ret = -1;
150
151         if (!path||!mntpath||!bdev||!fs)
152                 smb_panic("sys_path_to_bdev: called with NULL pointer");
153
154         (*mntpath) = NULL;
155         (*bdev) = NULL;
156         (*fs) = NULL;
157         
158         (*mntpath) = SMB_STRDUP(path);
159         if (*mntpath) {
160                 ret = 0;
161         } else {
162                 SAFE_FREE(*mntpath);
163                 ret = -1;
164         }
165
166         return ret;
167 }
168 #endif
169
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}   
182 };
183
184 static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
185 {
186         const char *get_quota_command;
187         char **lines = NULL;
188         
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;
195
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;
208                 }
209
210                 slprintf(syscmd, sizeof(syscmd)-1, 
211                         "%s \"%s\" %d %d", 
212                         get_quota_command, path, qtype, _id);
213
214                 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
215
216                 lines = file_lines_pload(syscmd, NULL);
217                 if (lines) {
218                         char *line = lines[0];
219
220                         DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
221
222                         /* we need to deal with long long unsigned here, if supported */
223
224                         dp->qflags = (enum SMB_QUOTA_TYPE)strtoul(line, &p2, 10);
225                         p = p2;
226                         while (p && *p && isspace(*p)) {
227                                 p++;
228                         }
229
230                         if (p && *p) {
231                                 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
232                         } else {
233                                 goto invalid_param;
234                         }
235
236                         while (p && *p && isspace(*p)) {
237                                 p++;
238                         }
239
240                         if (p && *p) {
241                                 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
242                         } else {
243                                 goto invalid_param;
244                         }
245
246                         while (p && *p && isspace(*p)) {
247                                 p++;
248                         }
249
250                         if (p && *p) {
251                                 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
252                         } else {
253                                 goto invalid_param;
254                         }
255
256                         while (p && *p && isspace(*p)) {
257                                 p++;
258                         }
259
260                         if (p && *p) {
261                                 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
262                         } else {
263                                 goto invalid_param;
264                         }
265
266                         while (p && *p && isspace(*p)) {
267                                 p++;
268                         }
269
270                         if (p && *p) {
271                                 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
272                         } else {
273                                 goto invalid_param;
274                         }
275
276                         while (p && *p && isspace(*p)) {
277                                 p++;
278                         }
279
280                         if (p && *p) {
281                                 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
282                         } else {
283                                 goto invalid_param;     
284                         }
285
286                         while (p && *p && isspace(*p)) {
287                                 p++;
288                         }
289
290                         if (p && *p) {
291                                 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
292                         } else {
293                                 dp->bsize = 1024;
294                         }
295
296                         file_lines_free(lines);
297                         lines = NULL;
298
299                         DEBUG (3, ("Parsed output of get_quota, ...\n"));
300
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;
321                 }
322
323                 DEBUG (0, ("get_quota_command failed!\n"));
324                 return -1;
325         }
326
327         errno = ENOSYS;
328         return -1;
329         
330 invalid_param:
331
332         file_lines_free(lines);
333         DEBUG(0,("The output of get_quota_command is invalid!\n"));
334         return -1;
335 }
336
337 static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
338 {
339         const char *set_quota_command;
340         
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;
346
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;
358                 }
359
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 */
379
380
381
382                 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
383
384                 lines = file_lines_pload(syscmd, NULL);
385                 if (lines) {
386                         char *line = lines[0];
387
388                         DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
389
390                         file_lines_free(lines);
391                         
392                         return 0;
393                 }
394                 DEBUG (0, ("set_quota_command failed!\n"));
395                 return -1;
396         }
397
398         errno = ENOSYS;
399         return -1;
400 }
401
402 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
403 {
404         int ret = -1;
405         int i;
406         BOOL ready = False;
407         char *mntpath = NULL;
408         char *bdev = NULL;
409         char *fs = NULL;
410
411         if (!path||!dp)
412                 smb_panic("sys_get_quota: called with NULL pointer");
413
414         if (command_get_quota(path, qtype, id, dp)==0) {        
415                 return 0;
416         } else if (errno != ENOSYS) {
417                 return -1;
418         }
419
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;
423         }
424
425         errno = 0;
426         DEBUG(10,("sys_get_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
427
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)));
437                         }
438                         ready = True;
439                         break;  
440                 }               
441         }
442
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)));
452                 }
453         }
454
455         SAFE_FREE(mntpath);
456         SAFE_FREE(bdev);
457         SAFE_FREE(fs);
458
459         if ((ret!=0)&& (errno == EDQUOT)) {
460                 DEBUG(10,("sys_get_quota() warning over quota!\n"));
461                 return 0;
462         }
463
464         return ret;
465 }
466
467 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
468 {
469         int ret = -1;
470         int i;
471         BOOL ready = False;
472         char *mntpath = NULL;
473         char *bdev = NULL;
474         char *fs = NULL;
475
476         /* find the block device file */
477
478         if (!path||!dp)
479                 smb_panic("get_smb_quota: called with NULL pointer");
480
481         if (command_set_quota(path, qtype, id, dp)==0) {        
482                 return 0;
483         } else if (errno != ENOSYS) {
484                 return -1;
485         }
486
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;
490         }
491
492         errno = 0;
493         DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid())); 
494
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)));
504                         }
505                         ready = True;
506                         break;
507                 }               
508         }
509
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)));
519                 }
520         }
521
522         SAFE_FREE(mntpath);
523         SAFE_FREE(bdev);
524         SAFE_FREE(fs);
525
526         if ((ret!=0)&& (errno == EDQUOT)) {
527                 DEBUG(10,("sys_set_quota() warning over quota!\n"));
528                 return 0;
529         }
530
531         return ret;             
532 }
533
534 #else /* HAVE_SYS_QUOTAS */
535  void dummy_sysquotas_c(void);
536
537  void dummy_sysquotas_c(void)
538 {
539         return;
540 }
541 #endif /* HAVE_SYS_QUOTAS */
542