File: hostnames.c

package info (click to toggle)
nessus-libraries 1.0.10-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,536 kB
  • ctags: 12,585
  • sloc: ansic: 72,626; asm: 25,921; sh: 19,570; makefile: 1,974; cpp: 560; pascal: 536; yacc: 234; lex: 203; lisp: 186; perl: 76; fortran: 24
file content (445 lines) | stat: -rw-r--r-- 10,383 bytes parent folder | download
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 *          Copyright (c) mjh-EDV Beratung, 1996-1999
 *     mjh-EDV Beratung - 63263 Neu-Isenburg - Rosenstrasse 12
 *          Tel +49 6102 328279 - Fax +49 6102 328278
 *                Email info@mjh.teddy-net.com
 *
 *    Author: Jordan Hrycaj <jordan@mjh.teddy-net.com>
 *
 *   $Id: hostnames.c,v 1.10.2.5 2001/02/26 09:32:43 jordan Exp $
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Library General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free
 *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include "common-stuff.h"

#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#else
# ifdef HAVE_SOCKET_H
#  include <socket.h>
# endif
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef HAVE_CTYPE_H
# include <ctype.h>
#endif

#include "messages.h"
#include "hostnames.h"

/* ---------------------------------------------------------------------- *
 *                         private helpers                                *
 * ---------------------------------------------------------------------- */

/* should be initialized in a threaded environment */
static const char *
get_myself
  (void)
{
  /* hold name for localhost */
  static char *myself;

  if (myself == 0) { 
    char buf [1024] ;
    if (gethostname (buf, sizeof (buf)) == 0 && buf [0])
      myself = XSTRDUP (buf) ;
  }
  
  return myself ;
}


/* return network byte order,
   upon parse error: set *is_error to 1 and return 0 */
static unsigned long
is_ip_pattern
  (const char *s)
{
  struct in_addr in;

# ifdef HAVE_INET_ATON
  if (inet_aton (s, &in)) {
    errno = 0 ;
    return in.s_addr ;
  }
# else
  unsigned long a, result;
  
  if ((a = strtol (s, (char**)&s, 10)) < 0 || a > 255 || s [0] != '.')
    goto parse_error;
  result = a ;
  
  if ((a = strtol (s+1, (char**)&s, 10)) < 0 || a > 255 || s [0] != '.')
    goto parse_error;
  result <<= 8 ;
  result  += a ;

  if ((a = strtol (s+1, (char**)&s, 10)) < 0 || a > 255 || s [0] != '.')
    goto parse_error;
  result <<= 8 ;
  result  += a ;

  if ((a = strtol (s+1, (char**)&s, 10)) < 0 || a > 255 || s [0] != '\0')
    goto parse_error;
  result <<= 8 ;
  result  += a ;
  
  return htonl (result) ;
 parse_error:
# endif

  errno = errnum (NO_IPv4_ADDRESS);
  return 0;
}


/* ascii netmask to long in network byte order */
static long
netmask2nl
 (const char *s)
{
  char *q;
  long m ;

  /* /general netmask notation a.b.c.d */
  if (strchr (s, '.') != 0) {
    if ((m = is_ip_pattern (s)) == 0 && errno)
      return 0;
    return m ;
  }

  /* /<number-of-leading-bits> notation */
  if ((m = strtol (s, &q, 10)) < 0 || m > 32 || q [0] != '\0') {
    /* improper address notation -- syntax error */
    errno = errnum (NO_IPv4_MASK_LEN);
    return 0 ;
  }
  if (m)		/* convert to bit notation */
    m = (m < 32 ? (((((long)1)<<m)-1)<<(32-m)) : -1);
  errno = 0;
  return htonl (m);	/* convert to network notation */
}


/* ascii hostname to long in network byte order */
static long
hostname2nl
 (const char *s)
{
  struct hostent *h;
  struct in_addr in;
  char *p = 0;

  /* resolve entry address numerically, ignore errors */
  if (isdigit (s [0]) &&
      (((in.s_addr = strtol (s, &p, 10)) != LONG_MIN &&
	in.s_addr != LONG_MAX) || errno != ERANGE) &&
      p != 0 && p [0] == '\0')
    return in.s_addr ;

  /* resolve the entry addressi as hostname */
  if ((h = gethostbyname (s)) == 0)
    return 0 ;
  
  memcpy (&in.s_addr, *h->h_addr_list, h->h_length); 
  return in.s_addr ;
}

/* ---------------------------------------------------------------------- *
 *                     compare host names                                 *
 * ---------------------------------------------------------------------- */

const char *
get_my_host_name
   (void)
{
  return get_myself () ;
}

/* returns sort of a canonical host name */
const char *
get_host_ipaddress
   (const char *name)
{
  struct hostent *h;
  struct in_addr in;
  
  if (name == 0 && (name = get_myself ()) == 0)
    return 0 ;
  
  if (is_ip_pattern (name))
    return name ;

  if ((h = gethostbyname (name)) == 0)
    return 0 ;

  /* convert to ip address */
  memcpy (&in.s_addr, *h->h_addr_list, h->h_length); 
  return inet_ntoa (in);
}

int
hostnomatch
  (const char *x,
   const char *y,
   size_t    len)
{
  return -1 ;
}


int
hostcmp
  (const char    *ntry,
   const char *pattern,
   size_t          len)
{
  char *buf, *p;
  long in;

  /* quick check for impossible hostnames */
  if (*pattern == '#' || *pattern == '@')
    return -1 ;

  /* this is easy, is it not ? */
  if (len <= 0) {
    if (strcasecmp (pattern, ntry) == 0)
      return 0;   
    len = strlen (pattern) ;
  } else {
    if (strncasecmp (pattern, ntry, len) == 0)
      return 0;
  }
  
  /* check, whether the entry is pattern like (speed up common cases) */
  if (strchr (ntry, '/') != 0 || strchr (ntry, '+') != 0)
    return -1;
  
  /* resolve entry address numerically, ignore errors */
  if ((in = hostname2nl (ntry)) == 0 && errno)
    return -1;
  
  /* now, the general case: cut off trailing garbage from the pattern */
  buf = ALLOCA (len + 1);
  memcpy (buf, pattern, len) ;
  buf [len] = '\0' ;

  /* for each segment of the pattern list (delimited by +) do */
  if ((pattern = strtok (buf, "+")) != 0)
    do {
      int n ;
      unsigned long mask = -1, addr ;
      long in1, in2 ;
      
      /* check for <entry-string> ::= <ip>/<mask> notation */
      if ((p = strchr (pattern, '/')) != 0) {
	if ((mask = netmask2nl (p+1)) == 0 && errno)
	  break ;	/* syntax error */
	p [0] = '\0' ;
      }

      /* resolve last address of a <from>-<to> range */
      if ((p = strchr (pattern, '-')) != 0) {
	/* resolve end address, ignore errors */
	if ((in2 = hostname2nl (p+1)) == 0 && errno) continue ;
	/* split off the end range */
	p [0] = '\0' ;
      }
    
      /* resolve current pattern address, ignore errors */
      if ((in1 = hostname2nl (pattern)) == 0 && errno) continue ;

      /* p == 0 unless end address given: set end of range */
      if (p == 0) in2 = in1 ;

      /* for ipv4, in.s_addr is a 32 bit type, eg. unsigned int */
      if ((in & mask) < in1 || in2 < (in & mask)) continue ;
      
      /* clean up: entry is in pattern */
      DEALLOCA (buf) ;
      return 0 ;
      
      /* get next segment */
    } while ((pattern = strtok (0, "+")) != 0);


  /* clean up: no match */
  DEALLOCA (buf) ;
  return -1 ;
}


char* 
tagresolve 
  (const char *pattern,
   size_t          len)
{
  char *p, *src, *trg, *trgbuf;
  unsigned trglen, start = 1 ;

  if (pattern == 0) {
    errno = errnum (NULL_TAG_PATTERN);
    return 0;
  }
  if (len == 0) 
    len = strlen (pattern);

  /* make a writable copy from the source */
  src = ALLOCA (len + 1);
  memcpy (src, pattern, len) ;
  src [len] = '\0' ;

  /* strip off any user part from the pattern */
  if ((p = strrchr (src, '@')) != 0) {
    /* allocate target bufer: public memory */
    trgbuf = pmalloc (trglen = p - src + 1);
    memcpy (trgbuf, src, trglen) ;
    trg = trgbuf + trglen ;
    len = 0 ;
    src = p + 1;
  } else
    /* allocate empty target bufer: public memory */
    trg = trgbuf = pmalloc (len = trglen = 0);
  
  /* for each segment of the pattern list (delimited by +) do */
  if ((pattern = strtok (src, "+")) != 0)
    do {

      int n ;
      unsigned long mask = -1, in, to ;
      struct in_addr in1;
      
      /* check maximal needed target space for: <from>=<to>/<mask> */
      if (len < 50) {
	trglen += 256 ;
	len    += 256 ;
	p      = xrealloc (trgbuf, trglen) ;
	trg    = p + (trg - trgbuf) ;
	trgbuf = p ;
      }

      /* check whether the continuation character '+' is needed */
      if (start)
	start = 0 ;
      else {
	* trg ++ = '+' ;
	len -- ;
      }
      
      /* check for <entry-string> ::= <ip>/<mask> notation */
      if ((p = strchr (pattern, '/')) != 0) {
	if ((mask = netmask2nl (p+1)) == 0 && errno) {
	  xfree (trgbuf);
	  return 0;	/* syntax error */
	}
	p [0] = '\0' ;
      }

      /* check for <from-ip> - <to-ip> notation */
      if ((p = strchr (pattern, '-')) != 0) {
	/* resolve current pattern address */
	if ((to = hostname2nl (p+1)) == 0 && errno) {
	  xfree (trgbuf);
	  return 0;
	}
	if (to != (to & mask)) {
	  errno = errnum (ILLEGAL_NETMASK);
	  xfree (trgbuf);
	  return 0;
	}
	/* set end mark */
	p [0] = '\0';
      }
      
      /* resolve current address, is start address */
      if ((in = hostname2nl (pattern)) == 0 && errno) {
	xfree (trgbuf);
	return 0;
      }
      
      /* p == 0 unless end address given: set end of range */
      if (p == 0) 
	to = in ;
      else {
	if (in > to) {		/* check for start <= end */
	  errno = errnum (ILLEGAL_RANGE) ;
	  xfree (trgbuf);
	  return 0;
	}
      }
	
      
      /* append host or network address */
      if (in == 0) {
	* trg ++ = '0' ;
	len -- ;
      }	else {
	in1.s_addr = in ;
	strcat (trg, inet_ntoa (in1));
	n    = strlen (trg);
	trg += n ;
	len -= n ;
      }

      if (mask != -1 && in != (in & mask)) {
	errno = errnum (ILLEGAL_NETMASK);
	xfree (trgbuf);
	return 0;
      }

      /* append range argument */  
      if (in < to) {
	* trg ++ = '-' ;
	len -- ;
	in1.s_addr = to ;
	strcat (trg, inet_ntoa (in1));
	n    = strlen (trg);
	trg += n ;
	len -= n ;
      }

      if (mask == -1)
	continue ;
     
      /* append network mask */  
      * trg ++ = '/' ;
      len -- ;
      if (mask == 0) {
	* trg ++ = '0' ;
	len -- ;
      }	else {
	in1.s_addr = mask ;
	strcat (trg, inet_ntoa (in1));
	n    = strlen (trg);
	trg += n ;
	len -= n ;
      }

      /* get next segment */
    } while ((pattern = strtok (0, "+")) != 0);
  
  trg [0] = '\0' ;
  return xrealloc (trgbuf, trglen - len + 1);
}