~costales/gui-ufw/renamed_object

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
# Gufw 19.10.0 - http://gufw.org
# Copyright (C) 2008-2019 Marcos Alvarez Costales https://launchpad.net/~costales
#
# Gufw 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 3 of the License, or
# (at your option) any later version.
#
# Gufw 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 Gufw; if not, see http://www.gnu.org/licenses for more
# information.

import os, sys
from stat import *

class Instance:
    def __init__(self):
        self.pid_file = '/tmp/gufw.pid'
        self._check_is_root()
        self._check_etc_writable()
        self._check_instance()
        self._start_application()
    
    def _check_is_root(self):
        if os.geteuid() != 0:
            from gi.repository import Gtk
            dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Run as superuser")
            dialog.format_secondary_text("Just run this command in the shell: gufw  or  sudo gufw")
            dialog.run()
            dialog.destroy()
            exit(0)
    
    def _check_etc_writable(self):
        if bin(os.stat("/etc")[ST_MODE])[-5] == '0' and bin(os.stat("/etc")[ST_MODE])[-2] == '0': # get chmod /etc
            return
        
        from gi.repository import Gtk
        import gettext
        from gettext import gettext as _
        gettext.textdomain('gufw')
        
        dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, _("Error: /etc is writable"))
        dialog.format_secondary_text(_("Your /etc directory is writable.\nFix it running from Terminal:") + "\n\nsudo chmod 755 /etc")
        dialog.run()
        dialog.destroy()
        exit(0)

    def _check_instance(self):
        if not os.path.isfile(self.pid_file):
            return
        
        # Read the pid from file
        pid = 0
        try:
            pid_file = open(self.pid_file, 'rt')
            data = pid_file.read()
            pid_file.close()
            pid = int(data)
        except Exception:
            pass
        
        # Check whether the process specified exists
        if pid == 0:
            return
        try:
            os.kill(pid, 0) # exception if the pid is invalid
        except Exception:
            return
        
        from gi.repository import Gtk
        import gettext
        from gettext import gettext as _
        gettext.textdomain('gufw')
        
        dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, _("Please, just one Gufw's instance"))
        dialog.format_secondary_text(_("Gufw is already running. If this is wrong, remove the file: ") + self.pid_file)
        dialog.run()
        dialog.destroy()
        exit(0)
    
    def _start_application(self):
        if self._under_ssh():
            return
        
        pid_file = open(self.pid_file, 'wt')
        pid_file.write(str(os.getpid()))
        pid_file.close()
    
    def _under_ssh(self):
        try:
            if sys.argv[1] == '-ssh':
                return True
        except Exception:
            pass
        
        return False
    
    def exit_app(self):
        try:
            os.remove(self.pid_file)
        except Exception:
            pass