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