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