1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include "config.h"
#include <assert.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <grp.h>
#define TEST_GROUP "test/test.group"
static char grfile[] = BASEDIR "/" TEST_GROUP;
#define ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
#define ALIGN(x, a) ALIGN_MASK(x, (typeof(x))(a) - 1)
static int test_getgrent_r(FILE *file, struct group *grp, char *buf,
size_t buflen, struct group **result)
{
char *line, *str, *remain;
int count, index = 0;
int gr_mem_cnt = 0;
*result = NULL;
line = fgets(buf, buflen, file);
if (!line)
return 0;
/* We'll stuff the gr_mem array in the remaining space in the buffer */
remain = buf + ALIGN(line + strlen(line) - buf, sizeof(char *));
grp->gr_mem = (char **)remain;
count = (buf + buflen - remain) / sizeof (char *);
if (!count) {
errno = ERANGE;
return -1;
}
grp->gr_mem[--count] = NULL;
while ((str = strtok(line, ":"))) {
char *ptr;
switch (index++) {
case 0:
grp->gr_name = str;
break;
case 1:
grp->gr_passwd = str;
break;
case 2:
errno = 0;
grp->gr_gid = strtol(str, NULL, 10);
if (errno)
return -1;
break;
case 3:
while ((str = strtok_r(str, ",", &ptr))) {
if (count-- <= 0) {
errno = ERANGE;
return -1;
}
grp->gr_mem[gr_mem_cnt++] = str;
str = NULL;
}
}
line = NULL;
}
*result = grp;
return 0;
}
static int test_getgr_match(struct group *grp, char *buf, size_t buflen,
struct group **result,
int (*match)(const struct group *, const void *),
const void *data)
{
FILE *file;
struct group *_result;
*result = NULL;
file = fopen(grfile, "r");
if (!file) {
errno = EBADF;
return -1;
}
errno = 0;
while (!test_getgrent_r(file, grp, buf, buflen, &_result)) {
if (!_result)
break;
else if (match(grp, data)) {
*result = grp;
break;
}
}
fclose(file);
if (!errno && !*result)
errno = ENOENT;
if (errno)
return -1;
return 0;
}
static int match_name(const struct group *grp, const void *data)
{
const char *name = data;
return !strcmp(grp->gr_name, name);
}
EXPORT
int getgrnam_r(const char *name, struct group *grp, char *buf, size_t buflen,
struct group **result)
{
static size_t last_buflen = -1;
assert(last_buflen == -1 || buflen > last_buflen);
if (buflen < 170000) {
last_buflen = buflen;
*result = NULL;
return ERANGE;
}
last_buflen = -1;
return test_getgr_match(grp, buf, buflen, result, match_name, name);
}
EXPORT
struct group *getgrnam(const char *name)
{
static char buf[16384];
static struct group grp;
struct group *result;
(void) getgrnam_r(name, &grp, buf, sizeof(buf), &result);
return result;
}
static int match_gid(const struct group *grp, const void *data)
{
gid_t gid = *(gid_t *)data;
return grp->gr_gid == gid;
}
EXPORT
int getgrgid_r(gid_t gid, struct group *grp, char *buf, size_t buflen,
struct group **result)
{
return test_getgr_match(grp, buf, buflen, result, match_gid, &gid);
}
EXPORT
struct group *getgrgid(gid_t gid)
{
static char buf[16384];
static struct group grp;
struct group *result;
(void) getgrgid_r(gid, &grp, buf, sizeof(buf), &result);
return result;
}
|