s3:torture: make use of auth_generic_set_creds() in test_smb2.c
[Samba.git] / examples / libsmbclient / testacl.c
blob055e38cc70f410b40feba90016fb3ef4013cee8e
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 = 0;
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 strncpy(path, poptGetArg(pc), sizeof(path));
143 path[sizeof(path)-1] = '\0';
145 if (smbc_init(get_auth_data_fn, debug) != 0)
147 printf("Could not initialize smbc_ library\n");
148 return 1;
151 if (full_time_names) {
152 SMBCCTX *context = smbc_set_context(NULL);
153 smbc_setOptionFullTimeNames(context, 1);
156 /* Perform requested action */
158 switch(mode)
160 case SMB_ACL_LIST:
161 ret = smbc_listxattr(path, value, sizeof(value)-2);
162 if (ret < 0)
164 printf("Could not get attribute list for [%s] %d: %s\n",
165 path, errno, strerror(errno));
166 return 1;
170 * The list of attributes has a series of null-terminated strings.
171 * The list of strings terminates with an extra null byte, thus two in
172 * a row. Ensure that our buffer, which is conceivably shorter than
173 * the list of attributes, actually ends with two null bytes in a row.
175 value[sizeof(value) - 2] = '\0';
176 value[sizeof(value) - 1] = '\0';
177 printf("Supported attributes:\n");
178 for (p = value; *p; p += strlen(p) + 1)
180 printf("\t%s\n", p);
182 break;
184 case SMB_ACL_GET:
187 if (the_acl == NULL)
189 if (numeric)
191 the_acl = "system.*";
193 else
195 the_acl = "system.*+";
198 ret = smbc_getxattr(path, the_acl, value, sizeof(value));
199 if (ret < 0)
201 printf("Could not get attributes for [%s] %d: %s\n",
202 path, errno, strerror(errno));
203 return 1;
206 printf("Attributes for [%s] are:\n%s\n", path, value);
208 if (stat_and_retry)
210 if (smbc_stat(path, &st) < 0)
212 perror("smbc_stat");
213 return 1;
217 --stat_and_retry;
218 } while (stat_and_retry >= 0);
219 break;
221 case SMB_ACL_ADD:
222 flags = SMBC_XATTR_FLAG_CREATE;
223 debugstr = "add attributes";
224 goto do_set;
226 case SMB_ACL_MODIFY:
227 flags = SMBC_XATTR_FLAG_REPLACE;
228 debugstr = "modify attributes";
229 goto do_set;
231 case SMB_ACL_CHOWN:
232 snprintf(value, sizeof(value),
233 "system.nt_sec_desc.owner%s:%s",
234 numeric ? "" : "+", the_acl);
235 the_acl = value;
236 debugstr = "chown owner";
237 goto do_set;
239 case SMB_ACL_CHGRP:
240 snprintf(value, sizeof(value),
241 "system.nt_sec_desc.group%s:%s",
242 numeric ? "" : "+", the_acl);
243 the_acl = value;
244 debugstr = "change group";
245 goto do_set;
247 case SMB_ACL_SET:
248 flags = 0;
249 debugstr = "set attributes";
251 do_set:
252 if ((p = strchr(the_acl, ':')) == NULL)
254 printf("Missing value. ACL must be name:value pair\n");
255 return 1;
258 *p++ = '\0';
260 ret = smbc_setxattr(path, the_acl, p, strlen(p), flags);
261 if (ret < 0)
263 printf("Could not %s for [%s] %d: %s\n",
264 debugstr, path, errno, strerror(errno));
265 return 1;
267 break;
269 case SMB_ACL_DELETE:
270 ret = smbc_removexattr(path, the_acl);
271 if (ret < 0)
273 printf("Could not remove attribute %s for [%s] %d:%s\n",
274 the_acl, path, errno, strerror(errno));
275 return 1;
277 break;
279 default:
280 printf("operation not yet implemented\n");
281 break;
284 return 0;