This file is indexed.

/usr/share/pyshared/wicd/misc.py is in python-wicd 1.7.2.4-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
""" misc - miscellaneous functions for wicd

This module contains a large variety of utility functions used
throughout wicd.

"""

#
#   Copyright (C) 2007 - 2009 Adam Blackburn
#   Copyright (C) 2007 - 2009 Dan O'Reilly
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License Version 2 as
#   published by the Free Software Foundation.
#
#   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, see <http://www.gnu.org/licenses/>.
#

import os
import locale
import sys
import re
import string
import gobject
from threading import Thread
from subprocess import Popen, STDOUT, PIPE, call
from commands import getoutput
from itertools import repeat, chain, izip
from pipes import quote

from wicd.translations import _

# wicd imports
import wpath

# Connection state constants
NOT_CONNECTED = 0
CONNECTING = 1
WIRELESS = 2
WIRED = 3
SUSPENDED = 4

# Automatic app selection constant
AUTO = 0

# DHCP Clients
DHCLIENT = 1
DHCPCD = 2
PUMP = 3
UDHCPC = 4

# Link detection tools
ETHTOOL = 1
MIITOOL = 2

# Route flushing tools
IP = 1
ROUTE = 2

# Graphical sudo apps
GKSUDO = 1
KDESU = 2
KTSUSS = 3
_sudo_dict = { 
    AUTO : "",
    GKSUDO : "gksudo",
    KDESU : "kdesu",
    KTSUSS: "ktsuss",
}

_status_dict = {
    'aborted': _('Connection Cancelled'),
    'association_failed': _('Connection failed: Could not contact the wireless access point.'),
    'bad_pass': _('Connection Failed: Bad password'),
    'configuring_interface': _('Configuring wireless interface...'),
    'dhcp_failed': _('Connection Failed: Unable to Get IP Address'),
    'done': _('Done connecting...'),
    'failed': _('Connection Failed.'),
    'flushing_routing_table': _('Flushing the routing table...'),
    'generating_psk': _('Generating PSK...'),
    'generating_wpa_config': _('Generating WPA configuration file...'),
    'interface_down': _('Putting interface down...'),
    'interface_up': _('Putting interface up...'),
    'no_dhcp_offers': _('Connection Failed: No DHCP offers received.'),
    'resetting_ip_address': _('Resetting IP address...'),
    'running_dhcp': _('Obtaining IP address...'),
    'setting_broadcast_address': _('Setting broadcast address...'),
    'setting_static_dns': _('Setting static DNS servers...'),
    'setting_static_ip': _('Setting static IP addresses...'),
    'success': _('Connection successful.'),
    'validating_authentication': _('Validating authentication...'),
    'verifying_association': _('Verifying access point association...'),
}

class WicdError(Exception):
    pass
    

def Run(cmd, include_stderr=False, return_pipe=False,
        return_obj=False, return_retcode=True):
    """ Run a command.

    Runs the given command, returning either the output
    of the program, or a pipe to read output from.

    keyword arguments --
    cmd - The command to execute
    include_std_err - Boolean specifying if stderr should
                      be included in the pipe to the cmd.
    return_pipe - Boolean specifying if a pipe to the
                  command should be returned.  If it is
                  False, all that will be returned is
                  one output string from the command.
    return_obj - If True, Run will return the Popen object
                 for the command that was run.

    """
    if not isinstance(cmd, list):
        cmd = to_unicode(str(cmd))
        cmd = cmd.split()
    if include_stderr:
        err = STDOUT
        fds = True
    else:
        err = None
        fds = False
    if return_obj:
        std_in = PIPE
    else:
        std_in = None
    
    # We need to make sure that the results of the command we run
    # are in English, so we set up a temporary environment.
    tmpenv = os.environ.copy()
    tmpenv["LC_ALL"] = "C"
    tmpenv["LANG"] = "C"
    
    try:
        f = Popen(cmd, shell=False, stdout=PIPE, stdin=std_in, stderr=err,
                  close_fds=fds, cwd='/', env=tmpenv)
    except OSError, e:
        print "Running command %s failed: %s" % (str(cmd), str(e))
        return ""
        
    if return_obj:
        return f
    if return_pipe:
        return f.stdout
    else:
        return f.communicate()[0]
    
def LaunchAndWait(cmd):
    """ Launches the given program with the given arguments, then blocks.

    cmd : A list contained the program name and its arguments.

    returns: The exit code of the process.
    
    """
    if not isinstance(cmd, list):
        cmd = to_unicode(str(cmd))
        cmd = cmd.split()
    p = Popen(cmd, shell=False, stdout=PIPE, stderr=STDOUT, stdin=None)
    return p.wait()

def IsValidIP(ip):
    """ Make sure an entered IP is valid. """
    if not ip: return False

    ipNumbers = ip.split('.')
    if len(ipNumbers) < 4:
        return False

    for number in ipNumbers:
        if not number.isdigit() or int(number) > 255:
            return False

    return ipNumbers

def PromptToStartDaemon():
    """ Prompt the user to start the daemon """
    daemonloc = wpath.sbin + 'wicd'
    sudo_prog = choose_sudo_prog()
    if not sudo_prog:
        return False
    if "gksu" in sudo_prog or "ktsuss" in sudo_prog:
        msg = '--message'
    else:
        msg = '--caption'
    sudo_args = [sudo_prog, msg, 
                 _("Wicd needs to access your computer's network cards."),
                 daemonloc]
    os.spawnvpe(os.P_WAIT, sudo_prog, sudo_args, os.environ)
    return True

def RunRegex(regex, string):
    """ runs a regex search on a string """
    m = regex.search(string)
    if m:
        return m.groups()[0]
    else:
        return None

def WriteLine(my_file, text):
    """ write a line to a file """
    my_file.write(text + "\n")

def ExecuteScripts(scripts_dir, verbose=False, extra_parameters=()):
    """ Execute every executable file in a given directory. """
    if not os.path.exists(scripts_dir):
        return
    for obj in sorted(os.listdir(scripts_dir)):
        if obj.startswith(".") or obj.endswith(("~", ".new", ".orig")):
            continue
        obj = os.path.abspath(os.path.join(scripts_dir, obj))
        if os.path.isfile(obj) and os.access(obj, os.X_OK):
            ExecuteScript(os.path.abspath(obj), verbose=verbose,
                          extra_parameters=extra_parameters)

def ExecuteScript(script, verbose=False, extra_parameters=()):
    """ Execute a command and send its output to the bit bucket. """
    extra_parameters = [ quote(s) for s in extra_parameters ]
    params = ' '.join(extra_parameters)
    # escape script name
    script = quote(script)
    if verbose:
        print "Executing %s with params %s" % (script, params)
    ret = call('%s %s > /dev/null 2>&1' % (script, params), shell=True)
    if verbose:
        print "%s returned %s" % (script, ret)

def ReadFile(filename):
    """ read in a file and return it's contents as a string """
    if not os.path.exists(filename):
        return None
    my_file = open(filename,'r')
    data = my_file.read().strip()
    my_file.close()
    return str(data)

def to_bool(var):
    """ Convert a string to type bool, but make "False"/"0" become False. """
    if var in ("False", "0"):
        var = False
    else:
        var = bool(var)
    return var

def Noneify(variable):
    """ Convert string types to either None or booleans"""
    #set string Nones to real Nones
    if variable in ("None", "", None):
        return None
    if variable in ("False", "0"):
        return False
    if variable in ("True", "1"):
        return True
    return variable

def ParseEncryption(network):
    """ Parse through an encryption template file

    Parses an encryption template, reading in a network's info
    and creating a config file for it

    """
    enctemplate = open(wpath.encryption + network["enctype"])
    template = enctemplate.readlines()
    if network.get('essid'):
        config_file = "ap_scan=1\n"
    else:
        config_file = "ap_scan=0\n"
    should_replace = False
    for index, line in enumerate(template):
        if not should_replace:
            if line.strip().startswith('---'):
                should_replace = True
        else:
            if line.strip().startswith("}"):
                # This is the last line, so we just write it.
                config_file = ''.join([config_file, line])
            elif "$_" in line: 
                cur_val = re.findall('\$_([A-Z0-9_]+)', line)
                if cur_val:
                    if cur_val[0] == 'SCAN':
                        #TODO should this be hardcoded?
                        line = line.replace("$_SCAN", "1")
                        config_file = ''.join([config_file, line])
                    else:
                        rep_val = network.get(cur_val[0].lower())
                        if rep_val:
                            line = line.replace("$_%s" % cur_val[0], 
                                                str(rep_val))
                            config_file = ''.join([config_file, line])
                        else:
                            print "Ignoring template line: '%s'" % line
                else:
                    print "Weird parsing error occurred"
            else:  # Just a regular entry.
                config_file = ''.join([config_file, line])

    # Write the data to the files then chmod them so they can't be read 
    # by normal users.
    if network.get('bssid'):
        file_name = network['bssid'].replace(":", "").lower()
    else:
        file_name = 'wired'
    file_loc = os.path.join(wpath.networks, file_name)
    f = open(file_loc, "w")
    os.chmod(file_loc, 0600)
    os.chown(file_loc, 0, 0)
    # We could do this above, but we'd like to read protect
    # them before we write, so that it can't be read.
    f.write(config_file)
    f.close()

def LoadEncryptionMethods(wired = False):
    """ Load encryption methods from configuration files

    Loads all the encryption methods from the template files
    in /encryption/templates into a data structure.  To be
    loaded, the template must be listed in the "active" file.

    """
    if wired:
        active_fname = "active_wired"
    else:
        active_fname = "active"
    try:
        enctypes = open(wpath.encryption + active_fname,"r").readlines()
    except IOError, e:
        print "Fatal Error: template index file is missing."
        raise IOError(e)
    
    # Parse each encryption method
    encryptionTypes = []
    for enctype in enctypes:
        parsed_template = _parse_enc_template(enctype.strip())
        if parsed_template:
            encryptionTypes.append(parsed_template)
    return encryptionTypes

def __parse_field_ent(fields, field_type='require'):
    fields = fields.split(" ")
    ret = []
    # We need an even number of entries in the line for it to be valid.
    if (len(fields) % 2) != 0:
        return None
    else:
        for val, disp_val in grouper(2, fields, fillvalue=None):
            if val.startswith("*") or not disp_val.startswith("*"):
                return None
            ret.append([val, disp_val[1:]])
        return ret

def _parse_enc_template(enctype):
    """ Parse an encryption template. """
    def parse_ent(line, key):
        return line.replace(key, "").replace("=", "").strip()

    try:
        f = open(os.path.join(wpath.encryption, enctype), "r")
    except IOError:
        print "Failed to open template file %s" % enctype
        return None

    cur_type = {}
    cur_type["type"] = enctype
    cur_type["fields"] = []
    cur_type['optional'] = []
    cur_type['required'] = []
    cur_type['protected'] = []
    cur_type['name'] = ""
    for index, line in enumerate(f):
        if line.startswith("name") and not cur_type["name"]:
            cur_type["name"] = parse_ent(line, "name")
        elif line.startswith("require"):
            cur_type["required"] = __parse_field_ent(parse_ent(line, "require"))
            if not cur_type["required"]:
                # An error occured parsing the require line.
                print "Invalid 'required' line found in template %s" % enctype
                continue
        elif line.startswith("optional"):
            cur_type["optional"] = __parse_field_ent(parse_ent(line,
                                                               "optional"),
                                                     field_type="optional")
            if not cur_type["optional"]:
                # An error occured parsing the optional line.
                print "Invalid 'optional' line found in template %s" % enctype
                continue
        elif line.startswith("protected"):
            cur_type["protected"] = __parse_field_ent(parse_ent(line, "protected"),
                                                    field_type="protected")
            if not cur_type["protected"]:
                # An error occured parsing the protected line.
                print "Invalid 'protected' line found in template %s" % enctype
                continue
        elif line.startswith("----"):
            # We're done.
            break
    f.close()
    if not cur_type["required"]:
        print "Failed to find a 'require' line in template %s" % enctype
        return None
    if not cur_type["name"]:
        print "Failed to find a 'name' line in template %s" % enctype
        return None
    else:
        return cur_type

def noneToString(text):
    """ Convert None, "None", or "" to string type "None"

    Used for putting text in a text box.  If the value to put in is 'None',
    the box will be blank.

    """
    if text in (None, ""):
        return "None"
    else:
        return to_unicode(text)

def sanitize_config(s):
    """ Sanitize property names to be used in config-files. """
    allowed = string.ascii_letters + '_' + string.digits
    table = string.maketrans(allowed, ' ' * len(allowed))
    return s.encode('ascii', 'replace').translate(None, table)

def sanitize_escaped(s):
    """ Sanitize double-escaped unicode strings. """
    lastpos = -1
    while True:
        lastpos = s.find('\\x', lastpos + 1)
        #print lastpos
        if lastpos == -1:
            break
        c = s[lastpos+2:lastpos+4]  # i.e. get the next two characters
        s = s.replace('\\x'+c, chr(int(c, 16)))
    return s

def to_unicode(x):
    """ Attempts to convert a string to utf-8. """
    # If this is a unicode string, encode it and return
    if not isinstance(x, basestring):
        return x
    if isinstance(x, unicode):
        return x.encode('utf-8')

    x = sanitize_escaped(x)

    encoding = locale.getpreferredencoding()
    try:
        ret = x.decode(encoding).encode('utf-8')
    except UnicodeError:
        try:
            ret = x.decode('utf-8').encode('utf-8')
        except UnicodeError:
            try:
                ret = x.decode('latin-1').encode('utf-8')
            except UnicodeError:
                ret = x.decode('utf-8', 'replace').encode('utf-8')
            
    return ret
    
def RenameProcess(new_name):
    """ Renames the process calling the function to the given name. """
    if 'linux' not in sys.platform:
        print 'Unsupported platform'
        return False
    try:
        import ctypes
        from ctypes.util import find_library
        libc = ctypes.CDLL(find_library('c'))
        libc.prctl(15, new_name, 0, 0, 0)
        return True
    except:
        print "rename failed"
        return False
    
def detect_desktop_environment():
    """ Try to determine which desktop environment is in use. 
    
    Choose between kde, gnome, or xfce based on environment
    variables and a call to xprop.
    
    """
    desktop_environment = 'generic'
    if os.environ.get('KDE_FULL_SESSION') == 'true':
        desktop_environment = 'kde'
    elif os.environ.get('GNOME_DESKTOP_SESSION_ID'):
        desktop_environment = 'gnome'
    else:
        try:
            info = getoutput('xprop -root _DT_SAVE_MODE')
            if ' = "xfce4"' in info:
                desktop_environment = 'xfce'
        except (OSError, RuntimeError):
            pass
    return desktop_environment

def get_sudo_cmd(msg, prog_num=0):
    """ Returns a graphical sudo command for generic use. """
    sudo_prog = choose_sudo_prog(prog_num)
    if not sudo_prog: return None
    if re.search("(ktsuss|gksu|gksudo)$", sudo_prog):
        msg_flag = "-m"
    else:
        msg_flag = "--caption"
    return [sudo_prog, msg_flag, msg]

def choose_sudo_prog(prog_num=0):
    """ Try to intelligently decide which graphical sudo program to use. """
    if prog_num:
        return find_path(_sudo_dict[prog_num])
    desktop_env = detect_desktop_environment()
    env_path = os.environ['PATH'].split(":")
    paths = []
    
    if desktop_env == "kde":
        progs = ["kdesu", "kdesudo", "ktsuss"]
    else:
        progs = ["gksu", "gksudo", "ktsuss"]
        
    for prog in progs:
        paths.extend([os.path.join(p, prog) for p in env_path])
        
    for path in paths:
        if os.path.exists(path):
            return path
    return ""

def find_path(cmd):
    """ Try to find a full path for a given file name. 
    
    Search the all the paths in the environment variable PATH for
    the given file name, or return None if a full path for
    the file can not be found.
    
    """
    paths = os.getenv("PATH").split(':')
    if not paths:
        paths = ["/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin",
                 "/sbin", "/bin"]
    for path in paths:
        if os.path.exists(os.path.join(path, cmd)):
            return os.path.join(path, cmd)
    return None

def noneToBlankString(text):
    """ Converts NoneType or "None" to a blank string. """
    if text in (None, "None"):
        return ""
    else:
        return str(text)

def stringToNone(text):
    """ Performs opposite function of noneToString. """
    if text in ("", None, "None"):
        return None
    else:
        return str(text)

def checkboxTextboxToggle(checkbox, textboxes):
    for textbox in textboxes:
        textbox.set_sensitive(checkbox.get_active())

def threaded(f):
    """ A decorator that will make any function run in a new thread. """

    def wrapper(*args, **kwargs):
        t = Thread(target=f, args=args, kwargs=kwargs)
        t.setDaemon(True)
        t.start()

    wrapper.__name__ = f.__name__
    wrapper.__dict__ = f.__dict__
    wrapper.__doc__ = f.__doc__
    wrapper.__module__ = f.__module__

    return wrapper

def timeout_add(time, func, milli=False):
    """ Convience function for running a function on a timer. """
    if hasattr(gobject, "timeout_add_seconds") and not milli:
        return gobject.timeout_add_seconds(time, func)
    else:
        if not milli: time = time * 1000
        return gobject.timeout_add(time, func)

def izip_longest(*args, **kwds):
    """ Implement the itertools.izip_longest method.
    
    We implement the method here because its new in Python 2.6.
    
    """
    # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
    fillvalue = kwds.get('fillvalue')
    def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
        yield counter()         # yields the fillvalue, or raises IndexError
    fillers = repeat(fillvalue)
    iters = [chain(it, sentinel(), fillers) for it in args]
    try:
        for tup in izip(*iters):
            yield tup
    except IndexError:
        pass

def grouper(n, iterable, fillvalue=None):
    """ Iterate over several elements at once

    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"

    """
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)