| 1 |
#!/usr/bin/env python |
|---|
| 2 |
|
|---|
| 3 |
## |
|---|
| 4 |
## Cherokee 0.5.x to 0.6.x configuration converter |
|---|
| 5 |
## |
|---|
| 6 |
## Copyright: Alvaro Lopez Ortega <alvaro@alobbs.com> |
|---|
| 7 |
## Licensed: GPL v2 |
|---|
| 8 |
## |
|---|
| 9 |
|
|---|
| 10 |
import sys |
|---|
| 11 |
import string |
|---|
| 12 |
|
|---|
| 13 |
FAKED_DOCUMENT_ROOT = '/faked' |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
class Syntax: |
|---|
| 17 |
srv_entries = {'port': 'int', |
|---|
| 18 |
'porttls': 'int', |
|---|
| 19 |
'ipv6': 'int', |
|---|
| 20 |
'timeout': 'int', |
|---|
| 21 |
'keepalive': 'int', |
|---|
| 22 |
'maxkeepaliverequests': 'int', |
|---|
| 23 |
'logflushinterval': 'int', |
|---|
| 24 |
'maxfds': 'int', |
|---|
| 25 |
'maxconnectionreuse': 'int', |
|---|
| 26 |
'listenqueuesize': 'int', |
|---|
| 27 |
'servertokens': 'str', |
|---|
| 28 |
'listen': 'str', |
|---|
| 29 |
'user': 'str', |
|---|
| 30 |
'group': 'str', |
|---|
| 31 |
'pollmethod': 'str', |
|---|
| 32 |
'pidfile': 'path', |
|---|
| 33 |
'mimefile': 'path', |
|---|
| 34 |
'chroot': 'path', |
|---|
| 35 |
'panicaction': 'path' |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
vsrv_entries = {'directoryindex': 'list', |
|---|
| 39 |
'sslcertificatefile': 'path', |
|---|
| 40 |
'sslcertificatekeyfile': 'path', |
|---|
| 41 |
'sslcalistfile': 'path' |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
icons_entries = {'directory': 'str', |
|---|
| 45 |
'parentdirectory': 'str', |
|---|
| 46 |
'default': 'str'} |
|---|
| 47 |
|
|---|
| 48 |
similar_entries = {'maxkeepaliverequests': 'keepalive_max_requests', |
|---|
| 49 |
'servertokens': 'server_tokens', |
|---|
| 50 |
'pidfile': 'pid_file', |
|---|
| 51 |
'mimefile': 'mime_file', |
|---|
| 52 |
'documentroot': 'document_root', |
|---|
| 53 |
'errorhandler': 'error_handler', |
|---|
| 54 |
'accesslog': 'access_log', |
|---|
| 55 |
'errorlog': 'error_log', |
|---|
| 56 |
'name': 'realm', |
|---|
| 57 |
'logflushinterval': 'log_flush_elapse', |
|---|
| 58 |
'maxfds': 'max_fds', |
|---|
| 59 |
'maxconnectionreuse': 'max_connection_reuse', |
|---|
| 60 |
'panicaction': 'panic_action', |
|---|
| 61 |
'pollmethod': 'poll_method', |
|---|
| 62 |
'listenqueuesize': 'listen_queue_size', |
|---|
| 63 |
'sslcertificatefile': 'ssl_certificate_file', |
|---|
| 64 |
'sslcertificatekeyfile': 'ssl_certificate_key_file', |
|---|
| 65 |
'sslcalistfile': 'ssl_cal_list_file', |
|---|
| 66 |
'directoryindex': 'directory_index', |
|---|
| 67 |
'scriptalias': 'script_alias' |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
def __init__ (self, lex): |
|---|
| 71 |
self._lex = lex |
|---|
| 72 |
|
|---|
| 73 |
def _process_entry_guts (self, prefix): |
|---|
| 74 |
kind, val = self._lex.get_token() |
|---|
| 75 |
if kind == 'handler': |
|---|
| 76 |
kind, val = self._lex.get_token() |
|---|
| 77 |
if kind == 'file': |
|---|
| 78 |
kind = 'str' |
|---|
| 79 |
val = 'file' |
|---|
| 80 |
if kind != 'str': |
|---|
| 81 |
raise "Expected a str" |
|---|
| 82 |
print '%s!handler = %s' % (prefix, val) |
|---|
| 83 |
|
|---|
| 84 |
kind, val = self._lex.get_token() |
|---|
| 85 |
if kind != '{': |
|---|
| 86 |
self._lex.rewind() |
|---|
| 87 |
return True |
|---|
| 88 |
|
|---|
| 89 |
regex_num = 1 |
|---|
| 90 |
while True: |
|---|
| 91 |
kind, val_prop = self._lex.get_token() |
|---|
| 92 |
|
|---|
| 93 |
# Special cases |
|---|
| 94 |
if kind == '}': |
|---|
| 95 |
break |
|---|
| 96 |
|
|---|
| 97 |
elif kind == 'server': |
|---|
| 98 |
kind, val_prop_val = self._lex.get_token() |
|---|
| 99 |
print '%s!handler!balancer = round_robin' % (prefix) |
|---|
| 100 |
print '%s!handler!balancer!type = interpreter' % (prefix) |
|---|
| 101 |
print '%s!handler!balancer!entry1!host = %s' % (prefix, val_prop_val) |
|---|
| 102 |
|
|---|
| 103 |
kind, val = self._lex.get_token() |
|---|
| 104 |
if kind != '{': |
|---|
| 105 |
self._lex.rewind() |
|---|
| 106 |
continue |
|---|
| 107 |
|
|---|
| 108 |
while True: |
|---|
| 109 |
kind, val = self._lex.get_token() |
|---|
| 110 |
if kind == '}': |
|---|
| 111 |
break |
|---|
| 112 |
elif kind == 'str': |
|---|
| 113 |
kind_prop, val_prop = self._lex.get_token() |
|---|
| 114 |
|
|---|
| 115 |
val = val.lower() |
|---|
| 116 |
if val == 'env': |
|---|
| 117 |
kind_prop_val, val_prop_val = self._lex.get_token() |
|---|
| 118 |
print "%s!handler!balancer!entry1!env!%s = %s" % (prefix, val_prop, val_prop_val) |
|---|
| 119 |
|
|---|
| 120 |
else: |
|---|
| 121 |
print "%s!handler!balancer!entry1!%s = %s" % (prefix, val, val_prop) |
|---|
| 122 |
continue |
|---|
| 123 |
|
|---|
| 124 |
elif kind == 'str': |
|---|
| 125 |
val_prop_low = val_prop.lower() |
|---|
| 126 |
if val_prop_low == 'justabout': |
|---|
| 127 |
print '%s!handler!just_about = 1' % (prefix) |
|---|
| 128 |
continue |
|---|
| 129 |
|
|---|
| 130 |
elif val_prop_low == 'env': |
|---|
| 131 |
kind, val_env_name = self._lex.get_token() |
|---|
| 132 |
if kind != 'str': raise "Expected a str" |
|---|
| 133 |
|
|---|
| 134 |
kind, val_env_val = self._lex.get_token() |
|---|
| 135 |
if kind != 'str': raise "Expected a str" |
|---|
| 136 |
|
|---|
| 137 |
print '%s!handler!env!%s = %s' % (prefix, val_env_name, val_env_val) |
|---|
| 138 |
continue |
|---|
| 139 |
|
|---|
| 140 |
elif val_prop_low == 'show' or \ |
|---|
| 141 |
val_prop_low == 'rewrite': |
|---|
| 142 |
kind, val_prop = self._lex.get_token() |
|---|
| 143 |
|
|---|
| 144 |
# dirlist |
|---|
| 145 |
if val_prop_low == 'show': |
|---|
| 146 |
if 'size' in val_prop or \ |
|---|
| 147 |
'date' in val_prop or \ |
|---|
| 148 |
'owner' in val_prop: |
|---|
| 149 |
print '%s!handler!size = %d' % (prefix, 'size' in val_prop) |
|---|
| 150 |
print '%s!handler!date = %d' % (prefix, 'date' in val_prop) |
|---|
| 151 |
print '%s!handler!user = %d' % (prefix, 'owner' in val_prop) |
|---|
| 152 |
continue |
|---|
| 153 |
|
|---|
| 154 |
# redir |
|---|
| 155 |
rewrite = (kind == 'str' and val_prop.lower() == 'rewrite') |
|---|
| 156 |
if not rewrite: self._lex.rewind() |
|---|
| 157 |
|
|---|
| 158 |
print '%s!handler!rewrite!%d!show = %d' % (prefix, regex_num, rewrite) |
|---|
| 159 |
|
|---|
| 160 |
kind, regex_val = self._lex.get_token() |
|---|
| 161 |
if kind != 'str': raise "Expected a str" |
|---|
| 162 |
|
|---|
| 163 |
kind, url_val = self._lex.get_token() |
|---|
| 164 |
if kind == '}': |
|---|
| 165 |
self._lex.rewind() |
|---|
| 166 |
print '%s!handler!rewrite!%d!substring = %s' % (prefix, regex_num, regex_val) |
|---|
| 167 |
regex_num = regex_num + 1 |
|---|
| 168 |
continue |
|---|
| 169 |
|
|---|
| 170 |
elif kind == 'str': |
|---|
| 171 |
print '%s!handler!rewrite!%d!regex = %s' % (prefix, regex_num, regex_val) |
|---|
| 172 |
print '%s!handler!rewrite!%d!substring = %s' % (prefix, regex_num, url_val) |
|---|
| 173 |
regex_num = regex_num + 1 |
|---|
| 174 |
continue |
|---|
| 175 |
|
|---|
| 176 |
else: |
|---|
| 177 |
raise "Expected a str or '}'" |
|---|
| 178 |
|
|---|
| 179 |
# Default case |
|---|
| 180 |
val_prop_low = val_prop.lower() |
|---|
| 181 |
if val_prop_low in self.similar_entries: |
|---|
| 182 |
val_prop = self.similar_entries[val_prop_low] |
|---|
| 183 |
else: |
|---|
| 184 |
val_prop = val_prop_low |
|---|
| 185 |
|
|---|
| 186 |
# Get property value |
|---|
| 187 |
kind, val_prop_val = self._lex.get_token() |
|---|
| 188 |
print '%s!handler!%s = %s' % (prefix, val_prop, val_prop_val) |
|---|
| 189 |
|
|---|
| 190 |
elif kind == 'documentroot': |
|---|
| 191 |
kind, val = self._lex.get_token() |
|---|
| 192 |
if kind != 'path': raise "Malformed DocumentRoot" |
|---|
| 193 |
print '%s!document_root = %s' % (prefix, val) |
|---|
| 194 |
|
|---|
| 195 |
elif kind == 'auth': |
|---|
| 196 |
return self._process_auth (prefix) |
|---|
| 197 |
|
|---|
| 198 |
elif kind == 'allow': |
|---|
| 199 |
kind, val = self._lex.get_token() |
|---|
| 200 |
if kind != 'str': raise "Malformed Allow" |
|---|
| 201 |
|
|---|
| 202 |
kind, val_ip = self._lex.get_token() |
|---|
| 203 |
if not kind in ['str', 'list']: raise "Malformed Allow from" |
|---|
| 204 |
|
|---|
| 205 |
print '%s!allow_from = %s' % (prefix, val_ip) |
|---|
| 206 |
|
|---|
| 207 |
else: |
|---|
| 208 |
self._lex.rewind() |
|---|
| 209 |
return False |
|---|
| 210 |
|
|---|
| 211 |
return True |
|---|
| 212 |
|
|---|
| 213 |
def _process_auth (self, prefix): |
|---|
| 214 |
kind, val_method = self._lex.get_token() |
|---|
| 215 |
if kind not in ['str', 'list']: raise "Malformed Auth" |
|---|
| 216 |
|
|---|
| 217 |
val_method = val_method.lower() |
|---|
| 218 |
print "%s!auth!methods = %s" % (prefix, val_method) |
|---|
| 219 |
|
|---|
| 220 |
kind, val = self._lex.get_token() |
|---|
| 221 |
if kind != '{': raise "Malformed Auth" |
|---|
| 222 |
|
|---|
| 223 |
while True: |
|---|
| 224 |
kind, val_prop_name = self._lex.get_token() |
|---|
| 225 |
|
|---|
| 226 |
if kind == 'user': |
|---|
| 227 |
kind = 'str' |
|---|
| 228 |
val_prop_name = 'user' |
|---|
| 229 |
|
|---|
| 230 |
if kind == '}': |
|---|
| 231 |
self._lex.rewind() |
|---|
| 232 |
break |
|---|
| 233 |
elif kind != 'str': |
|---|
| 234 |
raise "Malformed Auth" |
|---|
| 235 |
|
|---|
| 236 |
kind, val_prop = self._lex.get_token() |
|---|
| 237 |
if kind != 'str': raise "Malformed Auth" |
|---|
| 238 |
|
|---|
| 239 |
val_prop_name_low = val_prop_name.lower() |
|---|
| 240 |
if val_prop_name_low in self.similar_entries: |
|---|
| 241 |
val_prop_name = self.similar_entries[val_prop_name_low] |
|---|
| 242 |
else: |
|---|
| 243 |
val_prop_name = val_prop_name_low |
|---|
| 244 |
|
|---|
| 245 |
if val_prop_name == 'method': |
|---|
| 246 |
print "%s!auth = %s" % (prefix, val_prop) |
|---|
| 247 |
else: |
|---|
| 248 |
print "%s!auth!%s = %s" % (prefix, val_prop_name, val_prop) |
|---|
| 249 |
|
|---|
| 250 |
kind, val = self._lex.get_token() |
|---|
| 251 |
if kind == '{': |
|---|
| 252 |
while True: |
|---|
| 253 |
kind, val_prop = self._lex.get_token() |
|---|
| 254 |
if kind == '}': |
|---|
| 255 |
break |
|---|
| 256 |
else: |
|---|
| 257 |
val_prop = val_prop.lower() |
|---|
| 258 |
|
|---|
| 259 |
kind, val_val = self._lex.get_token() |
|---|
| 260 |
print "%s!auth!%s = %s" % (prefix, val_prop, val_val) |
|---|
| 261 |
else: |
|---|
| 262 |
self._lex.rewind() |
|---|
| 263 |
|
|---|
| 264 |
kind, val = self._lex.get_token() |
|---|
| 265 |
if kind != '}' : raise "Malformed Auth" |
|---|
| 266 |
return True |
|---|
| 267 |
|
|---|
| 268 |
def _process_entry (self, prefix): |
|---|
| 269 |
while True: |
|---|
| 270 |
more = self._process_entry_guts (prefix) |
|---|
| 271 |
if not more: break |
|---|
| 272 |
|
|---|
| 273 |
try: |
|---|
| 274 |
priority = self._vserver_last_priority * 10 |
|---|
| 275 |
except AttributeError: |
|---|
| 276 |
self._vserver_last_priority = 1 |
|---|
| 277 |
priority = 10 |
|---|
| 278 |
print "%s!priority = %d" % (prefix, priority * 10) |
|---|
| 279 |
self._vserver_last_priority += 1 |
|---|
| 280 |
|
|---|
| 281 |
def _process_encoder (self, prefix): |
|---|
| 282 |
while True: |
|---|
| 283 |
kind, val = self._lex.get_token() |
|---|
| 284 |
if kind not in ['allow', 'deny']: |
|---|
| 285 |
raise "Malformed encoder" |
|---|
| 286 |
|
|---|
| 287 |
kind_val, val_val = self._lex.get_token() |
|---|
| 288 |
if kind_val != 'list': raise "Malformed encoder" |
|---|
| 289 |
|
|---|
| 290 |
print "%s!%s = %s" % (prefix, kind, val_val) |
|---|
| 291 |
|
|---|
| 292 |
kind, val = self._lex.get_token() |
|---|
| 293 |
self._lex.rewind() |
|---|
| 294 |
if kind == '}': |
|---|
| 295 |
break |
|---|
| 296 |
|
|---|
| 297 |
def _process_log (self, prefix): |
|---|
| 298 |
while True: |
|---|
| 299 |
kind, val1 = self._lex.get_token() |
|---|
| 300 |
if kind == '}': |
|---|
| 301 |
self._lex.rewind() |
|---|
| 302 |
break |
|---|
| 303 |
if kind != 'str': raise "Malformed Log" |
|---|
| 304 |
|
|---|
| 305 |
val1 = val1.lower() |
|---|
| 306 |
|
|---|
| 307 |
if val1.startswith('access'): |
|---|
| 308 |
entry = 'access' |
|---|
| 309 |
elif val1.startswith('error'): |
|---|
| 310 |
entry = 'error' |
|---|
| 311 |
else: |
|---|
| 312 |
raise "Unknown Log entry " + val1 |
|---|
| 313 |
|
|---|
| 314 |
kind, val2 = self._lex.get_token() |
|---|
| 315 |
if kind != 'path': raise "Malformed Log" |
|---|
| 316 |
|
|---|
| 317 |
if val2[0] == '/' or val2[1:2] == ':\\': |
|---|
| 318 |
print "%s!%s!type = file" % (prefix, entry) |
|---|
| 319 |
print "%s!%s!filename = %s" % (prefix, entry, val2) |
|---|
| 320 |
elif val == 'syslog': |
|---|
| 321 |
print "%s!%s!type = syslog" % (prefix, entry) |
|---|
| 322 |
|
|---|
| 323 |
def _process_icons_content (self): |
|---|
| 324 |
kind, val = self._lex.get_token() |
|---|
| 325 |
if kind is None: |
|---|
| 326 |
return False |
|---|
| 327 |
|
|---|
| 328 |
if kind in self.icons_entries: |
|---|
| 329 |
val_kind, val_val = self._lex.get_token() |
|---|
| 330 |
expected_type = self.icons_entries[kind] |
|---|
| 331 |
if val_kind != expected_type: |
|---|
| 332 |
raise "Bad %s value: type=%s expected=%s" % \ |
|---|
| 333 |
(kind, val_kind, expected_type) |
|---|
| 334 |
|
|---|
| 335 |
if kind in self.similar_entries: |
|---|
| 336 |
kind = self.similar_entries[kind] |
|---|
| 337 |
|
|---|
| 338 |
print "icons!%s = %s" % (kind, str(val_val)) |
|---|
| 339 |
|
|---|
| 340 |
elif kind == 'file': |
|---|
| 341 |
kind, val = self._lex.get_token() |
|---|
| 342 |
if kind != '{': raise "Malformed Icon file entry" |
|---|
| 343 |
|
|---|
| 344 |
while True: |
|---|
| 345 |
kind, val_file = self._lex.get_token() |
|---|
| 346 |
if kind == '}': |
|---|
| 347 |
break |
|---|
| 348 |
elif kind != 'str': |
|---|
| 349 |
raise "Malformed Icon file entry" |
|---|
| 350 |
|
|---|
| 351 |
kind, val_pattern = self._lex.get_token() |
|---|
| 352 |
if kind != 'str': |
|---|
| 353 |
raise "Malformed Icon file entry" |
|---|
| 354 |
|
|---|
| 355 |
print "icons!file!%s = %s" % (val_file, val_pattern) |
|---|
| 356 |
|
|---|
| 357 |
elif kind == 'suffix': |
|---|
| 358 |
kind, val = self._lex.get_token() |
|---|
| 359 |
if kind != '{': raise "Malformed Icon suffix entry" |
|---|
| 360 |
|
|---|
| 361 |
while True: |
|---|
| 362 |
kind, val_file = self._lex.get_token() |
|---|
| 363 |
if kind == '}': |
|---|
| 364 |
break |
|---|
| 365 |
elif kind != 'str': |
|---|
| 366 |
raise "Malformed Icon suffix entry" |
|---|
| 367 |
|
|---|
| 368 |
kind, val_suffixes = self._lex.get_token() |
|---|
| 369 |
if kind not in ['str', 'list']: |
|---|
| 370 |
raise "Malformed Icon suffix entry" |
|---|
| 371 |
|
|---|
| 372 |
print "icons!suffix!%s = %s" % (val_file, val_suffixes) |
|---|
| 373 |
|
|---|
| 374 |
else: |
|---|
| 375 |
self._lex.rewind() |
|---|
| 376 |
return False |
|---|
| 377 |
|
|---|
| 378 |
return True |
|---|
| 379 |
|
|---|
| 380 |
def _process_virtual_server_content (self, vserver='default'): |
|---|
| 381 |
kind, val = self._lex.get_token() |
|---|
| 382 |
if kind is None: |
|---|
| 383 |
return False |
|---|
| 384 |
|
|---|
| 385 |
if kind in self.vsrv_entries: |
|---|
| 386 |
val_kind, val_val = self._lex.get_token() |
|---|
| 387 |
expected_type = self.vsrv_entries[kind] |
|---|
| 388 |
if val_kind != expected_type: |
|---|
| 389 |
raise "Bad %s value: type=%s expected=%s" % \ |
|---|
| 390 |
(kind, val_kind, expected_type) |
|---|
| 391 |
|
|---|
| 392 |
if kind in self.similar_entries: |
|---|
| 393 |
kind = self.similar_entries[kind] |
|---|
| 394 |
|
|---|
| 395 |
print "vserver!%s!%s = %s" % (vserver, kind, str(val_val)) |
|---|
| 396 |
|
|---|
| 397 |
elif kind == 'documentroot': |
|---|
| 398 |
kind, val = self._lex.get_token() |
|---|
| 399 |
if kind != 'path': raise "Malformed DocumentRoot" |
|---|
| 400 |
|
|---|
| 401 |
print "vserver!%s!document_root = %s" % (vserver, val) |
|---|
| 402 |
self._vserver_has_document_root = True |
|---|
| 403 |
|
|---|
| 404 |
elif kind == 'directory': |
|---|
| 405 |
kind, dir_val = self._lex.get_token() |
|---|
| 406 |
|
|---|
| 407 |
if kind == 'str': |
|---|
| 408 |
# If the token kind is a string, it means that |
|---|
| 409 |
# it's processing an icon. Return and let the |
|---|
| 410 |
# icon management method to take care for us. |
|---|
| 411 |
return False |
|---|
| 412 |
|
|---|
| 413 |
elif kind != 'path': |
|---|
| 414 |
raise "Malformed directory" |
|---|
| 415 |
|
|---|
| 416 |
kind, val = self._lex.get_token() |
|---|
| 417 |
if kind != '{': raise "Malformed directory" |
|---|
| 418 |
|
|---|
| 419 |
prefix = 'vserver!%s!directory!%s' % (vserver, dir_val) |
|---|
| 420 |
self._process_entry (prefix) |
|---|
| 421 |
|
|---|
| 422 |
kind, val = self._lex.get_token() |
|---|
| 423 |
if kind != '}': raise "Malformed directory", self._lex._txt |
|---|
| 424 |
|
|---|
| 425 |
elif kind == 'extension': |
|---|
| 426 |
kind, ext_val = self._lex.get_token() |
|---|
| 427 |
if not kind in ['list', 'str']: raise "Malformed extension" |
|---|
| 428 |
|
|---|
| 429 |
kind, val = self._lex.get_token() |
|---|
| 430 |
if kind != '{': raise "Malformed extension" |
|---|
| 431 |
|
|---|
| 432 |
prefix = 'vserver!%s!extensions!%s' % (vserver, ext_val) |
|---|
| 433 |
self._process_entry (prefix) |
|---|
| 434 |
|
|---|
| 435 |
kind, val = self._lex.get_token() |
|---|
| 436 |
if kind != '}': raise "Malformed extension" |
|---|
| 437 |
|
|---|
| 438 |
elif kind == 'request': |
|---|
| 439 |
kind, req_val = self._lex.get_token() |
|---|
| 440 |
if kind != 'str': raise "Malformed request" |
|---|
| 441 |
|
|---|
| 442 |
kind, val = self._lex.get_token() |
|---|
| 443 |
if kind != '{': raise "Malformed request" |
|---|
| 444 |
|
|---|
| 445 |
prefix = 'vserver!%s!request!%s' % (vserver, req_val) |
|---|
| 446 |
self._process_entry (prefix) |
|---|
| 447 |
|
|---|
| 448 |
kind, val = self._lex.get_token() |
|---|
| 449 |
if kind != '}': raise "Malformed request" |
|---|
| 450 |
|
|---|
| 451 |
elif kind == 'userdir': |
|---|
| 452 |
kind, pub_val = self._lex.get_token() |
|---|
| 453 |
if kind != 'str': raise "Malformed Userdir" |
|---|
| 454 |
|
|---|
| 455 |
kind, val = self._lex.get_token() |
|---|
| 456 |
if kind != '{': raise "Malformed Userdir" |
|---|
| 457 |
|
|---|
| 458 |
prefix = 'vserver!%s!user_dir!%s' % (vserver, pub_val) |
|---|
| 459 |
while True: |
|---|
| 460 |
more = self._process_virtual_server_content (prefix) |
|---|
| 461 |
if not more: break |
|---|
| 462 |
|
|---|
| 463 |
kind, val = self._lex.get_token() |
|---|
| 464 |
if kind != '}': raise "Malformed Userdir" |
|---|
| 465 |
|
|---|
| 466 |
elif kind == 'errorhandler': |
|---|
| 467 |
kind, handler_val = self._lex.get_token() |
|---|
| 468 |
if kind != 'str': raise "Malformed ErrorHandler" |
|---|
| 469 |
|
|---|
| 470 |
kind, val = self._lex.get_token() |
|---|
| 471 |
if kind != '{': raise "Malformed ErrorHandler" |
|---|
| 472 |
|
|---|
| 473 |
prefix = 'vserver!%s!error_handler' % (vserver) |
|---|
| 474 |
print "%s = %s" % (prefix, handler_val) |
|---|
| 475 |
|
|---|
| 476 |
while True: |
|---|
| 477 |
kind, error_val = self._lex.get_token() |
|---|
| 478 |
if kind == '}': |
|---|
| 479 |
self._lex.rewind() |
|---|
| 480 |
break |
|---|
| 481 |
elif kind != 'int': |
|---|
| 482 |
raise "Malformed ErrorHandler" |
|---|
| 483 |
|
|---|
| 484 |
kind, url_val = self._lex.get_token() |
|---|
| 485 |
if kind != 'str': |
|---|
| 486 |
raise "Malformed ErrorHandler" |
|---|
| 487 |
|
|---|
| 488 |
print "%s!%s = %s" % (prefix, error_val, url_val) |
|---|
| 489 |
|
|---|
| 490 |
kind, val = self._lex.get_token() |
|---|
| 491 |
if kind != '}': raise "Malformed ErrorHandler" |
|---|
| 492 |
|
|---|
| 493 |
elif kind == 'log': |
|---|
| 494 |
kind, log_val = self._lex.get_token() |
|---|
| 495 |
if kind != 'str': raise "Malformed Log" |
|---|
| 496 |
|
|---|
| 497 |
kind, val = self._lex.get_token() |
|---|
| 498 |
if kind != '{': |
|---|
| 499 |
self._lex.rewind() |
|---|
| 500 |
return True |
|---|
| 501 |
|
|---|
| 502 |
prefix = 'vserver!%s!logger' % (vserver) |
|---|
| 503 |
print "%s = %s" % (prefix, log_val) |
|---|
| 504 |
self._process_log (prefix) |
|---|
| 505 |
|
|---|
| 506 |
kind, val = self._lex.get_token() |
|---|
| 507 |
if kind != '}': raise "Malformed Log" |
|---|
| 508 |
|
|---|
| 509 |
else: |
|---|
| 510 |
self._lex.rewind() |
|---|
| 511 |
return False |
|---|
| 512 |
|
|---|
| 513 |
return True |
|---|
| 514 |
|
|---|
| 515 |
def _process_server_content (self): |
|---|
| 516 |
kind, val = self._lex.get_token() |
|---|
| 517 |
if kind is None: return False |
|---|
| 518 |
|
|---|
| 519 |
if kind in self.srv_entries: |
|---|
| 520 |
val_kind, val_val = self._lex.get_token() |
|---|
| 521 |
expected_type = self.srv_entries[kind] |
|---|
| 522 |
if val_kind != expected_type: |
|---|
| 523 |
raise "Bad %s value: type=%s expected=%s" % \ |
|---|
| 524 |
(kind, val_kind, expected_type) |
|---|
| 525 |
|
|---|
| 526 |
if kind in self.similar_entries: |
|---|
| 527 |
kind = self.similar_entries[kind] |
|---|
| 528 |
|
|---|
| 529 |
print "server!%s = %s" % (kind, str(val_val)) |
|---|
| 530 |
|
|---|
| 531 |
elif kind == 'icons': |
|---|
| 532 |
kind, icons_val = self._lex.get_token() |
|---|
| 533 |
if kind != 'path': raise "Malformed Icons" |
|---|
| 534 |
|
|---|
| 535 |
elif kind == 'include': |
|---|
| 536 |
kind, incl_val = self._lex.get_token() |
|---|
| 537 |
if kind != 'path': raise "Malformed Include" |
|---|
| 538 |
|
|---|
| 539 |
print "include = %s" % (incl_val) |
|---|
| 540 |
|
|---|
| 541 |
elif kind == 'encoder': |
|---|
| 542 |
kind, enc_val = self._lex.get_token() |
|---|
| 543 |
if kind != 'str': raise "Malformed encoder" |
|---|
| 544 |
|
|---|
| 545 |
kind, val = self._lex.get_token() |
|---|
| 546 |
if kind != '{': raise "Malformed encoder" |
|---|
| 547 |
|
|---|
| 548 |
prefix = 'server!encoder!%s' % (enc_val) |
|---|
| 549 |
self._process_encoder (prefix) |
|---|
| 550 |
|
|---|
| 551 |
kind, val = self._lex.get_token() |
|---|
| 552 |
if kind != '}': raise "Malformed encoder" |
|---|
| 553 |
|
|---|
| 554 |
elif kind == 'server': |
|---|
| 555 |
self._lex.rewind() |
|---|
| 556 |
self._process_server() |
|---|
| 557 |
|
|---|
| 558 |
elif kind == 'threadnumber': |
|---|
| 559 |
kind, val_num = self._lex.get_token() |
|---|
| 560 |
if kind != 'int': raise "Malformed threadnumber" |
|---|
| 561 |
|
|---|
| 562 |
print "server!thread_number = %s" % (val_num) |
|---|
| 563 |
|
|---|
| 564 |
kind, val = self._lex.get_token() |
|---|
| 565 |
if kind != '{': |
|---|
| 566 |
self._lex.rewind() |
|---|
| 567 |
return True |
|---|
| 568 |
|
|---|
| 569 |
kind, val_type = self._lex.get_token() |
|---|
| 570 |
if kind != 'policy': |
|---|
| 571 |
raise "Malformed threadnumber" |
|---|
| 572 |
|
|---|
| 573 |
kind, val_val = self._lex.get_token() |
|---|
| 574 |
if kind != 'str': raise "Malformed threadnumber" |
|---|
| 575 |
|
|---|
| 576 |
print "server!thread_policy = %s" % (val_val) |
|---|
| 577 |
|
|---|
| 578 |
kind, val = self._lex.get_token() |
|---|
| 579 |
if kind != '}': raise "Malformed threadnumber" |
|---|
| 580 |
|
|---|
| 581 |
elif kind == 'sendfile': |
|---|
| 582 |
kind, val = self._lex.get_token() |
|---|
| 583 |
if kind != '{': raise "Malformed Sendfile" |
|---|
| 584 |
|
|---|
| 585 |
while True: |
|---|
| 586 |
kind, val = self._lex.get_token() |
|---|
| 587 |
if kind == '}': |
|---|
| 588 |
break |
|---|
| 589 |
elif kind != 'str': |
|---|
| 590 |
raise "Malformed Sendfile" |
|---|
| 591 |
|
|---|
| 592 |
val = val.lower() |
|---|
| 593 |
|
|---|
| 594 |
if val == 'minsize': |
|---|
| 595 |
kind, val_num = self._lex.get_token() |
|---|
| 596 |
if kind != 'int': raise "Malformed Sendfile" |
|---|
| 597 |
|
|---|
| 598 |
print "server!sendfile_min = %s" % (val_num) |
|---|
| 599 |
|
|---|
| 600 |
elif val == 'maxsize': |
|---|
| 601 |
kind, val_num = self._lex.get_token() |
|---|
| 602 |
if kind != 'int': raise "Malformed Sendfile" |
|---|
| 603 |
|
|---|
| 604 |
print "server!sendfile_max = %s" % (val_num) |
|---|
| 605 |
|
|---|
| 606 |
else: |
|---|
| 607 |
raise "Malformed Sendfile" |
|---|
| 608 |
|
|---|
| 609 |
else: |
|---|
| 610 |
self._lex.rewind() |
|---|
| 611 |
return False |
|---|
| 612 |
|
|---|
| 613 |
return True |
|---|
| 614 |
|
|---|
| 615 |
def _process_server (self): |
|---|
| 616 |
self._vserver_last_priority = 1 |
|---|
| 617 |
|
|---|
| 618 |
kind, val = self._lex.get_token() |
|---|
| 619 |
if kind != 'server': raise "Expected server" |
|---|
| 620 |
|
|---|
| 621 |
kind, val = self._lex.get_token() |
|---|
| 622 |
if kind not in ['list', 'str']: raise "Malformed server" |
|---|
| 623 |
|
|---|
| 624 |
if kind == 'list': |
|---|
| 625 |
l = map(lambda x: x.strip(), val.split(',')) |
|---|
| 626 |
domains = l |
|---|
| 627 |
else: |
|---|
| 628 |
domains = [val] |
|---|
| 629 |
|
|---|
| 630 |
vserver = domains[0] |
|---|
| 631 |
|
|---|
| 632 |
kind, val = self._lex.get_token() |
|---|
| 633 |
if kind != '{': raise "Expected {" |
|---|
| 634 |
|
|---|
| 635 |
i = 0 |
|---|
| 636 |
for v in domains: |
|---|
| 637 |
i = i + 1 |
|---|
| 638 |
print "vserver!%s!domain!%d = %s" % (vserver, i, v) |
|---|
| 639 |
|
|---|
| 640 |
self._vserver_has_document_root = False |
|---|
| 641 |
|
|---|
| 642 |
while True: |
|---|
| 643 |
more = self._process_virtual_server_content (vserver) |
|---|
| 644 |
if not more: break |
|---|
| 645 |
|
|---|
| 646 |
kind, val = self._lex.get_token() |
|---|
| 647 |
if kind != '}': raise "Expected }, got %s=%s" % (kind,val) |
|---|
| 648 |
|
|---|
| 649 |
if not self._vserver_has_document_root: |
|---|
| 650 |
print "vserver!%s!document_root = %s" % (vserver, FAKED_DOCUMENT_ROOT) |
|---|
| 651 |
|
|---|
| 652 |
print |
|---|
| 653 |
|
|---|
| 654 |
def process_sentence (self): |
|---|
| 655 |
kind, val = self._lex.get_token() |
|---|
| 656 |
if kind == None: return False |
|---|
| 657 |
self._lex.rewind() |
|---|
| 658 |
|
|---|
| 659 |
# Global server stuff |
|---|
| 660 |
ok = self._process_server_content () |
|---|
| 661 |
if ok: return True |
|---|
| 662 |
|
|---|
| 663 |
# Default virtual server |
|---|
| 664 |
ok = self._process_virtual_server_content () |
|---|
| 665 |
if ok: return True |
|---|
| 666 |
|
|---|
| 667 |
# Icons |
|---|
| 668 |
ok = self._process_icons_content () |
|---|
| 669 |
if ok: return True |
|---|
| 670 |
|
|---|
| 671 |
# Unknown: report error |
|---|
| 672 |
kind, val = self._lex.get_token() |
|---|
| 673 |
raise "Unknown token: %s = %s" % (kind, str(val)) |
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
def convert (self): |
|---|
| 677 |
print '# Cherokee 0.6.x configuration file generated by 05to06.py\n' |
|---|
| 678 |
print '# Copyright (C) 2006 Alvaro Lopez Ortega <alvaro@alobbs.com>\n' |
|---|
| 679 |
|
|---|
| 680 |
print '# This program is distributed in the hope that it will be useful,' |
|---|
| 681 |
print '# but WITHOUT ANY WARRANTY, to the extent permitted by law; without' |
|---|
| 682 |
print '# even the implied warranty of MERCHANTABILITY or FITNESS FOR A' |
|---|
| 683 |
print '# PARTICULAR PURPOSE.\n\n' |
|---|
| 684 |
|
|---|
| 685 |
while True: |
|---|
| 686 |
more = self.process_sentence() |
|---|
| 687 |
if not more: |
|---|
| 688 |
break |
|---|
| 689 |
|
|---|
| 690 |
|
|---|
| 691 |
class Lexer: |
|---|
| 692 |
special_strings = ['server_info'] |
|---|
| 693 |
|
|---|
| 694 |
reserved_words = ['{', '}', 'porttls', 'port', 'directoryindex', 'directory', 'extension', |
|---|
| 695 |
'request', 'handler', 'ipv6', 'timeout', 'keepalive', 'logflushinterval', |
|---|
| 696 |
'maxkeepaliverequests', 'servertokens', 'encoder', 'log', 'server', |
|---|
| 697 |
'allow', 'deny', 'pidfile', 'icons', 'mimefile', 'errorhandler', 'auth', |
|---|
| 698 |
'include', 'listenqueuesize', 'listen', 'userdir', 'user','group', 'chroot', |
|---|
| 699 |
'maxfds', 'maxconnectionreuse', 'panicaction', 'pollmethod', 'documentroot', |
|---|
| 700 |
'threadnumber', 'policy', 'sendfile', 'file', 'suffix', 'parentdirectory', |
|---|
| 701 |
'default', 'sslcertificatefile', 'sslcertificatekeyfile', 'sslcalistfile'] |
|---|
| 702 |
|
|---|
| 703 |
def __init__ (self, txt): |
|---|
| 704 |
self._txt = txt |
|---|
| 705 |
self._last = None,None |
|---|
| 706 |
self._rewinded = False |
|---|
| 707 |
|
|---|
| 708 |
def _find_consume (self, s): |
|---|
| 709 |
found = (self._txt.lower().find (s.lower()) == 0) |
|---|
| 710 |
if not found: return False |
|---|
| 711 |
|
|---|
| 712 |
self._txt = self._txt[len(s):] |
|---|
| 713 |
return True |
|---|
| 714 |
|
|---|
| 715 |
def _get_word (self): |
|---|
| 716 |
if len(self._txt) == 0: |
|---|
| 717 |
return '' |
|---|
| 718 |
|
|---|
| 719 |
word = '' |
|---|
| 720 |
|
|---|
| 721 |
# Quoted string |
|---|
| 722 |
if self._txt[0] == '"': |
|---|
| 723 |
word += self._txt[0] |
|---|
| 724 |
self._txt = self._txt[1:] |
|---|
| 725 |
while True: |
|---|
| 726 |
word += self._txt[0] |
|---|
| 727 |
self._txt = self._txt[1:] |
|---|
| 728 |
if word[-2] != '\\': |
|---|
| 729 |
if word[-1] == '"' or len(self._txt) is 0: break |
|---|
| 730 |
return word |
|---|
| 731 |
|
|---|
| 732 |
# Common word |
|---|
| 733 |
while True: |
|---|
| 734 |
while self._txt[0] not in " \t\r\n": |
|---|
| 735 |
word += self._txt[0] |
|---|
| 736 |
self._txt = self._txt[1:] |
|---|
| 737 |
if len(self._txt) is 0: break |
|---|
| 738 |
if word[-1] != ',': break |
|---|
| 739 |
while self._txt[0] in " \t": |
|---|
| 740 |
self._txt = self._txt[1:] |
|---|
| 741 |
return word |
|---|
| 742 |
|
|---|
| 743 |
def rewind (self): |
|---|
| 744 |
self._rewinded = True |
|---|
| 745 |
|
|---|
| 746 |
def get_token (self): |
|---|
| 747 |
if self._rewinded: |
|---|
| 748 |
self._rewinded = False |
|---|
| 749 |
return self._last |
|---|
| 750 |
|
|---|
| 751 |
self._last = self._get_token_guts() |
|---|
| 752 |
return self._last |
|---|
| 753 |
|
|---|
| 754 |
def _get_token_guts (self): |
|---|
| 755 |
self._txt = self._txt.strip() |
|---|
| 756 |
|
|---|
| 757 |
# Special cases |
|---|
| 758 |
for word in self.special_strings: |
|---|
| 759 |
if self._find_consume(word): |
|---|
| 760 |
return 'str',word |
|---|
| 761 |
|
|---|
| 762 |
# Reserved words |
|---|
| 763 |
for word in self.reserved_words: |
|---|
| 764 |
if self._find_consume(word): |
|---|
| 765 |
return word,None |
|---|
| 766 |
|
|---|
| 767 |
# Int |
|---|
| 768 |
int_val = None |
|---|
| 769 |
|
|---|
| 770 |
word = self._get_word() |
|---|
| 771 |
if len(word) is 0: |
|---|
| 772 |
return None, None |
|---|
| 773 |
|
|---|
| 774 |
try: |
|---|
| 775 |
int_val = int(word) |
|---|
| 776 |
except: |
|---|
| 777 |
pass |
|---|
| 778 |
|
|---|
| 779 |
if int_val is not None: |
|---|
| 780 |
return 'int',int_val |
|---|
| 781 |
|
|---|
| 782 |
# Path |
|---|
| 783 |
if word[0] == '/' or word[1:3] in [':\\', ':/']: |
|---|
| 784 |
return 'path',word |
|---|
| 785 |
|
|---|
| 786 |
# Quoted string |
|---|
| 787 |
if word[0] == word[-1] == '"': |
|---|
| 788 |
return 'str',word[1:-1] |
|---|
| 789 |
|
|---|
| 790 |
# List |
|---|
| 791 |
if word.find(',') != -1: |
|---|
| 792 |
return 'list',word |
|---|
| 793 |
|
|---|
| 794 |
# Boolean |
|---|
| 795 |
if word.lower() == 'on': return 'int',1 |
|---|
| 796 |
if word.lower() == 'off': return 'int',0 |
|---|
| 797 |
|
|---|
| 798 |
# Name |
|---|
| 799 |
is_name = True |
|---|
| 800 |
for c in word: |
|---|
| 801 |
if not c in string.letters and \ |
|---|
| 802 |
not c in string.digits and \ |
|---|
| 803 |
not c in "._-&?=:\\/*": |
|---|
| 804 |
is_name = False |
|---|
| 805 |
if is_name: |
|---|
| 806 |
return 'str',word |
|---|
| 807 |
|
|---|
| 808 |
raise "Couldn't extract token " + word |
|---|
| 809 |
|
|---|
| 810 |
|
|---|
| 811 |
class Converter: |
|---|
| 812 |
def __init__ (self, fin, fout): |
|---|
| 813 |
self._in = fin |
|---|
| 814 |
self._out = fout |
|---|
| 815 |
|
|---|
| 816 |
def convert (self): |
|---|
| 817 |
tmp = filter (lambda x: len(x) > 0 and x[0] != '#', |
|---|
| 818 |
map (lambda x: x.strip(), |
|---|
| 819 |
self._in.readlines())) |
|---|
| 820 |
if not len(tmp): return |
|---|
| 821 |
|
|---|
| 822 |
incoming = reduce (lambda x,y: x+' '+y, tmp) |
|---|
| 823 |
self.lex = Lexer (incoming) |
|---|
| 824 |
self.syn = Syntax (self.lex) |
|---|
| 825 |
|
|---|
| 826 |
self.syn.convert() |
|---|
| 827 |
|
|---|
| 828 |
def main (): |
|---|
| 829 |
c = Converter (sys.stdin, sys.stdout) |
|---|
| 830 |
c.convert() |
|---|
| 831 |
|
|---|
| 832 |
if __name__ == "__main__": |
|---|
| 833 |
main() |
|---|