|
| 1 | +/* |
| 2 | + * Copyright (C) 2015-2020 Leah Neukirchen |
| 3 | + * Parts of code derived from musl libc, which is |
| 4 | + * Copyright (C) 2005-2014 Rich Felker, et al. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | + * a copy of this software and associated documentation files (the |
| 8 | + * "Software"), to deal in the Software without restriction, including |
| 9 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + * permit persons to whom the Software is furnished to do so, subject to |
| 12 | + * the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be |
| 15 | + * included in all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 | + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 | + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include <sys/types.h> |
| 27 | +#include <pwd.h> |
| 28 | +#include <grp.h> |
| 29 | + |
| 30 | +/* AA-tree implementation, adapted from https://github.com/ccxvii/minilibs */ |
| 31 | +struct idtree { |
| 32 | + long id; |
| 33 | + char *name; |
| 34 | + struct idtree *left, *right; |
| 35 | + int level; |
| 36 | +}; |
| 37 | + |
| 38 | +static struct idtree idtree_sentinel = { 0, 0, &idtree_sentinel, &idtree_sentinel, 0 }; |
| 39 | + |
| 40 | +static struct idtree * |
| 41 | +idtree_make(long id, char *name) |
| 42 | +{ |
| 43 | + struct idtree *node = malloc(sizeof (struct idtree)); |
| 44 | + node->id = id; |
| 45 | + node->name = name; |
| 46 | + node->left = node->right = &idtree_sentinel; |
| 47 | + node->level = 1; |
| 48 | + return node; |
| 49 | +} |
| 50 | + |
| 51 | +char * |
| 52 | +idtree_lookup(struct idtree *node, long id) |
| 53 | +{ |
| 54 | + if (node) { |
| 55 | + while (node != &idtree_sentinel) { |
| 56 | + if (id == node->id) |
| 57 | + return node->name; |
| 58 | + else if (id < node->id) |
| 59 | + node = node->left; |
| 60 | + else |
| 61 | + node = node->right; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
| 67 | + |
| 68 | +static struct idtree * |
| 69 | +idtree_skew(struct idtree *node) |
| 70 | +{ |
| 71 | + if (node->left->level == node->level) { |
| 72 | + struct idtree *save = node; |
| 73 | + node = node->left; |
| 74 | + save->left = node->right; |
| 75 | + node->right = save; |
| 76 | + } |
| 77 | + return node; |
| 78 | +} |
| 79 | + |
| 80 | +static struct idtree * |
| 81 | +idtree_split(struct idtree *node) |
| 82 | +{ |
| 83 | + if (node->right->right->level == node->level) { |
| 84 | + struct idtree *save = node; |
| 85 | + node = node->right; |
| 86 | + save->right = node->left; |
| 87 | + node->left = save; |
| 88 | + node->level++; |
| 89 | + } |
| 90 | + return node; |
| 91 | +} |
| 92 | + |
| 93 | +struct idtree * |
| 94 | +idtree_insert(struct idtree *node, long id, char *name) |
| 95 | +{ |
| 96 | + if (node && node != &idtree_sentinel) { |
| 97 | + if (id == node->id) |
| 98 | + return node; |
| 99 | + else if (id < node->id) |
| 100 | + node->left = idtree_insert(node->left, id, name); |
| 101 | + else |
| 102 | + node->right = idtree_insert(node->right, id, name); |
| 103 | + node = idtree_skew(node); |
| 104 | + node = idtree_split(node); |
| 105 | + return node; |
| 106 | + } |
| 107 | + return idtree_make(id, name); |
| 108 | +} |
| 109 | +/**/ |
| 110 | + |
| 111 | +static char * |
| 112 | +strid(long id) |
| 113 | +{ |
| 114 | + static char buf[32]; |
| 115 | + snprintf(buf, sizeof buf, "%ld", id); |
| 116 | + return buf; |
| 117 | +} |
| 118 | + |
| 119 | +static char * |
| 120 | +groupname(struct idtree *groups, gid_t gid) |
| 121 | +{ |
| 122 | + char *name = idtree_lookup(groups, gid); |
| 123 | + |
| 124 | + if (name) |
| 125 | + return name; |
| 126 | + |
| 127 | + struct group *g = getgrgid(gid); |
| 128 | + if (g) { |
| 129 | + char *name = strdup(g->gr_name); |
| 130 | + groups = idtree_insert(groups, gid, name); |
| 131 | + return name; |
| 132 | + } |
| 133 | + |
| 134 | + return strid(gid); |
| 135 | +} |
| 136 | + |
| 137 | +static char * |
| 138 | +username(struct idtree *users, uid_t uid) |
| 139 | +{ |
| 140 | + char *name = idtree_lookup(users, uid); |
| 141 | + |
| 142 | + if (name) |
| 143 | + return name; |
| 144 | + |
| 145 | + struct passwd *p = getpwuid(uid); |
| 146 | + if (p) { |
| 147 | + char *name = strdup(p->pw_name); |
| 148 | + users = idtree_insert(users, uid, name); |
| 149 | + return name; |
| 150 | + } |
| 151 | + |
| 152 | + return strid(uid); |
| 153 | +} |
0 commit comments