r24943: Some stackframes
[Samba.git] / source / utils / smbcquotas.c
blob8d954ac20ae93e5bdb03156719ed3ac91b4e6cab
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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
26 static pstring server;
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;
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};
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;
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 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;
58 /* Open policy handle */
60 if (!got_policy_hnd) {
62 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
63 but NT sends 0x2000000 so we might as well do it too. */
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;
70 got_policy_hnd = True;
73 return True;
76 /* convert a SID to a string, either numeric or username/group */
77 static void SidToString(fstring str, DOM_SID *sid, BOOL _numeric)
79 char **domains = NULL;
80 char **names = NULL;
81 enum lsa_SidType *types = NULL;
83 sid_to_string(str, sid);
85 if (_numeric) return;
87 /* Ask LSA to convert the sid to a name */
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;
97 /* Converted OK */
99 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
100 domains[0], lp_winbind_separator(),
101 names[0]);
105 /* convert a string to a SID, either numeric or username/group */
106 static BOOL StringToSid(DOM_SID *sid, const char *str)
108 enum lsa_SidType *types = NULL;
109 DOM_SID *sids = NULL;
110 BOOL result = True;
112 if (strncmp(str, "S-", 2) == 0) {
113 return string_to_sid(sid, str);
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;
124 sid_copy(sid, &sids[0]);
125 done:
127 return result;
130 #define QUOTA_GET 1
131 #define QUOTA_SETLIM 2
132 #define QUOTA_SETFLAGS 3
133 #define QUOTA_LIST 4
135 enum {PARSE_FLAGS,PARSE_LIM};
137 static int parse_quota_set(pstring set_str, pstring username_str, enum SMB_QUOTA_TYPE *qtype, int *cmd, SMB_NTQUOTA_STRUCT *pqt)
139 char *p = set_str,*p2;
140 int todo;
141 BOOL stop = False;
142 BOOL enable = False;
143 BOOL deny = False;
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;
154 *p2 = '\0';
155 p2++;
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;
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;
183 break;
184 case PARSE_FLAGS:
185 while (!stop) {
187 if ((p2=strstr(p,"/"))==NULL) {
188 stop = True;
189 } else {
190 *p2 = '\0';
191 p2++;
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;
206 p=p2;
209 if (deny) {
210 pqt->qflags |= QUOTAS_DENY_DISK;
211 } else if (enable) {
212 pqt->qflags |= QUOTAS_ENABLED;
215 break;
218 return 0;
221 static int do_quota(struct cli_state *cli, enum SMB_QUOTA_TYPE qtype, uint16 cmd, pstring username_str, SMB_NTQUOTA_STRUCT *pqt)
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);
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;
235 if (!(fs_attrs & FILE_VOLUME_QUOTAS)) {
236 d_printf("Quotas are not supported by the server.\n");
237 return 0;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
338 dump_ntquota(&qt,True,numeric,NULL);
339 break;
340 default:
341 d_printf("Unknown Error\n");
342 return -1;
344 break;
345 default:
346 d_printf("Unknown Error\n");
347 return -1;
350 cli_close(cli, quota_fnum);
352 return 0;
355 /*****************************************************
356 return a connection to a server
357 *******************************************************/
358 static struct cli_state *connect_one(const char *share)
360 struct cli_state *c;
361 struct in_addr ip;
362 NTSTATUS nt_status;
363 zero_ip(&ip);
365 if (!cmdline_auth_info.got_pass) {
366 char *pass = getpass("Password: ");
367 if (pass) {
368 pstrcpy(cmdline_auth_info.password, pass);
369 cmdline_auth_info.got_pass = True;
373 if (NT_STATUS_IS_OK(nt_status = cli_full_connection(&c, global_myname(), server,
374 &ip, 0,
375 share, "?????",
376 cmdline_auth_info.username, lp_workgroup(),
377 cmdline_auth_info.password, 0,
378 cmdline_auth_info.signing_state, NULL))) {
379 return c;
380 } else {
381 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
382 return NULL;
386 /****************************************************************************
387 main program
388 ****************************************************************************/
389 int main(int argc, const char *argv[])
391 char *share;
392 int opt;
393 int result;
394 int todo = 0;
395 pstring username_str = {0};
396 pstring path = {0};
397 pstring set_str = {0};
398 enum SMB_QUOTA_TYPE qtype = SMB_INVALID_QUOTA_TYPE;
399 int cmd = 0;
400 static BOOL test_args = False;
401 struct cli_state *cli;
402 BOOL fix_user = False;
403 SMB_NTQUOTA_STRUCT qt;
404 TALLOC_CTX *frame = talloc_stackframe();
405 poptContext pc;
406 struct poptOption long_options[] = {
407 POPT_AUTOHELP
408 { "user", 'u', POPT_ARG_STRING, NULL, 'u', "Show quotas for user", "user" },
409 { "list", 'L', POPT_ARG_NONE, NULL, 'L', "List user quotas" },
410 { "fs", 'F', POPT_ARG_NONE, NULL, 'F', "Show filesystem quotas" },
411 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls\n\
412 SETSTRING:\n\
413 UQLIM:<username>/<softlimit>/<hardlimit> for user quotas\n\
414 FSQLIM:<softlimit>/<hardlimit> for filesystem defaults\n\
415 FSQFLAGS:QUOTA_ENABLED/DENY_DISK/LOG_SOFTLIMIT/LOG_HARD_LIMIT", "SETSTRING" },
416 { "numeric", 'n', POPT_ARG_NONE, &numeric, True, "Don't resolve sids or limits to names" },
417 { "verbose", 'v', POPT_ARG_NONE, &verbose, True, "be verbose" },
418 { "test-args", 't', POPT_ARG_NONE, &test_args, True, "Test arguments"},
419 POPT_COMMON_SAMBA
420 POPT_COMMON_CREDENTIALS
421 { NULL }
424 load_case_tables();
426 ZERO_STRUCT(qt);
428 /* set default debug level to 1 regardless of what smb.conf sets */
429 setup_logging( "smbcquotas", True );
430 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
431 dbf = x_stderr;
432 x_setbuf( x_stderr, NULL );
434 setlinebuf(stdout);
436 fault_setup(NULL);
438 lp_load(dyn_CONFIGFILE,True,False,False,True);
439 load_interfaces();
441 pc = poptGetContext("smbcquotas", argc, argv, long_options, 0);
443 poptSetOtherOptionHelp(pc, "//server1/share1");
445 while ((opt = poptGetNextOpt(pc)) != -1) {
446 switch (opt) {
447 case 'L':
448 if (todo != 0) {
449 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
450 exit(EXIT_PARSE_ERROR);
452 todo = LIST_QUOTA;
453 break;
455 case 'F':
456 if (todo != 0) {
457 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
458 exit(EXIT_PARSE_ERROR);
460 todo = FS_QUOTA;
461 break;
463 case 'u':
464 if (todo != 0) {
465 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
466 exit(EXIT_PARSE_ERROR);
468 pstrcpy(username_str,poptGetOptArg(pc));
469 todo = USER_QUOTA;
470 fix_user = True;
471 break;
473 case 'S':
474 if (todo != 0) {
475 d_printf("Please specify only one option of <-L|-F|-S|-u>\n");
476 exit(EXIT_PARSE_ERROR);
478 pstrcpy(set_str,poptGetOptArg(pc));
479 todo = SET_QUOTA;
480 break;
484 if (todo == 0)
485 todo = USER_QUOTA;
487 if (!fix_user)
488 pstrcpy(username_str,cmdline_auth_info.username);
490 /* Make connection to server */
491 if(!poptPeekArg(pc)) {
492 poptPrintUsage(pc, stderr, 0);
493 exit(EXIT_PARSE_ERROR);
496 pstrcpy(path, poptGetArg(pc));
498 all_string_sub(path,"/","\\",0);
500 pstrcpy(server,path+2);
501 share = strchr_m(server,'\\');
502 if (!share) {
503 printf("Invalid argument: %s\n", share);
504 exit(EXIT_PARSE_ERROR);
507 *share = 0;
508 share++;
510 if (todo == SET_QUOTA) {
511 if (parse_quota_set(set_str, username_str, &qtype, &cmd, &qt)) {
512 printf("Invalid argument: -S %s\n", set_str);
513 exit(EXIT_PARSE_ERROR);
517 if (!test_args) {
518 cli = connect_one(share);
519 if (!cli) {
520 exit(EXIT_FAILED);
522 } else {
523 exit(EXIT_OK);
527 /* Perform requested action */
529 switch (todo) {
530 case FS_QUOTA:
531 result = do_quota(cli,SMB_USER_FS_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
532 break;
533 case LIST_QUOTA:
534 result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_LIST, username_str, NULL);
535 break;
536 case USER_QUOTA:
537 result = do_quota(cli,SMB_USER_QUOTA_TYPE, QUOTA_GET, username_str, NULL);
538 break;
539 case SET_QUOTA:
540 result = do_quota(cli, qtype, cmd, username_str, &qt);
541 break;
542 default:
544 result = EXIT_FAILED;
545 break;
548 talloc_free(frame);
550 return result;