File: host.cc

package info (click to toggle)
wfnetobjs 0.2.2-1.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,876 kB
  • ctags: 1,572
  • sloc: cpp: 12,405; sh: 8,357; ansic: 6,531; makefile: 536; yacc: 316; xml: 47; sed: 16; perl: 12
file content (475 lines) | stat: -rw-r--r-- 11,063 bytes parent folder | download | duplicates (2)
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
 * WallFire -- a comprehensive firewall administration tool.
 * 
 * Copyright (C) 2001 Herv Eychenne <rv@wallfire.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */

using namespace std;

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sstream>
#include <algorithm>

#include <stdio.h>
#include <stdlib.h>
/* <<< guess */
#include <unistd.h>
#include <sys/param.h> /* for MAXHOSTNAMELEN */
#include <sys/utsname.h> /* for uname */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <net/if.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#include <sys/ioctl.h>
#include <errno.h>
/* guess >>> */

#include "checks.h"
#include "wfhost.h"
#include "list1.h"
#include "defs.h"

/* Warning: this must match enum order. */
static const char* wf_osnames[] = {
  "Unknown", "Linux", "FreeBSD", "NetBSD", "OpenBSD", "Solaris", "SunOS",
  "Irix", "HP-UX" };

/* Warning: this must match enum order. */
static const char* wf_lindistrib[] = {
  "Unknown", "Debian", "Mandrake", "Redhat"
}; /* supported */
/*
  "Unknown", "Debian", "Slackware", "Mandrake", "Redhat", "SuSe", "TurboLinux"
*/

wf_host::wf_host() :
  name(),
  fqdn(),
  comment(),
  ipaddr(),
  ifaces(),
  services(),
  os(WF_OS_UNKNOWN),
  os_version_major(-1),
  os_version_minor(-1),
  os_version_micro(-1),
  lindistrib(WF_LINDISTRIB_UNKNOWN),
  lindistrib_version_major(-1),
  lindistrib_version_minor(-1)
{
}

wf_host::~wf_host() {}

// default wf_host::wf_host(const wf_host& host)


bool
wf_host::iface_isdefined(const string& name) const {
  list<wf_iface>::const_iterator first = ifaces.begin(), last = ifaces.end();
  for (; first != last; ++first)
    if (first->name == name)
      return true;
  return false;
}

bool
wf_host::iface_add(const wf_iface& iface) {
  if (iface_isdefined(iface.name))
    return false;  /* interface with the same name already defined */
  ifaces.push_back(iface);
  return true;
}

void
wf_host::iface_del(const wf_iface& iface) {
  ifaces.remove(iface);
}

void
wf_host::iface_del(const string& name) {
  list<wf_iface>::iterator first = ifaces.begin(), last = ifaces.end();
  for (; first != last; ++first) {
    if (first->name == name) {
      ifaces.erase(first);
      return; /* we infer there cannot be 2 interfaces with the same name */
    }
  }
}


bool
wf_host::service_add(const wf_service& service) {
  if (find(services.begin(), services.end(), service) != services.end())
    return false;  /* service already present in the list */
  
  services.push_back(service);
  return true;
}

void
wf_host::service_del(const wf_service& service) {
  services.remove(service);
}

bool
wf_host::os_version_guess() {
  struct utsname utsname;
  if (uname(&utsname))
    return false;

  int major, minor, micro;
  if (sscanf(utsname.release, "%i.%i.%i", &major, &minor, &micro) != 3) {
    if (sscanf(utsname.release, "%i.%i", &major, &minor) != 2)
      return false;
  }
  else
    os_version_micro = micro;

  os_version_major = major;
  os_version_minor = minor;
  return true;
}

bool
wf_host::os_guess() {
  struct utsname utsname;
  if (uname(&utsname))
    return false;
  /*
    cerr <<
    "sysname: " << utsname.sysname << endl <<
    "release: " << utsname.release << endl <<
    "machine: " << utsname.machine << endl;
  */
  unsigned int i;
  for (i = 1 /* skip "Unknown" */;
       i < sizeof(wf_osnames) / sizeof(char*);
       i++) {
    if (!strcmp(utsname.sysname, wf_osnames[i])) {
      os = (enum wf_os)i;
      break;
    }
  }
#if 0 // always return true for the moment ALL@@3
  if (i == sizeof(wf_osnames) / sizeof(char*))
    return false; /* not found */
#endif

  os_version_guess();

  return true;
}

string
wf_host::os_tostr() const {
  if (os < 0 || (unsigned)os >= (sizeof(wf_osnames) / sizeof(char*)))
    return "Invalid";
  return wf_osnames[os];
}

string
wf_host::os_version_tostr() const {
  if (os == WF_OS_UNKNOWN)
    return "";

  ostringstream outs;

  if (os_version_major != -1) {
    outs << os_version_major;
    if (os_version_minor != -1) {
      outs << '.' << os_version_minor;
      if (os_version_micro != -1)
	outs << '.' << os_version_micro;
    }
  }

  return outs.str();
}

#ifdef linux
/* We must figure it at runtime because configure time probing would
   prevent from shipping generic executables. */
bool
wf_host::linux_distribution_guess() {
  FILE* file;
  int major, minor;

  if ((file = fopen("/etc/debian_version", "r")) != NULL) {
    char line[32];
    lindistrib = WF_LINDISTRIB_DEBIAN;
    if (fgets(line, sizeof(line), file) == NULL)
      return false;
    if (sscanf(line, "%i.%i", &major, &minor) != 2)
      return false;
  }
  else if ((file = fopen("/etc/mandrake-release", "r")) != NULL) {
    lindistrib = WF_LINDISTRIB_MANDRAKE;
    if (fscanf(file, "Linux Mandrake release %i.%i", &major, &minor) != 2)
      return false;
  }
  else if ((file = fopen("/etc/redhat-release", "r")) != NULL) {
    lindistrib = WF_LINDISTRIB_REDHAT;
    if (fscanf(file, "Linux Redhat release %i.%i", &major, &minor) != 2)
      return false;
  }
  else
    return false;

  lindistrib_version_major = major;
  lindistrib_version_minor = minor;
  return true;
}

#endif /* linux */

string
wf_host::linux_distribution_tostr() const {
  if (os != WF_OS_LINUX ||
      lindistrib < 0 ||
      (unsigned)lindistrib >= sizeof(wf_lindistrib) / sizeof(char*))
    return "Invalid";
  return wf_lindistrib[lindistrib];
}

string
wf_host::linux_distribution_version_tostr() const {
  ostringstream outs;

  if (os != WF_OS_LINUX || lindistrib == WF_LINDISTRIB_UNKNOWN)
    return "";

  if (lindistrib_version_major != -1) {
    outs << lindistrib_version_major;
    if (lindistrib_version_minor != -1)
      outs << '.' << lindistrib_version_minor;
  }

  return outs.str();
}


bool
wf_host::name_guess() {
  char hostname[MAXHOSTNAMELEN + 1];

  if (gethostname(hostname, MAXHOSTNAMELEN) == 0)
    name = hostname;
  return true;
}

static string
replace_colon(const string& str) {
  string res;
  string::const_iterator first = str.begin(), last = str.end();
  for ( ; first != last; ++first) {
    switch (*first) {
    case ':':
      res += '_';
      break;
    default:
      res += *first;
    }
  }
  return res;
}

/* We consider that no field is already defined, except name. */
bool
wf_host::ifaces_guess(bool skip_useless) {
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
  struct ifaddrs *ifap, *ifa;
  if (getifaddrs(&ifap) < 0) {
    perror("getifaddrs");
    return false;
  }
  
  for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    if (iface_isdefined(ifa->ifa_name))
      continue;
    wf_iface iface;
    if (iface.guess(ifa->ifa_name, skip_useless))
      iface_add(iface);
  }
  freeifaddrs(ifap);
#else
  int sock;
  char iflist[2048];
  struct ifconf ifc;
  struct ifreq* ifr;
  int n;
  
  sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  if (sock < 0) {
    perror("socket creation");
    return false;
  }
  /* get interface list */
  ifc.ifc_buf = iflist;
  ifc.ifc_len = sizeof(iflist);
  if (ioctl(sock, SIOCGIFCONF, (char*)&ifc) < 0) {
    perror("ioctl IFCONF");
    close(sock);
    return false;
  }

  ifr = ifc.ifc_req;
  for (n = ifc.ifc_len / sizeof(struct ifreq); --n >= 0; ++ifr) {
    wf_iface iface;
    if (iface.guess(ifr, sock, skip_useless))
      iface_add(iface);
  }
  close(sock);
#endif

  /* complete ifaces' names and comments */
  list<wf_iface>::iterator first = ifaces.begin(), last = ifaces.end();
  for (; first != last; ++first) {
    wf_iface& iface = *first;

    wf_network& network = iface.network;
    if (network.network.isdefined()) {
      struct netent* net;
      net = getnetbyaddr(ntohl(network.network.get()), AF_INET);
      if (net != NULL)
	network.name = net->n_name;
      else
	network.name = name + "_"  + replace_colon(iface.name) + "_net";
    }
    else
      network.name = name + "_"  + replace_colon(iface.name) + "_net";
    
    char* str;
    {
      char tmp[256];
      str = _("%s %s network");
      snprintf(tmp, sizeof(tmp), str, name.c_str(), iface.name.c_str());
      network.comment = tmp;
    }

    string devname;
    if (iface.flags & IFF_LOOPBACK)
      devname = "Loopback";
    else if (iface.flags & IFF_POINTOPOINT)
      devname = "PPP";
    else
      devname = iface.name;

    {
      char tmp[256];
      str = _("%s interface");
      snprintf(tmp, sizeof(tmp), str, devname.c_str());
      iface.comment = tmp;
    }


  }

  return true;
}

bool
wf_host::check() const {
  if (name.empty()) {
    fprintf(stderr, _("Host: no name is defined.\n"));
    return false;
  }

  if (wf_object_name_check(name) == false) {
    fprintf(stderr, _("Host `%s': invalid name.\n"), name.c_str());
    return false;
  }
  if (fqdn.empty() == false) {
    if (wf_hostname_check(fqdn) == false) {
      fprintf(stderr, _("Host `%s': invalid fqdn.\n"), fqdn.c_str());
      return false;
    }
  }
  /* check ifaces */
  list<wf_iface>::const_iterator first = ifaces.begin(), last = ifaces.end();
  for (; first != last; ++first) {
    if (first->check() == false) {
      fprintf(stderr, _("Host `%s': wrong interface.\n"), name.c_str());
      return false;
    }
  }

  // check services?? ALL@@2
  return true;
}

/* We consider that no field is already defined. */
bool
wf_host::guess(bool skip_useless) {
  name_guess();

  if (os_guess() == false || ifaces_guess(skip_useless) == false)
    return false;
  
  /* We do not return false if distribution guess fails. */
#ifdef linux
  if (os == WF_OS_LINUX)
    linux_distribution_guess();
#endif

  return true;
}

template<class T>
struct debugprint_class {
  debugprint_class(ostream& __os) : os(__os) {}
  void operator()(T x) { x.debugprint(os); }
  ostream& os;
};

ostream&
wf_host::debugprint(ostream& os) const {
  os << _("Host:") << endl;

  os << _("comment:\t") << comment << endl;

  os << _("name:\t\t") ;
  if (name.empty() == false)
    os << name;
  else
    os << _("(undefined)");
  os << endl;

  os << _("fqdn:\t\t");
  if (fqdn.empty() == false)
    os << fqdn;
  else
    os << _("(undefined)");
  os << endl;

  os << _("address:\t") << ipaddr << endl;

  for_each(ifaces.begin(), ifaces.end(), debugprint_class<wf_iface>(os));
  os << endl;

  for_each(services.begin(), services.end(), debugprint_class<wf_service>(os));

  return os;
}