r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[kai/samba.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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25
26 static pstring server;
27
28 /* numeric is set when the user wants numeric SIDs and ACEs rather
29    than going via LSA calls to resolve them */
30 static BOOL numeric;
31 static BOOL verbose;
32
33 enum todo_values {NOOP_QUOTA=0,FS_QUOTA,USER_QUOTA,LIST_QUOTA,SET_QUOTA};
34 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
35
36 static struct cli_state *cli_ipc;
37 static struct rpc_pipe_client *global_pipe_hnd;
38 static POLICY_HND pol;
39 static BOOL got_policy_hnd;
40
41 static struct cli_state *connect_one(const char *share);
42
43 /* Open cli connection and policy handle */
44
45 static BOOL cli_open_policy_hnd(void)
46 {
47         /* Initialise cli LSA connection */
48
49         if (!cli_ipc) {
50                 NTSTATUS ret;
51                 cli_ipc = connect_one("IPC$");
52                 global_pipe_hnd = cli_rpc_pipe_open_noauth(cli_ipc, PI_LSARPC, &ret);
53                 if (!global_pipe_hnd) {
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(rpccli_lsa_open_policy(global_pipe_hnd, 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         enum lsa_SidType *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(rpccli_lsa_lookup_sids(global_pipe_hnd, 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         enum lsa_SidType *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(rpccli_lsa_lookup_names(global_pipe_hnd, cli_ipc->mem_ctx, 
118                                                   &pol, 1, &str, NULL, 1, &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 (strnequal(set_str,"UQLIM:",6)) {
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 (strnequal(set_str,"FSQLIM:",7)) {
160                 p +=7;
161                 *qtype = SMB_USER_FS_QUOTA_TYPE;
162                 *cmd = QUOTA_SETLIM;
163                 todo = PARSE_LIM;
164         } else if (strnequal(set_str,"FSQFLAGS:",9)) {
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 (strnequal(p,"QUOTA_ENABLED",13)) {
195                                         enable = True;
196                                 } else if (strnequal(p,"DENY_DISK",9)) {
197                                         deny = True;
198                                 } else if (strnequal(p,"LOG_SOFTLIMIT",13)) {
199                                         pqt->qflags |= QUOTAS_LOG_THRESHOLD;
200                                 } else if (strnequal(p,"LOG_HARDLIMIT",13)) {
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("Quotas are not enabled on this share.\n");
242                 d_printf("Failed to open %s  %s.\n",
243                         FAKE_FILE_NAME_QUOTA_WIN32,cli_errstr(cli));
244                 return -1;
245         }
246
247         switch(qtype) {
248                 case SMB_USER_QUOTA_TYPE:
249                         if (!StringToSid(&qt.sid, username_str)) {
250                                 d_printf("StringToSid() failed for [%s]\n",username_str);
251                                 return -1;
252                         }
253                         
254                         switch(cmd) {
255                                 case QUOTA_GET:
256                                         if (!cli_get_user_quota(cli, quota_fnum, &qt)) {
257                                                 d_printf("%s cli_get_user_quota %s\n",
258                                                          cli_errstr(cli),username_str);
259                                                 return -1;
260                                         }
261                                         dump_ntquota(&qt,verbose,numeric,SidToString);
262                                         break;
263                                 case QUOTA_SETLIM:
264                                         pqt->sid = qt.sid;
265                                         if (!cli_set_user_quota(cli, quota_fnum, pqt)) {
266                                                 d_printf("%s cli_set_user_quota %s\n",
267                                                          cli_errstr(cli),username_str);
268                                                 return -1;
269                                         }
270                                         if (!cli_get_user_quota(cli, quota_fnum, &qt)) {
271                                                 d_printf("%s cli_get_user_quota %s\n",
272                                                          cli_errstr(cli),username_str);
273                                                 return -1;
274                                         }
275                                         dump_ntquota(&qt,verbose,numeric,SidToString);
276                                         break;
277                                 case QUOTA_LIST:
278                                         if (!cli_list_user_quota(cli, quota_fnum, &qtl)) {
279                                                 d_printf("%s cli_set_user_quota %s\n",
280                                                          cli_errstr(cli),username_str);
281                                                 return -1;
282                                         }
283                                         dump_ntquota_list(&qtl,verbose,numeric,SidToString);
284                                         free_ntquota_list(&qtl);                                        
285                                         break;
286                                 default:
287                                         d_printf("Unknown Error\n");
288                                         return -1;
289                         } 
290                         break;
291                 case SMB_USER_FS_QUOTA_TYPE:
292                         switch(cmd) {
293                                 case QUOTA_GET:
294                                         if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
295                                                 d_printf("%s cli_get_fs_quota_info\n",
296                                                          cli_errstr(cli));
297                                                 return -1;
298                                         }
299                                         dump_ntquota(&qt,True,numeric,NULL);
300                                         break;
301                                 case QUOTA_SETLIM:
302                                         if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
303                                                 d_printf("%s cli_get_fs_quota_info\n",
304                                                          cli_errstr(cli));
305                                                 return -1;
306                                         }
307                                         qt.softlim = pqt->softlim;
308                                         qt.hardlim = pqt->hardlim;
309                                         if (!cli_set_fs_quota_info(cli, quota_fnum, &qt)) {
310                                                 d_printf("%s cli_set_fs_quota_info\n",
311                                                          cli_errstr(cli));
312                                                 return -1;
313                                         }
314                                         if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
315                                                 d_printf("%s cli_get_fs_quota_info\n",
316                                                          cli_errstr(cli));
317                                                 return -1;
318                                         }
319                                         dump_ntquota(&qt,True,numeric,NULL);
320                                         break;
321                                 case QUOTA_SETFLAGS:
322                                         if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
323                                                 d_printf("%s cli_get_fs_quota_info\n",
324                                                          cli_errstr(cli));
325                                                 return -1;
326                                         }
327                                         qt.qflags = pqt->qflags;
328                                         if (!cli_set_fs_quota_info(cli, quota_fnum, &qt)) {
329                                                 d_printf("%s cli_set_fs_quota_info\n",
330                                                          cli_errstr(cli));
331                                                 return -1;
332                                         }
333                                         if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
334                                                 d_printf("%s cli_get_fs_quota_info\n",
335                                                          cli_errstr(cli));
336                                                 return -1;
337                                         }
338                                         dump_ntquota(&qt,True,numeric,NULL);
339                                         break;
340                                 default:
341                                         d_printf("Unknown Error\n");
342                                         return -1;
343                         }               
344                         break;
345                 default:
346                         d_printf("Unknown Error\n");
347                         return -1;
348         }
349
350         cli_close(cli, quota_fnum);
351
352         return 0;
353 }
354
355 /***************************************************** 
356 return a connection to a server
357 *******************************************************/
358 static struct cli_state *connect_one(const char *share)
359 {
360         struct cli_state *c;
361         struct in_addr ip;
362         NTSTATUS nt_status;
363         zero_ip(&ip);
364         
365         if (!cmdline_auth_info.got_pass) {
366                 char *pass = getpass("Password: ");
367                 if (pass) {
368                         pstrcpy(cmdline_auth_info.password, pass);
369                         cmdline_auth_info.got_pass = True;
370                 }
371         }
372
373         if (NT_STATUS_IS_OK(nt_status = cli_full_connection(&c, global_myname(), server, 
374                                                             &ip, 0,
375                                                             share, "?????",  
376                                                             cmdline_auth_info.username, lp_workgroup(),
377                                                             cmdline_auth_info.password, 0,
378                                                             cmdline_auth_info.signing_state, NULL))) {
379                 return c;
380         } else {
381                 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
382                 return NULL;
383         }
384 }
385
386 /****************************************************************************
387   main program
388 ****************************************************************************/
389  int main(int argc, const char *argv[])
390 {
391         char *share;
392         int opt;
393         int result;
394         int todo = 0;
395         pstring username_str = {0};
396         pstring path = {0};
397         pstring set_str = {0};
398         enum SMB_QUOTA_TYPE qtype = SMB_INVALID_QUOTA_TYPE;
399         int cmd = 0;
400         static BOOL test_args = False;
401         struct cli_state *cli;
402         BOOL fix_user = False;
403         SMB_NTQUOTA_STRUCT qt;
404         poptContext pc;
405         struct poptOption long_options[] = {
406                 POPT_AUTOHELP
407                 { "user", 'u', POPT_ARG_STRING, NULL, 'u', "Show quotas for user", "user" },
408                 { "list", 'L', POPT_ARG_NONE, NULL, 'L', "List user quotas" },
409                 { "fs", 'F', POPT_ARG_NONE, NULL, 'F', "Show filesystem quotas" },
410                 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls\n\
411 SETSTRING:\n\
412 UQLIM:<username>/<softlimit>/<hardlimit> for user quotas\n\
413 FSQLIM:<softlimit>/<hardlimit> for filesystem defaults\n\
414 FSQFLAGS:QUOTA_ENABLED/DENY_DISK/LOG_SOFTLIMIT/LOG_HARD_LIMIT", "SETSTRING" },
415                 { "numeric", 'n', POPT_ARG_NONE, &numeric, True, "Don't resolve sids or limits to names" },
416                 { "verbose", 'v', POPT_ARG_NONE, &verbose, True, "be verbose" },
417                 { "test-args", 't', POPT_ARG_NONE, &test_args, True, "Test arguments"},
418                 POPT_COMMON_SAMBA
419                 POPT_COMMON_CREDENTIALS
420                 { NULL }
421         };
422
423         load_case_tables();
424
425         ZERO_STRUCT(qt);
426
427         /* set default debug level to 1 regardless of what smb.conf sets */
428         setup_logging( "smbcquotas", True );
429         DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
430         dbf = x_stderr;
431         x_setbuf( x_stderr, NULL );
432
433         setlinebuf(stdout);
434
435         fault_setup(NULL);
436
437         lp_load(dyn_CONFIGFILE,True,False,False,True);
438         load_interfaces();
439
440         pc = poptGetContext("smbcquotas", argc, argv, long_options, 0);
441         
442         poptSetOtherOptionHelp(pc, "//server1/share1");
443
444         while ((opt = poptGetNextOpt(pc)) != -1) {
445                 switch (opt) {
446                 case 'L':
447                         if (todo != 0) {
448                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
449                                 exit(EXIT_PARSE_ERROR);
450                         }
451                         todo = LIST_QUOTA;
452                         break;
453
454                 case 'F':
455                         if (todo != 0) {
456                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
457                                 exit(EXIT_PARSE_ERROR);
458                         }
459                         todo = FS_QUOTA;
460                         break;
461                 
462                 case 'u':
463                         if (todo != 0) {
464                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
465                                 exit(EXIT_PARSE_ERROR);
466                         }
467                         pstrcpy(username_str,poptGetOptArg(pc));
468                         todo = USER_QUOTA;
469                         fix_user = True;
470                         break;
471                 
472                 case 'S':
473                         if (todo != 0) {
474                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
475                                 exit(EXIT_PARSE_ERROR);
476                         }
477                         pstrcpy(set_str,poptGetOptArg(pc));
478                         todo = SET_QUOTA;
479                         break;
480                 }
481         }
482
483         if (todo == 0)
484                 todo = USER_QUOTA;
485
486         if (!fix_user)
487                 pstrcpy(username_str,cmdline_auth_info.username);
488
489         /* Make connection to server */
490         if(!poptPeekArg(pc)) { 
491                 poptPrintUsage(pc, stderr, 0);
492                 exit(EXIT_PARSE_ERROR);
493         }
494         
495         pstrcpy(path, poptGetArg(pc));
496
497         all_string_sub(path,"/","\\",0);
498
499         pstrcpy(server,path+2);
500         share = strchr_m(server,'\\');
501         if (!share) {
502                 printf("Invalid argument: %s\n", share);
503                 exit(EXIT_PARSE_ERROR);
504         }
505
506         *share = 0;
507         share++;
508
509         if (todo == SET_QUOTA) {
510                 if (parse_quota_set(set_str, username_str, &qtype, &cmd, &qt)) {
511                         printf("Invalid argument: -S %s\n", set_str);
512                         exit(EXIT_PARSE_ERROR);
513                 }
514         }
515
516         if (!test_args) {
517                 cli = connect_one(share);
518                 if (!cli) {
519                         exit(EXIT_FAILED);
520                 }
521         } else {
522                 exit(EXIT_OK);
523         }
524
525
526         /* Perform requested action */
527
528         switch (todo) {
529                 case FS_QUOTA:
530                         result = do_quota(cli,SMB_USER_FS_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
531                         break;
532                 case LIST_QUOTA:
533                         result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_LIST, username_str, NULL);
534                         break;
535                 case USER_QUOTA:
536                         result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
537                         break;
538                 case SET_QUOTA:
539                         result = do_quota(cli, qtype, cmd, username_str, &qt);
540                         break;
541                 default: 
542                         
543                         result = EXIT_FAILED;
544                         break;
545         }
546
547         return result;
548 }
549