1 /*- 2 * Copyright (c) 2002 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 [-fhq] 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_quiet = 0; 111 int flag_string = 0; 112 int flag_hex = 0; 113 114 visbuflen = buflen = 0; 115 visbuf = buf = NULL; 116 117 p = basename(argv[0]); 118 if (p == NULL) 119 p = argv[0]; 120 if (!strcmp(p, "getextattr")) { 121 what = EAGET; 122 options = "fhqsx"; 123 minargc = 3; 124 } else if (!strcmp(p, "setextattr")) { 125 what = EASET; 126 options = "fhq"; 127 minargc = 4; 128 } else if (!strcmp(p, "rmextattr")) { 129 what = EARM; 130 options = "fhq"; 131 minargc = 3; 132 } else if (!strcmp(p, "lsextattr")) { 133 what = EALS; 134 options = "fhq"; 135 minargc = 2; 136 } else { 137 usage(); 138 } 139 140 while ((ch = getopt(argc, argv, options)) != -1) { 141 switch (ch) { 142 case 'f': 143 flag_force = 1; 144 break; 145 case 'h': 146 flag_nofollow = 1; 147 break; 148 case 'q': 149 flag_quiet = 1; 150 break; 151 case 's': 152 flag_string = 1; 153 break; 154 case 'x': 155 flag_hex = 1; 156 break; 157 case '?': 158 default: 159 usage(); 160 } 161 } 162 163 argc -= optind; 164 argv += optind; 165 166 if (argc < minargc) 167 usage(); 168 169 error = extattr_string_to_namespace(argv[0], &attrnamespace); 170 if (error) 171 err(-1, argv[0]); 172 argc--; argv++; 173 174 if (what == EALS) { 175 attrname = ""; 176 } else { 177 attrname = argv[0]; 178 argc--; argv++; 179 } 180 181 if (what == EASET) { 182 mkbuf(&buf, &buflen, strlen(argv[0]) + 1); 183 strcpy(buf, argv[0]); 184 argc--; argv++; 185 } 186 187 for (arg_counter = 0; arg_counter < argc; arg_counter++) { 188 switch (what) { 189 case EARM: 190 if (flag_nofollow) 191 error = extattr_delete_link(argv[arg_counter], 192 attrnamespace, attrname); 193 else 194 error = extattr_delete_file(argv[arg_counter], 195 attrnamespace, attrname); 196 if (error >= 0) 197 continue; 198 break; 199 case EASET: 200 if (flag_nofollow) 201 error = extattr_set_link(argv[arg_counter], 202 attrnamespace, attrname, buf, strlen(buf)); 203 else 204 error = extattr_set_file(argv[arg_counter], 205 attrnamespace, attrname, buf, strlen(buf)); 206 if (error >= 0) 207 continue; 208 break; 209 case EALS: 210 case EAGET: 211 if (flag_nofollow) 212 error = extattr_get_link(argv[arg_counter], 213 attrnamespace, attrname, NULL, 0); 214 else 215 error = extattr_get_file(argv[arg_counter], 216 attrnamespace, attrname, NULL, 0); 217 if (error < 0) 218 break; 219 mkbuf(&buf, &buflen, error); 220 if (flag_nofollow) 221 error = extattr_get_link(argv[arg_counter], 222 attrnamespace, attrname, buf, buflen); 223 else 224 error = extattr_get_file(argv[arg_counter], 225 attrnamespace, attrname, buf, buflen); 226 if (error < 0) 227 break; 228 if (!flag_quiet) 229 printf("%s\t", argv[arg_counter]); 230 if (what == EALS) { 231 for (i = 0; i < error; i += buf[i] + 1) 232 printf("%s%*.*s", i ? "\t" : "", 233 buf[i], buf[i], buf + i + 1); 234 printf("\n"); 235 continue; 236 } 237 if (flag_string) { 238 mkbuf(&visbuf, &visbuflen, error * 4 + 1); 239 strvisx(visbuf, buf, error, 240 VIS_SAFE | VIS_WHITE); 241 printf("\"%s\"\n", visbuf); 242 continue; 243 } else if (flag_hex) { 244 for (i = 0; i < error; i++) 245 printf("%s%02x", i ? " " : "", 246 buf[i]); 247 printf("\n"); 248 continue; 249 } else { 250 fwrite(buf, buflen, 1, stdout); 251 printf("\n"); 252 continue; 253 } 254 default: 255 break; 256 } 257 if (!flag_quiet) 258 warn("%s: failed", argv[arg_counter]); 259 if (flag_force) 260 continue; 261 return(1); 262 } 263 return (0); 264 } 265