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

« back to all changes in this revision

Viewing changes to ypserv-2.18/lib/securenets.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, 1997, 1998, 1999, 2000, 2003, 2005 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 as
 
6
   published by the Free Software Foundation; either version 2 of the
 
7
   License, or (at your option) any later version.
 
8
 
 
9
   The YP Server is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public
 
15
   License along with the YP Server; see the file COPYING. If
 
16
   not, write to the Free Software Foundation, Inc., 675 Mass Ave,
 
17
   Cambridge, MA 02139, USA. */
 
18
 
 
19
#ifdef HAVE_CONFIG_H
 
20
#include "config.h"
 
21
#endif
 
22
 
 
23
#include <netdb.h>
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <sys/stat.h>
 
28
#include <sys/socket.h>
 
29
#include <netinet/in.h>
 
30
#include <arpa/inet.h>
 
31
 
 
32
#include "access.h"
 
33
#include "log_msg.h"
 
34
 
 
35
#ifndef SECURENETS
 
36
#define SECURENETS "securenets"
 
37
#endif
 
38
 
 
39
typedef struct securenet
 
40
{
 
41
  struct in_addr netmask;
 
42
  struct in_addr network;
 
43
  struct securenet *next;
 
44
}
 
45
securenet_t;
 
46
 
 
47
static securenet_t *securenets = NULL;
 
48
 
 
49
void
 
50
load_securenets (void)
 
51
{
 
52
  char buf1[128], buf2[128], buf3[128];
 
53
  FILE *in;
 
54
  securenet_t *work, *tmp;
 
55
  int line = 0;
 
56
 
 
57
  /* If securenets isn't NULL, we should reload the securents file. */
 
58
  if (securenets != NULL)
 
59
    {
 
60
      log_msg ("Reloading securenets file\n");
 
61
      while (securenets != NULL)
 
62
        {
 
63
          work = securenets;
 
64
          securenets = securenets->next;
 
65
          free (work);
 
66
        }
 
67
    }
 
68
  securenets = NULL;
 
69
  work = NULL;
 
70
  tmp = NULL;
 
71
 
 
72
  if ((in = fopen (SECURENETS, "r")) == NULL)
 
73
    {
 
74
      log_msg ("WARNING: no %s file found!\n", SECURENETS);
 
75
      return;
 
76
    }
 
77
 
 
78
  while (!feof (in))
 
79
    {
 
80
      int host = 0;
 
81
 
 
82
      memset (buf1, 0, sizeof (buf1));
 
83
      memset (buf2, 0, sizeof (buf2));
 
84
      memset (buf3, 0, sizeof (buf3));
 
85
      fgets (buf3, 128, in);
 
86
      line++;
 
87
 
 
88
      if (buf3[0] == '\0' || buf3[0] == '#' || buf3[0] == '\n')
 
89
        continue;
 
90
 
 
91
      if (sscanf (buf3, "%s %s", buf1, buf2) != 2)
 
92
        {
 
93
          log_msg ("securenets(%d): malformed line, ignore it\n", line);
 
94
          continue;
 
95
        }
 
96
 
 
97
      if ((tmp = malloc (sizeof (securenet_t))) == NULL)
 
98
        {
 
99
          log_msg ("ERROR: could not allocate enough memory! [%s|%d]\n",
 
100
                   __FILE__, __LINE__);
 
101
          exit (1);
 
102
        }
 
103
 
 
104
      tmp->next = NULL;
 
105
 
 
106
      if (strcmp (buf1, "host") == 0)
 
107
        {
 
108
          strcpy (buf1, "255.255.255.255");
 
109
          host = 1;
 
110
        }
 
111
      else if (strcmp (buf1, "255.255.255.255") == 0)
 
112
        host = 1;
 
113
 
 
114
#if defined(HAVE_INET_ATON)
 
115
      if (!inet_aton (buf1, &tmp->netmask) && !host)
 
116
#else
 
117
      if ((tmp->netmask.s_addr = inet_addr (buf1)) == (-1) && !host)
 
118
#endif
 
119
        {
 
120
          log_msg ("securenets(%d): %s is not a correct netmask!\n", line,
 
121
                   buf1);
 
122
          free (tmp);
 
123
          continue;
 
124
        }
 
125
 
 
126
#if defined(HAVE_INET_ATON)
 
127
      if (!inet_aton (buf2, &tmp->network))
 
128
#else
 
129
      if ((tmp->network.s_addr = inet_addr (buf2)) == (-1))
 
130
#endif
 
131
        {
 
132
          log_msg ("securenets(%d): %s is not a correct network address!\n",
 
133
                   line, buf2);
 
134
          free (tmp);
 
135
          continue;
 
136
        }
 
137
 
 
138
      if (work == NULL)
 
139
        {
 
140
          work = tmp;
 
141
          securenets = work;
 
142
        }
 
143
      else
 
144
        {
 
145
          work->next = tmp;
 
146
          work = work->next;
 
147
        }
 
148
    }
 
149
  fclose (in);
 
150
 
 
151
  if (debug_flag)
 
152
    {
 
153
      tmp = securenets;
 
154
      while (tmp)
 
155
        {
 
156
          char *p1 = strdup (inet_ntoa (tmp->netmask));
 
157
          char *p2 = strdup (inet_ntoa (tmp->network));
 
158
 
 
159
          if (p1 != NULL && p2 != NULL)
 
160
            {
 
161
              log_msg ("Find securenet: %s %s", p1, p2);
 
162
              free (p1);
 
163
              free (p2);
 
164
            }
 
165
 
 
166
          tmp = tmp->next;
 
167
        }
 
168
    }
 
169
}
 
170
 
 
171
int
 
172
securenet_host (const struct in_addr sin_addr)
 
173
{
 
174
  securenet_t *ptr;
 
175
 
 
176
  ptr = securenets;
 
177
 
 
178
  if (ptr == NULL)
 
179
    return 1;
 
180
  else
 
181
    while (ptr != NULL)
 
182
      {
 
183
        if ((ptr->netmask.s_addr & sin_addr.s_addr) == ptr->network.s_addr)
 
184
          return 1;
 
185
        ptr = ptr->next;
 
186
      }
 
187
  return 0;
 
188
}