Remove use of deprecated function
[Samba/gebeck_regimport.git] / examples / libsmbclient / testacl.c
blob00e1c2c9da9c838cee7ba9b73900aada4cd4b45f
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 full_time_names = 0;
28 enum acl_mode mode = SMB_ACL_LIST;
29 static char *the_acl = NULL;
30 int ret;
31 char *p;
32 char *debugstr;
33 char path[1024];
34 char value[1024];
35 poptContext pc;
36 struct poptOption long_options[] =
38 POPT_AUTOHELP
40 "numeric", 'n', POPT_ARG_NONE, &numeric,
41 1, "Don't resolve sids or masks to names"
44 "debug", 'd', POPT_ARG_INT, &debug,
45 0, "Set debug level (0-100)"
48 "full_time_names", 'f', POPT_ARG_NONE, &full_time_names,
50 "Use new style xattr names, which include CREATE_TIME"
53 "delete", 'D', POPT_ARG_STRING, NULL,
54 'D', "Delete an acl", "ACL"
57 "modify", 'M', POPT_ARG_STRING, NULL,
58 'M', "Modify an acl", "ACL"
61 "add", 'a', POPT_ARG_STRING, NULL,
62 'a', "Add an acl", "ACL"
65 "set", 'S', POPT_ARG_STRING, NULL,
66 'S', "Set acls", "ACLS"
69 "chown", 'C', POPT_ARG_STRING, NULL,
70 'C', "Change ownership of a file", "USERNAME"
73 "chgrp", 'G', POPT_ARG_STRING, NULL,
74 'G', "Change group ownership of a file", "GROUPNAME"
77 "get", 'g', POPT_ARG_STRING, NULL,
78 'g', "Get a specific acl attribute", "ACL"
81 NULL
85 setbuf(stdout, NULL);
87 pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
89 poptSetOtherOptionHelp(pc, "smb://server1/share1/filename");
91 while ((opt = poptGetNextOpt(pc)) != -1) {
92 switch (opt) {
93 case 'S':
94 the_acl = strdup(poptGetOptArg(pc));
95 mode = SMB_ACL_SET;
96 break;
98 case 'D':
99 the_acl = strdup(poptGetOptArg(pc));
100 mode = SMB_ACL_DELETE;
101 break;
103 case 'M':
104 the_acl = strdup(poptGetOptArg(pc));
105 mode = SMB_ACL_MODIFY;
106 break;
108 case 'a':
109 the_acl = strdup(poptGetOptArg(pc));
110 mode = SMB_ACL_ADD;
111 break;
113 case 'g':
114 the_acl = strdup(poptGetOptArg(pc));
115 mode = SMB_ACL_GET;
116 break;
118 case 'C':
119 the_acl = strdup(poptGetOptArg(pc));
120 mode = SMB_ACL_CHOWN;
121 break;
123 case 'G':
124 the_acl = strdup(poptGetOptArg(pc));
125 mode = SMB_ACL_CHGRP;
126 break;
130 /* Make connection to server */
131 if(!poptPeekArg(pc)) {
132 poptPrintUsage(pc, stderr, 0);
133 return 1;
136 strcpy(path, poptGetArg(pc));
138 if (smbc_init(get_auth_data_fn, debug) != 0)
140 printf("Could not initialize smbc_ library\n");
141 return 1;
144 if (full_time_names) {
145 SMBCCTX *context = smbc_set_context(NULL);
146 smbc_setOptionFullTimeNames(context, 1);
149 /* Perform requested action */
151 switch(mode)
153 case SMB_ACL_LIST:
154 ret = smbc_listxattr(path, value, sizeof(value)-2);
155 if (ret < 0)
157 printf("Could not get attribute list for [%s] %d: %s\n",
158 path, errno, strerror(errno));
159 return 1;
163 * The list of attributes has a series of null-terminated strings.
164 * The list of strings terminates with an extra null byte, thus two in
165 * a row. Ensure that our buffer, which is conceivably shorter than
166 * the list of attributes, actually ends with two null bytes in a row.
168 value[sizeof(value) - 2] = '\0';
169 value[sizeof(value) - 1] = '\0';
170 printf("Supported attributes:\n");
171 for (p = value; *p; p += strlen(p) + 1)
173 printf("\t%s\n", p);
175 break;
177 case SMB_ACL_GET:
178 if (the_acl == NULL)
180 if (numeric)
182 the_acl = "system.*";
184 else
186 the_acl = "system.*+";
189 ret = smbc_getxattr(path, the_acl, value, sizeof(value));
190 if (ret < 0)
192 printf("Could not get attributes for [%s] %d: %s\n",
193 path, errno, strerror(errno));
194 return 1;
197 printf("Attributes for [%s] are:\n%s\n", path, value);
198 break;
200 case SMB_ACL_ADD:
201 flags = SMBC_XATTR_FLAG_CREATE;
202 debugstr = "add attributes";
203 goto do_set;
205 case SMB_ACL_MODIFY:
206 flags = SMBC_XATTR_FLAG_REPLACE;
207 debugstr = "modify attributes";
208 goto do_set;
210 case SMB_ACL_CHOWN:
211 snprintf(value, sizeof(value),
212 "system.nt_sec_desc.owner%s:%s",
213 numeric ? "" : "+", the_acl);
214 the_acl = value;
215 debugstr = "chown owner";
216 goto do_set;
218 case SMB_ACL_CHGRP:
219 snprintf(value, sizeof(value),
220 "system.nt_sec_desc.group%s:%s",
221 numeric ? "" : "+", the_acl);
222 the_acl = value;
223 debugstr = "change group";
224 goto do_set;
226 case SMB_ACL_SET:
227 flags = 0;
228 debugstr = "set attributes";
230 do_set:
231 if ((p = strchr(the_acl, ':')) == NULL)
233 printf("Missing value. ACL must be name:value pair\n");
234 return 1;
237 *p++ = '\0';
239 ret = smbc_setxattr(path, the_acl, p, strlen(p), flags);
240 if (ret < 0)
242 printf("Could not %s for [%s] %d: %s\n",
243 debugstr, path, errno, strerror(errno));
244 return 1;
246 break;
248 case SMB_ACL_DELETE:
249 ret = smbc_removexattr(path, the_acl);
250 if (ret < 0)
252 printf("Could not remove attribute %s for [%s] %d:%s\n",
253 the_acl, path, errno, strerror(errno));
254 return 1;
256 break;
258 default:
259 printf("operation not yet implemented\n");
260 break;
263 return 0;