Patch from metze and me that adds dummy smb_register_*() functions
[obnox/samba/samba-obnox.git] / source3 / utils / smbcquotas.c
1 /* 
2    Unix SMB/CIFS implementation.
3    QUOTA get/set utility
4    
5    Copyright (C) Andrew Tridgell                2000
6    Copyright (C) Tim Potter                     2000
7    Copyright (C) Jeremy Allison                 2000
8    Copyright (C) Stefan (metze) Metzmacher      2003
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 #include "module_dummy.h"
28
29 static pstring server;
30
31 /* numeric is set when the user wants numeric SIDs and ACEs rather
32    than going via LSA calls to resolve them */
33 static BOOL numeric;
34 static BOOL verbose;
35
36 enum todo_values {NOOP_QUOTA=0,FS_QUOTA,USER_QUOTA,LIST_QUOTA,SET_QUOTA};
37 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
38
39 static struct cli_state *cli_ipc = NULL;
40 static POLICY_HND pol;
41 static BOOL got_policy_hnd;
42
43 static struct cli_state *connect_one(const char *share);
44
45 /* Open cli connection and policy handle */
46
47 static BOOL cli_open_policy_hnd(void)
48 {
49         /* Initialise cli LSA connection */
50
51         if (!cli_ipc) {
52                 cli_ipc = connect_one("IPC$");
53                 if (!cli_nt_session_open (cli_ipc, PI_LSARPC)) {
54                                 return False;
55                 }
56         }
57         
58         /* Open policy handle */
59
60         if (!got_policy_hnd) {
61
62                 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
63                    but NT sends 0x2000000 so we might as well do it too. */
64
65                 if (!NT_STATUS_IS_OK(cli_lsa_open_policy(cli_ipc, cli_ipc->mem_ctx, True, 
66                                                          GENERIC_EXECUTE_ACCESS, &pol))) {
67                         return False;
68                 }
69
70                 got_policy_hnd = True;
71         }
72         
73         return True;
74 }
75
76 /* convert a SID to a string, either numeric or username/group */
77 static void SidToString(fstring str, DOM_SID *sid, BOOL _numeric)
78 {
79         char **domains = NULL;
80         char **names = NULL;
81         uint32 *types = NULL;
82
83         sid_to_string(str, sid);
84
85         if (_numeric) return;
86
87         /* Ask LSA to convert the sid to a name */
88
89         if (!cli_open_policy_hnd() ||
90             !NT_STATUS_IS_OK(cli_lsa_lookup_sids(cli_ipc, cli_ipc->mem_ctx,  
91                                                  &pol, 1, sid, &domains, 
92                                                  &names, &types)) ||
93             !domains || !domains[0] || !names || !names[0]) {
94                 return;
95         }
96
97         /* Converted OK */
98
99         slprintf(str, sizeof(fstring) - 1, "%s%s%s",
100                  domains[0], lp_winbind_separator(),
101                  names[0]);
102         
103 }
104
105 /* convert a string to a SID, either numeric or username/group */
106 static BOOL StringToSid(DOM_SID *sid, const char *str)
107 {
108         uint32 *types = NULL;
109         DOM_SID *sids = NULL;
110         BOOL result = True;
111
112         if (strncmp(str, "S-", 2) == 0) {
113                 return string_to_sid(sid, str);
114         }
115
116         if (!cli_open_policy_hnd() ||
117             !NT_STATUS_IS_OK(cli_lsa_lookup_names(cli_ipc, cli_ipc->mem_ctx, 
118                                                   &pol, 1, &str, &sids, 
119                                                   &types))) {
120                 result = False;
121                 goto done;
122         }
123
124         sid_copy(sid, &sids[0]);
125  done:
126
127         return result;
128 }
129
130 #define QUOTA_GET 1
131 #define QUOTA_SETLIM 2
132 #define QUOTA_SETFLAGS 3
133 #define QUOTA_LIST 4
134
135 enum {PARSE_FLAGS,PARSE_LIM};
136
137 static int parse_quota_set(pstring set_str, pstring username_str, enum SMB_QUOTA_TYPE *qtype, int *cmd, SMB_NTQUOTA_STRUCT *pqt)
138 {
139         char *p = set_str,*p2;
140         int todo;
141         BOOL stop = False;
142         BOOL enable = False;
143         BOOL deny = False;
144         
145         if (strncasecmp(set_str,"UQLIM:",6)==0) {
146                 p += 6;
147                 *qtype = SMB_USER_QUOTA_TYPE;
148                 *cmd = QUOTA_SETLIM;
149                 todo = PARSE_LIM;
150                 if ((p2=strstr(p,":"))==NULL) {
151                         return -1;
152                 }
153                 
154                 *p2 = '\0';
155                 p2++;
156                 
157                 fstrcpy(username_str,p);
158                 p = p2;
159         } else if (strncasecmp(set_str,"FSQLIM:",7)==0) {
160                 p +=7;
161                 *qtype = SMB_USER_FS_QUOTA_TYPE;
162                 *cmd = QUOTA_SETLIM;
163                 todo = PARSE_LIM;
164         } else if (strncasecmp(set_str,"FSQFLAGS:",9)==0) {
165                 p +=9;
166                 todo = PARSE_FLAGS;
167                 *qtype = SMB_USER_FS_QUOTA_TYPE;
168                 *cmd = QUOTA_SETFLAGS;
169         } else {
170                 return -1;
171         }
172
173         switch (todo) {
174                 case PARSE_LIM:
175 #if defined(HAVE_LONGLONG)
176                         if (sscanf(p,"%llu/%llu",&pqt->softlim,&pqt->hardlim)!=2) {
177 #else
178                         if (sscanf(p,"%lu/%lu",&pqt->softlim,&pqt->hardlim)!=2) {
179 #endif
180                                 return -1;
181                         }
182                         
183                         break;
184                 case PARSE_FLAGS:
185                         while (!stop) {
186
187                                 if ((p2=strstr(p,"/"))==NULL) {
188                                         stop = True;
189                                 } else {
190                                         *p2 = '\0';
191                                         p2++;
192                                 }
193
194                                 if (strncasecmp(p,"QUOTA_ENABLED",13)==0) {
195                                         enable = True;
196                                 } else if (strncasecmp(p,"DENY_DISK",9)==0) {
197                                         deny = True;
198                                 } else if (strncasecmp(p,"LOG_SOFTLIMIT",13)==0) {
199                                         pqt->qflags |= QUOTAS_LOG_THRESHOLD;
200                                 } else if (strncasecmp(p,"LOG_HARDLIMIT",13)==0) {
201                                         pqt->qflags |= QUOTAS_LOG_LIMIT;
202                                 } else {
203                                         return -1;
204                                 }
205
206                                 p=p2;
207                         }
208
209                         if (deny) {
210                                 pqt->qflags |= QUOTAS_DENY_DISK;
211                         } else if (enable) {
212                                 pqt->qflags |= QUOTAS_ENABLED;
213                         }
214                         
215                         break;  
216         }
217
218         return 0;
219 }
220
221 static int do_quota(struct cli_state *cli, enum SMB_QUOTA_TYPE qtype, uint16 cmd, pstring username_str, SMB_NTQUOTA_STRUCT *pqt)
222 {
223         uint32 fs_attrs = 0;
224         int quota_fnum = 0;
225         SMB_NTQUOTA_LIST *qtl = NULL;
226         SMB_NTQUOTA_STRUCT qt;
227         ZERO_STRUCT(qt);
228
229         if (!cli_get_fs_attr_info(cli, &fs_attrs)) {
230                 d_printf("Failed to get the filesystem attributes %s.\n",
231                         cli_errstr(cli));
232                 return -1;
233         }
234
235         if (!(fs_attrs & FILE_VOLUME_QUOTAS)) {
236                 d_printf("Quotas are not supported by the server.\n");
237                 return 0;       
238         }
239
240         if (!cli_get_quota_handle(cli, &quota_fnum)) {
241                 d_printf("Failed to open \\%s  %s.\n",
242                         FAKE_FILE_NAME_QUOTA,cli_errstr(cli));
243                 return -1;
244         }
245
246         switch(qtype) {
247                 case SMB_USER_QUOTA_TYPE:
248                         if (!StringToSid(&qt.sid, username_str)) {
249                                 d_printf("StringToSid() failed for [%s]\n",username_str);
250                                 return -1;
251                         }
252                         
253                         switch(cmd) {
254                                 case QUOTA_GET:
255                                         if (!cli_get_user_quota(cli, quota_fnum, &qt)) {
256                                                 d_printf("%s cli_get_user_quota %s\n",
257                                                          cli_errstr(cli),username_str);
258                                                 return -1;
259                                         }
260                                         dump_ntquota(&qt,verbose,numeric,SidToString);
261                                         break;
262                                 case QUOTA_SETLIM:
263                                         pqt->sid = qt.sid;
264                                         if (!cli_set_user_quota(cli, quota_fnum, pqt)) {
265                                                 d_printf("%s cli_set_user_quota %s\n",
266                                                          cli_errstr(cli),username_str);
267                                                 return -1;
268                                         }
269                                         if (!cli_get_user_quota(cli, quota_fnum, &qt)) {
270                                                 d_printf("%s cli_get_user_quota %s\n",
271                                                          cli_errstr(cli),username_str);
272                                                 return -1;
273                                         }
274                                         dump_ntquota(&qt,verbose,numeric,SidToString);
275                                         break;
276                                 case QUOTA_LIST:
277                                         if (!cli_list_user_quota(cli, quota_fnum, &qtl)) {
278                                                 d_printf("%s cli_set_user_quota %s\n",
279                                                          cli_errstr(cli),username_str);
280                                                 return -1;
281                                         }
282                                         dump_ntquota_list(&qtl,verbose,numeric,SidToString);
283                                         free_ntquota_list(&qtl);                                        
284                                         break;
285                                 default:
286                                         d_printf("Unknown Error\n");
287                                         return -1;
288                         } 
289                         break;
290                 case SMB_USER_FS_QUOTA_TYPE:
291                         switch(cmd) {
292                                 case QUOTA_GET:
293                                         if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
294                                                 d_printf("%s cli_get_fs_quota_info\n",
295                                                          cli_errstr(cli));
296                                                 return -1;
297                                         }
298                                         dump_ntquota(&qt,True,numeric,NULL);
299                                         break;
300                                 case QUOTA_SETLIM:
301                                         if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
302                                                 d_printf("%s cli_get_fs_quota_info\n",
303                                                          cli_errstr(cli));
304                                                 return -1;
305                                         }
306                                         qt.softlim = pqt->softlim;
307                                         qt.hardlim = pqt->hardlim;
308                                         if (!cli_set_fs_quota_info(cli, quota_fnum, &qt)) {
309                                                 d_printf("%s cli_set_fs_quota_info\n",
310                                                          cli_errstr(cli));
311                                                 return -1;
312                                         }
313                                         if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
314                                                 d_printf("%s cli_get_fs_quota_info\n",
315                                                          cli_errstr(cli));
316                                                 return -1;
317                                         }
318                                         dump_ntquota(&qt,True,numeric,NULL);
319                                         break;
320                                 case QUOTA_SETFLAGS:
321                                         if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
322                                                 d_printf("%s cli_get_fs_quota_info\n",
323                                                          cli_errstr(cli));
324                                                 return -1;
325                                         }
326                                         qt.qflags = pqt->qflags;
327                                         if (!cli_set_fs_quota_info(cli, quota_fnum, &qt)) {
328                                                 d_printf("%s cli_set_fs_quota_info\n",
329                                                          cli_errstr(cli));
330                                                 return -1;
331                                         }
332                                         if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
333                                                 d_printf("%s cli_get_fs_quota_info\n",
334                                                          cli_errstr(cli));
335                                                 return -1;
336                                         }
337                                         dump_ntquota(&qt,True,numeric,NULL);
338                                         break;
339                                 default:
340                                         d_printf("Unknown Error\n");
341                                         return -1;
342                         }               
343                         break;
344                 default:
345                         d_printf("Unknown Error\n");
346                         return -1;
347         }
348
349         cli_close(cli, quota_fnum);
350
351         return 0;
352 }
353
354 /***************************************************** 
355 return a connection to a server
356 *******************************************************/
357 static struct cli_state *connect_one(const char *share)
358 {
359         struct cli_state *c;
360         struct in_addr ip;
361         NTSTATUS nt_status;
362         zero_ip(&ip);
363         
364         if (!cmdline_auth_info.got_pass) {
365                 char *pass = getpass("Password: ");
366                 if (pass) {
367                         pstrcpy(cmdline_auth_info.password, pass);
368                         cmdline_auth_info.got_pass = True;
369                 }
370         }
371
372         if (NT_STATUS_IS_OK(nt_status = cli_full_connection(&c, global_myname(), server, 
373                                                             &ip, 0,
374                                                             share, "?????",  
375                                                             cmdline_auth_info.username, lp_workgroup(),
376                                                             cmdline_auth_info.password, 0, NULL))) {
377                 return c;
378         } else {
379                 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
380                 return NULL;
381         }
382 }
383
384 /****************************************************************************
385   main program
386 ****************************************************************************/
387  int main(int argc, const char *argv[])
388 {
389         char *share;
390         int opt;
391         int result;
392         int todo = 0;
393         pstring username_str = {0};
394         pstring path = {0};
395         pstring set_str = {0};
396         enum SMB_QUOTA_TYPE qtype;
397         int cmd = 0;
398         static BOOL test_args = False;
399         struct cli_state *cli;
400         BOOL fix_user = False;
401         SMB_NTQUOTA_STRUCT qt;
402         poptContext pc;
403         struct poptOption long_options[] = {
404                 POPT_AUTOHELP
405                 { "user", 'u', POPT_ARG_STRING, NULL, 'u', "Show quotas for user", "user" },
406                 { "list", 'L', POPT_ARG_NONE, NULL, 'L', "List user quotas" },
407                 { "fs", 'F', POPT_ARG_NONE, NULL, 'F', "Show filesystem quotas" },
408                 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls\n\
409 SETSTRING:\n\
410 UQLIM:<username>/<softlimit>/<hardlimit> for user quotas\n\
411 FSQLIM:<softlimit>/<hardlimit> for filesystem defaults\n\
412 FSQFLAGS:QUOTA_ENABLED/DENY_DISK/LOG_SOFTLIMIT/LOG_HARD_LIMIT", "SETSTRING" },
413                 { "numeric", 'n', POPT_ARG_NONE, &numeric, True, "Don't resolve sids or limits to names" },
414                 { "verbose", 'v', POPT_ARG_NONE, &verbose, True, "be verbose" },
415                 { "test-args", 't', POPT_ARG_NONE, &test_args, True, "Test arguments"},
416                 POPT_COMMON_SAMBA
417                 POPT_COMMON_CREDENTIALS
418                 { NULL }
419         };
420
421         ZERO_STRUCT(qt);
422
423         setlinebuf(stdout);
424
425         dbf = x_stderr;
426
427         fault_setup(NULL);
428
429         setup_logging(argv[0],True);
430
431
432         lp_load(dyn_CONFIGFILE,True,False,False);
433         load_interfaces();
434
435         pc = poptGetContext("smbcquotas", argc, argv, long_options, 0);
436         
437         poptSetOtherOptionHelp(pc, "//server1/share1");
438
439         while ((opt = poptGetNextOpt(pc)) != -1) {
440                 switch (opt) {
441                 case 'L':
442                         if (todo != 0) {
443                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
444                                 exit(EXIT_PARSE_ERROR);
445                         }
446                         todo = LIST_QUOTA;
447                         break;
448
449                 case 'F':
450                         if (todo != 0) {
451                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
452                                 exit(EXIT_PARSE_ERROR);
453                         }
454                         todo = FS_QUOTA;
455                         break;
456                 
457                 case 'u':
458                         if (todo != 0) {
459                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
460                                 exit(EXIT_PARSE_ERROR);
461                         }
462                         pstrcpy(username_str,poptGetOptArg(pc));
463                         todo = USER_QUOTA;
464                         fix_user = True;
465                         break;
466                 
467                 case 'S':
468                         if (todo != 0) {
469                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
470                                 exit(EXIT_PARSE_ERROR);
471                         }
472                         pstrcpy(set_str,poptGetOptArg(pc));
473                         todo = SET_QUOTA;
474                         break;
475                 }
476         }
477
478         if (todo == 0)
479                 todo = USER_QUOTA;
480
481         if (!fix_user)
482                 pstrcpy(username_str,cmdline_auth_info.username);
483
484         /* Make connection to server */
485         if(!poptPeekArg(pc)) { 
486                 poptPrintUsage(pc, stderr, 0);
487                 exit(EXIT_PARSE_ERROR);
488         }
489         
490         pstrcpy(path, poptGetArg(pc));
491
492         all_string_sub(path,"/","\\",0);
493
494         pstrcpy(server,path+2);
495         share = strchr_m(server,'\\');
496         if (!share) {
497                 share = strchr_m(server,'/');
498                 if (!share) {
499                         printf("Invalid argument: %s\n", share);
500                         exit(EXIT_PARSE_ERROR);
501                 }
502         }
503
504         *share = 0;
505         share++;
506
507         if (todo == SET_QUOTA) {
508                 if (parse_quota_set(set_str, username_str, &qtype, &cmd, &qt)) {
509                         printf("Invalid argument: -S %s\n", set_str);
510                         exit(EXIT_PARSE_ERROR);
511                 }
512         }
513
514         if (!test_args) {
515                 cli = connect_one(share);
516                 if (!cli) {
517                         exit(EXIT_FAILED);
518                 }
519         } else {
520                 exit(EXIT_OK);
521         }
522
523
524         /* Perform requested action */
525
526         switch (todo) {
527                 case FS_QUOTA:
528                         result = do_quota(cli,SMB_USER_FS_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
529                         break;
530                 case LIST_QUOTA:
531                         result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_LIST, username_str, NULL);
532                         break;
533                 case USER_QUOTA:
534                         result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
535                         break;
536                 case SET_QUOTA:
537                         result = do_quota(cli, qtype, cmd, username_str, &qt);
538                         break;
539                 default: 
540                         
541                         result = EXIT_FAILED;
542                         break;
543         }
544
545         return result;
546 }
547