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