File: debiface.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 (263 lines) | stat: -rw-r--r-- 7,317 bytes parent folder | download | duplicates (3)
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
/*
 * 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;

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "wfnetobjs.h"
#include "output_common.h"
#include "defs.h"


static bool
iface_check(const wf_iface& iface,
	    const string& filename, unsigned int lineno) {
  if (iface.network.belong(iface.ipaddr) == false) {
    fprintf(stderr, _("Error: %s:%i: interface %s: address %s does not belong to network %s\n"),
	    filename.c_str(), lineno,
	    iface.name.c_str(), iface.ipaddr.tostr().c_str(),
	    iface.network.tostr().c_str());
    return false;
  }
  if (iface.broadcast.isdefined() &&
      iface.broadcast != iface.network.broadcast()) {
    fprintf(stderr, _("Warning: %s:%i: interface %s: broadcast address %s is different from the broadcast address (%s) of network %s\n"),
	    filename.c_str(), lineno,
	    iface.name.c_str(), iface.broadcast.tostr().c_str(),
	    iface.network.broadcast().tostr().c_str(),
	    iface.network.tostr().c_str());
  }
  return true;
}

/* parsing is really ugly ALL@@2 */
bool
debiface_file_tohost(wf_host& host, FILE* file, const string& filename) {
  if (file == NULL)
    return false;

  char line[1024];
  unsigned int lineno = 0, iface_lineno = 0;
  wf_iface* iface = NULL;

  while (fgets(line, sizeof(line), file) != NULL) {
    char *tmp = line, *comment, *word;

    lineno++;
    line[strlen(line) - 1] = '\0';

    for (comment = line; *comment != '\n'; comment++) {
      if (*comment == '#') {
	*comment = '\0';
	break;
      }
    }
    
    // cerr << '[' << line << ']' << '\n';
    while ((word = strsep(&tmp, "\t ")) != NULL) {
      char *name, *type;

      // cerr << '|' << word << '|' << '\n';
      if (*word == '\0')
	continue;

      if (!strcmp(word, "iface")) {
	if (iface != NULL) {
	  /* The interface is now complete: do some checking. */
	  if (iface_check(*iface, filename, iface_lineno) == false) {
	    /* Do some cleaning... RV@@6 */
	    return false;
	  }
	  host.iface_add(*iface);
	}

	iface = new wf_iface();
	if (iface == NULL)
	  return false; /* Do some cleaning... RV@@6 */

	iface_lineno = lineno;
	if ((name = strsep(&tmp, "\t ")) != NULL) {
	  //	  fprintf(stderr, "interface %s\n", name);
	  iface->name = name;
	}
	if ((type = strsep(&tmp, "\t ")) != NULL &&
	    !strcmp(type, "inet")) {
	  if ((type = strsep(&tmp, "\t ")) != NULL) {
	    if (!strcmp(type, "static"))
	      break;
	    else if (!strcmp(type, "loopback")) {
	      iface->ipaddr.set("127.0.0.1");
	      iface->network.network_set("127.0.0.0/8");
	    }
	  }
	}
      }
      else if (!strcmp(word, "address")) {
	char *addr;
	if ((addr = strsep(&tmp, "\t ")) != NULL)
	  iface->ipaddr.set(addr);
      }
      else if (!strcmp(word, "network")) {
	char *addr;
	if ((addr = strsep(&tmp, "\t ")) != NULL)
	  iface->network.network.set(addr);
      }
      else if (!strcmp(word, "netmask")) {
	char *addr;
	if ((addr = strsep(&tmp, "\t ")) != NULL)
	  iface->network.netmask.set(addr);
      }
      else if (!strcmp(word, "broadcast")) {
	char *addr;
	if ((addr = strsep(&tmp, "\t ")) != NULL)
	  iface->broadcast.set(addr);
      }
      else if (!strcmp(word, "gateway")) {
	/* ignored for the moment RV@@2 */
	break;
      }
      else if (!strcmp(word, "auto") ||
	       !strcmp(word, "pointopoint") ||  !strcmp(word, "media") ||
	       !strcmp(word, "hwaddress") ||  !strcmp(word, "mtu") ||
	       !strcmp(word, "up") || !strcmp(word, "down") ||
	       !strcmp(word, "pre-up") || !strcmp(word, "post-down"))
	break; /* ignored */
      else {
	fprintf(stderr, _("Error: %s:%i: unknown keywork `%s'\n"),
		filename.c_str(), lineno, word);
	return false;
      }
    }
  }

  if (iface != NULL) {
    /* The interface is now complete: do some checking. */
    if (iface_check(*iface, filename, iface_lineno) == false) {
      /* Do some cleaning... RV@@6 */
      return false;
    }
    host.iface_add(*iface);
  }

  return true;
}

bool
debiface_file_tohost(wf_host& host, const string& filename) {
  FILE* file = fopen(filename.c_str(), "r");
  if (file == NULL) {
    cerr << _("Error: ") << filename << ": " << strerror(errno) << '\n';
    return false;
  }

  if (debiface_file_tohost(host, file, filename) == false) {
    cerr << _("Error: ") << filename << ": " << _("parsing failed") << '\n';
    return false;
  }
  return true;
}

string
iface_todebiface_file(const wf_iface& iface) {
  string str;

  if (iface.name.empty()) {
    cerr << _("Error: interface has no name") << '\n';
    return str;
  }

  if (iface.name.rfind(':') == string::npos) {
    if (iface.network.name.empty())
      str += string_printf(_("# interface %s"), iface.name.c_str());
    else
      str += string_printf(_("# interface %s to network %s"),
			   iface.name.c_str(), iface.network.name.c_str());
  }
  else {
    if (iface.network.name.empty())
      str += string_printf(_("# interface %s (aliased)"),
			   iface.name.c_str());
    else
      str += string_printf(_("# interface %s (aliased) to network %s"),
			   iface.name.c_str(), iface.network.name.c_str());
  }
  str += '\n';
  
  str += wf_shell_comment(iface.comment) +=
    "iface " + iface.name + " inet ";

  if (iface.name == "lo") {
    str += "loopback\n";
    return str;
  }

  str += "static\n";

  if (iface.ipaddr.isdefined())
    str += "\taddress " + iface.ipaddr.tostr();
  else
    str += "#\taddress (unknown)";
  str += '\n';

  if (iface.network.network.isdefined())
    str += "\tnetwork " + iface.network.network.tostr();
  else
    str += "#\tnetwork (unknown)";
  str += '\n';

  if (iface.network.netmask.isdefined())
    str += "\tnetmask " + iface.network.netmask.tostr() + " # /" +
      string_printf("%i", iface.network.netmask_tobitmask());
  else
    str += "#\tnetmask (unknown)";
  str += '\n';

  if (iface.broadcast.isdefined()) {
    str += "\tbroadcast " + iface.broadcast.tostr();
    if (iface.broadcast != iface.network.broadcast()) {
      str += " # " +
	string_printf(_("different from %s (theorical network broadcast address)"),
		      iface.network.broadcast().tostr().c_str());
    }
  }
  else
    str += "#\tbroadcast " + iface.network.broadcast().tostr();
  str += '\n';

  return str;
}


string
host_todebiface_file(const wf_host& host) {
  string str;
  list<wf_iface>::const_iterator first = host.ifaces.begin(),
    last = host.ifaces.end();

  str += wf_shell_comment(host.comment) + '\n';

  for (; first != last; ++first)
    str += iface_todebiface_file(*first) + '\n';
  return str;
}