1
/* Copyright (c) 1996, 1999, 2001 Thorsten Kukuk
2
Author: Thorsten Kukuk <kukuk@suse.de>
4
The YP Server is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License
6
version 2 as published by the Free Software Foundation.
8
The YP Server is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
General Public License for more details.
13
You should have received a copy of the GNU General Public
14
License along with the YP Server; see the file COPYING. If
15
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16
Cambridge, MA 02139, USA. */
20
#if defined(HAVE_CONFIG_H)
32
#define TABLESIZE 997 /*Should be a prime */
34
typedef struct hash_liste {
37
struct hash_liste *next;
41
char *xstrtok (char *cp, int delim)
43
static char *str = NULL;
54
while (*str && (!isspace(*str)))
57
while (*str && *str != delim)
66
static inline void *xmalloc(unsigned long size)
74
fprintf(stderr,"ERROR: out of memory!\n");
81
static hash_liste_t* user_liste[TABLESIZE];
82
static hash_liste_t* host_liste[TABLESIZE];
84
static char uid_liste[65535];
91
memset(user_liste, 0, sizeof(user_liste));
92
memset(host_liste, 0, sizeof(host_liste));
93
memset(uid_liste, 0, sizeof(uid_liste));
96
int insert_user(const char *key, const char *domain,
97
const char *uid, const char *gid )
102
if(first) init_table();
109
if(uid_liste[id] == 1)
115
for(i = 0; i < strlen(key); i++)
116
hkey = (256*hkey +key[i]) % TABLESIZE;
118
if(user_liste[hkey] != NULL)
120
hash_liste_t *work, *ptr;
122
work = user_liste[hkey]->next;
123
ptr = user_liste[hkey];
131
ptr->next = xmalloc(sizeof(hash_liste_t));
134
work->key = xmalloc(strlen(key)+1);
135
strcpy(work->key,key);
136
work->val = xmalloc(strlen(domain)+2*strlen(uid)+strlen(gid)+100);
137
sprintf(work->val,"unix.%s@%s\t%s:%s",uid,domain,uid,gid);
141
user_liste[hkey] = xmalloc(sizeof(hash_liste_t));
142
user_liste[hkey]->key = xmalloc(strlen(key)+1);
143
strcpy(user_liste[hkey]->key,key);
144
user_liste[hkey]->next = NULL;
145
user_liste[hkey]->val = xmalloc(strlen(domain)+2*strlen(uid)+strlen(gid)+10);
146
sprintf(user_liste[hkey]->val,"unix.%s@%s\t%s:%s",uid,domain,uid,gid);
151
int add_group(const char *key, const char *grp)
157
if(first) init_table();
160
for(i = 0; i < strlen(key); i++)
161
hkey = (256*hkey +key[i]) % TABLESIZE;
163
if(user_liste[hkey] == NULL)
166
if(strcmp(user_liste[hkey]->key,key)!=0)
168
if(user_liste[hkey]->next == NULL)
172
work=user_liste[hkey]->next;
174
if(strcmp(work->key,key)==0)
183
work = user_liste[hkey];
185
if(strcmp(key,work->key)==0)
189
tmp = strdup(work->val);
190
ptr = xstrtok(tmp,':');
192
while((ptr = xstrtok(NULL,','))!= NULL)
193
if(strcmp(ptr,grp)==0) return 0;
195
ptr = xmalloc(strlen(work->val)+strlen(grp)+5);
196
strcpy(ptr,work->val);
208
int insert_host(const char *host, const char *domain)
213
if(first) init_table();
216
for(i = 0; i < strlen(host); i++)
217
hkey = (256*hkey +host[i]) % TABLESIZE;
219
if(host_liste[hkey] != NULL)
221
if(strcmp(host_liste[hkey]->key,host)==0)
225
hash_liste_t *work, *ptr;
227
work=host_liste[hkey]->next;
228
ptr = host_liste[hkey];
231
if(strcmp(work->key,host)==0)
239
ptr->next = xmalloc(sizeof(hash_liste_t));
242
work->key = strdup(host);
243
work->val = xmalloc(strlen(host)*2+strlen(domain)+20);
244
sprintf(work->val,"unix.%s@%s\t0:%s",host,domain,host);
249
host_liste[hkey] = xmalloc(sizeof(hash_liste_t));
250
host_liste[hkey]->key = strdup(host);
251
host_liste[hkey]->next = NULL;
252
host_liste[hkey]->val = xmalloc(strlen(host)*2+strlen(domain)+20);
253
sprintf(host_liste[hkey]->val,"unix.%s@%s\t0:%s",host,domain,host);
263
for(i = 0; i < TABLESIZE; i++)
265
work = user_liste[i];
268
printf("%s\n",work->val);
273
for(i = 0; i < TABLESIZE; i++)
275
work = host_liste[i];
278
printf("%s\n",work->val);