aboutsummaryrefslogtreecommitdiff
path: root/access_control.rb
blob: 4fffec9abdeb3e117b979c6ae50d3f7a48e83841 (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
#!/usr/bin/env ruby

############################################################################
# Copyright 2009 Benjamin Kellermann                                       #
#                                                                          #
# This file is part of dudle.                                              #
#                                                                          #
# Dudle is free software: you can redistribute it and/or modify it under   #
# the terms of the GNU Affero General Public License as published by       #
# the Free Software Foundation, either version 3 of the License, or        #
# (at your option) any later version.                                      #
#                                                                          #
# Dudle 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 Affero General Public     #
# License for more details.                                                #
#                                                                          #
# You should have received a copy of the GNU Affero General Public License #
# along with dudle.  If not, see <http://www.gnu.org/licenses/>.           #
############################################################################

require "cgi"

if __FILE__ == $0

$cgi = CGI.new

load "../html.rb"

acusers = {}

File.open(".htdigest","r").each_line{|l| 
	user,realm = l.scan(/^(.*):(.*):.*$/).flatten
	acusers[user] = realm
}

def writehtaccess(acusers)
	File.open(".htaccess","w"){|htaccess|
		if acusers.values.include?("config")
			htaccess << <<HTACCESS
<Files ~ "^(edit_columns|access_control|delete_poll).cgi$">
AuthType digest
AuthName "config"
AuthUserFile "#{File.expand_path(".").gsub('"','\\\\"')}/.htdigest"
Require valid-user
</Files>
HTACCESS
		end
		if acusers.values.include?("vote")
			htaccess << <<HTACCESS
AuthType digest
AuthName "vote"
AuthUserFile "#{File.expand_path(".").gsub('"','\\\\"')}/.htdigest"
Require valid-user
HTACCESS
			VCS.commit("Access Control changed")
		end
	}
end

if $cgi.include?("ac_user")
	user = $cgi["ac_user"]
	type = $cgi["ac_type"]
	if !(user =~ /^[\w]*$/)
		# add user

		usercreatenotice = "<div class='error'>Only uppercase, lowercase, digits are allowed in the username.</div>"
	elsif $cgi["ac_password1"] != $cgi["ac_password2"]
		usercreatenotice = "<div class='error'>Passwords do not match.</div>"
	else
		if $cgi.include?("ac_create")
			if type == "config" || type == "vote"
				fork {
					IO.popen("htdigest .htdigest #{type} #{user}","w+"){|htdigest|
						htdigest.sync
						htdigest.puts($cgi["ac_password1"])
						htdigest.puts($cgi["ac_password2"])
					}
				}
				acusers[user] = type 
				writehtaccess(acusers)
			end
		end

		# delete user
		deleteuser = ""
		deleteaction = ""
		acusers.each{|user,action|
			if $cgi.include?("ac_delete_#{user}_#{action}")
				deleteuser = user
				deleteaction = action
			end
		}
		acusers.delete(deleteuser)
		htdigest = []
		File.open(".htdigest","r"){|file|
			htdigest = file.readlines
		}
		File.open(".htdigest","w"){|f|
			htdigest.each{|line|
				f << line unless line =~ /^#{deleteuser}:#{deleteaction}:/
			}
		}
		writehtaccess(acusers)
	end
end

POLL = File.basename(File.expand_path("."))
$html = HTML.new("dudle - #{POLL} - Access Control Settings")
$html.header["Cache-Control"] = "no-cache"
load "../charset.rb"
$html.add_css("../dudle.css")

$html << "<body>"
$html << Dudle::tabs("Access Control")

$html << <<TABLE
	<div id='main'>
TABLE

# ACCESS CONTROL
$accesslevels = { "vote" => "Vote Interface", "config" => "Config Interface" }
$html << <<ACL
<div id='access_control'>
	<h1>#{POLL}</h1>
	<h2>Change Access Control Settings</h2>
	<form method='post' action=''>
		<table>
			<tr>
				<th>Access to</th><th>Username</th><th>Password</th><th>Password (repeat)</th>
			</tr>
ACL
acusers.each{|user,action|
		$html << <<USER
<tr>
	<td>#{$accesslevels[action]}</td>
	<td>#{user}</td>
	<td>*****************</td>
	<td>*****************</td>
	<td>
		<input type='submit' name='ac_delete_#{user}_#{action}' value='delete' />
	</td>
</tr>
USER
}

$html << <<ACL
<tr>
	<td>
		<select name='ac_type'>
ACL
	$accesslevels.each{|action,description| 
		$html << "<option value='#{action}'>#{description}</option>"
	}
	$html << <<ACL
		</select>
	</td>
	<td><input size='6' value="" type='text' name='ac_user' /></td>
	<td><input size='6' value="" type='password' name='ac_password1' /></td>
	<td><input size='6' value="" type='password' name='ac_password2' /></td>
	<td>
		<input type='submit' name='ac_create' value='Add' />
	</td>
</tr>
ACL

$html << <<ACL
			</table>
		</form>
		#{usercreatenotice}
</div>
ACL

$html << "</div></body>"

$html.out($cgi)
end