summaryrefslogtreecommitdiffstats
path: root/tcosmonitor.py
blob: 1538a8d4de0e510aebde796008a75f1ed52ed744 (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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#    TcosMonitor version __VERSION__
#
# Copyright (c) 2006-2011 Mario Izquierdo <mariodebian@gmail.com>
#
# This package 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 package 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import sys
import os

#if not os.path.isfile("Initialize.py"):
#    #print "DEBUG: append tcosmonitor dir"
#    sys.path.append("/usr/share/tcosmonitor")

import pygtk
pygtk.require('2.0')
import gtk

from time import time
import getopt
from gettext import gettext as _
from threading import Thread

gtk.gdk.threads_init()

import gobject

from tcosmonitor import shared

import grp, pwd

import gettext

# Medusa 
import asyncore
from medusa import default_handler
from medusa import filesys
from medusa import ftp_server
from medusa import status_handler

# M2Crypto
try:
    from tcosmonitor import ftps_server
except:
    print "NO FTP TLS support"
    pass
from threading import Thread
import M2Crypto

class TCOSFTPSERVER (Thread):
    def __init__(self):
         self.FTP_PORT = 8997
         M2Crypto.Rand.load_file('/tmp/randpooltcos.dat', -1) 
         self.ssl_ctx=M2Crypto.SSL.Context('sslv23')
         self.ssl_ctx.load_cert('/etc/tcos/ssl/tcos_server.pem')
         self.ssl_ctx.load_client_CA('/etc/tcos/ssl/tcos_ca.crt')
         self.ssl_ctx.set_verify(M2Crypto.SSL.verify_none, 10)
         self.ssl_ctx.set_session_id_ctx('127.0.0.1:9443')
         self.ssl_ctx.set_tmp_dh('/etc/tcos/ssl/tcos_dh1024.pem')
         self.ssl_ctx.set_info_callback()
         Thread.__init__(self)
    def run(self):
         if not os.path.isdir("/tmp/tcos_share"): os.mkdir("/tmp/tcos_share")
         self.fauthz = ftp_server.anon_authorizer('/tmp/tcos_share')
         self.ftps = ftps_server.ftp_tls_server(self.fauthz, self.ssl_ctx, port=self.FTP_PORT)
         self.ftps.status()
         self.sh=status_handler.status_extension([self.ftps])
         M2Crypto.Rand.save_file('/tmp/randpooltcos.dat')
         asyncore.loop()
         
         
gettext.bindtextdomain(shared.PACKAGE, shared.LOCALE_DIR)
gettext.textdomain(shared.PACKAGE)


def print_debug(txt):
    if shared.debug:
        print >>sys.stderr, "%s::%s" % ("tcosmonitor", txt)
        #print("%s::%s" % ("tcosmonitor", txt), file=sys.stderr)
    return

def crono(start, txt):
    print_debug ("crono(), %s get %f seconds" %(txt, (time() - float(start))) )
    return

def usage():
    print ("TcosMonitor help:")
    print ("")
    print ("   tcosmonitor -d [--debug]  (write debug data to stdout)")
    print ("   tcosmonitor -h [--help]   (this help)")


try:
    OPTS, ARGS = getopt.getopt(sys.argv[1:], ":hd", ["help", "debug"])
except getopt.error, msg:
    print (msg)
    print ("for command line options use tcosconfig --help")
    sys.exit(2)

# process options
for o, a in OPTS:
    if o in ("-d", "--debug"):
        print ("DEBUG ACTIVE")
        shared.debug = True
    if o in ("-h", "--help"):
        usage()
        sys.exit()

import tcosmonitor

class TcosMonitor(object):
    def __init__(self):
        # if true auto-update is active, false only one update        
        self.updating=False
        self.name="TcosMonitor"
        self.force_selected_ip=None
        # register triggers
        self.triggers={}
        self.mainloop = gobject.MainLoop()
        
        self.groupconf=self.loadconf( os.path.abspath(shared.GLOBAL_CONF) )
        
        try:
            if int(self.groupconf['check_tcosmonitor_user_group']) == 1:
                shared.check_tcosmonitor_user_group=True
        except Exception, err:
            print_debug("__init__() Exception getting group, error=%s"%err)
            pass
        
        if self.groupconf['dont_show_users_in_group'] != '':
            shared.dont_show_users_in_group=self.groupconf['dont_show_users_in_group']
        #else:
        #    shared.dont_show_users_in_group=None

        if self.groupconf['tnc_only_ports'] != "no":
            shared.tnc_only_ports="yes"
            
        ##################################################
        
        self.worker_running=False
        self.ingroup_tcos=False
        
        if shared.check_tcosmonitor_user_group:
            for group in os.getgroups():
                if grp.getgrgid(group)[0] == "tcos":
                    self.ingroup_tcos=True
            if os.getuid() == 0:
                self.ingroup_tcos=True
            if self.ingroup_tcos == False and os.getuid() != 0:
                shared.error_msg( _("The user \"%s\" must be member of the group \"tcos\"\
 to exec tcosmonitor.\n\nIf you are system administrator, add your\
 user to tcos group." %pwd.getpwuid(os.getuid())[0]))
                sys.exit(1)

        
        # Widgets
        self.ui = gtk.Builder()
        self.ui.set_translation_domain(shared.PACKAGE)
        print_debug("Loading ui file...")
        self.ui.add_from_file(shared.GLADE_DIR + 'tcosmonitor-mainwindow.ui')
        
        
        self.mainwindow = self.ui.get_object('mainwindow')
        self.mainwindow.set_icon_from_file(shared.IMG_DIR +\
                                     'tcos-icon-32x32.png')
        
        self.is_fullscreen=False
        
        # close windows signals
        self.mainwindow.connect('destroy', self.quitapp )
        self.mainwindow.connect("delete_event", self.quitapp)

        # reduce mainwindow size if running in height < 1024x768
        #>>> gtk.gdk.screen_height()
        #>>> gtk.gdk.screen_width()
        if gtk.gdk.screen_height() < 768:
            print_debug("set lower size of mainwindow")
            self.mainwindow.set_size_request(760,520)

        # ui file (mainwindow) visible=False, show now
        self.mainwindow.show()
        
        
        # FIXME
        self.scrolledtextview = self.ui.get_object('scrolledtextview')
        #import htmltextview
        #htmltextview.HtmlHandler().set_main(self)
        
        self.datatxt = tcosmonitor.htmltextview.HtmlTextView(self)
        self.datatxt.show()
        self.scrolledtextview.add(self.datatxt)
        self.datatxt.clean()
        
        
        
        # init classes
        self.common=tcosmonitor.TcosCommon.TcosCommon(self)
        self.config=tcosmonitor.TcosConf.TcosConf(self)
        self.localdata=tcosmonitor.LocalData.LocalData(self)
        self.xmlrpc=tcosmonitor.TcosXmlRpc.TcosXmlRpc(self)
        self.xauth=tcosmonitor.TcosXauth.TcosXauth(self)
        self.preferences=tcosmonitor.TcosPreferences.TcosPreferences(self)
        
        self.menus=tcosmonitor.TcosMenus.TcosMenus(self)
        
        self.actions=tcosmonitor.TcosActions.TcosActions(self)
        
        # views
        self.listview=tcosmonitor.TcosListView.TcosListView(self)
        self.iconview=tcosmonitor.TcosIconView.TcosIconView(self)
        self.classview=tcosmonitor.TcosClassView.TcosClassView(self)
        
        self.init=tcosmonitor.Initialize.Initialize(self)
        
        self.static=tcosmonitor.TcosStaticHosts.TcosStaticHosts(self)
        
        
        #########  init some elements ###########
        self.init.init_progressbar()
        self.init.initabouttcos()
        #########################################
        self.init.initbuttons()
        self.preferences.populate_pref()
        

        
        self.extloader=tcosmonitor.TcosExtensions.TcosExtLoader(self)
        
        
        if not shared.dbus_disabled:
            self.dbus_action=tcosmonitor.TcosDBus.TcosDBusAction(self, 
                                  admin=self.config.GetVar("xmlrpc_username"),
                                  passwd=self.config.GetVar("xmlrpc_password"))
        
        self.stop_running_actions=[]
        # generate host list if checked
        if self.config.GetVar("populate_list_at_startup") == "1":
            self.populate_host_list()
        self.actions.update_hostlist()
            
        try:
            self.ftp_thread = TCOSFTPSERVER() 
            self.ftp_thread.setDaemon(1)
            self.ftp_thread.start()
        except Exception, err:
            pass

    def loadconf(self, conffile):
        conf={}
        print_debug ( "loadconf() conffile=%s" %conffile )
        if os.path.isfile(conffile):
            print_debug ("loadconf() found conf file %s" %conffile)
            f=open(conffile, "r")
            data=f.readlines()
            f.close()
            for line in data:
                if line == '\n':
                    continue
                if line.find('#') == 0:
                    continue
                line=line.replace('\n', '')
                if "=" in line:
                    try:
                        conf["%s"%line.split('=')[0]] = line.split('=')[1].replace('"', '')
                    except Exception, err:
                        print_debug("loadconf() Exception: %s" %err)
                        pass
        print_debug( "loadconf conf=%s" %conf )
        return conf

    def button_actions(self, widget, action):
        print_debug ( "button_actionst() action=%s" %action)

        if action == "audio":
            if self.actions.button_action_audio != None:
                self.actions.button_action_audio()
        elif action == "chat":
            if self.actions.button_action_chat != None:
                self.actions.button_action_chat()
        elif action == "list":
            if self.actions.button_action_list != None:
                self.actions.button_action_list()
        elif action == "video":
            if self.actions.button_action_video != None:
                self.actions.button_action_video()
        elif action == "send":
            if self.actions.button_action_send != None:
                self.actions.button_action_send()
        elif action == "exe":
            if self.actions.button_action_exe != None:
                self.actions.button_action_exe()
        elif action == "text":
            if self.actions.button_action_text != None:
                self.actions.button_action_text()
        return

    def search_host(self, widget):
        print_debug ( "search_host()" )
        txt=self.searchtxt.get_text()
        if txt == "":
            allclients=self.localdata.GetAllClients( self.config.GetVar("scan_network_method") )
            Thread( target=self.actions.populate_hostlist, args=([allclients]) ).start()
        model=self.tabla.get_model()
        notvalid=[]
        model.foreach(self.delete_not_searched, (notvalid))
        notvalid.reverse()
        for host in notvalid:
            model.remove( model.get_iter(host) )
        
    def delete_not_searched(self, model, path, iter, data):
        txt=self.searchtxt.get_text()
        hostname=model.get_value(iter, 0)
        ip=model.get_value(iter, 1)
        username=model.get_value(iter, 2)
        if txt != hostname and txt != ip and txt != username:
            data.append(path)    
    
    def write_into_statusbar(self, msg, *args):
        #print_debug("STATUSBAR: Writing \"%s\" into statusbar" % msg)
        context_id=self.statusbar.get_context_id("status")
        self.statusbar.pop(context_id)
        self.statusbar.push(context_id, msg)
        return    

    def quitapp(self, *args):
        print_debug ( _("Exiting") )
        #gtk.main_quit()
        widgets=self.stop_running_actions[:]
        print_debug("Running actions: %s" %len(widgets))
        for widget in widgets:
            print_debug("Stop running action... widget=%s" %(widget))
            try:
                widget.clicked()
            except:
                pass
        if os.path.isdir("/tmp/tcos_share/"):
            for filename in os.listdir("/tmp/tcos_share/"):
                if os.path.isfile("/tmp/tcos_share/%s" %filename):
                    os.remove("/tmp/tcos_share/%s" %filename)
            if os.path.isdir("/tmp/tcos_share"):
                os.rmdir("/tmp/tcos_share")
                
        if os.path.isfile(os.path.expanduser('~/.tcosvnc')):
            os.remove(os.path.expanduser('~/.tcosvnc'))
        
        self.mainloop.quit()


    def run (self):
        try:
            self.mainloop.run()
        except KeyboardInterrupt: # Press Ctrl+C
            self.quitapp()
        
        
    
if __name__ == '__main__':
    app = TcosMonitor ()
    # Run app
    app.run ()