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