~ubuntu-branches/ubuntu/gutsy/gdhcpd/gutsy

« back to all changes in this revision

Viewing changes to src/add_range.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2006-11-26 10:59:00 UTC
  • mfrom: (3.1.3 feisty)
  • Revision ID: james.westby@ubuntu.com-20061126105900-ds7bx3lgjj2l6l1l
Tags: 0.3.1-2
* Adjusting the icondir.
* Makeing use of su-to-root in the desktop file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* GDHCPD, a GTK+ frontend for ISC DHCPD
2
 
 * Copyright C 2004, 2005 Magnus Loef <magnus-swe@telia.com> 
 
2
 * Copyright C 2004, 2005, 2006 Magnus Loef <magnus-swe@telia.com>
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or modify
5
5
 * it under the terms of the GNU General Public License as published by
25
25
#include "widgets.h"
26
26
#include "gettext.h"
27
27
#include "reread_conf.h"
28
 
 
29
 
extern char DHCPD_CONF_BUF[1024];
30
 
 
31
28
#include "allocate.h"
32
29
#include "show_info.h"
33
30
#include "clear_ptr_array.h"
34
31
#include "set_num_ranges.h"
35
32
#include "populate_ranges.h"
36
33
#include "populate_scope_settings.h"
37
 
 
38
 
 
39
 
void add_range(struct gdhcpd_widgets *widgets)
 
34
#include "failover_functions.h"
 
35
 
 
36
 
 
37
extern char global_netmask[1024];
 
38
extern char global_subnet[1024];
 
39
extern char global_nic[1024];
 
40
 
 
41
extern char DHCPD_CONF_BUF[1024];
 
42
 
 
43
 
 
44
 
 
45
void add_range(struct w *widgets)
40
46
{
41
47
    FILE *fp;
42
 
    long conf_size=0;
43
 
    int range_added=0;
 
48
    long conf_size = 0;
 
49
    int range_added = 0;
 
50
    int range_found = 0;
 
51
    int nic_found = 0;
 
52
    int have_failover = 0;
44
53
    char *line;
45
54
    char *new_conf;
46
 
    gchar *info;
 
55
    gchar *range_line, *info;
47
56
    G_CONST_RETURN gchar *range_from;
48
57
    G_CONST_RETURN gchar *range_to;
49
58
 
 
59
    /* Check if this is subnet has a failover declaration */
 
60
    gchar *nic = g_strdup_printf("%s", global_nic);
 
61
    gchar *subnet = g_strdup_printf("%s", global_subnet);
 
62
    gchar *netmask = g_strdup_printf("%s", global_netmask);
 
63
 
 
64
    have_failover = failover_exists(nic, subnet, netmask);
 
65
 
 
66
    g_free(nic);
 
67
    g_free(subnet);
 
68
    g_free(netmask);
 
69
 
 
70
    /* get the ranges from the entries */
50
71
    range_from = gtk_entry_get_text(GTK_ENTRY(widgets->ranges_add_from_entry));
51
72
    range_to = gtk_entry_get_text(GTK_ENTRY(widgets->ranges_add_to_entry));
52
73
 
53
74
    if( strlen(range_from) < 7 )
54
75
    {
55
 
       info = g_strdup_printf(_("The range from IP-address is too short, range not added\n"));
 
76
       info = g_strdup_printf(_("The range from IP-address is too short, the range was not added.\n"));
56
77
       show_info(info);
57
78
       g_free(info);
58
79
       return;
60
81
 
61
82
    if( strlen(range_to) < 7 )
62
83
    {
63
 
       info = g_strdup_printf(_("The range to IP-address is too short, range not added\n"));
 
84
       info = g_strdup_printf(_("The range to IP-address is too short, the range was not added.\n"));
64
85
       show_info(info);
65
86
       g_free(info);
66
87
       return;
74
95
 
75
96
    if((fp=fopen(DHCPD_CONF_BUF, "r"))==NULL)
76
97
    {
77
 
        printf("Couldnt find: %s\n", DHCPD_CONF_BUF);
 
98
        printf("Couldnt open dhcpd.conf here: %s\n", DHCPD_CONF_BUF);
78
99
        return;
79
100
    }
80
101
    fseek(fp, 0, SEEK_END);
81
102
    conf_size = ftell(fp);
82
103
    rewind(fp);
83
104
 
 
105
    line = allocate(conf_size+1);
 
106
 
 
107
    /* If this range already exists, dont add it */
 
108
    range_line = g_strdup_printf("range %s %s;\n", range_from, range_to);
 
109
 
 
110
    if( conf_size > 1 )
 
111
    while(fgets(line, conf_size, fp)!=NULL)
 
112
    {
 
113
        if( ( strstr(line, scope_line1) || strstr(line, scope_line2) 
 
114
        || strstr(line, scope_line3) ) && ! strstr(line, "#") )
 
115
        {
 
116
            while(fgets(line, conf_size, fp)!=NULL)
 
117
            {
 
118
                if( strstr(line, nic_line) && ! strstr(line, "#") )
 
119
                {
 
120
                    nic_found = 1;
 
121
                    break;
 
122
                }
 
123
            }
 
124
            
 
125
            if( nic_found )
 
126
            {
 
127
                nic_found = 0;
 
128
                while(fgets(line, conf_size, fp)!=NULL)
 
129
                {
 
130
                    if( strstr(line, range_line) && ! strstr(line, "#") )
 
131
                    {
 
132
                        range_found = 1;
 
133
                        break;
 
134
                    }
 
135
                }
 
136
            }
 
137
        }
 
138
    }
 
139
    g_free(range_line);
 
140
    
 
141
    if( range_found )
 
142
    {
 
143
        fclose(fp);
 
144
        free(line);
 
145
        g_free(scope_line1);
 
146
        g_free(scope_line2);
 
147
        g_free(scope_line3);
 
148
        g_free(nic_line);
 
149
 
 
150
        info = g_strdup_printf(_("The range already exists, the range was not added.\n"));
 
151
        show_info(info);
 
152
        g_free(info);
 
153
        return;
 
154
    }
 
155
 
 
156
 
84
157
    /* Widget range limited anti-overflow */
85
158
    new_conf = allocate(conf_size+1024);
86
159
 
87
 
    line = allocate(conf_size);
88
 
 
 
160
 
 
161
    /* Add the range */
 
162
    rewind(fp);
 
163
 
 
164
    if( conf_size > 1 )
89
165
    while(fgets(line, conf_size, fp)!=NULL)
90
166
    {
91
167
        if( ( strstr(line, scope_line1) || strstr(line, scope_line2) 
96
172
            while(fgets(line, conf_size, fp)!=NULL)
97
173
            {
98
174
                strcat(new_conf, line);
99
 
 
100
 
                if( strstr(line, nic_line) && ! strstr(line, "#") )
 
175
                
 
176
                /* Adds a new range after the interface line if its not a failover configuration */
 
177
                if( strstr(line, nic_line) && ! have_failover && ! strstr(line, "#") )
101
178
                {
102
 
                    range_added=1;
 
179
                    range_added = 1;
103
180
                    /* Cosmetics and readability. */
104
181
                    strcat(new_conf, "    range ");
105
182
                    strcat(new_conf, range_from);
108
185
                    strcat(new_conf, ";\n");
109
186
                    break;
110
187
                }
 
188
 
 
189
                /* Adds a new range after the dynamic bootp line if its a failover configuration. */
 
190
                if( strstr(line, "deny dynamic bootp clients") && have_failover && ! strstr(line, "#") )  
 
191
                {
 
192
                    range_added = 1;
 
193
                    /* Cosmetics and readability. */
 
194
                    strcat(new_conf, "        range ");
 
195
                    strcat(new_conf, range_from);
 
196
                    strcat(new_conf, " ");
 
197
                    strcat(new_conf, range_to);
 
198
                    strcat(new_conf, ";\n");
 
199
                    break;
 
200
                }
111
201
            }
112
202
        }
113
203
        else
114
204
          strcat(new_conf, line);
115
205
    }
116
206
    fclose(fp);
117
 
 
118
 
 
119
 
    if( range_added )
120
 
    {
121
 
        if((fp=fopen(DHCPD_CONF_BUF, "w+"))==NULL)
122
 
        {
123
 
            printf("Couldnt find: %s\n", DHCPD_CONF_BUF);
124
 
            return;
125
 
        }
126
 
        else
127
 
          {
128
 
             fputs(new_conf, fp);
129
 
             fclose(fp);
130
 
          }
131
 
    }
132
 
    free(new_conf);
133
207
    free(line);
134
 
 
135
208
    g_free(scope_line1);
136
209
    g_free(scope_line2);
137
210
    g_free(scope_line3);
 
211
    g_free(nic_line);
 
212
 
 
213
 
 
214
    if( range_added )
 
215
    {
 
216
        if((fp=fopen(DHCPD_CONF_BUF, "w+"))==NULL)
 
217
        {
 
218
            printf("Could not write dhcpd.conf here: %s\n", DHCPD_CONF_BUF);
 
219
            free(new_conf);
 
220
            return;
 
221
        }
 
222
        fputs(new_conf, fp);
 
223
        fclose(fp);
 
224
    }
 
225
    free(new_conf);
 
226
 
138
227
 
139
228
    if( range_added )
140
229
    {
147
236
    }
148
237
    else
149
238
      {
150
 
         info = g_strdup_printf(_("Scope not found, couldnt add the new range\n"));
 
239
         info = g_strdup_printf(_("The scope was not found, could not add the new range.\n"));
151
240
         show_info(info);
152
241
         g_free(info);
153
242
      }