Copyright (C) Andrew Tridgell 2000
Copyright (C) Tim Potter 2000
+ Copyright (C) Jeremy Allison 2000
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "includes.h"
static fstring password;
-static fstring username;
+static pstring username;
+static pstring owner_username;
static fstring server;
static int got_pass;
static int test_args;
+TALLOC_CTX *ctx;
+
+#define CREATE_ACCESS_READ READ_CONTROL_ACCESS
+#define CREATE_ACCESS_WRITE (WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS)
/* numeric is set when the user wants numeric SIDs and ACEs rather
than going via LSA calls to resolve them */
static int numeric;
-enum acl_mode {ACL_SET, ACL_DELETE, ACL_MODIFY, ACL_ADD};
+enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
+enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP};
+enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
struct perm_value {
char *perm;
/* Open cli connection and policy handle */
-static BOOL open_policy_hnd(void)
+static BOOL cacls_open_policy_hnd(void)
{
creds.pwd.null_pwd = 1;
/* Open policy handle */
if (!got_policy_hnd) {
- if (cli_lsa_open_policy(&lsa_cli, True,
- SEC_RIGHTS_MAXIMUM_ALLOWED, &pol)
+
+ /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
+ but NT sends 0x2000000 so we might as well do it too. */
+
+ if (cli_lsa_open_policy(&lsa_cli, lsa_cli.mem_ctx, True,
+ GENERIC_EXECUTE_ACCESS, &pol)
!= NT_STATUS_NOPROBLEMO) {
return False;
}
/* Ask LSA to convert the sid to a name */
- if (open_policy_hnd() &&
- cli_lsa_lookup_sids(&lsa_cli, &pol, 1, sid, &names, &types,
- &num_names) == NT_STATUS_NOPROBLEMO) {
-
- /* Converted OK */
-
- fstrcpy(str, names[0]);
-
- safe_free(names[0]);
- safe_free(names);
- safe_free(types);
+ if (!cacls_open_policy_hnd() ||
+ cli_lsa_lookup_sids(&lsa_cli, lsa_cli.mem_ctx, &pol, 1, sid, &names, &types,
+ &num_names) != NT_STATUS_NOPROBLEMO ||
+ !names || !names[0]) {
+ return;
}
+
+ /* Converted OK */
+
+ fstrcpy(str, names[0]);
}
/* convert a string to a SID, either numeric or username/group */
-static BOOL StringToSid(DOM_SID *sid, fstring str)
+static BOOL StringToSid(DOM_SID *sid, char *str)
{
uint32 *types = NULL;
DOM_SID *sids = NULL;
int num_sids;
- BOOL result = False;
+ BOOL result = True;
- /* Short cut */
-
if (strncmp(str, "S-", 2) == 0) {
- result = string_to_sid(sid, str);
- goto done;
+ return string_to_sid(sid, str);
}
- if (open_policy_hnd() &&
- cli_lsa_lookup_names(&lsa_cli, &pol, 1, &str, &sids, &types,
- &num_sids) == NT_STATUS_NOPROBLEMO) {
- result = string_to_sid(sid, str);
+ if (!cacls_open_policy_hnd() ||
+ cli_lsa_lookup_names(&lsa_cli, lsa_cli.mem_ctx, &pol, 1, &str, &sids, &types,
+ &num_sids) != NT_STATUS_NOPROBLEMO) {
+ result = False;
goto done;
}
sid_copy(sid, &sids[0]);
- safe_free(sids);
- safe_free(types);
-
done:
+
return result;
}
fprintf(f, "%s:", sidstr);
if (numeric) {
- fprintf(f, "%d/%d/0x%08x\n",
+ fprintf(f, "%d/%d/0x%08x",
ace->type, ace->flags, ace->info.mask);
return;
}
for (v = standard_values; v->perm; v++) {
if (ace->info.mask == v->mask) {
- fprintf(f, "%s\n", v->perm);
+ fprintf(f, "%s", v->perm);
return;
}
}
goto again;
}
}
-
- fprintf(f, "\n");
}
/* Only numeric form accepted for flags at present */
- if (!(next_token(NULL, tok, "/", sizeof(fstring)) &&
+ if (!(next_token(&p, tok, "/", sizeof(fstring)) &&
sscanf(tok, "%i", &aflags))) {
return False;
}
- if (!next_token(NULL, tok, "/", sizeof(fstring))) {
+ if (!next_token(&p, tok, "/", sizeof(fstring))) {
return False;
}
SEC_ACL *new;
SEC_ACE *aces;
if (! *the_acl) {
- (*the_acl) = make_sec_acl(3, 1, ace);
+ (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
return True;
}
aces = calloc(1+(*the_acl)->num_aces,sizeof(SEC_ACE));
memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
- new = make_sec_acl((*the_acl)->revision,1+(*the_acl)->num_aces, aces);
- free_sec_acl(the_acl);
+ new = make_sec_acl(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
free(aces);
(*the_acl) = new;
return True;
char *p = str;
fstring tok;
SEC_DESC *ret;
- unsigned sd_size;
+ size_t sd_size;
DOM_SID *grp_sid=NULL, *owner_sid=NULL;
SEC_ACL *dacl=NULL;
int revision=1;
- while (next_token(&p, tok, " \t,\r\n", sizeof(tok))) {
+ while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
if (strncmp(tok,"REVISION:", 9) == 0) {
revision = strtol(tok+9, NULL, 16);
if (strncmp(tok,"ACL:", 4) == 0) {
SEC_ACE ace;
- if (!parse_ace(&ace, tok+4) ||
- !add_ace(&dacl, &ace)) {
- printf("Failed to parse ACL\n");
+ if (!parse_ace(&ace, tok+4)) {
+ printf("Failed to parse ACL %s\n", tok);
+ return NULL;
+ }
+ if(!add_ace(&dacl, &ace)) {
+ printf("Failed to add ACL %s\n", tok);
return NULL;
}
continue;
return NULL;
}
- ret = make_sec_desc(revision, owner_sid, grp_sid,
+ ret = make_sec_desc(ctx,revision, owner_sid, grp_sid,
NULL, dacl, &sd_size);
- free_sec_acl(&dacl);
-
if (grp_sid) free(grp_sid);
if (owner_sid) free(owner_sid);
SEC_ACE *ace = &sd->dacl->ace[i];
fprintf(f, "ACL:");
print_ace(f, ace);
+ fprintf(f, "\n");
}
}
-
/*****************************************************
dump the acls for a file
*******************************************************/
-static void cacl_dump(struct cli_state *cli, char *filename)
+static int cacl_dump(struct cli_state *cli, char *filename)
{
int fnum;
SEC_DESC *sd;
- if (test_args) return;
+ if (test_args) return EXIT_OK;
- fnum = cli_nt_create(cli, filename, 0x20000);
+ fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
if (fnum == -1) {
printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
- return;
+ return EXIT_FAILED;
}
- sd = cli_query_secdesc(cli, fnum);
+ sd = cli_query_secdesc(cli, fnum, ctx);
if (!sd) {
printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
- return;
+ return EXIT_FAILED;
}
sec_desc_print(stdout, sd);
- free_sec_desc(&sd);
+ cli_close(cli, fnum);
+
+ return EXIT_OK;
+}
+
+/*****************************************************
+Change the ownership or group ownership of a file. Just
+because the NT docs say this can't be done :-). JRA.
+*******************************************************/
+
+static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
+ char *filename, char *new_username)
+{
+ int fnum;
+ DOM_SID sid;
+ SEC_DESC *sd, *old;
+ size_t sd_size;
+
+ fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
+
+ if (fnum == -1) {
+ printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
+ return EXIT_FAILED;
+ }
+
+ if (!StringToSid(&sid, new_username))
+ return EXIT_PARSE_ERROR;
+
+ old = cli_query_secdesc(cli, fnum, ctx);
+
+ cli_close(cli, fnum);
+
+ if (!old) {
+ printf("owner_set: Failed to query old descriptor\n");
+ return EXIT_FAILED;
+ }
+
+ sd = make_sec_desc(ctx,old->revision,
+ (change_mode == REQUEST_CHOWN) ? &sid : old->owner_sid,
+ (change_mode == REQUEST_CHGRP) ? &sid : old->grp_sid,
+ NULL, old->dacl, &sd_size);
+
+ fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
+
+ if (fnum == -1) {
+ printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
+ return EXIT_FAILED;
+ }
+
+ if (!cli_set_secdesc(cli, fnum, sd)) {
+ printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
+ }
cli_close(cli, fnum);
+
+ return EXIT_OK;
+}
+
+
+/* The MSDN is contradictory over the ordering of ACE entries in an ACL.
+ However NT4 gives a "The information may have been modified by a
+ computer running Windows NT 5.0" if denied ACEs do not appear before
+ allowed ACEs. */
+
+static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
+{
+ if (sec_ace_equal(ace1, ace2)) return 0;
+ if (ace1->type != ace2->type) return ace2->type - ace1->type;
+ if (sid_compare(&ace1->sid, &ace2->sid)) return sid_compare(&ace1->sid, &ace2->sid);
+ if (ace1->flags != ace2->flags) return ace1->flags - ace2->flags;
+ if (ace1->info.mask != ace2->info.mask) return ace1->info.mask - ace2->info.mask;
+ if (ace1->size != ace2->size) return ace1->size - ace2->size;
+ return memcmp(ace1, ace2, sizeof(SEC_ACE));
+}
+
+static void sort_acl(SEC_ACL *the_acl)
+{
+ int i;
+ if (!the_acl) return;
+
+ qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
+
+ for (i=1;i<the_acl->num_aces;) {
+ if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
+ int j;
+ for (j=i; j<the_acl->num_aces-1; j++) {
+ the_acl->ace[j] = the_acl->ace[j+1];
+ }
+ the_acl->num_aces--;
+ } else {
+ i++;
+ }
+ }
}
/*****************************************************
set the ACLs on a file given an ascii description
*******************************************************/
-static void cacl_set(struct cli_state *cli, char *filename,
- char *the_acl, enum acl_mode mode)
+static int cacl_set(struct cli_state *cli, char *filename,
+ char *the_acl, enum acl_mode mode)
{
int fnum;
SEC_DESC *sd, *old;
int i, j;
- unsigned sd_size;
+ size_t sd_size;
+ int result = EXIT_OK;
sd = sec_desc_parse(the_acl);
- if (!sd) return;
- if (test_args) return;
+ if (!sd) return EXIT_PARSE_ERROR;
+ if (test_args) return EXIT_OK;
+
+ /* The desired access below is the only one I could find that works
+ with NT4, W2KP and Samba */
+
+ fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
- /* the desired access below is the only one I could find that works with
- NT4, W2KP and Samba */
- fnum = cli_nt_create(cli, filename, MAXIMUM_ALLOWED_ACCESS | 0x60000);
if (fnum == -1) {
- printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
- return;
+ printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
+ return EXIT_FAILED;
}
- old = cli_query_secdesc(cli, fnum);
+ old = cli_query_secdesc(cli, fnum, ctx);
+
+ if (!old) {
+ printf("calc_set: Failed to query old descriptor\n");
+ return EXIT_FAILED;
+ }
+
+ cli_close(cli, fnum);
/* the logic here is rather more complex than I would like */
switch (mode) {
- case ACL_DELETE:
+ case SMB_ACL_DELETE:
for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
BOOL found = False;
for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
if (sec_ace_equal(&sd->dacl->ace[i],
&old->dacl->ace[j])) {
- if (j != old->dacl->num_aces-1) {
- old->dacl->ace[j] = old->dacl->ace[j+1];
+ int k;
+ for (k=j; k<old->dacl->num_aces-1;k++) {
+ old->dacl->ace[k] = old->dacl->ace[k+1];
}
old->dacl->num_aces--;
if (old->dacl->num_aces == 0) {
}
if (!found) {
- fstring str;
-
- SidToString(str, &sd->dacl->ace[i].sid);
- printf("ACL for SID %s not found\n", str);
+ printf("ACL for ACE:");
+ print_ace(stdout, &sd->dacl->ace[i]);
+ printf(" not found\n");
}
}
break;
- case ACL_MODIFY:
+ case SMB_ACL_MODIFY:
for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
BOOL found = False;
break;
- case ACL_ADD:
+ case SMB_ACL_ADD:
for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
add_ace(&old->dacl, &sd->dacl->ace[i]);
}
break;
- case ACL_SET:
- free_sec_desc(&old);
+ case SMB_ACL_SET:
old = sd;
break;
}
- if (sd != old) {
- free_sec_desc(&sd);
- }
+ /* Denied ACE entries must come before allowed ones */
+ sort_acl(old->dacl);
- sd = make_sec_desc(old->revision, old->owner_sid, old->grp_sid,
+ /* Create new security descriptor and set it */
+ sd = make_sec_desc(ctx,old->revision, old->owner_sid, old->grp_sid,
NULL, old->dacl, &sd_size);
+ fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
+
+ if (fnum == -1) {
+ printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
+ return EXIT_FAILED;
+ }
+
if (!cli_set_secdesc(cli, fnum, sd)) {
printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
- return;
+ result = EXIT_FAILED;
}
- free_sec_desc(&sd);
- free_sec_desc(&old);
+ /* Clean up */
cli_close(cli, fnum);
+
+ return result;
}
if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
!cli_connect(c, server_n, &ip)) {
DEBUG(0,("Connection to %s failed\n", server_n));
+ cli_shutdown(c);
+ safe_free(c);
return NULL;
}
if (!cli_session_request(c, &calling, &called)) {
DEBUG(0,("session request to %s failed\n", called.name));
cli_shutdown(c);
+ safe_free(c);
if (strcmp(called.name, "*SMBSERVER")) {
make_nmb_name(&called , "*SMBSERVER", 0x20);
goto again;
if (!cli_negprot(c)) {
DEBUG(0,("protocol negotiation failed\n"));
cli_shutdown(c);
+ safe_free(c);
return NULL;
}
password, strlen(password),
lp_workgroup())) {
DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
+ cli_shutdown(c);
+ safe_free(c);
return NULL;
}
password, strlen(password)+1)) {
DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
cli_shutdown(c);
+ safe_free(c);
return NULL;
}
static void usage(void)
{
printf(
-"Usage: smbcacls //server1/share1 filename -U username [options]\n\
+"Usage: smbcacls //server1/share1 filename [options]\n\
\n\
\t-D <acls> delete an acl\n\
\t-M <acls> modify an acl\n\
\t-A <acls> add an acl\n\
\t-S <acls> set acls\n\
+\t-C username change ownership of a file\n\
+\t-G username change group ownership of a file\n\
\t-n don't resolve sids or masks to names\n\
\t-h print help\n\
\n\
int main(int argc,char *argv[])
{
char *share;
- char *filename;
+ pstring filename;
extern char *optarg;
extern int optind;
extern FILE *dbf;
int opt;
char *p;
- int seed;
static pstring servicesf = CONFIGFILE;
- struct cli_state *cli;
- enum acl_mode mode;
+ struct cli_state *cli=NULL;
+ enum acl_mode mode = SMB_ACL_SET;
char *the_acl = NULL;
+ enum chown_mode change_mode = REQUEST_NONE;
+ int result;
+
+ ctx=talloc_init();
setlinebuf(stdout);
dbf = stderr;
- if (argc < 4 || argv[1][0] == '-') {
+ if (argc < 3 || argv[1][0] == '-') {
usage();
- exit(1);
+ talloc_destroy(ctx);
+ exit(EXIT_PARSE_ERROR);
}
setup_logging(argv[0],True);
share = argv[1];
- filename = argv[2];
+ pstrcpy(filename, argv[2]);
all_string_sub(share,"/","\\",0);
argc -= 2;
if (getenv("USER")) {
pstrcpy(username,getenv("USER"));
- }
- seed = time(NULL);
+ if ((p=strchr(username,'%'))) {
+ *p = 0;
+ pstrcpy(password,p+1);
+ got_pass = True;
+ memset(strchr(getenv("USER"), '%') + 1, 'X',
+ strlen(password));
+ }
+ }
- while ((opt = getopt(argc, argv, "U:nhS:D:A:M:t")) != EOF) {
+ while ((opt = getopt(argc, argv, "U:nhS:D:A:M:C:G:t")) != EOF) {
switch (opt) {
case 'U':
pstrcpy(username,optarg);
case 'S':
the_acl = optarg;
- mode = ACL_SET;
+ mode = SMB_ACL_SET;
break;
case 'D':
the_acl = optarg;
- mode = ACL_DELETE;
+ mode = SMB_ACL_DELETE;
break;
case 'M':
the_acl = optarg;
- mode = ACL_MODIFY;
+ mode = SMB_ACL_MODIFY;
break;
case 'A':
the_acl = optarg;
- mode = ACL_ADD;
+ mode = SMB_ACL_ADD;
+ break;
+
+ case 'C':
+ pstrcpy(owner_username,optarg);
+ change_mode = REQUEST_CHOWN;
+ break;
+
+ case 'G':
+ pstrcpy(owner_username,optarg);
+ change_mode = REQUEST_CHGRP;
break;
case 'n':
case 'h':
usage();
- exit(1);
+ talloc_destroy(ctx);
+ exit(EXIT_PARSE_ERROR);
default:
printf("Unknown option %c (%d)\n", (char)opt, opt);
- exit(1);
+ talloc_destroy(ctx);
+ exit(EXIT_PARSE_ERROR);
}
}
if (argc > 0) {
usage();
- exit(1);
+ talloc_destroy(ctx);
+ exit(EXIT_PARSE_ERROR);
}
+ /* Make connection to server */
+
if (!test_args) {
cli = connect_one(share);
- if (!cli) exit(1);
+ if (!cli) {
+ talloc_destroy(ctx);
+ exit(EXIT_FAILED);
+ }
+ }
+
+ all_string_sub(filename, "/", "\\", 0);
+ if (filename[0] != '\\') {
+ pstring s;
+ s[0] = '\\';
+ safe_strcpy(&s[1], filename, sizeof(pstring)-1);
+ pstrcpy(filename, s);
}
- if (the_acl) {
- cacl_set(cli, filename, the_acl, mode);
+ /* Perform requested action */
+
+ if (change_mode != REQUEST_NONE) {
+ result = owner_set(cli, change_mode, filename, owner_username);
+ } else if (the_acl) {
+ result = cacl_set(cli, filename, the_acl, mode);
} else {
- cacl_dump(cli, filename);
+ result = cacl_dump(cli, filename);
}
- return(0);
+ talloc_destroy(ctx);
+
+ return result;
}