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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/* The shadow group functions */
#include "../_pwdb_internal.h"
#define MAXMEM 1024
static FILE *shadow;
static char sgrbuf[BUFSIZ*4];
static char *members[MAXMEM+1];
static char *admins[MAXMEM+1];
static struct __pwdb_sgrp sgroup;
#define FIELDS 4
static char ** list (char *s, char **l)
{
int nmembers = 0;
while (s && *s) {
l[nmembers++] = s;
if ((s = strchr (s, ',')))
*s++ = '\0';
}
l[nmembers] = (char *) 0;
return l;
}
void __pwdb_setsgent (void)
{
if (shadow)
rewind (shadow);
else
shadow = fopen(__PWDB_SGROUP_FILE, "r");
return;
}
void __pwdb_endsgent (void)
{
if (shadow)
(void) fclose (shadow);
shadow = (FILE *) 0;
return;
}
struct __pwdb_sgrp * __pwdb_sgetsgent (const char *string)
{
char *fields[FIELDS];
char *cp;
int i;
strncpy (sgrbuf, string, (int) sizeof sgrbuf - 1);
sgrbuf[sizeof sgrbuf - 1] = '\0';
if ((cp = strrchr (sgrbuf, '\n')))
*cp = '\0';
/*
* There should be exactly 4 colon separated fields. Find
* all 4 of them and save the starting addresses in fields[].
*/
for (cp = sgrbuf, i = 0;i < FIELDS && cp;i++) {
fields[i] = cp;
if ((cp = strchr (cp, ':')))
*cp++ = '\0';
}
/*
* If there was an extra field somehow, or perhaps not enough,
* the line is invalid.
*/
if (cp || i != FIELDS)
return 0;
sgroup.sg_name = fields[0];
sgroup.sg_passwd = fields[1];
sgroup.sg_adm = list (fields[2], admins);
sgroup.sg_mem = list (fields[3], members);
return &sgroup;
}
/*
* fgetsgent - convert next line in stream to (struct sgrp)
*
* fgetsgent() reads the next line from the provided stream and
* converts it to a (struct sgrp). NULL is returned on EOF.
*/
struct __pwdb_sgrp * __pwdb_fgetsgent (FILE *fp)
{
char buf[sizeof sgrbuf];
char *cp;
if (! fp)
return (0);
if (__pwdb_fgetsx (buf, sizeof buf, fp) != (char *) 0)
{
if ((cp = strchr (buf, '\n')))
*cp = '\0';
return (__pwdb_sgetsgent (buf));
}
return 0;
}
/*
* getsgent - get a single shadow group entry
*/
struct __pwdb_sgrp * __pwdb_getsgent (void)
{
if (! shadow)
__pwdb_setsgent ();
return (__pwdb_fgetsgent (shadow));
}
/*
* getsgnam - get a shadow group entry by name
*/
struct __pwdb_sgrp * __pwdb_getsgnam (const char *name)
{
struct __pwdb_sgrp *sgrp;
__pwdb_setsgent ();
while ((sgrp = __pwdb_getsgent ()) != (struct __pwdb_sgrp *) 0) {
if (strcmp (name, sgrp->sg_name) == 0)
break;
}
if (sgrp)
return sgrp;
return (0);
}
/*
* putsgent - output shadow group entry in text form
*
* putsgent() converts the contents of a (struct sgrp) to text and
* writes the result to the given stream. This is the logical
* opposite of fgetsgent.
*/
int __pwdb_putsgent (const struct __pwdb_sgrp *sgrp, FILE *fp)
{
int i;
char *cp;
char *buf, *rbuf;
size_t size;
if (! sgrp || ! fp)
return -1;
/* be very safe first */
if (!sgrp->sg_name || !sgrp->sg_passwd)
return -1;
size=1024; /* alloc plenty, avoid reallocs if two small */
if (!(buf = malloc(size)))
return -1;
bzero(buf,size);
sprintf (buf, "%s:%s:", sgrp->sg_name, sgrp->sg_passwd);
cp = buf + strlen(buf);
/*
* Copy the administrators, separating each from the other
* with a ",".
*/
for (i = 0;sgrp->sg_adm[i];i++) {
if ((cp - buf) + strlen (sgrp->sg_adm[i]) + 2 >= size) {
size += size;
rbuf = realloc(buf, size);
if (!rbuf) {
free(buf);
return -1; /* No more hope.. */
}
buf = rbuf;
}
if (i > 0) {
strcpy (cp, ",");
cp++;
}
strcpy (cp, sgrp->sg_adm[i]);
cp += strlen (cp);
}
*cp++ = ':';
/*
* Now do likewise with the group members.
*/
for (i = 0;sgrp->sg_mem[i];i++) {
if ((cp - buf) + strlen (sgrp->sg_mem[i]) + 2 >= size) {
size += size;
rbuf = realloc(buf, size);
if (!rbuf) {
free(buf);
return -1; /* No more hope.. */
}
buf = rbuf;
}
if (i > 0) {
strcpy (cp, ",");
cp++;
}
strcpy (cp, sgrp->sg_mem[i]);
cp += strlen (cp);
}
*cp++ = '\n';
*cp = '\0';
/*
* Output using the function which understands the line
* continuation conventions.
*/
if (__pwdb_fputsx (buf, fp) == EOF || ferror (fp)) {
free(buf);
return -1;
}
free(buf);
return 0;
}
|