summaryrefslogtreecommitdiffstats
path: root/textw/network_text.py
blob: c1734c4f5bf583c018f8575cc85ab8232a9bd804 (plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#
# network_text.py: text mode network configuration dialogs
#
# Jeremy Katz <katzj@redhat.com>
# Michael Fulbright <msf@redhat.com>
#
# Copyright 2000-2003 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import iutil
import os
import isys
import string
from network import isPtpDev, anyUsingDHCP, sanityCheckIPString
from network import sanityCheckHostname
from snack import *
from constants_text import *
from rhpl.translate import _
from rhpl.log import log


def badIPDisplay(screen, the_ip):
    ButtonChoiceWindow(screen, _("Invalid IP string"),
                       _("The entered IP '%s' is not a valid IP.") %(the_ip,),
                       buttons = [ _("OK") ])
    return

class NetworkDeviceWindow:
    def setsensitive(self):
        if self.dhcpCb.selected ():
            sense = FLAGS_SET
        else:
            sense = FLAGS_RESET

        for n in self.dhcpentries.values():
            n.setFlags (FLAG_DISABLED, sense)

    def calcNM(self):
        ip = self.entries["ipaddr"].value()
        if ip and not self.entries["netmask"].value ():
            try:
                mask = "255.255.255.0"
            except ValueError:
                return

            self.entries["netmask"].set (mask)
        
    def runScreen(self, screen, network, dev, showonboot=1):
        boot = dev.get("bootproto")
        onboot = dev.get("onboot")

        devnames = self.devices.keys()
        devnames.sort()
        if devnames.index(dev.get("DEVICE")) == 0 and not onboot:
            onbootIsOn = 1
        else:
            onbootIsOn = (onboot == "yes")
        if not boot:
            boot = "dhcp"

	options = [(_("IP Address"), "ipaddr", 1),
		   (_("Netmask"),    "netmask", 1)]
        if (isPtpDev(dev.info["DEVICE"])):
	    newopt = (_("Point to Point (IP)"), "remip", 1)
	    options.append(newopt)

        if isys.isWireless(dev.info["DEVICE"]):
            wireopt = [(_("ESSID"), "essid", 0),
                       (_("Encryption Key"), "key", 0)]
            options.extend(wireopt)

	descr = dev.get("desc")
        hwaddr = dev.get("hwaddr")
	if descr is None or len(descr) == 0:
	    descr = None
        if hwaddr is None or len(hwaddr) == 0:
            hwaddr = None

	topgrid = Grid(1, 3)

        topgrid.setField(Label (_("Network Device: %s")
                                %(dev.info['DEVICE'],)),
                         0, 0, padding = (0, 0, 0, 0), anchorLeft = 1,
                         growx = 1)

	if descr is not None:
	    topgrid.setField(Label (_("Description: %s") % (descr[:70],)),
			     0, 1, padding = (0, 0, 0, 0), anchorLeft = 1,
			     growx = 1)
        if hwaddr is not None:
            topgrid.setField(Label (_("Hardware Address: %s") %(hwaddr,)),
                             0, 2, padding = (0, 0, 0, 0), anchorLeft = 1,
                             growx = 1)

	botgrid = Grid(2, 2+len(options))
        self.dhcpCb = Checkbox(_("Configure using DHCP"),
                               isOn = (boot == "dhcp"))

	if not showonboot:
	    ypad = 1
	else:
	    ypad = 0

	currow = 0
        botgrid.setField(self.dhcpCb, 0, currow, anchorLeft = 1, growx = 1,
			 padding = (0, 0, 0, ypad))
	currow += 1
        
        self.onbootCb = Checkbox(_("Activate on boot"), isOn = onbootIsOn)
	if showonboot:
	    botgrid.setField(self.onbootCb, 0, currow, anchorLeft = 1, growx = 1,
			     padding = (0, 0, 0, 1))
	    currow += 1

	row = currow
        self.entries = {}
        self.dhcpentries = {}
        for (name, opt, dhcpdep) in options:
            botgrid.setField(Label(name), 0, row, anchorLeft = 1)

            entry = Entry (16)
            entry.set(dev.get(opt))
            botgrid.setField(entry, 1, row, padding = (1, 0, 0, 0))

            self.entries[opt] = entry
            if dhcpdep:
                self.dhcpentries[opt] = entry
            row = row + 1

        self.dhcpCb.setCallback(self.setsensitive)
        self.entries["ipaddr"].setCallback(self.calcNM)

        bb = ButtonBar(screen, (TEXT_OK_BUTTON, TEXT_BACK_BUTTON))

        toplevel = GridFormHelp(screen, _("Network Configuration for %s") %
                                (dev.info['DEVICE']), 
                                "networkdev", 1, 4)

        toplevel.add(topgrid,  0, 0, (0, 0, 0, 1), anchorLeft = 1)
        toplevel.add(botgrid,  0, 1, (0, 0, 0, 1), anchorLeft = 1)
        toplevel.add(bb, 0, 3, growx = 1)

        self.setsensitive()
        
        while 1:
            result = toplevel.run()
            rc = bb.buttonPressed (result)

            if rc == TEXT_BACK_CHECK:
                screen.popWindow()
                return INSTALL_BACK

            if self.onbootCb.selected() != 0:
                dev.set(("onboot", "yes"))
            else:
                dev.unset("onboot")

            if self.dhcpCb.selected() != 0:
                dev.set(("bootproto", "dhcp"))
                dev.unset("ipaddr", "netmask", "network", "broadcast", "remip")
            else:
                ip = self.entries["ipaddr"].value()
                nm = self.entries["netmask"].value()
                try:
                    (net, bc) = isys.inet_calcNetBroad(ip, nm)
                except:
                    if self.onbootCb.selected() != 0:
                        ButtonChoiceWindow(screen, _("Invalid information"),
                                           _("You must enter valid IP "
                                             "information to continue"),
                                           buttons = [ _("OK") ])
                        continue
                    else:
                        net = ""
                        bc = ""

                dev.set(("bootproto", "static"))
                if bc and net:
                    dev.set(("broadcast", bc), ("network", net))

            for val in self.entries.keys():
                if ((self.dhcpCb.selected() != 0) and
                    self.dhcpentries.has_key(val)):
                    continue
                if self.entries[val].value():
                    dev.set((val, self.entries[val].value()))
                        

            break

        screen.popWindow()
        return INSTALL_OK


    def __call__(self, screen, network, dir, intf, id, showonboot=1):

        self.devices = network.available()
        if not self.devices:
            return INSTALL_NOOP

        list = self.devices.keys()
        list.sort()
        devLen = len(list)
        if dir == 1:
            currentDev = 0
        else:
            currentDev = devLen - 1

        while currentDev < devLen and currentDev >= 0:
            rc = self.runScreen(screen, network,
                                self.devices[list[currentDev]],
                                showonboot)
            if rc == INSTALL_BACK:
                currentDev = currentDev - 1
            else:
                currentDev = currentDev + 1

        if currentDev < 0:
            return INSTALL_BACK
        else:
            return INSTALL_OK

class NetworkGlobalWindow:
    def getFirstGatewayGuess(self, devices):
        list = devices.keys()
        list.sort()
        for dev in list:
            thedev = devices[dev]
            bootproto = thedev.get("bootproto")
            if bootproto and bootproto == "dhcp":
                continue
            onboot = thedev.get("onboot")
            if onboot and onboot == "no":
                continue
            bcast = thedev.get("broadcast")
            if not bcast:
                continue
            return isys.inet_calcGateway(bcast)
        return ""
            
    def __call__(self, screen, network, dir, intf, id):
        devices = network.available()
        if not devices:
            return INSTALL_NOOP

        # we don't let you set gateway/dns if you've got any interfaces
        # using dhcp (for better or worse)
        if anyUsingDHCP(devices):
            return INSTALL_NOOP

        thegrid = Grid(2, 4)

        thegrid.setField(Label(_("Gateway:")), 0, 0, anchorLeft = 1)
        gwEntry = Entry(16)
        # if it's set already, use that... otherwise, get the first
        # non-dhcp and active device and use it to guess the gateway
        if network.gateway:
            gwEntry.set(network.gateway)
        else:
            gwEntry.set(self.getFirstGatewayGuess(devices))
        thegrid.setField(gwEntry, 1, 0, padding = (1, 0, 0, 0))
        
        thegrid.setField(Label(_("Primary DNS:")), 0, 1, anchorLeft = 1)
        ns1Entry = Entry(16)
        ns1Entry.set(network.primaryNS)
        thegrid.setField(ns1Entry, 1, 1, padding = (1, 0, 0, 0))
        
        thegrid.setField(Label(_("Secondary DNS:")), 0, 2, anchorLeft = 1)
        ns2Entry = Entry(16)
        ns2Entry.set(network.secondaryNS)
        thegrid.setField(ns2Entry, 1, 2, padding = (1, 0, 0, 0))
        
        thegrid.setField(Label(_("Tertiary DNS:")), 0, 3, anchorLeft = 1)
        ns3Entry = Entry(16)
        ns3Entry.set(network.ternaryNS)
        thegrid.setField(ns3Entry, 1, 3, padding = (1, 0, 0, 0))

        bb = ButtonBar (screen, (TEXT_OK_BUTTON, TEXT_BACK_BUTTON))

        toplevel = GridFormHelp (screen, _("Miscellaneous Network Settings"),
				 "miscnetwork", 1, 3)
        toplevel.add (thegrid, 0, 0, (0, 0, 0, 1), anchorLeft = 1)
        toplevel.add (bb, 0, 2, growx = 1)

        while 1:
            result = toplevel.run()
            rc = bb.buttonPressed (result)

            if rc == TEXT_BACK_CHECK:
                screen.popWindow()
                return INSTALL_BACK

            val = gwEntry.value()
            if val and sanityCheckIPString(val) is None:
                badIPDisplay(screen, val)
                continue
            network.gateway = val

            val = ns1Entry.value()
            if val and sanityCheckIPString(val) is None:
                badIPDisplay(screen, val)
                continue
            network.primaryNS = val

            val = ns2Entry.value()
            if val and sanityCheckIPString(val) is None:
                badIPDisplay(screen, val)
                continue
            network.secondaryNS = val

            val = ns3Entry.value()
            if val and sanityCheckIPString(val) is None:
                badIPDisplay(screen, val)
                continue
            network.ternaryNS = val
            break

        screen.popWindow()        
        return INSTALL_OK
        

class HostnameWindow:
    def hostTypeCb(self, (radio, hostEntry)):
        if radio.getSelection() != "manual":
            sense = FLAGS_SET
        else:
            sense = FLAGS_RESET

        hostEntry.setFlags(FLAG_DISABLED, sense)
            
    def __call__(self, screen, network, dir, intf, id):
        devices = network.available ()
        if not devices:
            return INSTALL_NOOP

        # figure out if the hostname is currently manually set
        if anyUsingDHCP(devices):
            if (network.hostname != "localhost.localdomain" and
                network.overrideDHCPhostname):
                manual = 1
            else:
                manual = 0
        else:
            manual = 1

        thegrid = Grid(2, 2)
        radio = RadioGroup()
        autoCb = radio.add(_("automatically via DHCP"), "dhcp",
                                not manual)
        thegrid.setField(autoCb, 0, 0, growx = 1, anchorLeft = 1)

        manualCb = radio.add(_("manually"), "manual", manual)
        thegrid.setField(manualCb, 0, 1, anchorLeft = 1)
        hostEntry = Entry(24)
        if network.hostname != "localhost.localdomain":
            hostEntry.set(network.hostname)
        thegrid.setField(hostEntry, 1, 1, padding = (1, 0, 0, 0),
                         anchorLeft = 1)            

        # disable the dhcp if we don't have any dhcp
        if anyUsingDHCP(devices):
            autoCb.w.checkboxSetFlags(FLAG_DISABLED, FLAGS_RESET)            
        else:
            autoCb.w.checkboxSetFlags(FLAG_DISABLED, FLAGS_SET)

        self.hostTypeCb((radio, hostEntry))

        autoCb.setCallback(self.hostTypeCb, (radio, hostEntry))
        manualCb.setCallback(self.hostTypeCb, (radio, hostEntry))

        toplevel = GridFormHelp(screen, _("Hostname Configuration"),
                                "hostname", 1, 4)
        text = TextboxReflowed(55,
                               _("If your system is part of a larger network "
                                 "where hostnames are assigned by DHCP, "
                                 "select automatically via DHCP. Otherwise, "
                                 "select manually and enter in a hostname for "
                                 "your system. If you do not, your system "
                                 "will be known as 'localhost.'"))
        toplevel.add(text, 0, 0, (0, 0, 0, 1))

        bb = ButtonBar(screen, (TEXT_OK_BUTTON, TEXT_BACK_BUTTON))
        toplevel.add(thegrid, 0, 1, padding = (0, 0, 0, 1))
        toplevel.add(bb, 0, 2, growx = 1)

        while 1:
            result = toplevel.run()
            rc = bb.buttonPressed(result)
            
            if rc == TEXT_BACK_CHECK:
                screen.popWindow()
                return INSTALL_BACK

            if radio.getSelection() != "manual":
                network.overrideDHCPhostname = 0
                network.hostname = "localhost.localdomain"
            else:
                hname = string.strip(hostEntry.value())
                if len(hname) == 0:
                    ButtonChoiceWindow(screen, _("Invalid Hostname"),
                                       _("You have not specified a hostname."),
                                       buttons = [ _("OK") ])
                    continue
                neterrors = sanityCheckHostname(hname)
                if neterrors is not None:
                    ButtonChoiceWindow(screen, _("Invalid Hostname"),
                                       _("The hostname \"%s\" is not valid "
                                         "for the following reason:\n\n%s")
                                       %(hname, neterrors),
                                       buttons = [ _("OK") ])
                    continue

                network.overrideDHCPhostname = 1
                network.hostname = hname
            break

        screen.popWindow()
        return INSTALL_OK