r19772: syncing some non-source files
[Samba.git] / examples / libsmbclient / testacl.c
blob4d327b39a7a11e0c7a0eea3768fd94ed66a50ced
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <popt.h>
5 #include "libsmbclient.h"
6 #include "get_auth_data_fn.h"
8 enum acl_mode
10 SMB_ACL_GET,
11 SMB_ACL_SET,
12 SMB_ACL_DELETE,
13 SMB_ACL_MODIFY,
14 SMB_ACL_ADD,
15 SMB_ACL_CHOWN,
16 SMB_ACL_CHGRP
20 int main(int argc, const char *argv[])
22 int opt;
23 int flags;
24 int debug = 0;
25 int numeric = 0;
26 int full_time_names = 0;
27 enum acl_mode mode = SMB_ACL_GET;
28 static char *the_acl = NULL;
29 int ret;
30 char *p;
31 char *debugstr;
32 char path[1024];
33 char value[1024];
34 poptContext pc;
35 struct poptOption long_options[] =
37 POPT_AUTOHELP
39 "numeric", 'n', POPT_ARG_NONE, &numeric,
40 1, "Don't resolve sids or masks to names"
43 "debug", 'd', POPT_ARG_INT, &debug,
44 0, "Set debug level (0-100)"
47 "full_time_names", 'f', POPT_ARG_NONE, &full_time_names,
49 "Use new style xattr names, which include CREATE_TIME"
52 "delete", 'D', POPT_ARG_STRING, NULL,
53 'D', "Delete an acl", "ACL"
56 "modify", 'M', POPT_ARG_STRING, NULL,
57 'M', "Modify an acl", "ACL"
60 "add", 'a', POPT_ARG_STRING, NULL,
61 'a', "Add an acl", "ACL"
64 "set", 'S', POPT_ARG_STRING, NULL,
65 'S', "Set acls", "ACLS"
68 "chown", 'C', POPT_ARG_STRING, NULL,
69 'C', "Change ownership of a file", "USERNAME"
72 "chgrp", 'G', POPT_ARG_STRING, NULL,
73 'G', "Change group ownership of a file", "GROUPNAME"
76 "get", 'g', POPT_ARG_STRING, NULL,
77 'g', "Get a specific acl attribute", "ACL"
80 NULL
84 setbuf(stdout, NULL);
86 pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
88 poptSetOtherOptionHelp(pc, "smb://server1/share1/filename");
90 while ((opt = poptGetNextOpt(pc)) != -1) {
91 switch (opt) {
92 case 'S':
93 the_acl = strdup(poptGetOptArg(pc));
94 mode = SMB_ACL_SET;
95 break;
97 case 'D':
98 the_acl = strdup(poptGetOptArg(pc));
99 mode = SMB_ACL_DELETE;
100 break;
102 case 'M':
103 the_acl = strdup(poptGetOptArg(pc));
104 mode = SMB_ACL_MODIFY;
105 break;
107 case 'a':
108 the_acl = strdup(poptGetOptArg(pc));
109 mode = SMB_ACL_ADD;
110 break;
112 case 'g':
113 the_acl = strdup(poptGetOptArg(pc));
114 mode = SMB_ACL_GET;
115 break;
117 case 'C':
118 the_acl = strdup(poptGetOptArg(pc));
119 mode = SMB_ACL_CHOWN;
120 break;
122 case 'G':
123 the_acl = strdup(poptGetOptArg(pc));
124 mode = SMB_ACL_CHGRP;
125 break;
129 /* Make connection to server */
130 if(!poptPeekArg(pc)) {
131 poptPrintUsage(pc, stderr, 0);
132 return 1;
135 strcpy(path, poptGetArg(pc));
137 if (smbc_init(get_auth_data_fn, debug) != 0)
139 printf("Could not initialize smbc_ library\n");
140 return 1;
143 if (full_time_names) {
144 SMBCCTX *context = smbc_set_context(NULL);
145 smbc_option_set(context, "full_time_names", 1);
148 /* Perform requested action */
150 switch(mode)
152 case SMB_ACL_GET:
153 if (the_acl == NULL)
155 if (numeric)
157 the_acl = "system.*";
159 else
161 the_acl = "system.*+";
164 ret = smbc_getxattr(path, the_acl, value, sizeof(value));
165 if (ret < 0)
167 printf("Could not get attributes for [%s] %d: %s\n",
168 path, errno, strerror(errno));
169 return 1;
172 printf("Attributes for [%s] are:\n%s\n", path, value);
173 break;
175 case SMB_ACL_ADD:
176 flags = SMBC_XATTR_FLAG_CREATE;
177 debugstr = "add attributes";
178 goto do_set;
180 case SMB_ACL_MODIFY:
181 flags = SMBC_XATTR_FLAG_REPLACE;
182 debugstr = "modify attributes";
183 goto do_set;
185 case SMB_ACL_CHOWN:
186 snprintf(value, sizeof(value),
187 "system.nt_sec_desc.owner%s:%s",
188 numeric ? "" : "+", the_acl);
189 the_acl = value;
190 debugstr = "chown owner";
191 goto do_set;
193 case SMB_ACL_CHGRP:
194 snprintf(value, sizeof(value),
195 "system.nt_sec_desc.group%s:%s",
196 numeric ? "" : "+", the_acl);
197 the_acl = value;
198 debugstr = "change group";
199 goto do_set;
201 case SMB_ACL_SET:
202 flags = 0;
203 debugstr = "set attributes";
205 do_set:
206 if ((p = strchr(the_acl, ':')) == NULL)
208 printf("Missing value. ACL must be name:value pair\n");
209 return 1;
212 *p++ = '\0';
214 ret = smbc_setxattr(path, the_acl, p, strlen(p), flags);
215 if (ret < 0)
217 printf("Could not %s for [%s] %d: %s\n",
218 debugstr, path, errno, strerror(errno));
219 return 1;
221 break;
223 case SMB_ACL_DELETE:
224 ret = smbc_removexattr(path, the_acl);
225 if (ret < 0)
227 printf("Could not remove attribute %s for [%s] %d:%s\n",
228 the_acl, path, errno, strerror(errno));
229 return 1;
231 break;
233 default:
234 printf("operation not yet implemented\n");
235 break;
238 return 0;