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