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