CVE-2020-1472(ZeroLogon): libcli/auth: add netlogon_creds_random_challenge()
[Samba.git] / source3 / utils / mvxattr.c
blobd15025b800f2fc6cc40fa84c977f90f7436b2492
1 /*
2 Unix SMB/CIFS implementation.
3 xattr renaming
4 Copyright (C) Ralph Boehme 2017
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "popt_common.h"
23 #include <ftw.h>
25 static struct rename_xattr_state {
26 int follow_symlink;
27 int print;
28 int force;
29 int verbose;
30 char *xattr_from;
31 char *xattr_to;
32 } state;
34 static int rename_xattr(const char *path,
35 const struct stat *sb,
36 int typeflag,
37 struct FTW *ftwbuf)
39 ssize_t len;
40 int ret;
42 if (typeflag == FTW_SL) {
43 d_printf("Ignoring symlink %s\n", path);
44 return 0;
47 if (state.verbose) {
48 d_printf("%s\n", path);
51 len = getxattr(path, state.xattr_from, NULL, 0);
52 if (len < 0) {
53 if (errno == ENOATTR) {
54 return 0;
56 d_printf("getxattr [%s] failed [%s]\n",
57 path, strerror(errno));
58 return -1;
62 uint8_t buf[len];
64 len = getxattr(path, state.xattr_from, &buf[0], len);
65 if (len == -1) {
66 d_printf("getxattr [%s] failed [%s]\n",
67 path, strerror(errno));
68 return -1;
71 ret = setxattr(path, state.xattr_to, &buf[0], len, XATTR_CREATE);
72 if (ret != 0) {
73 if (errno != EEXIST) {
74 d_printf("setxattr [%s] failed [%s]\n",
75 path, strerror(errno));
76 return -1;
78 if (!state.force) {
79 d_printf("destination [%s:%s] exists, use -f to force\n",
80 path, state.xattr_to);
81 return -1;
83 ret = setxattr(path, state.xattr_to, &buf[0], len, XATTR_REPLACE);
84 if (ret != 0) {
85 d_printf("setxattr [%s:%s] failed [%s]\n",
86 path, state.xattr_to, strerror(errno));
87 return -1;
91 ret = removexattr(path, state.xattr_from);
92 if (ret != 0) {
93 d_printf("removexattr [%s:%s] failed [%s]\n",
94 path, state.xattr_from, strerror(errno));
95 return -1;
98 if (state.print) {
99 d_printf("Renamed %s to %s on %s\n",
100 state.xattr_from, state.xattr_to, path);
104 return 0;
107 int main(int argc, const char *argv[])
109 int c;
110 const char *path = NULL;
111 poptContext pc = NULL;
112 struct poptOption long_options[] = {
113 POPT_AUTOHELP
115 .longName = "from",
116 .shortName = 's',
117 .argInfo = POPT_ARG_STRING,
118 .arg = &state.xattr_from,
119 .val = 's',
120 .descrip = "xattr source name",
123 .longName = "to",
124 .shortName = 'd',
125 .argInfo = POPT_ARG_STRING,
126 .arg = &state.xattr_to,
127 .val = 'd',
128 .descrip = "xattr destination name",
131 .longName = "follow-symlinks",
132 .shortName = 'l',
133 .argInfo = POPT_ARG_NONE,
134 .arg = &state.follow_symlink,
135 .val = 'l',
136 .descrip = "follow symlinks, the default is to "
137 "ignore them",
140 .longName = "print",
141 .shortName = 'p',
142 .argInfo = POPT_ARG_NONE,
143 .arg = &state.print,
144 .val = 'p',
145 .descrip = "print files where the xattr got "
146 "renamed",
149 .longName = "verbose",
150 .shortName = 'v',
151 .argInfo = POPT_ARG_NONE,
152 .arg = &state.verbose,
153 .val = 'v',
154 .descrip = "print files as they are checked",
157 .longName = "force",
158 .shortName = 'f',
159 .argInfo = POPT_ARG_NONE,
160 .arg = &state.force,
161 .val = 'f',
162 .descrip = "force overwriting of destination xattr",
164 POPT_TABLEEND
166 TALLOC_CTX *frame = talloc_stackframe();
167 const char *s = NULL;
168 int ret = 0;
170 if (getuid() != 0) {
171 d_printf("%s only works as root!\n", argv[0]);
172 ret = 1;
173 goto done;
176 pc = poptGetContext(NULL, argc, argv, long_options, 0);
177 poptSetOtherOptionHelp(pc, "-s STRING -d STRING PATH [PATH ...]");
179 while ((c = poptGetNextOpt(pc)) != -1) {
180 switch (c) {
181 case 's':
182 s = poptGetOptArg(pc);
183 state.xattr_from = talloc_strdup(frame, s);
184 if (state.xattr_from == NULL) {
185 ret = 1;
186 goto done;
188 break;
189 case 'd':
190 s = poptGetOptArg(pc);
191 state.xattr_to = talloc_strdup(frame, s);
192 if (state.xattr_to == NULL) {
193 ret = 1;
194 goto done;
196 break;
200 if (state.xattr_from == NULL || state.xattr_to == NULL) {
201 poptPrintUsage(pc, stderr, 0);
202 ret = 1;
203 goto done;
206 if (poptPeekArg(pc) == NULL) {
207 poptPrintUsage(pc, stderr, 0);
208 ret = 1;
209 goto done;
212 while ((path = poptGetArg(pc)) != NULL) {
213 ret = nftw(path, rename_xattr, 256,
214 state.follow_symlink ? 0 : FTW_PHYS);
217 done:
218 poptFreeContext(pc);
220 TALLOC_FREE(frame);
221 return ret;