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