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