~ubuntu-branches/ubuntu/oneiric/nis/oneiric-proposed

« back to all changes in this revision

Viewing changes to ypserv-2.18/mknetid/netid_hash.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2005-11-16 23:42:06 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20051116234206-p00omaw5ji5q0qhr
Tags: 3.15-3ubuntu1
Resynchronise with Debian.  (me)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 1996, 1999, 2001 Thorsten Kukuk
 
2
   Author: Thorsten Kukuk <kukuk@suse.de>
 
3
 
 
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.
 
7
 
 
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.
 
12
 
 
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. */
 
17
 
 
18
#define _GNU_SOURCE
 
19
 
 
20
#if defined(HAVE_CONFIG_H)
 
21
#include "config.h"
 
22
#endif
 
23
 
 
24
#include <ctype.h>
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
#include <memory.h>
 
29
 
 
30
#include "mknetid.h"
 
31
 
 
32
#define TABLESIZE 997 /*Should be a prime */
 
33
 
 
34
typedef struct hash_liste {
 
35
  char *key;
 
36
  char *val;
 
37
  struct hash_liste *next;
 
38
} hash_liste_t;
 
39
 
 
40
static
 
41
char *xstrtok (char *cp, int delim)
 
42
{
 
43
    static char *str = NULL;
 
44
 
 
45
    if (cp)
 
46
        str = cp;
 
47
 
 
48
    if (*str == '\0')
 
49
        return NULL;
 
50
 
 
51
    cp = str;
 
52
 
 
53
    if (delim == ' ')
 
54
       while (*str && (!isspace(*str)))
 
55
          str++;
 
56
    else
 
57
       while (*str && *str != delim)
 
58
          str++;
 
59
 
 
60
    if (*str)
 
61
        *str++ = '\0';
 
62
 
 
63
    return cp;
 
64
}
 
65
 
 
66
static inline void *xmalloc(unsigned long size)
 
67
{
 
68
  void *ptr;
 
69
 
 
70
  ptr = malloc(size);
 
71
 
 
72
  if(ptr == NULL)
 
73
    {
 
74
      fprintf(stderr,"ERROR: out of memory!\n");
 
75
      exit(1);
 
76
    }
 
77
 
 
78
  return ptr;
 
79
}
 
80
 
 
81
static hash_liste_t* user_liste[TABLESIZE];
 
82
static hash_liste_t* host_liste[TABLESIZE];
 
83
static int first = 1;
 
84
static char uid_liste[65535];
 
85
 
 
86
static void
 
87
init_table(void)
 
88
{
 
89
  first = 0;
 
90
 
 
91
  memset(user_liste, 0, sizeof(user_liste));
 
92
  memset(host_liste, 0, sizeof(host_liste));
 
93
  memset(uid_liste, 0, sizeof(uid_liste));
 
94
}
 
95
 
 
96
int insert_user(const char *key, const char *domain,
 
97
                const char *uid, const char *gid )
 
98
{
 
99
  long hkey, id;
 
100
  size_t i;
 
101
 
 
102
  if(first) init_table();
 
103
 
 
104
  id = atol(uid);
 
105
 
 
106
  if(id > 65534)
 
107
    return -2;
 
108
 
 
109
  if(uid_liste[id] == 1)
 
110
    return -1;
 
111
  else
 
112
    uid_liste[id] = 1;
 
113
 
 
114
  hkey = 0;
 
115
  for(i = 0; i < strlen(key); i++)
 
116
    hkey = (256*hkey +key[i]) % TABLESIZE;
 
117
 
 
118
  if(user_liste[hkey] != NULL)
 
119
    {
 
120
      hash_liste_t *work, *ptr;
 
121
 
 
122
      work = user_liste[hkey]->next;
 
123
      ptr = user_liste[hkey];
 
124
 
 
125
      while(work != NULL)
 
126
        {
 
127
          ptr = work;
 
128
          work = work->next;
 
129
        }
 
130
 
 
131
      ptr->next = xmalloc(sizeof(hash_liste_t));
 
132
      work = ptr->next;
 
133
      work->next = NULL;
 
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);
 
138
    }
 
139
  else
 
140
    {
 
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);
 
147
    }
 
148
  return 0;
 
149
}
 
150
 
 
151
int add_group(const char *key, const char *grp)
 
152
{
 
153
  hash_liste_t *work;
 
154
  long hkey;
 
155
  size_t i;
 
156
 
 
157
  if(first) init_table();
 
158
 
 
159
  hkey = 0;
 
160
  for(i = 0; i < strlen(key); i++)
 
161
    hkey = (256*hkey +key[i]) % TABLESIZE;
 
162
 
 
163
  if(user_liste[hkey] == NULL)
 
164
    return -1;
 
165
  else
 
166
    if(strcmp(user_liste[hkey]->key,key)!=0)
 
167
      {
 
168
        if(user_liste[hkey]->next == NULL)
 
169
          return -1;
 
170
        else
 
171
          {
 
172
            work=user_liste[hkey]->next;
 
173
            while(work != NULL)
 
174
              if(strcmp(work->key,key)==0)
 
175
                break;
 
176
              else
 
177
                work = work->next;
 
178
            if(work == NULL)
 
179
              return -1;
 
180
          }
 
181
      }
 
182
    else
 
183
      work = user_liste[hkey];
 
184
 
 
185
  if(strcmp(key,work->key)==0)
 
186
    {
 
187
      char *ptr, *tmp;
 
188
 
 
189
      tmp = strdup(work->val);
 
190
      ptr = xstrtok(tmp,':');
 
191
 
 
192
      while((ptr = xstrtok(NULL,','))!= NULL)
 
193
        if(strcmp(ptr,grp)==0) return 0;
 
194
 
 
195
      ptr = xmalloc(strlen(work->val)+strlen(grp)+5);
 
196
      strcpy(ptr,work->val);
 
197
      strcat(ptr,",");
 
198
      strcat(ptr,grp);
 
199
      free(work->val);
 
200
      work->val = ptr;
 
201
 
 
202
      return 0;
 
203
    }
 
204
 
 
205
  return -1;
 
206
}
 
207
 
 
208
int insert_host(const char *host, const char *domain)
 
209
{
 
210
  long hkey;
 
211
  size_t i;
 
212
 
 
213
  if(first) init_table();
 
214
 
 
215
  hkey = 0;
 
216
  for(i = 0; i < strlen(host); i++)
 
217
    hkey = (256*hkey +host[i]) % TABLESIZE;
 
218
 
 
219
  if(host_liste[hkey] != NULL)
 
220
    {
 
221
      if(strcmp(host_liste[hkey]->key,host)==0)
 
222
        return -1;
 
223
      else
 
224
        {
 
225
          hash_liste_t *work, *ptr;
 
226
 
 
227
          work=host_liste[hkey]->next;
 
228
          ptr = host_liste[hkey];
 
229
 
 
230
          while(work != NULL)
 
231
            if(strcmp(work->key,host)==0)
 
232
              return -1;
 
233
            else
 
234
              {
 
235
                ptr = work;
 
236
                work = work->next;
 
237
              }
 
238
 
 
239
          ptr->next = xmalloc(sizeof(hash_liste_t));
 
240
          work = ptr->next;
 
241
          work->next = NULL;
 
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);
 
245
        }
 
246
    }
 
247
  else
 
248
    {
 
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);
 
254
    }
 
255
  return 0;
 
256
}
 
257
 
 
258
void print_table()
 
259
{
 
260
  hash_liste_t *work;
 
261
  unsigned long i;
 
262
 
 
263
  for(i = 0; i < TABLESIZE; i++)
 
264
    {
 
265
      work = user_liste[i];
 
266
      while(work != NULL)
 
267
        {
 
268
          printf("%s\n",work->val);
 
269
          work = work->next;
 
270
        }
 
271
    }
 
272
 
 
273
  for(i = 0; i < TABLESIZE; i++)
 
274
    {
 
275
      work = host_liste[i];
 
276
      while(work != NULL)
 
277
        {
 
278
          printf("%s\n",work->val);
 
279
          work = work->next;
 
280
        }
 
281
    }
 
282
}