idl: add to_null property
[Samba/gbeck.git] / examples / libsmbclient / testacl.c
blobb602c5b185453f1420caa44b78af87cb5469f1c1
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_LIST,
11 SMB_ACL_GET,
12 SMB_ACL_SET,
13 SMB_ACL_DELETE,
14 SMB_ACL_MODIFY,
15 SMB_ACL_ADD,
16 SMB_ACL_CHOWN,
17 SMB_ACL_CHGRP
21 int main(int argc, const char *argv[])
23 int opt;
24 int flags;
25 int debug = 0;
26 int numeric = 0;
27 int stat_and_retry = 0;
28 int full_time_names = 0;
29 enum acl_mode mode = SMB_ACL_LIST;
30 static const char *the_acl = NULL;
31 int ret;
32 char *p;
33 const char *debugstr;
34 char path[1024];
35 char value[1024];
36 poptContext pc;
37 struct stat st;
38 struct poptOption long_options[] =
40 POPT_AUTOHELP
42 "numeric", 'n', POPT_ARG_NONE, &numeric,
43 1, "Don't resolve sids or masks to names"
46 "debug", 'd', POPT_ARG_INT, &debug,
47 0, "Set debug level (0-100)"
50 "full_time_names", 'f', POPT_ARG_NONE, &full_time_names,
52 "Use new style xattr names, which include CREATE_TIME"
55 "delete", 'D', POPT_ARG_STRING, NULL,
56 'D', "Delete an acl", "ACL"
59 "modify", 'M', POPT_ARG_STRING, NULL,
60 'M', "Modify an acl", "ACL"
63 "add", 'a', POPT_ARG_STRING, NULL,
64 'a', "Add an acl", "ACL"
67 "set", 'S', POPT_ARG_STRING, NULL,
68 'S', "Set acls", "ACLS"
71 "chown", 'C', POPT_ARG_STRING, NULL,
72 'C', "Change ownership of a file", "USERNAME"
75 "chgrp", 'G', POPT_ARG_STRING, NULL,
76 'G', "Change group ownership of a file", "GROUPNAME"
79 "get", 'g', POPT_ARG_STRING, NULL,
80 'g', "Get a specific acl attribute", "ACL"
83 "stat_and_retry", 'R', POPT_ARG_NONE, &stat_and_retry,
84 1, "After 'get' do 'stat' and another 'get'"
87 NULL
91 setbuf(stdout, NULL);
93 pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
95 poptSetOtherOptionHelp(pc, "smb://server1/share1/filename");
97 while ((opt = poptGetNextOpt(pc)) != -1) {
98 switch (opt) {
99 case 'S':
100 the_acl = strdup(poptGetOptArg(pc));
101 mode = SMB_ACL_SET;
102 break;
104 case 'D':
105 the_acl = strdup(poptGetOptArg(pc));
106 mode = SMB_ACL_DELETE;
107 break;
109 case 'M':
110 the_acl = strdup(poptGetOptArg(pc));
111 mode = SMB_ACL_MODIFY;
112 break;
114 case 'a':
115 the_acl = strdup(poptGetOptArg(pc));
116 mode = SMB_ACL_ADD;
117 break;
119 case 'g':
120 the_acl = strdup(poptGetOptArg(pc));
121 mode = SMB_ACL_GET;
122 break;
124 case 'C':
125 the_acl = strdup(poptGetOptArg(pc));
126 mode = SMB_ACL_CHOWN;
127 break;
129 case 'G':
130 the_acl = strdup(poptGetOptArg(pc));
131 mode = SMB_ACL_CHGRP;
132 break;
136 /* Make connection to server */
137 if(!poptPeekArg(pc)) {
138 poptPrintUsage(pc, stderr, 0);
139 return 1;
142 strcpy(path, poptGetArg(pc));
144 if (smbc_init(get_auth_data_fn, debug) != 0)
146 printf("Could not initialize smbc_ library\n");
147 return 1;
150 if (full_time_names) {
151 SMBCCTX *context = smbc_set_context(NULL);
152 smbc_setOptionFullTimeNames(context, 1);
155 /* Perform requested action */
157 switch(mode)
159 case SMB_ACL_LIST:
160 ret = smbc_listxattr(path, value, sizeof(value)-2);
161 if (ret < 0)
163 printf("Could not get attribute list for [%s] %d: %s\n",
164 path, errno, strerror(errno));
165 return 1;
169 * The list of attributes has a series of null-terminated strings.
170 * The list of strings terminates with an extra null byte, thus two in
171 * a row. Ensure that our buffer, which is conceivably shorter than
172 * the list of attributes, actually ends with two null bytes in a row.
174 value[sizeof(value) - 2] = '\0';
175 value[sizeof(value) - 1] = '\0';
176 printf("Supported attributes:\n");
177 for (p = value; *p; p += strlen(p) + 1)
179 printf("\t%s\n", p);
181 break;
183 case SMB_ACL_GET:
186 if (the_acl == NULL)
188 if (numeric)
190 the_acl = "system.*";
192 else
194 the_acl = "system.*+";
197 ret = smbc_getxattr(path, the_acl, value, sizeof(value));
198 if (ret < 0)
200 printf("Could not get attributes for [%s] %d: %s\n",
201 path, errno, strerror(errno));
202 return 1;
205 printf("Attributes for [%s] are:\n%s\n", path, value);
207 if (stat_and_retry)
209 if (smbc_stat(path, &st) < 0)
211 perror("smbc_stat");
212 return 1;
216 --stat_and_retry;
217 } while (stat_and_retry >= 0);
218 break;
220 case SMB_ACL_ADD:
221 flags = SMBC_XATTR_FLAG_CREATE;
222 debugstr = "add attributes";
223 goto do_set;
225 case SMB_ACL_MODIFY:
226 flags = SMBC_XATTR_FLAG_REPLACE;
227 debugstr = "modify attributes";
228 goto do_set;
230 case SMB_ACL_CHOWN:
231 snprintf(value, sizeof(value),
232 "system.nt_sec_desc.owner%s:%s",
233 numeric ? "" : "+", the_acl);
234 the_acl = value;
235 debugstr = "chown owner";
236 goto do_set;
238 case SMB_ACL_CHGRP:
239 snprintf(value, sizeof(value),
240 "system.nt_sec_desc.group%s:%s",
241 numeric ? "" : "+", the_acl);
242 the_acl = value;
243 debugstr = "change group";
244 goto do_set;
246 case SMB_ACL_SET:
247 flags = 0;
248 debugstr = "set attributes";
250 do_set:
251 if ((p = strchr(the_acl, ':')) == NULL)
253 printf("Missing value. ACL must be name:value pair\n");
254 return 1;
257 *p++ = '\0';
259 ret = smbc_setxattr(path, the_acl, p, strlen(p), flags);
260 if (ret < 0)
262 printf("Could not %s for [%s] %d: %s\n",
263 debugstr, path, errno, strerror(errno));
264 return 1;
266 break;
268 case SMB_ACL_DELETE:
269 ret = smbc_removexattr(path, the_acl);
270 if (ret < 0)
272 printf("Could not remove attribute %s for [%s] %d:%s\n",
273 the_acl, path, errno, strerror(errno));
274 return 1;
276 break;
278 default:
279 printf("operation not yet implemented\n");
280 break;
283 return 0;