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