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