r7997: Pointers don't kill people, people with pointers kill people...
[Samba/gbeck.git] / source / utils / smbcquotas.c
blob81f7dd42bbc2c56f014d32577c090693d00c825e
1 /*
2 Unix SMB/CIFS implementation.
3 QUOTA get/set utility
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
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.
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.
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.
25 #include "includes.h"
27 static pstring server;
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;
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};
37 static struct cli_state *cli_ipc = NULL;
38 static POLICY_HND pol;
39 static BOOL got_policy_hnd;
41 static struct cli_state *connect_one(const char *share);
43 /* Open cli connection and policy handle */
45 static BOOL cli_open_policy_hnd(void)
47 /* Initialise cli LSA connection */
49 if (!cli_ipc) {
50 cli_ipc = connect_one("IPC$");
51 if (!cli_nt_session_open (cli_ipc, PI_LSARPC)) {
52 return False;
56 /* Open policy handle */
58 if (!got_policy_hnd) {
60 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
61 but NT sends 0x2000000 so we might as well do it too. */
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;
68 got_policy_hnd = True;
71 return True;
74 /* convert a SID to a string, either numeric or username/group */
75 static void SidToString(fstring str, DOM_SID *sid, BOOL _numeric)
77 char **domains = NULL;
78 char **names = NULL;
79 uint32 *types = NULL;
81 sid_to_string(str, sid);
83 if (_numeric) return;
85 /* Ask LSA to convert the sid to a name */
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;
95 /* Converted OK */
97 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
98 domains[0], lp_winbind_separator(),
99 names[0]);
103 /* convert a string to a SID, either numeric or username/group */
104 static BOOL StringToSid(DOM_SID *sid, const char *str)
106 uint32 *types = NULL;
107 DOM_SID *sids = NULL;
108 BOOL result = True;
110 if (strncmp(str, "S-", 2) == 0) {
111 return string_to_sid(sid, str);
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;
122 sid_copy(sid, &sids[0]);
123 done:
125 return result;
128 #define QUOTA_GET 1
129 #define QUOTA_SETLIM 2
130 #define QUOTA_SETFLAGS 3
131 #define QUOTA_LIST 4
133 enum {PARSE_FLAGS,PARSE_LIM};
135 static int parse_quota_set(pstring set_str, pstring username_str, enum SMB_QUOTA_TYPE *qtype, int *cmd, SMB_NTQUOTA_STRUCT *pqt)
137 char *p = set_str,*p2;
138 int todo;
139 BOOL stop = False;
140 BOOL enable = False;
141 BOOL deny = False;
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;
152 *p2 = '\0';
153 p2++;
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;
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;
181 break;
182 case PARSE_FLAGS:
183 while (!stop) {
185 if ((p2=strstr(p,"/"))==NULL) {
186 stop = True;
187 } else {
188 *p2 = '\0';
189 p2++;
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;
204 p=p2;
207 if (deny) {
208 pqt->qflags |= QUOTAS_DENY_DISK;
209 } else if (enable) {
210 pqt->qflags |= QUOTAS_ENABLED;
213 break;
216 return 0;
219 static int do_quota(struct cli_state *cli, enum SMB_QUOTA_TYPE qtype, uint16 cmd, pstring username_str, SMB_NTQUOTA_STRUCT *pqt)
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);
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;
233 if (!(fs_attrs & FILE_VOLUME_QUOTAS)) {
234 d_printf("Quotas are not supported by the server.\n");
235 return 0;
238 if (!cli_get_quota_handle(cli, &quota_fnum)) {
239 d_printf("Quotas are not enabled on this share.\n");
240 d_printf("Failed to open %s %s.\n",
241 FAKE_FILE_NAME_QUOTA_WIN32,cli_errstr(cli));
242 return -1;
245 switch(qtype) {
246 case SMB_USER_QUOTA_TYPE:
247 if (!StringToSid(&qt.sid, username_str)) {
248 d_printf("StringToSid() failed for [%s]\n",username_str);
249 return -1;
252 switch(cmd) {
253 case QUOTA_GET:
254 if (!cli_get_user_quota(cli, quota_fnum, &qt)) {
255 d_printf("%s cli_get_user_quota %s\n",
256 cli_errstr(cli),username_str);
257 return -1;
259 dump_ntquota(&qt,verbose,numeric,SidToString);
260 break;
261 case QUOTA_SETLIM:
262 pqt->sid = qt.sid;
263 if (!cli_set_user_quota(cli, quota_fnum, pqt)) {
264 d_printf("%s cli_set_user_quota %s\n",
265 cli_errstr(cli),username_str);
266 return -1;
268 if (!cli_get_user_quota(cli, quota_fnum, &qt)) {
269 d_printf("%s cli_get_user_quota %s\n",
270 cli_errstr(cli),username_str);
271 return -1;
273 dump_ntquota(&qt,verbose,numeric,SidToString);
274 break;
275 case QUOTA_LIST:
276 if (!cli_list_user_quota(cli, quota_fnum, &qtl)) {
277 d_printf("%s cli_set_user_quota %s\n",
278 cli_errstr(cli),username_str);
279 return -1;
281 dump_ntquota_list(&qtl,verbose,numeric,SidToString);
282 free_ntquota_list(&qtl);
283 break;
284 default:
285 d_printf("Unknown Error\n");
286 return -1;
288 break;
289 case SMB_USER_FS_QUOTA_TYPE:
290 switch(cmd) {
291 case QUOTA_GET:
292 if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
293 d_printf("%s cli_get_fs_quota_info\n",
294 cli_errstr(cli));
295 return -1;
297 dump_ntquota(&qt,True,numeric,NULL);
298 break;
299 case QUOTA_SETLIM:
300 if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
301 d_printf("%s cli_get_fs_quota_info\n",
302 cli_errstr(cli));
303 return -1;
305 qt.softlim = pqt->softlim;
306 qt.hardlim = pqt->hardlim;
307 if (!cli_set_fs_quota_info(cli, quota_fnum, &qt)) {
308 d_printf("%s cli_set_fs_quota_info\n",
309 cli_errstr(cli));
310 return -1;
312 if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
313 d_printf("%s cli_get_fs_quota_info\n",
314 cli_errstr(cli));
315 return -1;
317 dump_ntquota(&qt,True,numeric,NULL);
318 break;
319 case QUOTA_SETFLAGS:
320 if (!cli_get_fs_quota_info(cli, quota_fnum, &qt)) {
321 d_printf("%s cli_get_fs_quota_info\n",
322 cli_errstr(cli));
323 return -1;
325 qt.qflags = pqt->qflags;
326 if (!cli_set_fs_quota_info(cli, quota_fnum, &qt)) {
327 d_printf("%s cli_set_fs_quota_info\n",
328 cli_errstr(cli));
329 return -1;
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;
336 dump_ntquota(&qt,True,numeric,NULL);
337 break;
338 default:
339 d_printf("Unknown Error\n");
340 return -1;
342 break;
343 default:
344 d_printf("Unknown Error\n");
345 return -1;
348 cli_close(cli, quota_fnum);
350 return 0;
353 /*****************************************************
354 return a connection to a server
355 *******************************************************/
356 static struct cli_state *connect_one(const char *share)
358 struct cli_state *c;
359 struct in_addr ip;
360 NTSTATUS nt_status;
361 zero_ip(&ip);
363 if (!cmdline_auth_info.got_pass) {
364 char *pass = getpass("Password: ");
365 if (pass) {
366 pstrcpy(cmdline_auth_info.password, pass);
367 cmdline_auth_info.got_pass = True;
371 if (NT_STATUS_IS_OK(nt_status = cli_full_connection(&c, global_myname(), server,
372 &ip, 0,
373 share, "?????",
374 cmdline_auth_info.username, lp_workgroup(),
375 cmdline_auth_info.password, 0,
376 cmdline_auth_info.signing_state, NULL))) {
377 return c;
378 } else {
379 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
380 return NULL;
384 /****************************************************************************
385 main program
386 ****************************************************************************/
387 int main(int argc, const char *argv[])
389 char *share;
390 int opt;
391 int result;
392 int todo = 0;
393 pstring username_str = {0};
394 pstring path = {0};
395 pstring set_str = {0};
396 enum SMB_QUOTA_TYPE qtype;
397 int cmd = 0;
398 static BOOL test_args = False;
399 struct cli_state *cli;
400 BOOL fix_user = False;
401 SMB_NTQUOTA_STRUCT qt;
402 poptContext pc;
403 struct poptOption long_options[] = {
404 POPT_AUTOHELP
405 { "user", 'u', POPT_ARG_STRING, NULL, 'u', "Show quotas for user", "user" },
406 { "list", 'L', POPT_ARG_NONE, NULL, 'L', "List user quotas" },
407 { "fs", 'F', POPT_ARG_NONE, NULL, 'F', "Show filesystem quotas" },
408 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls\n\
409 SETSTRING:\n\
410 UQLIM:<username>/<softlimit>/<hardlimit> for user quotas\n\
411 FSQLIM:<softlimit>/<hardlimit> for filesystem defaults\n\
412 FSQFLAGS:QUOTA_ENABLED/DENY_DISK/LOG_SOFTLIMIT/LOG_HARD_LIMIT", "SETSTRING" },
413 { "numeric", 'n', POPT_ARG_NONE, &numeric, True, "Don't resolve sids or limits to names" },
414 { "verbose", 'v', POPT_ARG_NONE, &verbose, True, "be verbose" },
415 { "test-args", 't', POPT_ARG_NONE, &test_args, True, "Test arguments"},
416 POPT_COMMON_SAMBA
417 POPT_COMMON_CREDENTIALS
418 { NULL }
421 ZERO_STRUCT(qt);
423 /* set default debug level to 1 regardless of what smb.conf sets */
424 setup_logging( "smbcquotas", True );
425 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
426 dbf = x_stderr;
427 x_setbuf( x_stderr, NULL );
429 setlinebuf(stdout);
431 fault_setup(NULL);
433 lp_load(dyn_CONFIGFILE,True,False,False);
434 load_interfaces();
436 pc = poptGetContext("smbcquotas", argc, argv, long_options, 0);
438 poptSetOtherOptionHelp(pc, "//server1/share1");
440 while ((opt = poptGetNextOpt(pc)) != -1) {
441 switch (opt) {
442 case 'L':
443 if (todo != 0) {
444 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
445 exit(EXIT_PARSE_ERROR);
447 todo = LIST_QUOTA;
448 break;
450 case 'F':
451 if (todo != 0) {
452 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
453 exit(EXIT_PARSE_ERROR);
455 todo = FS_QUOTA;
456 break;
458 case 'u':
459 if (todo != 0) {
460 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
461 exit(EXIT_PARSE_ERROR);
463 pstrcpy(username_str,poptGetOptArg(pc));
464 todo = USER_QUOTA;
465 fix_user = True;
466 break;
468 case 'S':
469 if (todo != 0) {
470 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
471 exit(EXIT_PARSE_ERROR);
473 pstrcpy(set_str,poptGetOptArg(pc));
474 todo = SET_QUOTA;
475 break;
479 if (todo == 0)
480 todo = USER_QUOTA;
482 if (!fix_user)
483 pstrcpy(username_str,cmdline_auth_info.username);
485 /* Make connection to server */
486 if(!poptPeekArg(pc)) {
487 poptPrintUsage(pc, stderr, 0);
488 exit(EXIT_PARSE_ERROR);
491 pstrcpy(path, poptGetArg(pc));
493 all_string_sub(path,"/","\\",0);
495 pstrcpy(server,path+2);
496 share = strchr_m(server,'\\');
497 if (!share) {
498 share = strchr_m(server,'/');
499 if (!share) {
500 printf("Invalid argument: %s\n", share);
501 exit(EXIT_PARSE_ERROR);
505 *share = 0;
506 share++;
508 if (todo == SET_QUOTA) {
509 if (parse_quota_set(set_str, username_str, &qtype, &cmd, &qt)) {
510 printf("Invalid argument: -S %s\n", set_str);
511 exit(EXIT_PARSE_ERROR);
515 if (!test_args) {
516 cli = connect_one(share);
517 if (!cli) {
518 exit(EXIT_FAILED);
520 } else {
521 exit(EXIT_OK);
525 /* Perform requested action */
527 switch (todo) {
528 case FS_QUOTA:
529 result = do_quota(cli,SMB_USER_FS_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
530 break;
531 case LIST_QUOTA:
532 result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_LIST, username_str, NULL);
533 break;
534 case USER_QUOTA:
535 result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
536 break;
537 case SET_QUOTA:
538 result = do_quota(cli, qtype, cmd, username_str, &qt);
539 break;
540 default:
542 result = EXIT_FAILED;
543 break;
546 return result;