File: netcfg.c

package info (click to toggle)
netcfg 1.08
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,316 kB
  • ctags: 131
  • sloc: ansic: 2,122; sh: 89; makefile: 73
file content (254 lines) | stat: -rw-r--r-- 6,392 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* 
   netcfg.c - Configure a network via DHCP or manual configuration 
   for debian-installer

   Copyright (C) 2000-2002  David Kimdon <dwhedon@debian.org>
                            and others (see debian/copyright)
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <cdebconf/debconfclient.h>
#include <debian-installer.h>
#include <iwlib.h>
#include "netcfg.h"

static method_t netcfg_method = DHCP;

response_t netcfg_get_method(struct debconfclient *client)
{
    int iret, ret;

    iret = debconf_input(client, "medium", "netcfg/use_dhcp");
    ret = debconf_go(client);

    if (ret == 30)
      return GO_BACK;
    
    debconf_get(client, "netcfg/use_dhcp");

    if (strcmp(client->value, "true") == 0)
      netcfg_method = DHCP;
    else 
      netcfg_method = STATIC;

    if (iret == 30)
      return NOT_ASKED;

    return 0;
}

int main(int argc, char *argv[])
{
    int num_interfaces = 0;
    enum { BACKUP, GET_INTERFACE, GET_HOSTNAME_ONLY, GET_METHOD, GET_DHCP, GET_STATIC, WCONFIG, WCONFIG_ESSID, WCONFIG_WEP, QUIT } state = GET_INTERFACE;
    static struct debconfclient *client;
    static int requested_wireless_tools = 0;
	char **ifaces;
	char *defiface = NULL, *defwireless = NULL;
    response_t res;

    /* initialize libd-i */
    di_system_init("netcfg");

    parse_args (argc, argv);
    reap_old_files ();
    open_sockets();

    /* initialize debconf */
    client = debconfclient_new();
    debconf_capb(client, "backup");

    /* always always always default back to DHCP, unless you've specified
     * disable_dhcp on the command line. */
    debconf_get(client, "netcfg/disable_dhcp");
    
    if (!strcmp(client->value, "true"))
      debconf_set(client, "netcfg/use_dhcp", "false");
    else
      debconf_set(client, "netcfg/use_dhcp", "true");

    for (;;)
    {
	switch(state)
	{
	case BACKUP:
	    return 10;
	case GET_INTERFACE:
		/* Choose a default from ethtool-lite */
		if (get_all_ifs(1, &ifaces) > 1)
		{
			while (*ifaces)
			{
				interface_up(*ifaces);
				
				usleep(250);
				
				if (ethtool_lite (*ifaces) == 1) /* CONNECTED */
				{
					defiface = strdup(*ifaces);
					interface_down(*ifaces);
					break;
				}
				else
				{
					struct wireless_config wc;

					if (iw_get_basic_config(wfd, *ifaces, &wc) == 0)
					{
						wc.essid[0] = '\0';
						wc.essid_on = 0;

						iw_set_basic_config(wfd, *ifaces, &wc);
						
						sleep(1);

						iw_get_basic_config(wfd, *ifaces, &wc);

						if (!empty_str(wc.essid))
						{
							di_info("%s is associated with %s. Selecting as default", *ifaces, wc.essid);
							defiface = strdup(*ifaces);
							interface_down(*ifaces);
							break;
						}
						else
						{
							di_info("%s is not associated. Relegating to defwireless", *ifaces);
							if (defwireless != NULL)
								free (defwireless);
							defwireless = strdup(*ifaces);
						}
					}
					else
						di_info("%s is not a wireless interface. Continuing.", *ifaces);

					interface_down(*ifaces);
				}

				interface_down(*ifaces);

				ifaces++;
			}
		}
	    
		if (!defiface && defwireless)
			defiface = defwireless;

		if(netcfg_get_interface(client, &interface, &num_interfaces, defiface))
	      state = BACKUP;
	    else if (! interface || ! num_interfaces)
	      state = GET_HOSTNAME_ONLY;
	    else
	    {
	      if (is_wireless_iface (interface))
		state = WCONFIG;
	      else
		state = GET_METHOD;
	    }
	    break;
	case GET_HOSTNAME_ONLY:
	    if(netcfg_get_hostname(client, "netcfg/get_hostname", &hostname, 0))
	      state = BACKUP;
	    else
	    {
	      struct in_addr null_ipaddress;
	      null_ipaddress.s_addr = 0;
	      netcfg_write_common(null_ipaddress, hostname, NULL);
	      state = QUIT;
	    }
	    break;
	case GET_METHOD:
	    if ((res = netcfg_get_method(client)) == GO_BACK)
		state = (num_interfaces == 1) ? BACKUP : GET_INTERFACE;
	    else
	    {
              if (netcfg_method == DHCP) 
                state = GET_DHCP;
              else
                state = GET_STATIC;
	    }
	    break;

	case GET_DHCP:
            switch (netcfg_activate_dhcp(client))
            {
              case 0:
                state = QUIT;
                break;
              case 10:
                /*
                 * It doesn't make sense to go back to GET_METHOD because
                 * the user has already been asked whether he wants to
                 * try an alternate method.
                 */
                state = (num_interfaces == 1) ? BACKUP : GET_INTERFACE;
                break;
              case 15:
                state = GET_STATIC;
                break;
              default:
                return 1;
            }
	    break;

	case GET_STATIC:
        {
            int ret;
	    /* Misnomer - this should actually take care of activation */
	    if ((ret = netcfg_get_static(client)) == 10)
		state = GET_INTERFACE;
            else if (ret)
                state = GET_METHOD;
	    else
	        state = QUIT;
	    break;
        }
        
        case WCONFIG:
	    if (requested_wireless_tools == 0)
	    {
	      di_exec_shell_log("apt-install wireless-tools");
	      requested_wireless_tools = 1;
	    }
	    state = WCONFIG_ESSID;
	    break;

	case WCONFIG_ESSID:
	    if (netcfg_wireless_set_essid (client, interface, NULL) == GO_BACK)
	      state = BACKUP;
	    else
	      state = WCONFIG_WEP;
	    break;
            
	case WCONFIG_WEP:
	    if (netcfg_wireless_set_wep (client, interface) == GO_BACK)
	      state = WCONFIG_ESSID;
	    else
	      state = GET_METHOD;
	    break;

	case QUIT:
	    return 0;
	}
    }

}