1 /*- 2 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc. 3 * Copyright (c) 2002 Poul-Henning Kamp. 4 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson 5 * All rights reserved. 6 * 7 * This software was developed for the FreeBSD Project by Poul-Henning 8 * Kamp and Network Associates Laboratories, the Security Research Division 9 * of Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 10 * ("CBOSS"), as part of the DARPA CHATS research program 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. The names of the authors may not be used to endorse or promote 21 * products derived from this software without specific prior written 22 * permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * $FreeBSD$ 37 */ 38 39 #include <sys/types.h> 40 #include <sys/uio.h> 41 #include <sys/extattr.h> 42 43 #include <libgen.h> 44 #include <libutil.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include <vis.h> 50 #include <err.h> 51 #include <errno.h> 52 53 static enum { EADUNNO, EAGET, EASET, EARM, EALS } what = EADUNNO; 54 55 static void __dead2 56 usage(void) 57 { 58 59 switch (what) { 60 case EAGET: 61 fprintf(stderr, "usage: getextattr [-fhqsx] attrnamespace"); 62 fprintf(stderr, " attrname filename ...\n"); 63 exit(-1); 64 case EASET: 65 fprintf(stderr, "usage: setextattr [-fhnq] attrnamespace"); 66 fprintf(stderr, " attrname attrvalue filename ...\n"); 67 exit(-1); 68 case EARM: 69 fprintf(stderr, "usage: rmextattr [-fhq] attrnamespace"); 70 fprintf(stderr, " attrname filename ...\n"); 71 exit(-1); 72 case EALS: 73 fprintf(stderr, "usage: lsextattr [-fhq] attrnamespace"); 74 fprintf(stderr, " filename ...\n"); 75 exit(-1); 76 case EADUNNO: 77 default: 78 fprintf(stderr, "usage: (getextattr|lsextattr|rmextattr"); 79 fprintf(stderr, "|setextattr)\n"); 80 exit (-1); 81 } 82 } 83 84 static void 85 mkbuf(char **buf, int *oldlen, int newlen) 86 { 87 88 if (*oldlen >= newlen) 89 return; 90 if (*buf != NULL) 91 free(*buf); 92 *buf = malloc(newlen); 93 if (*buf == NULL) 94 err(1, "malloc"); 95 *oldlen = newlen; 96 return; 97 } 98 99 int 100 main(int argc, char *argv[]) 101 { 102 char *buf, *visbuf, *p; 103 104 const char *options, *attrname; 105 int buflen, visbuflen, ch, error, i, arg_counter, attrnamespace, 106 minargc; 107 108 int flag_force = 0; 109 int flag_nofollow = 0; 110 int flag_null = 0; 111 int flag_quiet = 0; 112 int flag_string = 0; 113 int flag_hex = 0; 114 115 visbuflen = buflen = 0; 116 visbuf = buf = NULL; 117 118 p = basename(argv[0]); 119 if (p == NULL) 120 p = argv[0]; 121 if (!strcmp(p, "getextattr")) { 122 what = EAGET; 123 options = "fhqsx"; 124 minargc = 3; 125 } else if (!strcmp(p, "setextattr")) { 126 what = EASET; 127 options = "fhnq"; 128 minargc = 4; 129 } else if (!strcmp(p, "rmextattr")) { 130 what = EARM; 131 options = "fhq"; 132 minargc = 3; 133 } else if (!strcmp(p, "lsextattr")) { 134 what = EALS; 135 options = "fhq"; 136 minargc = 2; 137 } else { 138 usage(); 139 } 140 141 while ((ch = getopt(argc, argv, options)) != -1) { 142 switch (ch) { 143 case 'f': 144 flag_force = 1; 145 break; 146 case 'h': 147 flag_nofollow = 1; 148 break; 149 case 'n': 150 flag_null = 1; 151 break; 152 case 'q': 153 flag_quiet = 1; 154 break; 155 case 's': 156 flag_string = 1; 157 break; 158 case 'x': 159 flag_hex = 1; 160 break; 161 case '?': 162 default: 163 usage(); 164 } 165 } 166 167 argc -= optind; 168 argv += optind; 169 170 if (argc < minargc) 171 usage(); 172 173 error = extattr_string_to_namespace(argv[0], &attrnamespace); 174 if (error) 175 err(-1, argv[0]); 176 argc--; argv++; 177 178 if (what != EALS) { 179 attrname = argv[0]; 180 argc--; argv++; 181 } else 182 attrname = NULL; 183 184 if (what == EASET) { 185 mkbuf(&buf, &buflen, strlen(argv[0]) + 1); 186 strcpy(buf, argv[0]); 187 argc--; argv++; 188 } 189 190 for (arg_counter = 0; arg_counter < argc; arg_counter++) { 191 switch (what) { 192 case EARM: 193 if (flag_nofollow) 194 error = extattr_delete_link(argv[arg_counter], 195 attrnamespace, attrname); 196 else 197 error = extattr_delete_file(argv[arg_counter], 198 attrnamespace, attrname); 199 if (error >= 0) 200 continue; 201 break; 202 case EASET: 203 if (flag_nofollow) 204 error = extattr_set_link(argv[arg_counter], 205 attrnamespace, attrname, buf, 206 strlen(buf) + flag_null); 207 else 208 error = extattr_set_file(argv[arg_counter], 209 attrnamespace, attrname, buf, 210 strlen(buf) + flag_null); 211 if (error >= 0) 212 continue; 213 break; 214 case EALS: 215 if (flag_nofollow) 216 error = extattr_list_link(argv[arg_counter], 217 attrnamespace, NULL, 0); 218 else 219 error = extattr_list_file(argv[arg_counter], 220 attrnamespace, NULL, 0); 221 if (error < 0) 222 break; 223 mkbuf(&buf, &buflen, error); 224 if (flag_nofollow) 225 error = extattr_list_link(argv[arg_counter], 226 attrnamespace, buf, buflen); 227 else 228 error = extattr_list_file(argv[arg_counter], 229 attrnamespace, buf, buflen); 230 if (error < 0) 231 break; 232 if (!flag_quiet) 233 printf("%s\t", argv[arg_counter]); 234 for (i = 0; i < error; i += buf[i] + 1) 235 printf("%s%*.*s", i ? "\t" : "", 236 buf[i], buf[i], buf + i + 1); 237 printf("\n"); 238 continue; 239 case EAGET: 240 if (flag_nofollow) 241 error = extattr_get_link(argv[arg_counter], 242 attrnamespace, attrname, NULL, 0); 243 else 244 error = extattr_get_file(argv[arg_counter], 245 attrnamespace, attrname, NULL, 0); 246 if (error < 0) 247 break; 248 mkbuf(&buf, &buflen, error); 249 if (flag_nofollow) 250 error = extattr_get_link(argv[arg_counter], 251 attrnamespace, attrname, buf, buflen); 252 else 253 error = extattr_get_file(argv[arg_counter], 254 attrnamespace, attrname, buf, buflen); 255 if (error < 0) 256 break; 257 if (!flag_quiet) 258 printf("%s\t", argv[arg_counter]); 259 if (flag_string) { 260 mkbuf(&visbuf, &visbuflen, error * 4 + 1); 261 strvisx(visbuf, buf, error, 262 VIS_SAFE | VIS_WHITE); 263 printf("\"%s\"\n", visbuf); 264 continue; 265 } else if (flag_hex) { 266 for (i = 0; i < error; i++) 267 printf("%s%02x", i ? " " : "", 268 buf[i]); 269 printf("\n"); 270 continue; 271 } else { 272 fwrite(buf, buflen, 1, stdout); 273 printf("\n"); 274 continue; 275 } 276 default: 277 break; 278 } 279 if (!flag_quiet) 280 warn("%s: failed", argv[arg_counter]); 281 if (flag_force) 282 continue; 283 return(1); 284 } 285 return (0); 286 } 287