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