s3: Convert cli_set_fs_quota_info to cli_trans
[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 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                                         status = cli_get_fs_quota_info(
325                                                 cli, quota_fnum, &qt);
326                                         if (!NT_STATUS_IS_OK(status)) {
327                                                 d_printf("%s cli_get_fs_quota_info\n",
328                                                          nt_errstr(status));
329                                                 return -1;
330                                         }
331                                         dump_ntquota(&qt,True,numeric,NULL);
332                                         break;
333                                 case QUOTA_SETLIM:
334                                         status = cli_get_fs_quota_info(
335                                                 cli, quota_fnum, &qt);
336                                         if (!NT_STATUS_IS_OK(status)) {
337                                                 d_printf("%s cli_get_fs_quota_info\n",
338                                                          nt_errstr(status));
339                                                 return -1;
340                                         }
341                                         qt.softlim = pqt->softlim;
342                                         qt.hardlim = pqt->hardlim;
343                                         status = cli_set_fs_quota_info(
344                                                 cli, quota_fnum, &qt);
345                                         if (!NT_STATUS_IS_OK(status)) {
346                                                 d_printf("%s cli_set_fs_quota_info\n",
347                                                          nt_errstr(status));
348                                                 return -1;
349                                         }
350                                         status = cli_get_fs_quota_info(
351                                                 cli, quota_fnum, &qt);
352                                         if (!NT_STATUS_IS_OK(status)) {
353                                                 d_printf("%s cli_get_fs_quota_info\n",
354                                                          nt_errstr(status));
355                                                 return -1;
356                                         }
357                                         dump_ntquota(&qt,True,numeric,NULL);
358                                         break;
359                                 case QUOTA_SETFLAGS:
360                                         status = cli_get_fs_quota_info(
361                                                 cli, quota_fnum, &qt);
362                                         if (!NT_STATUS_IS_OK(status)) {
363                                                 d_printf("%s cli_get_fs_quota_info\n",
364                                                          nt_errstr(status));
365                                                 return -1;
366                                         }
367                                         qt.qflags = pqt->qflags;
368                                         status = cli_set_fs_quota_info(
369                                                 cli, quota_fnum, &qt);
370                                         if (!NT_STATUS_IS_OK(status)) {
371                                                 d_printf("%s cli_set_fs_quota_info\n",
372                                                          nt_errstr(status));
373                                                 return -1;
374                                         }
375                                         status = cli_get_fs_quota_info(
376                                                 cli, quota_fnum, &qt);
377                                         if (!NT_STATUS_IS_OK(status)) {
378                                                 d_printf("%s cli_get_fs_quota_info\n",
379                                                          nt_errstr(status));
380                                                 return -1;
381                                         }
382                                         dump_ntquota(&qt,True,numeric,NULL);
383                                         break;
384                                 default:
385                                         d_printf("Unknown Error\n");
386                                         return -1;
387                         }
388                         break;
389                 default:
390                         d_printf("Unknown Error\n");
391                         return -1;
392         }
393
394         cli_close(cli, quota_fnum);
395
396         return 0;
397 }
398
399 /*****************************************************
400  Return a connection to a server.
401 *******************************************************/
402
403 static struct cli_state *connect_one(const char *share)
404 {
405         struct cli_state *c;
406         struct sockaddr_storage ss;
407         NTSTATUS nt_status;
408         uint32_t flags = 0;
409
410         zero_sockaddr(&ss);
411
412         if (get_cmdline_auth_info_use_machine_account(smbcquotas_auth_info) &&
413             !set_cmdline_auth_info_machine_account_creds(smbcquotas_auth_info)) {
414                 return NULL;
415         }
416
417         if (get_cmdline_auth_info_use_kerberos(smbcquotas_auth_info)) {
418                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
419                          CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
420
421         }
422
423         set_cmdline_auth_info_getpass(smbcquotas_auth_info);
424
425         nt_status = cli_full_connection(&c, global_myname(), server, 
426                                             &ss, 0,
427                                             share, "?????",
428                                             get_cmdline_auth_info_username(smbcquotas_auth_info),
429                                             lp_workgroup(),
430                                             get_cmdline_auth_info_password(smbcquotas_auth_info),
431                                             flags,
432                                             get_cmdline_auth_info_signing_state(smbcquotas_auth_info));
433         if (!NT_STATUS_IS_OK(nt_status)) {
434                 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
435                 return NULL;
436         }
437
438         if (get_cmdline_auth_info_smb_encrypt(smbcquotas_auth_info)) {
439                 nt_status = cli_cm_force_encryption(c,
440                                         get_cmdline_auth_info_username(smbcquotas_auth_info),
441                                         get_cmdline_auth_info_password(smbcquotas_auth_info),
442                                         lp_workgroup(),
443                                         share);
444                 if (!NT_STATUS_IS_OK(nt_status)) {
445                         cli_shutdown(c);
446                         return NULL;
447                 }
448         }
449
450         return c;
451 }
452
453 /****************************************************************************
454   main program
455 ****************************************************************************/
456  int main(int argc, const char *argv[])
457 {
458         char *share;
459         int opt;
460         int result;
461         int todo = 0;
462         char *username_str = NULL;
463         char *path = NULL;
464         char *set_str = NULL;
465         enum SMB_QUOTA_TYPE qtype = SMB_INVALID_QUOTA_TYPE;
466         int cmd = 0;
467         static bool test_args = False;
468         struct cli_state *cli;
469         bool fix_user = False;
470         SMB_NTQUOTA_STRUCT qt;
471         TALLOC_CTX *frame = talloc_stackframe();
472         poptContext pc;
473         struct poptOption long_options[] = {
474                 POPT_AUTOHELP
475                 { "user", 'u', POPT_ARG_STRING, NULL, 'u', "Show quotas for user", "user" },
476                 { "list", 'L', POPT_ARG_NONE, NULL, 'L', "List user quotas" },
477                 { "fs", 'F', POPT_ARG_NONE, NULL, 'F', "Show filesystem quotas" },
478                 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls\n\
479 SETSTRING:\n\
480 UQLIM:<username>/<softlimit>/<hardlimit> for user quotas\n\
481 FSQLIM:<softlimit>/<hardlimit> for filesystem defaults\n\
482 FSQFLAGS:QUOTA_ENABLED/DENY_DISK/LOG_SOFTLIMIT/LOG_HARD_LIMIT", "SETSTRING" },
483                 { "numeric", 'n', POPT_ARG_NONE, NULL, 'n', "Don't resolve sids or limits to names" },
484                 { "verbose", 'v', POPT_ARG_NONE, NULL, 'v', "be verbose" },
485                 { "test-args", 't', POPT_ARG_NONE, NULL, 'r', "Test arguments"},
486                 POPT_COMMON_SAMBA
487                 POPT_COMMON_CREDENTIALS
488                 { NULL }
489         };
490
491         load_case_tables();
492
493         ZERO_STRUCT(qt);
494
495         /* set default debug level to 1 regardless of what smb.conf sets */
496         setup_logging( "smbcquotas", DEBUG_STDERR);
497         lp_set_cmdline("log level", "1");
498
499         setlinebuf(stdout);
500
501         fault_setup(NULL);
502
503         lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
504         load_interfaces();
505
506         smbcquotas_auth_info = user_auth_info_init(frame);
507         if (smbcquotas_auth_info == NULL) {
508                 exit(1);
509         }
510         popt_common_set_auth_info(smbcquotas_auth_info);
511
512         pc = poptGetContext("smbcquotas", argc, argv, long_options, 0);
513
514         poptSetOtherOptionHelp(pc, "//server1/share1");
515
516         while ((opt = poptGetNextOpt(pc)) != -1) {
517                 switch (opt) {
518                 case 'n':
519                         numeric = true;
520                         break;
521                 case 'v':
522                         verbose = true;
523                         break;
524                 case 't':
525                         test_args = true;
526                         break;
527                 case 'L':
528                         if (todo != 0) {
529                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
530                                 exit(EXIT_PARSE_ERROR);
531                         }
532                         todo = LIST_QUOTA;
533                         break;
534
535                 case 'F':
536                         if (todo != 0) {
537                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
538                                 exit(EXIT_PARSE_ERROR);
539                         }
540                         todo = FS_QUOTA;
541                         break;
542
543                 case 'u':
544                         if (todo != 0) {
545                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
546                                 exit(EXIT_PARSE_ERROR);
547                         }
548                         username_str = talloc_strdup(frame, poptGetOptArg(pc));
549                         if (!username_str) {
550                                 exit(EXIT_PARSE_ERROR);
551                         }
552                         todo = USER_QUOTA;
553                         fix_user = True;
554                         break;
555
556                 case 'S':
557                         if (todo != 0) {
558                                 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
559                                 exit(EXIT_PARSE_ERROR);
560                         }
561                         set_str = talloc_strdup(frame, poptGetOptArg(pc));
562                         if (!set_str) {
563                                 exit(EXIT_PARSE_ERROR);
564                         }
565                         todo = SET_QUOTA;
566                         break;
567                 }
568         }
569
570         if (todo == 0)
571                 todo = USER_QUOTA;
572
573         if (!fix_user) {
574                 username_str = talloc_strdup(
575                         frame, get_cmdline_auth_info_username(smbcquotas_auth_info));
576                 if (!username_str) {
577                         exit(EXIT_PARSE_ERROR);
578                 }
579         }
580
581         /* Make connection to server */
582         if(!poptPeekArg(pc)) {
583                 poptPrintUsage(pc, stderr, 0);
584                 exit(EXIT_PARSE_ERROR);
585         }
586
587         path = talloc_strdup(frame, poptGetArg(pc));
588         if (!path) {
589                 printf("Out of memory\n");
590                 exit(EXIT_PARSE_ERROR);
591         }
592
593         string_replace(path, '/', '\\');
594
595         server = SMB_STRDUP(path+2);
596         if (!server) {
597                 printf("Out of memory\n");
598                 exit(EXIT_PARSE_ERROR);
599         }
600         share = strchr_m(server,'\\');
601         if (!share) {
602                 printf("Invalid argument: %s\n", share);
603                 exit(EXIT_PARSE_ERROR);
604         }
605
606         *share = 0;
607         share++;
608
609         if (todo == SET_QUOTA) {
610                 if (parse_quota_set(talloc_tos(), set_str, &username_str, &qtype, &cmd, &qt)) {
611                         printf("Invalid argument: -S %s\n", set_str);
612                         exit(EXIT_PARSE_ERROR);
613                 }
614         }
615
616         if (!test_args) {
617                 cli = connect_one(share);
618                 if (!cli) {
619                         exit(EXIT_FAILED);
620                 }
621         } else {
622                 exit(EXIT_OK);
623         }
624
625
626         /* Perform requested action */
627
628         switch (todo) {
629                 case FS_QUOTA:
630                         result = do_quota(cli,SMB_USER_FS_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
631                         break;
632                 case LIST_QUOTA:
633                         result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_LIST, username_str, NULL);
634                         break;
635                 case USER_QUOTA:
636                         result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
637                         break;
638                 case SET_QUOTA:
639                         result = do_quota(cli, qtype, cmd, username_str, &qt);
640                         break;
641                 default: 
642                         result = EXIT_FAILED;
643                         break;
644         }
645
646         talloc_free(frame);
647
648         return result;
649 }