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
|
diff --git a/src/backends/NetworkManagerDebian.c b/src/backends/NetworkManagerDebian.c
index 740572a..93c2914 100644
--- a/src/backends/NetworkManagerDebian.c
+++ b/src/backends/NetworkManagerDebian.c
@@ -37,6 +37,7 @@ #include "interface_parser.h"
#include "nm-utils.h"
#define ARPING "/usr/sbin/arping"
+#define RESOLVCONF "resolvconf"
/*
* nm_system_init
@@ -238,14 +239,14 @@ void nm_system_kill_all_dhcp_daemons (vo
/*
* nm_system_update_dns
*
- * Make glibc/nscd aware of any changes to the resolv.conf file by
- * restarting nscd.
+ * Invalidate the nscd host cache, if it exists, since
+ * we changed resolv.conf.
*
*/
void nm_system_update_dns (void)
{
- nm_spawn_process ("/usr/sbin/invoke-rc.d nscd restart");
-
+ nm_info ("Clearing nscd hosts cache.");
+ nm_spawn_process ("/usr/sbin/nscd -i hosts");
}
@@ -514,13 +515,60 @@ gboolean nm_system_device_get_use_dhcp (
/*
* nm_system_device_get_disabled
*
- * Return whether the distro-specific system config tells us to use
- * dhcp for this device.
+ * Return whether the distro-specific system config tells us to disable
+ * this device.
*
*/
gboolean nm_system_device_get_disabled (NMDevice *dev)
{
- return FALSE;
+ const char *iface;
+ if_block *curr_device, *curr_b;
+ if_data *curr_d;
+ gboolean blacklist = TRUE;
+
+ g_return_val_if_fail (dev != NULL, TRUE);
+
+ iface = nm_device_get_iface (dev);
+
+ ifparser_init ();
+
+ /* If the device is listed in a mapping, do not control it */
+ if (ifparser_getmapping (iface) != NULL) {
+ blacklist = TRUE;
+ goto out;
+ }
+
+ /* If the interface isn't listed in /etc/network/interfaces then
+ * it's considered okay to control it.
+ */
+ curr_device = ifparser_getif (iface);
+ if (curr_device == NULL) {
+ blacklist = FALSE;
+ goto out;
+ }
+
+ /* If the interface is listed and isn't marked "auto" then it's
+ * definitely not okay to control it.
+ */
+ for (curr_b = ifparser_getfirst (); curr_b; curr_b = curr_b->next) {
+ if ((!strcmp (curr_b->type, "auto") || !strcmp (curr_b->type, "allow-hotplug"))
+ && strstr (curr_b->name, iface))
+ blacklist = FALSE;
+ }
+
+ /* If the interface has no options other than just "inet dhcp"
+ * it's probably ok to fiddle with it.
+ */
+ for (curr_d = curr_device->info; curr_d; curr_d = curr_d->next) {
+ if (strcmp (curr_d->key, "inet")
+ || strcmp (curr_d->data, "dhcp" ))
+ blacklist = TRUE;
+ }
+
+out:
+ ifparser_destroy ();
+
+ return blacklist;
}
@@ -563,12 +611,18 @@ gboolean nm_system_deactivate_dialup (GS
if (strcmp (dialup, config->name) == 0)
{
char *cmd;
+ int status;
nm_info ("Deactivating dialup device %s (%s) ...", dialup, (char *) config->data);
cmd = g_strdup_printf ("/sbin/ifdown %s", (char *) config->data);
nm_spawn_process (cmd);
g_free (cmd);
- ret = TRUE;
+ if (status == 0) {
+ ret = TRUE;
+ } else {
+ nm_warning ("Couldn't deactivate dialup device %s (%s) - %d", dialup, (char *) config->data, status);
+ ret = FALSE;
+ }
break;
}
}
@@ -587,12 +641,18 @@ gboolean nm_system_activate_dialup (GSLi
if (strcmp (dialup, config->name) == 0)
{
char *cmd;
+ int status;
nm_info ("Activating dialup device %s (%s) ...", dialup, (char *) config->data);
cmd = g_strdup_printf ("/sbin/ifup %s", (char *) config->data);
nm_spawn_process (cmd);
g_free (cmd);
- ret = TRUE;
+ if (status == 0) {
+ ret = TRUE;
+ } else {
+ nm_warning ("Couldn't activate dialup device %s (%s) - %d", dialup, (char *) config->data, status);
+ ret = FALSE;
+ }
break;
}
}
@@ -603,23 +663,26 @@ gboolean nm_system_activate_dialup (GSLi
GSList * nm_system_get_dialup_config (void)
{
const char *buf;
- unsigned int i = 0;
+ const char *provider;
GSList *list = NULL;
if_block *curr;
ifparser_init();
/* FIXME: get all ppp(and others?) lines from /e/n/i here */
curr = ifparser_getfirst();
- while(curr!=NULL)
+ while (curr != NULL)
{
NMDialUpConfig *config;
- if (strcmp(curr->type,"iface")==0)
+ if (strcmp(curr->type, "iface") == 0)
{
- buf = ifparser_getkey(curr,"inet");
- if (buf && strcmp (buf, "ppp")==0)
+ buf = ifparser_getkey(curr, "inet");
+ if (buf && strcmp (buf, "ppp") == 0)
{
+ provider = ifparser_getkey(curr, "provider");
+ if (!provider)
+ provider = "default provider";
config = g_malloc (sizeof (NMDialUpConfig));
- config->name = g_strdup_printf ("Modem (#%d)", i++);
+ config->name = g_strdup_printf ("%s via Modem", provider);
config->data = g_strdup (curr->name); /* interface name */
list = g_slist_append (list, config);
@@ -631,14 +694,6 @@ GSList * nm_system_get_dialup_config (vo
}
ifparser_destroy();
- /* Hack: Go back and remove the "(#0)" if there is only one device */
- if (i == 1)
- {
- NMDialUpConfig *config = (NMDialUpConfig *) list->data;
- g_free (config->name);
- config->name = g_strdup ("Modem");
- }
-
return list;
}
@@ -679,7 +734,10 @@ void nm_system_set_hostname (NMIP4Config
*/
gboolean nm_system_should_modify_resolv_conf (void)
{
- return TRUE;
+ if (g_find_program_in_path(RESOLVCONF) != NULL)
+ return FALSE;
+ else
+ return TRUE;
}
diff --git a/src/backends/interface_parser.c b/src/backends/interface_parser.c
index 53d6487..b3ad300 100644
--- a/src/backends/interface_parser.c
+++ b/src/backends/interface_parser.c
@@ -50,11 +50,13 @@ void add_block(const char *type, const c
void add_data(const char *key,const char *data)
{
+ if_data *ret;
+
// Check if there is a block where we can attach our data
if (first == NULL)
return;
- if_data *ret = (if_data*)calloc(1,sizeof(struct _if_data));
+ ret = (if_data*) calloc(1,sizeof(struct _if_data));
ret->key = g_strdup(key);
ret->data = g_strdup(data);
@@ -71,14 +73,18 @@ void add_data(const char *key,const char
//printf("added data '%s' with key '%s'\n",data,key);
}
-#define SPACE_OR_TAB(string,ret) {ret = strchr(string,' ');ret=(ret == NULL?strchr(string,'\t'):ret);}
+// define what we consider a whitespace
+#define WS " \t"
void ifparser_init(void)
{
FILE *inp = fopen(INTERFACES,"r");
int ret = 0;
+ int pos;
+ int len;
char *line;
- char *space;
+ char *key;
+ char *data;
char rline[255];
if (inp == NULL)
@@ -89,7 +95,7 @@ void ifparser_init(void)
first = last = NULL;
while(1)
{
- line = space = NULL;
+ line = NULL;
ret = fscanf(inp,"%255[^\n]\n",rline);
if (ret == EOF)
break;
@@ -102,50 +108,71 @@ void ifparser_init(void)
line = rline;
while(line[0] == ' ')
line++;
- if (line[0]=='#' || line[0]=='\0')
+ if (line[0] == '#' || line[0] == '\0')
continue;
- SPACE_OR_TAB(line,space)
- if (space == NULL)
+ len = strlen(line);
+ pos = 0;
+ while (!strchr(WS, line[pos]) && pos < len) pos++;
+
+ // terminate key string and skip further whitespaces
+ line[pos++] = '\0';
+ while (strchr(WS, line[pos]) && pos < len) pos++;
+
+ if (pos >= len)
{
- nm_warning ("Error: Can't parse interface line '%s'\n",line);
+ nm_warning ("Error: Can't parse line '%s'\n", line);
continue;
}
- space[0] = '\0';
-
+ key = &line[0];
+ data = &line[pos];
+
// There are four different stanzas:
// iface, mapping, auto and allow-*. Create a block for each of them.
- if (strcmp(line,"iface")==0)
+ if (strcmp(key, "iface") == 0)
{
- char *space2 = strchr(space+1,' ');
- if (space2 == NULL)
+ char *key2;
+
+ while (!strchr(WS, line[pos]) && pos < len ) pos++;
+
+ // terminate first data string and skip further whitespaces
+ line[pos++] = '\0';
+ while (strchr(WS, line[pos]) && pos < len) pos++;
+ if (pos >= len)
{
- nm_warning ("Error: Can't parse iface line '%s'\n",space+1);
+ nm_warning ("Error: Can't parse iface line '%s'\n", data);
continue;
}
- space2[0]='\0';
- add_block(line,space+1);
+ key2 = &line[pos];
+
+ add_block(key, data);
- if (space2[1]!='\0')
+ if (pos < len)
{
- space = strchr(space2+1,' ');
- if (space == NULL)
+
+ while (!strchr(WS, line[pos]) && pos < len ) pos++;
+
+ // terminate key2 string and skip further whitespaces
+ line[pos++] = '\0';
+ while (strchr(WS, line[pos]) && pos < len) pos++;
+ if (pos >= len)
{
- nm_warning ("Error: Can't parse data '%s'\n",space2+1);
+ nm_warning ("Error: Can't parse inet line '%s'\n", key2);
continue;
}
- space[0] = '\0';
- add_data(space2+1,space+1);
+ data = &line[pos];
+
+ add_data(key2, data);
}
}
- else if (strcmp(line,"auto")==0)
- add_block(line,space+1);
- else if (strcmp(line,"mapping")==0)
- add_block(line,space+1);
- else if (strncmp(line,"allow-",6)==0)
- add_block(line,space+1);
+ else if (strcmp(key, "auto") == 0)
+ add_block(key, data);
+ else if (strcmp(key, "mapping") == 0)
+ add_block(key, data);
+ else if (strncmp(key, "allow-", 6) == 0)
+ add_block(key, data);
else
- add_data(line,space+1);
+ add_data(key, data);
//printf("line: '%s' ret=%d\n",rline,ret);
}
@@ -198,6 +225,18 @@ if_block *ifparser_getif(const char* ifa
return NULL;
}
+if_block *ifparser_getmapping(const char* iface)
+{
+ if_block *curr = first;
+ while(curr!=NULL)
+ {
+ if (strcmp(curr->type,"mapping")==0 && strcmp(curr->name,iface)==0)
+ return curr;
+ curr = curr->next;
+ }
+ return NULL;
+}
+
const char *ifparser_getkey(if_block* iface, const char *key)
{
if_data *curr = iface->info;
diff --git a/src/backends/interface_parser.h b/src/backends/interface_parser.h
index 075f454..8914283 100644
--- a/src/backends/interface_parser.h
+++ b/src/backends/interface_parser.h
@@ -44,6 +44,7 @@ void ifparser_init(void);
void ifparser_destroy(void);
if_block *ifparser_getif(const char* iface);
+if_block *ifparser_getmapping(const char* iface);
if_block *ifparser_getfirst(void);
const char *ifparser_getkey(if_block* iface, const char *key);
|