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

################################
# Author:  Benjamin Kellermann #
# License: CC-by-sa 3.0        #
#          see License         #
################################

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 ~ "^(config|remove).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

$html = HTML.new("dudle - 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>Change Access Control Settings</h1>
		<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