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