From W. Borgert:
[obnox/wireshark/wip.git] / disabled_protos.c
1 /* disabled_protos.c
2  * Code for reading and writing the disabled protocols file.
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <errno.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <glib.h>
39
40 #include <epan/filesystem.h>
41 #include <epan/proto.h>
42
43 #include "disabled_protos.h"
44 #include "file_util.h"
45
46 #define GLOBAL_PROTOCOLS_FILE_NAME      "disabled_protos"
47 #define PROTOCOLS_FILE_NAME             "disabled_protos"
48
49 /*
50  * List of disabled protocols
51  */
52 static GList *global_disabled_protos = NULL;
53 static GList *disabled_protos = NULL;
54
55 #define INIT_BUF_SIZE   128
56
57 /*
58  * Read in a list of disabled protocols.
59  *
60  * On success, "*pref_path_return" is set to NULL.
61  * On error, "*pref_path_return" is set to point to the pathname of
62  * the file we tried to read - it should be freed by our caller -
63  * and "*open_errno_return" is set to the error if an open failed
64  * or "*read_errno_return" is set to the error if a read failed.
65  */
66
67 static int read_disabled_protos_list_file(const char *ff_path, FILE *ff,
68                                           GList **flp);
69
70 void
71 read_disabled_protos_list(char **gpath_return, int *gopen_errno_return,
72                           int *gread_errno_return,
73                           char **path_return, int *open_errno_return,
74                           int *read_errno_return)
75 {
76   int         err;
77   char       *gff_path, *ff_path;
78   FILE       *ff;
79
80   /* Construct the pathname of the global disabled protocols file. */
81   gff_path = get_datafile_path(GLOBAL_PROTOCOLS_FILE_NAME);
82
83   /* Read the global disabled protocols file, if it exists. */
84   *gpath_return = NULL;
85   if ((ff = eth_fopen(gff_path, "r")) != NULL) {
86     /* We succeeded in opening it; read it. */
87     err = read_disabled_protos_list_file(gff_path, ff,
88                                          &global_disabled_protos);
89     if (err != 0) {
90       /* We had an error reading the file; return the errno and the
91          pathname, so our caller can report the error. */
92       *gopen_errno_return = 0;
93       *gread_errno_return = err;
94       *gpath_return = gff_path;
95     } else
96       g_free(gff_path);
97     fclose(ff);
98   } else {
99     /* We failed to open it.  If we failed for some reason other than
100        "it doesn't exist", return the errno and the pathname, so our
101        caller can report the error. */
102     if (errno != ENOENT) {
103       *gopen_errno_return = errno;
104       *gread_errno_return = 0;
105       *gpath_return = gff_path;
106     } else
107       g_free(gff_path);
108   }
109
110   /* Construct the pathname of the user's disabled protocols file. */
111   ff_path = get_persconffile_path(PROTOCOLS_FILE_NAME, FALSE);
112
113   /* Read the user's disabled protocols file, if it exists. */
114   *path_return = NULL;
115   if ((ff = eth_fopen(ff_path, "r")) != NULL) {
116     /* We succeeded in opening it; read it. */
117     err = read_disabled_protos_list_file(ff_path, ff, &disabled_protos);
118     if (err != 0) {
119       /* We had an error reading the file; return the errno and the
120          pathname, so our caller can report the error. */
121       *open_errno_return = 0;
122       *read_errno_return = err;
123       *path_return = ff_path;
124     } else
125       g_free(ff_path);
126     fclose(ff);
127   } else {
128     /* We failed to open it.  If we failed for some reason other than
129        "it doesn't exist", return the errno and the pathname, so our
130        caller can report the error. */
131     if (errno != ENOENT) {
132       *open_errno_return = errno;
133       *read_errno_return = 0;
134       *path_return = ff_path;
135     } else
136       g_free(ff_path);
137   }
138 }
139
140 static int
141 read_disabled_protos_list_file(const char *ff_path, FILE *ff,
142                                GList **flp)
143 {
144   GList      *fl_ent;
145   protocol_def *prot;
146   int         c;
147   char       *prot_name;
148   int         prot_name_len;
149   int         prot_name_index;
150   int         line = 1;
151
152   /* If we already have a list of protocols, discard it. */
153   if (*flp != NULL) {
154     fl_ent = g_list_first(*flp);
155     while (fl_ent != NULL) {
156       prot = (protocol_def *) fl_ent->data;
157       g_free(prot->name);
158       g_free(prot);
159       fl_ent = fl_ent->next;
160     }
161     g_list_free(*flp);
162     *flp = NULL;
163   }
164
165   /* Allocate the protocol name buffer. */
166   prot_name_len = INIT_BUF_SIZE;
167   prot_name = g_malloc(prot_name_len + 1);
168
169   for (line = 1; ; line++) {
170     /* Lines in a disabled protocol file contain the "filter name" of
171        a protocol to be disabled. */
172
173     /* Skip over leading white space, if any. */
174     while ((c = getc(ff)) != EOF && isspace(c)) {
175       if (c == '\n') {
176         /* Blank line. */
177         continue;
178       }
179     }
180
181     if (c == EOF) {
182       if (ferror(ff))
183         goto error;     /* I/O error */
184       else
185         break;  /* Nothing more to read */
186     }
187     ungetc(c, ff);      /* Unread the non-white-space character. */
188
189     /* Get the name of the protocol. */
190     prot_name_index = 0;
191     for (;;) {
192       c = getc(ff);
193       if (c == EOF)
194         break;  /* End of file, or I/O error */
195       if (isspace(c))
196         break;  /* Trailing white space, or end of line. */
197       if (c == '#')
198         break;  /* Start of comment, running to end of line. */
199       /* Add this character to the protocol name string. */
200       if (prot_name_index >= prot_name_len) {
201         /* protocol name buffer isn't long enough; double its length. */
202         prot_name_len *= 2;
203         prot_name = g_realloc(prot_name, prot_name_len + 1);
204       }
205       prot_name[prot_name_index] = c;
206       prot_name_index++;
207     }
208
209     if (isspace(c) && c != '\n') {
210       /* Skip over trailing white space. */
211       while ((c = getc(ff)) != EOF && c != '\n' && isspace(c))
212         ;
213       if (c != EOF && c != '\n' && c != '#') {
214         /* Non-white-space after the protocol name; warn about it,
215            in case we come up with a reason to use it. */
216         g_warning("'%s' line %d has extra stuff after the protocol name.",
217                   ff_path, line);
218       }
219     }
220     if (c != EOF && c != '\n') {
221       /* Skip to end of line. */
222       while ((c = getc(ff)) != EOF && c != '\n')
223         ;
224     }
225
226     if (c == EOF) {
227       if (ferror(ff))
228         goto error;     /* I/O error */
229       else {
230         /* EOF, not error; no newline seen before EOF */
231         g_warning("'%s' line %d doesn't have a newline.", ff_path,
232                   line);
233       }
234       break;    /* nothing more to read */
235     }
236
237     /* Null-terminate the protocol name. */
238     if (prot_name_index >= prot_name_len) {
239       /* protocol name buffer isn't long enough; double its length. */
240       prot_name_len *= 2;
241       prot_name = g_realloc(prot_name, prot_name_len + 1);
242     }
243     prot_name[prot_name_index] = '\0';
244
245     /* Add the new protocol to the list of disabled protocols */
246     prot         = (protocol_def *) g_malloc(sizeof(protocol_def));
247     prot->name   = g_strdup(prot_name);
248     *flp = g_list_append(*flp, prot);
249   }
250   g_free(prot_name);
251   return 0;
252
253 error:
254   return errno;
255 }
256
257 /*
258  * Disable protocols as per the stored configuration
259  */
260 void
261 set_disabled_protos_list(void)
262 {
263   gint i;
264   GList *fl_ent;
265   protocol_def *prot;
266
267   /*
268    * assume all protocols are enabled by default
269    */
270   if (disabled_protos == NULL)
271     goto skip;
272
273   fl_ent = g_list_first(disabled_protos);
274
275   while (fl_ent != NULL) {
276     prot = (protocol_def *) fl_ent->data;
277     i = proto_get_id_by_filter_name(prot->name);
278     if (i == -1) {
279       /* XXX - complain here? */
280     } else {
281       if (proto_can_toggle_protocol(i))
282         proto_set_decoding(i, FALSE);
283     }
284
285     fl_ent = fl_ent->next;
286   }
287
288 skip:
289   if (global_disabled_protos == NULL)
290     return;
291
292   fl_ent = g_list_first(global_disabled_protos);
293
294   while (fl_ent != NULL) {
295     prot = (protocol_def *) fl_ent->data;
296     i = proto_get_id_by_filter_name(prot->name);
297     if (i == -1) {
298       /* XXX - complain here? */
299     } else {
300       if (proto_can_toggle_protocol(i)) {
301         proto_set_decoding(i, FALSE);
302         proto_set_cant_toggle(i);
303       }
304     }
305
306     fl_ent = fl_ent->next;
307   }
308 }
309
310 /*
311  * Write out a list of disabled protocols.
312  *
313  * On success, "*pref_path_return" is set to NULL.
314  * On error, "*pref_path_return" is set to point to the pathname of
315  * the file we tried to read - it should be freed by our caller -
316  * and "*errno_return" is set to the error.
317  */
318 void
319 save_disabled_protos_list(char **pref_path_return, int *errno_return)
320 {
321   gchar       *ff_path, *ff_path_new;
322   const gchar *ff_name;
323   FILE        *ff;
324   gint         i;
325   protocol_t  *protocol;
326   void        *cookie;
327
328   *pref_path_return = NULL;     /* assume no error */
329
330   ff_name = PROTOCOLS_FILE_NAME;
331
332   ff_path = get_persconffile_path(ff_name, TRUE);
333
334   /* Write to "XXX.new", and rename if that succeeds.
335      That means we don't trash the file if we fail to write it out
336      completely. */
337   ff_path_new = g_strdup_printf("%s.new", ff_path);
338
339   if ((ff = eth_fopen(ff_path_new, "w")) == NULL) {
340     *pref_path_return = ff_path;
341     *errno_return = errno;
342     g_free(ff_path_new);
343     return;
344   }
345
346   /* Iterate over all the protocols */
347
348   for (i = proto_get_first_protocol(&cookie); i != -1;
349        i = proto_get_next_protocol(&cookie)) {
350
351     if (!proto_can_toggle_protocol(i)) {
352       continue;
353     }
354
355     protocol = find_protocol_by_id(i);
356     if (proto_is_protocol_enabled(protocol)) {
357       continue;
358     }
359
360     /* Write out the protocol name. */
361     fprintf(ff, "%s\n", proto_get_protocol_filter_name(i));
362   }
363
364   if (fclose(ff) == EOF) {
365     *pref_path_return = ff_path;
366     *errno_return = errno;
367     eth_unlink(ff_path_new);
368     g_free(ff_path_new);
369     return;
370   }
371
372 #ifdef _WIN32
373   /* ANSI C doesn't say whether "rename()" removes the target if it
374      exists; the Win32 call to rename files doesn't do so, which I
375      infer is the reason why the MSVC++ "rename()" doesn't do so.
376      We must therefore remove the target file first, on Windows. */
377   if (eth_remove(ff_path) < 0 && errno != ENOENT) {
378     /* It failed for some reason other than "it's not there"; if
379        it's not there, we don't need to remove it, so we just
380        drive on. */
381     *pref_path_return = ff_path;
382     *errno_return = errno;
383     eth_unlink(ff_path_new);
384     g_free(ff_path_new);
385     return;
386   }
387 #endif
388
389   if (eth_rename(ff_path_new, ff_path) < 0) {
390     *pref_path_return = ff_path;
391     *errno_return = errno;
392     eth_unlink(ff_path_new);
393     g_free(ff_path_new);
394     return;
395   }
396   g_free(ff_path_new);
397   g_free(ff_path);
398 }