1 /* lookup_table.c -- 2 * Copyright 2009 Red Hat Inc., Durham, North Carolina. 3 * All Rights Reserved. 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * 19 * Authors: 20 * Steve Grubb <sgrubb (at) redhat.com> 21 */ 22 23 #include "config.h" 24 #include <stddef.h> 25 #include <linux/capability.h> 26 #include <strings.h> 27 28 29 #ifndef CAP_LAST_CAP 30 #define CAP_LAST_CAP CAP_AUDIT_CONTROL 31 #endif 32 #undef cap_valid 33 #define cap_valid(x) ((x) <= CAP_LAST_CAP) 34 35 36 struct transtab { 37 int value; 38 int offset; 39 }; 40 41 #define MSGSTRFIELD(line) MSGSTRFIELD1(line) 42 #define MSGSTRFIELD1(line) str##line 43 44 45 /* To create the following tables in a DSO-friendly way we split them in 46 two separate variables: a long string which is created by concatenating 47 all strings referenced in the table and the table itself, which uses 48 offsets instead of string pointers. To do this without increasing 49 the maintenance burden we use a lot of preprocessor magic. All the 50 maintainer has to do is to add a new entry to the included file and 51 recompile. */ 52 53 static const union captab_msgstr_t { 54 struct { 55 #define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof (s)]; 56 #include "captab.h" 57 #undef _S 58 }; 59 char str[0]; 60 } captab_msgstr = { { 61 #define _S(n, s) s, 62 #include "captab.h" 63 #undef _S 64 } }; 65 static const struct transtab captab[] = { 66 #define _S(n, s) { n, offsetof(union captab_msgstr_t, \ 67 MSGSTRFIELD(__LINE__)) }, 68 #include "captab.h" 69 #undef _S 70 }; 71 #define CAP_NG_CAPABILITY_NAMES (sizeof(captab)/sizeof(captab[0])) 72 73 74 75 76 static int capng_lookup_name(const struct transtab *table, 77 const char *tabstr, size_t length, const char *name) 78 { 79 size_t i; 80 81 for (i = 0; i < length; i++) { 82 if (!strcasecmp(tabstr + table[i].offset, name)) 83 return table[i].value; 84 } 85 return -1; 86 } 87 88 static const char *capng_lookup_number(const struct transtab *table, 89 const char *tabstr, size_t length, 90 int number) 91 { 92 size_t i; 93 94 for (i = 0; i < length; i++) { 95 if (table[i].value == number) 96 return tabstr + table[i].offset; 97 } 98 return NULL; 99 } 100 101 int capng_name_to_capability(const char *name) 102 { 103 return capng_lookup_name(captab, captab_msgstr.str, 104 CAP_NG_CAPABILITY_NAMES, name); 105 } 106 107 const char *capng_capability_to_name(unsigned int capability) 108 { 109 if (!cap_valid(capability)) 110 return NULL; 111 112 return capng_lookup_number(captab, captab_msgstr.str, 113 CAP_NG_CAPABILITY_NAMES, capability); 114 } 115 116