~rapache-devel/rapache/rapache-stage0

« back to all changes in this revision

Viewing changes to plugins/basic_authentication/plugin.py

  • Committer: Stefano Forenza
  • Date: 2008-09-15 06:53:55 UTC
  • mfrom: (81.1.84 src)
  • Revision ID: tacone@gmail.com-20080915065355-iuhq5as33jrybs2r
New major release (0.7 - Early Eagle):
  - added DEPENDENCIES: python-lxml python-crypto
  - removed mono plugin as it proved not useful
  - added new parser and testcases for the parser
  - added Advanced plugin
  - added Basic Authentication plugin
  - added SSL plugin
  - Apache status is now displayed in the UI and in the Icon
  - virtual hosts editor supports backups
  - added Log Files tab
  - reworked Problems tab. Apache errors get displayed
  - added Apache start/stop/restart in the menu
  - tidied up menus
  - several bugfixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Rapache - Apache Configuration Tool
 
2
# Copyright (C) 2008 Stefano Forenza,  Jason Taylor, Emanuele Gentili
 
3
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation, either version 3 of the License, or
 
7
# (at your option) any later version.
 
8
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
import os
 
18
from RapacheCore.PluginBase import PluginBaseObject
 
19
from RapacheCore import Shell
 
20
from htpasswd import Htpasswd
 
21
from user_credentials import UserCredentials
 
22
try:
 
23
     import pygtk
 
24
     pygtk.require("2.0")
 
25
except:
 
26
      pass
 
27
try:
 
28
    import gtk
 
29
    import gtk.glade
 
30
except:
 
31
    sys.exit(1)
 
32
 
 
33
class BasicAuthenticationPlugin(PluginBaseObject):
 
34
 
 
35
    def __init__(self, path):
 
36
        
 
37
        # The path to the plugin
 
38
        self.path = path
 
39
    
 
40
        # module this plugin works with
 
41
        self.module = "auth_basic"
 
42
        
 
43
        # Define what additional config should be read from vhost file
 
44
        self.vhosts_config = { "AuthType" : 0, "AuthName" : 0, "AuthUserFile" : 0, "Require" : 1 } # 0 value | 1 options
 
45
        
 
46
        self.default_location = "/etc/apache2/basic-auth/passwords"
 
47
        
 
48
        self.users = Htpasswd()
 
49
        self.users_active = []
 
50
    
 
51
    def treeview_users_toggled(self, cell, path):
 
52
        # toggle check box value
 
53
        iter = self.treeview_users_store.get_iter((int(path),))
 
54
        self.treeview_users_store.set_value(iter, 0, not self.treeview_users_store.get_value(iter, 0))
 
55
        return
 
56
            
 
57
    def on_toolbutton_user_add_clicked(self, widget):
 
58
        uc = UserCredentials(self.path)
 
59
        result= uc.run()
 
60
        
 
61
        if result:
 
62
            self.users.update( result[0], result[1] )
 
63
            self.users_active.append( result[0] )
 
64
            self.update_users()
 
65
        
 
66
        return       
 
67
        
 
68
    def on_toolbutton_user_edit_clicked(self, widget):
 
69
 
 
70
        model, iter = self.treeview_users.get_selection().get_selected()
 
71
        if not iter: return
 
72
        username = model.get_value(iter, 2)
 
73
 
 
74
        uc = UserCredentials(self.path)
 
75
        uc.load( username )
 
76
        result = uc.run()
 
77
        
 
78
        if result:
 
79
            self.users.update( result[0], result[1] )
 
80
            self.users_active.append( result[0] )
 
81
            self.update_users()
 
82
 
 
83
        return         
 
84
        
 
85
    def on_toolbutton_user_delete_clicked(self, widget):
 
86
        
 
87
        model, iter = self.treeview_users.get_selection().get_selected()
 
88
        if not iter: return
 
89
        username = model.get_value(iter, 2)
 
90
        
 
91
        md = gtk.MessageDialog(None, flags=0, type=gtk.MESSAGE_QUESTION, buttons=gtk.BUTTONS_OK_CANCEL, message_format="Are you sure you want to delete '"+ username+"' ?") 
 
92
        result = md.run()
 
93
        md.destroy()
 
94
        if result == gtk.RESPONSE_OK:
 
95
            self.users.delete(username)
 
96
            self.update_users()
 
97
 
 
98
        return          
 
99
        
 
100
    def on_treeview_users_row_activated(self, a,b,c):
 
101
        model, iter = self.treeview_users.get_selection().get_selected()
 
102
        if not iter: return
 
103
        username = model.get_value(iter, 2)
 
104
 
 
105
        uc = UserCredentials(self.path)
 
106
        uc.load( username )
 
107
        result = uc.run()
 
108
        
 
109
        if result:
 
110
            self.users.update( result[0], result[1] )
 
111
            self.users_active.append( result[0] )
 
112
            self.update_users()
 
113
        
 
114
        
 
115
    def on_button_location_clear_clicked(self, widget):
 
116
        return None          
 
117
        
 
118
    def update_users(self):
 
119
        self.treeview_users_store = gtk.ListStore(bool, str, str)
 
120
        self.treeview_users.set_model(self.treeview_users_store)   
 
121
 
 
122
        for user in self.users.entries:
 
123
            self.treeview_users_store.append((user[0] in self.users_active , user[0], user[0]))         
 
124
        
 
125
        
 
126
    def init_vhost_properties(self):
 
127
 
 
128
        # Get glade file XML
 
129
        f = open( os.path.join(self.path, "basic_auth.glade") ,"r")
 
130
        self.glade_vhost_xml =  f.read()
 
131
        f.close()
 
132
 
 
133
        wtree = gtk.glade.xml_new_from_buffer(self.glade_vhost_xml, len(self.glade_vhost_xml), "hbox_auth_basic")
 
134
        hbox_auth_basic = wtree.get_widget("hbox_auth_basic") 
 
135
        
 
136
        self.checkbutton_enable_auth_basic = wtree.get_widget("checkbutton_enable_auth_basic") 
 
137
        self.entry_warning_message = wtree.get_widget("entry_warning_message") 
 
138
        self.treeview_users = wtree.get_widget("treeview_users") 
 
139
        self.entry_location = wtree.get_widget("entry_location")      
 
140
        
 
141
        signals = {
 
142
            "on_toolbutton_user_add_clicked"    : self.on_toolbutton_user_add_clicked,
 
143
            "on_toolbutton_user_edit_clicked"   : self.on_toolbutton_user_edit_clicked,
 
144
            "on_toolbutton_user_delete_clicked" : self.on_toolbutton_user_delete_clicked,
 
145
            "on_treeview_users_row_activated"   : self.on_treeview_users_row_activated,
 
146
            "on_button_location_clear_clicked"  : self.on_button_location_clear_clicked
 
147
        }
 
148
        wtree.signal_autoconnect(signals)  
 
149
        
 
150
        # Setup tree
 
151
        column = gtk.TreeViewColumn((''))
 
152
        column.set_spacing(4)
 
153
        cell = gtk.CellRendererToggle()
 
154
        cell.connect('toggled', self.treeview_users_toggled)
 
155
        column.pack_start(cell, False)
 
156
        column.set_attributes(cell, active=0)
 
157
        self.treeview_users.append_column(column)
 
158
 
 
159
        column = gtk.TreeViewColumn(('User'))
 
160
        cell = gtk.CellRendererText()
 
161
        column.pack_start(cell, True)
 
162
        column.set_attributes(cell, markup=1)
 
163
        self.treeview_users.append_column(column)
 
164
 
 
165
        self.entry_location.set_text(self.default_location)
 
166
        self.users.load(Shell.command.read_file(self.default_location))
 
167
        self.entry_warning_message.set_text("Enter your password")
 
168
        
 
169
 
 
170
        self.update_users()
 
171
        
 
172
        icon_theme = gtk.icon_theme_get_default()
 
173
        pixbuf = icon_theme.lookup_icon("gtk-dialog-authentication", 24, 0).load_icon()
 
174
 
 
175
        return hbox_auth_basic, "Basic Security", pixbuf
 
176
 
 
177
    # Customise the vhost properties window
 
178
    def load_vhost_properties(self, vhost):
 
179
        self.users_active = []
 
180
        self.checkbutton_enable_auth_basic.set_active(False)
 
181
        
 
182
        ds = vhost.config.Directory.search(  [vhost.get_document_root()]  )
 
183
 
 
184
        if len(  ds    ) > 0:
 
185
            d = ds[0]
 
186
            if d.AuthType:
 
187
                self.checkbutton_enable_auth_basic.set_active(d.AuthType.value.lower() == "basic")
 
188
                
 
189
            # only load if value changes
 
190
            if d.AuthUserFile:
 
191
                if d.AuthUserFile.value != self.entry_location.get_text():
 
192
                    self.entry_location.set_text(d.AuthUserFile.value)
 
193
                    content = Shell.command.read_file(self.entry_location.get_text())
 
194
                    if content:
 
195
                        self.users.load( content )
 
196
        
 
197
            if d.AuthName:
 
198
                self.entry_warning_message.set_text(d.AuthName.value)
 
199
                
 
200
            if d.Require:
 
201
                self.users_active = list(d.Require.opts)[1:]
 
202
                self.update_users()
 
203
                
 
204
        return True, None
 
205
 
 
206
 
 
207
    # Perform action on vhost properties update request
 
208
    def update_vhost_properties(self, vhost):
 
209
        
 
210
        self.users_active = []
 
211
        iter = self.treeview_users_store.get_iter_first()
 
212
        while 1:
 
213
            if not iter: break
 
214
            if self.treeview_users_store.get_value(iter, 0):
 
215
                self.users_active.append( self.treeview_users_store.get_value(iter, 2) )
 
216
            iter = self.treeview_users_store.iter_next(iter)
 
217
        
 
218
        ds = vhost.config.Directory.search(  [vhost.get_document_root()]  )
 
219
        d = None
 
220
        if len(ds) == 0:
 
221
            d = vhost.config.sections.create("Directory", vhost.get_document_root())
 
222
        else:
 
223
            d = ds[0]
 
224
        
 
225
        if self.checkbutton_enable_auth_basic.get_active() and len(self.users_active) > 0:
 
226
            d.AuthType.value = "Basic"
 
227
            
 
228
            if self.entry_warning_message.get_text():
 
229
                d.AuthName.value = self.entry_warning_message.get_text() 
 
230
            elif d.AuthName:
 
231
                del d.AuthName
 
232
                
 
233
            d.AuthUserFile.value =  self.entry_location.get_text()
 
234
            d.Require.opts = ["user"] + self.users_active
 
235
            
 
236
        else:
 
237
            if d.AuthType : del d.AuthType
 
238
            if d.AuthName : del d.AuthName
 
239
            if d.AuthUserFile : del d.AuthUserFile
 
240
            if d.Require : del d.Require
 
241
 
 
242
        return True, None
 
243
 
 
244
 
 
245
    # Perform action on vhost properties save
 
246
    def save_vhost_properties(self, vhost):
 
247
 
 
248
        Shell.command.write_file( self.entry_location.get_text(), self.users.save())
 
249
        
 
250
        return True, None
 
251
 
 
252
 
 
253
def register( path ):
 
254
    return BasicAuthenticationPlugin( path )
 
255