|
|
|
|
|
|
|
|
constant cvs_version="$Id: roxen.pike,v 1.946 2006/11/14 15:11:06 anders Exp $"; |
|
|
|
|
|
|
ArgCache argcache; |
|
|
#define IN_ROXEN |
#include <roxen.h> |
#include <config.h> |
#include <module.h> |
#include <variables.h> |
#include <stat.h> |
#include <timers.h> |
|
|
inherit "global_variables"; |
#ifdef SNMP_AGENT |
inherit "snmpagent"; |
#endif |
inherit "hosts"; |
inherit "disk_cache"; |
|
inherit "supports"; |
inherit "module_support"; |
inherit "config_userdb"; |
|
#ifdef THREADS |
|
Thread.Thread backend_thread; |
#endif /* THREADS */ |
|
|
|
|
|
#define LOC_S(X,Y) _STR_LOCALE("roxen_start",X,Y) |
#define LOC_M(X,Y) _STR_LOCALE("roxen_message",X,Y) |
#define CALL_M(X,Y) _LOCALE_FUN("roxen_message",X,Y) |
|
|
|
#ifdef SSL3_DEBUG |
# define SSL3_WERR(X) report_debug("TLS port %s: %s\n", get_url(), X) |
#else |
# define SSL3_WERR(X) |
#endif |
|
#ifdef THREAD_DEBUG |
# define THREAD_WERR(X) report_debug("Thread: "+X+"\n") |
#else |
# define THREAD_WERR(X) |
#endif |
|
|
#if constant(System.dumpable) |
#define enable_coredumps(X) System.dumpable(X) |
#elif constant(system.dumpable) |
|
#define enable_coredumps(X) system.dumpable(X) |
#else |
#define enable_coredumps(X) |
#endif |
|
#define DDUMP(X) sol( combine_path( __FILE__, "../../" + X ), dump ) |
static function sol = master()->set_on_load; |
|
#ifdef TEST_EUID_CHANGE |
int test_euid_change; |
#endif |
|
string md5( string what ) |
{ |
return Gmp.mpz(Crypto.md5()->update( what )->digest(),256) |
->digits(32); |
} |
|
string query_configuration_dir() |
{ |
return configuration_dir; |
} |
|
string filename( program|object o ) |
{ |
if( objectp( o ) ) |
o = object_program( o ); |
|
string fname = master()->program_name( o ); |
if( !fname ) |
fname = "Unknown Program"; |
return fname-(getcwd()+"/"); |
} |
|
static int once_mode; |
|
|
|
|
|
array(string) compat_levels = ({"2.1", "2.2", "2.4", "2.5", |
"3.3", "3.4", |
"4.0", "4.5", "4.6"}); |
|
#ifdef THREADS |
mapping(string:string) thread_names = ([]); |
string thread_name( object thread ) |
{ |
string tn; |
if( thread_names[ tn=sprintf("%O",thread) ] ) |
return thread_names[tn]; |
return tn; |
} |
|
void name_thread( object thread, string name ) |
{ |
catch(thread->set_name( name )); |
thread_names[ sprintf( "%O", thread ) ] = name; |
} |
|
|
Thread.Mutex euid_egid_lock = Thread.Mutex(); |
#endif /* THREADS */ |
|
|
|
|
|
|
|
int privs_level; |
|
static class Privs |
{ |
#if efun(seteuid) |
|
int saved_uid; |
int saved_gid; |
|
int new_uid; |
int new_gid; |
|
#define LOGP (variables && variables->audit && variables->audit->query()) |
|
#if constant(geteuid) && constant(getegid) && constant(seteuid) && constant(setegid) |
#define HAVE_EFFECTIVE_USER |
#endif |
|
static private string _getcwd() |
{ |
if (catch{return(getcwd());}) { |
return("Unknown directory (no x-bit on current directory?)"); |
} |
} |
|
static private string dbt(array t) |
{ |
if(!arrayp(t) || (sizeof(t)<2)) return ""; |
return (((t[0]||"Unknown program")-(_getcwd()+"/"))-"base_server/")+":"+t[1]+"\n"; |
} |
|
#ifdef THREADS |
static mixed mutex_key; |
static object threads_disabled; |
#endif /* THREADS */ |
|
int p_level; |
|
void create(string reason, int|string|void uid, int|string|void gid) |
{ |
|
if(getuid()) return; |
|
#ifdef PRIVS_DEBUG |
report_debug(sprintf("Privs(%O, %O, %O)\n" |
"privs_level: %O\n", |
reason, uid, gid, privs_level)); |
#endif /* PRIVS_DEBUG */ |
|
#ifdef HAVE_EFFECTIVE_USER |
array u; |
|
#ifdef THREADS |
if (euid_egid_lock) { |
catch { mutex_key = euid_egid_lock->lock(); }; |
} |
threads_disabled = _disable_threads(); |
#endif /* THREADS */ |
|
p_level = privs_level++; |
|
|
|
|
saved_uid = geteuid(); |
saved_gid = getegid(); |
seteuid(0); |
|
|
if(stringp(uid) && (replace(uid,"0123456789"/"",({""})*10)=="")) |
uid = (int)uid; |
|
if(stringp(gid) && (replace(gid, "0123456789"/"", ({"" })*10) == "")) |
gid = (int)gid; |
|
if(!stringp(uid)) |
u = getpwuid(uid); |
else |
{ |
u = getpwnam(uid); |
if(u) |
uid = u[2]; |
} |
|
if(u && !gid) |
gid = u[3]; |
|
if(!u) |
{ |
if (uid && (uid != "root")) |
{ |
if (intp(uid) && (uid >= 60000)) |
{ |
report_warning(sprintf("Privs: User %d is not in the password database.\n" |
"Assuming nobody.\n", uid)); |
|
gid = gid || uid; |
u = ({ "fake-nobody", "x", uid, gid, "A real nobody", "/", "/sbin/sh" }); |
} else { |
error("Unknown user: "+uid+"\n"); |
} |
} else { |
u = ({ "root", "x", 0, gid, "The super-user", "/", "/sbin/sh" }); |
} |
} |
|
if(LOGP) |
report_notice(LOC_M(1, "Change to %s(%d):%d privs wanted (%s), from %s"), |
(string)u[0], (int)uid, (int)gid, |
(string)reason, |
(string)dbt(backtrace()[-2])); |
|
if (u[2]) { |
#if efun(cleargroups) |
catch { cleargroups(); }; |
#endif /* cleargroups */ |
#if efun(initgroups) |
catch { initgroups(u[0], u[3]); }; |
#endif |
} |
gid = gid || getgid(); |
int err = (int)setegid(new_gid = gid); |
if (err < 0) { |
report_warning(LOC_M(2, "Privs: WARNING: Failed to set the " |
"effective group id to %d!\n" |
"Check that your password database is correct " |
"for user %s(%d),\n and that your group " |
"database is correct.\n"), |
gid, (string)u[0], (int)uid); |
int gid2 = gid; |
#ifdef HPUX_KLUDGE |
if (gid >= 60000) { |
|
|
|
|
|
|
|
report_debug("Privs: WARNING: Assuming nobody-group.\n" |
"Trying some alternatives...\n"); |
|
foreach(({ 60001, 65534, -2 }), gid2) { |
report_debug("%d... ", gid2); |
if (initgroups(u[0], gid2) >= 0) { |
if ((err = setegid(new_gid = gid2)) >= 0) { |
report_debug("Success!\n"); |
break; |
} |
} |
} |
} |
#endif /* HPUX_KLUDGE */ |
if (err < 0) { |
report_debug("Privs: Failed\n"); |
error ("Failed to set EGID to %d\n", gid); |
} |
report_debug("Privs: WARNING: Set egid to %d instead of %d.\n", |
gid2, gid); |
gid = gid2; |
} |
if(getgid()!=gid) setgid(gid||getgid()); |
seteuid(new_uid = uid); |
enable_coredumps(1); |
#endif /* HAVE_EFFECTIVE_USER */ |
} |
|
void destroy() |
{ |
|
if(getuid()) return; |
|
#ifdef PRIVS_DEBUG |
report_debug(sprintf("Privs->destroy()\n" |
"privs_level: %O\n", |
privs_level)); |
#endif /* PRIVS_DEBUG */ |
|
#ifdef HAVE_EFFECTIVE_USER |
|
if (p_level >= privs_level) { |
report_error(sprintf("Change back to uid#%d gid#%d from uid#%d gid#%d\n" |
"in wrong order! Saved level:%d Current level:%d\n" |
"Occurs in:\n%s\n", |
saved_uid, saved_gid, new_uid, new_gid, |
p_level, privs_level, |
describe_backtrace(backtrace()))); |
return(0); |
} |
if (p_level != privs_level-1) { |
report_error(sprintf("Change back to uid#%d gid#%d from uid#%d gid#%d\n" |
"Skips privs level. Saved level:%d Current level:%d\n" |
"Occurs in:\n%s\n", |
saved_uid, saved_gid, new_uid, new_gid, |
p_level, privs_level, |
describe_backtrace(backtrace()))); |
} |
privs_level = p_level; |
|
if(LOGP) { |
catch { |
array bt = backtrace(); |
if (sizeof(bt) >= 2) { |
report_notice(LOC_M(3,"Change back to uid#%d gid#%d, from %s")+"\n", |
saved_uid, saved_gid, dbt(bt[-2])); |
} else { |
report_notice(LOC_M(4,"Change back to uid#%d gid#%d, " |
"from backend")+"\n", saved_uid, saved_gid); |
} |
}; |
} |
|
#ifdef PRIVS_DEBUG |
int uid = geteuid(); |
if (uid != new_uid) { |
report_debug("Privs: UID #%d differs from expected #%d\n" |
"%s\n", |
uid, new_uid, describe_backtrace(backtrace())); |
} |
int gid = getegid(); |
if (gid != new_gid) { |
report_debug("Privs: GID #%d differs from expected #%d\n" |
"%s\n", |
gid, new_gid, describe_backtrace(backtrace())); |
} |
#endif /* PRIVS_DEBUG */ |
|
seteuid(0); |
array u = getpwuid(saved_uid); |
#if efun(cleargroups) |
catch { cleargroups(); }; |
#endif /* cleargroups */ |
if(u && (sizeof(u) > 3)) { |
catch { initgroups(u[0], u[3]); }; |
} |
setegid(saved_gid); |
seteuid(saved_uid); |
enable_coredumps(1); |
#endif /* HAVE_EFFECTIVE_USER */ |
} |
#else /* efun(seteuid) */ |
void create(string reason, int|string|void uid, int|string|void gid){} |
#endif /* efun(seteuid) */ |
} |
|
|
|
|
static Privs PRIVS(string r, int|string|void u, int|string|void g) |
{ |
return Privs(r, u, g); |
} |
|
|
Thread.Local current_configuration = Thread.Local(); |
|
|
|
|
|
|
class Fonts |
{ |
class Font |
{ |
Image.Image write( string ... what ); |
array(int) text_extents( string ... what ); |
}; |
array available_font_versions(string name, int size); |
string describe_font_type(string n); |
Font get_font(string f, int size, int bold, int italic, |
string justification, float|int xspace, float|int yspace); |
|
Font resolve_font(string f, string|void justification); |
array(string) available_fonts(int(0..1)|void force_reload); |
} |
Fonts fonts; |
|
|
program _configuration; |
|
array(Configuration) configurations = ({}); |
|
private void stop_all_configurations() |
{ |
configurations->unregister_urls(); |
#ifdef THREADS |
|
|
hold_handler_threads(); |
release_handler_threads(3); |
#endif |
configurations->stop(1); |
} |
|
|
private void really_low_shutdown(int exit_code) |
{ |
|
#ifdef THREADS |
catch (stop_handler_threads()); |
#endif /* THREADS */ |
if (!exit_code || once_mode) { |
|
catch { report_notice("Shutting down MySQL.\n"); }; |
catch { |
Sql.sql db = connect_to_my_mysql(0, "mysql"); |
db->shutdown(); |
}; |
} |
destruct (cache); |
#if 0 |
|
|
|
catch { |
if (exit_code && !once_mode) |
report_notice("Restarting Roxen.\n"); |
else |
report_notice("Shutting down Roxen.\n"); |
}; |
#endif |
roxenloader.real_exit( exit_code ); |
} |
|
private int _recurse; |
|
|
|
|
private void low_shutdown(int exit_code) |
{ |
if(_recurse >= 4) |
{ |
catch (report_notice("Exiting roxen (spurious signals received).\n")); |
catch (stop_all_configurations()); |
destruct(cache); |
#ifdef THREADS |
catch (stop_handler_threads()); |
#endif /* THREADS */ |
roxenloader.real_exit(exit_code); |
} |
if (_recurse++) return; |
|
catch(stop_all_configurations()); |
|
#ifdef SNMP_AGENT |
if(objectp(snmpagent)) { |
snmpagent->stop_trap(); |
snmpagent->disable(); |
} |
#endif |
|
call_out(really_low_shutdown, 0.1, exit_code); |
} |
|
|
|
|
void restart(float|void i, void|int exit_code) |
|
{ |
call_out(low_shutdown, i, exit_code || -1); |
} |
|
void shutdown(float|void i) |
|
{ |
call_out(low_shutdown, i, 0); |
} |
|
void exit_when_done() |
{ |
report_notice("Interrupt request received.\n"); |
low_shutdown(-1); |
} |
|
|
|
|
|
|
#ifdef THREADS |
|
|
Thread do_thread_create(string id, function f, mixed ... args) |
{ |
Thread.Thread t = thread_create(f, @args); |
name_thread( t, id ); |
return t; |
} |
|
|
|
class Queue |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
inherit Thread.Condition : r_cond; |
array buffer=allocate(8); |
int r_ptr, w_ptr; |
|
int size() |
{ |
return w_ptr - r_ptr; |
} |
|
mixed read() |
{ |
while(!(w_ptr - r_ptr)) { |
|
|
|
|
Thread.Mutex m = Thread.Mutex(); |
r_cond::wait (m->lock()); |
} |
mixed tmp = buffer[r_ptr]; |
buffer[r_ptr++] = 0; |
return tmp; |
} |
|
mixed tryread() |
{ |
if (!(w_ptr - r_ptr)) return ([])[0]; |
mixed tmp = buffer[r_ptr]; |
buffer[r_ptr++] = 0; |
return tmp; |
} |
|
|
void write(mixed v) |
{ |
if(w_ptr >= sizeof(buffer)) |
{ |
buffer=buffer[r_ptr..]+allocate(8); |
w_ptr-=r_ptr; |
r_ptr=0; |
} |
buffer[w_ptr++]=v; |
r_cond::signal(); |
} |
} |
|
|
|
|
|
|
|
|
|
|
|
|
local static Queue handle_queue = Queue(); |
|
|
|
local static int thread_reap_cnt; |
|
|
static int threads_on_hold; |
|
|
local static void handler_thread(int id) |
|
|
|
|
{ |
THREAD_WERR("Handle thread ["+id+"] started"); |
mixed h, q; |
set_u_and_gid (1); |
#ifdef TEST_EUID_CHANGE |
if (test_euid_change) { |
Stdio.File f = Stdio.File(); |
if (f->open ("rootonly", "r") && f->read()) |
werror ("Handler thread %d can read rootonly\n", id); |
else |
werror ("Handler thread %d can't read rootonly\n", id); |
} |
#endif |
while(1) |
{ |
int thread_flagged_as_busy; |
if(q=catch { |
do { |
|
THREAD_WERR("Handle thread ["+id+"] waiting for next event"); |
if(arrayp(h=handle_queue->read()) && h[0]) { |
THREAD_WERR(sprintf("Handle thread [%O] calling %O(%{%O, %})", |
id, h[0], h[1] / 1)); |
set_locale(); |
busy_threads++; |
thread_flagged_as_busy = 1; |
h[0](@h[1]); |
h=0; |
busy_threads--; |
thread_flagged_as_busy = 0; |
} else if(!h) { |
|
report_debug("Handle thread ["+id+"] stopped.\n"); |
thread_reap_cnt--; |
#ifdef NSERIOUS |
if(!thread_reap_cnt) report_debug("+++ATH\n"); |
#endif |
return; |
} |
#ifdef DEBUG |
else if (h != 1) |
error ("Unknown message in handle_queue: %O\n", h); |
#endif |
else { |
num_hold_messages--; |
THREAD_WERR("Handle thread [" + id + "] put on hold"); |
threads_on_hold++; |
if (Thread.Condition cond = hold_wakeup_cond) { |
|
|
|
|
|
Thread.Mutex m = Thread.Mutex(); |
cond->wait (m->lock()); |
} |
threads_on_hold--; |
THREAD_WERR("Handle thread [" + id + "] released"); |
} |
} while(1); |
}) { |
if (thread_flagged_as_busy) |
busy_threads--; |
if (h = catch { |
report_error( |
|
describe_backtrace(q)); |
if (q = catch {h = 0;}) { |
report_error(LOC_M(5, "Uncaught error in handler thread: %sClient " |
"will not get any response from Roxen.")+"\n", |
describe_backtrace(q)); |
catch (q = 0); |
} |
}) { |
catch { |
report_error("Error reporting error:\n"); |
report_error(sprintf("Raw error: %O\n", h[0])); |
report_error(sprintf("Original raw error: %O\n", q[0])); |
}; |
catch (q = 0); |
catch (h = 0); |
} |
} |
} |
} |
|
void handle(function f, mixed ... args) |
{ |
handle_queue->write(({f, args })); |
} |
|
int number_of_threads; |
|
|
int busy_threads; |
|
|
static array(object) handler_threads = ({}); |
|
|
void start_handler_threads() |
{ |
if (query("numthreads") <= 1) { |
set( "numthreads", 1 ); |
report_warning (LOC_S(1, "Starting one thread to handle requests.")+"\n"); |
} else { |
report_notice (LOC_S(2, "Starting %d threads to handle requests.")+"\n", |
query("numthreads") ); |
} |
array(object) new_threads = ({}); |
for(; number_of_threads < query("numthreads"); number_of_threads++) |
new_threads += ({ do_thread_create( "Handle thread [" + |
number_of_threads + "]", |
handler_thread, number_of_threads ) }); |
handler_threads += new_threads; |
} |
|
static int num_hold_messages; |
static Thread.Condition hold_wakeup_cond = Thread.Condition(); |
|
|
|
|
|
|
|
void hold_handler_threads() |
|
|
{ |
if (!hold_wakeup_cond) { |
THREAD_WERR("Ignoring request to hold handler threads during stop"); |
return; |
} |
|
int timeout=10; |
#if constant(_reset_dmalloc) |
|
timeout *= 10; |
#endif /* constant(_reset_dmalloc) */ |
|
THREAD_WERR("Putting " + (number_of_threads - threads_on_hold) + |
" handler threads on hold, " + |
threads_on_hold + " on hold already"); |
|
for (int i = number_of_threads - threads_on_hold - num_hold_messages; i-- > 0;) { |
handle_queue->write (1); |
num_hold_messages++; |
} |
while (threads_on_hold < number_of_threads && timeout--) |
sleep (0.1); |
|
THREAD_WERR(threads_on_hold + " handler threads on hold, " + |
(number_of_threads - threads_on_hold) + " not responding"); |
} |
|
void release_handler_threads (int numthreads) |
|
|
|
|
{ |
if (Thread.Condition cond = hold_wakeup_cond) { |
|
for (int i = handle_queue->size(); i && num_hold_messages; i--) { |
mixed task = handle_queue->tryread(); |
if (task == 1) num_hold_messages--; |
else handle_queue->write (task); |
} |
#ifdef DEBUG |
if (num_hold_messages) |
error ("num_hold_messages is bogus (%d).\n", num_hold_messages); |
#endif |
num_hold_messages = 0; |
|
int blocked_threads = number_of_threads - threads_on_hold; |
int threads_to_create = numthreads - threads_on_hold; |
|
THREAD_WERR("Releasing " + threads_on_hold + " threads on hold"); |
cond->broadcast(); |
|
if (threads_to_create > 0) { |
array(object) new_threads = ({}); |
for (int n = 0; n < threads_to_create; number_of_threads++, n++) |
new_threads += ({ do_thread_create( "Handle thread [" + |
number_of_threads + "]", |
handler_thread, number_of_threads ) }); |
handler_threads += new_threads; |
report_notice ("Created %d new handler threads to compensate " |
"for %d blocked ones.\n", threads_to_create, blocked_threads); |
} |
} |
else { |
THREAD_WERR("Ignoring request to release handler threads during stop"); |
return; |
} |
} |
|
static Thread.MutexKey backend_block_lock; |
|
void stop_handler_threads() |
|
|
{ |
int timeout=10; |
#if constant(_reset_dmalloc) |
|
timeout *= 10; |
#endif /* constant(_reset_dmalloc) */ |
report_debug("Stopping all request handler threads.\n"); |
|
|
|
if (Thread.Condition cond = hold_wakeup_cond) { |
hold_wakeup_cond = 0; |
cond->broadcast(); |
} |
|
while(number_of_threads>0) { |
number_of_threads--; |
handle_queue->write(0); |
thread_reap_cnt++; |
} |
handler_threads = ({}); |
|
if (this_thread() != backend_thread && !backend_block_lock) { |
thread_reap_cnt++; |
Thread.Mutex mutex = Thread.Mutex(); |
backend_block_lock = mutex->lock(); |
call_out (lambda () { |
thread_reap_cnt--; |
report_debug("Backend thread stopped.\n"); |
mutex->lock(); |
error("Backend stop failed.\n"); |
}, 0); |
} |
|
while(thread_reap_cnt) { |
sleep(0.1); |
if(--timeout<=0) { |
report_debug("Giving up waiting on threads; " |
"%d threads blocked.\n", thread_reap_cnt); |
#ifdef DEBUG |
describe_all_threads(); |
#endif |
return; |
} |
} |
} |
|
#else |
|
void handle(function f, mixed ... args) |
{ |
f(@args); |
} |
|
|
|
#endif /* THREADS */ |
|
function async_sig_start( function f, int really ) |
{ |
class SignalAsyncVerifier( function f ) |
{ |
static int async_called; |
|
void really_call( array args ) |
{ |
async_called = 0; |
f( @args ); |
} |
|
void call( mixed ... args ) |
{ |
if( async_called && async_called-time() ) |
{ |
report_debug("Received signal %s\n", (string) signame( args[0] ) ); |
report_debug("\n\n" |
"Async calling failed for %O, calling synchronous\n", f); |
report_debug("Backtrace at time of hangup:\n%s\n", |
describe_backtrace( backtrace() )); |
f( @args ); |
return; |
} |
if( !async_called ) |
{ |
report_debug("Received signal %s\n", (string) signame( args[0] ) ); |
async_called=time(); |
call_out( really_call, 0, args ); |
} |
} |
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( really > 0 ) |
return lambda( mixed ... args ){ call_out( f, 0, @args ); }; |
if( really < 0 ) |
return f; |
return SignalAsyncVerifier( f )->call; |
} |
|
#ifdef THREADS |
static Thread.Queue bg_queue = Thread.Queue(); |
static int bg_process_running; |
|
|
|
|
|
|
|
static constant bg_time_buffer_max = 30; |
static constant bg_time_buffer_min = 0; |
static int bg_last_busy = 0; |
|
static void bg_process_queue() |
{ |
if (bg_process_running) return; |
|
bg_process_running = 1; |
|
int maxbeats = |
min (time() - bg_last_busy, bg_time_buffer_max) * (int) (1 / 0.04); |
|
if (mixed err = catch { |
while (bg_queue->size()) { |
|
array task = bg_queue->read(); |
|
|
if (busy_threads > 1) { |
for (maxbeats = max (maxbeats, (int) (bg_time_buffer_min / 0.04)); |
busy_threads > 1 && maxbeats > 0; |
maxbeats--) |
|
sleep (0.04); |
bg_last_busy = time(); |
} |
|
#ifdef DEBUG_BACKGROUND_RUN |
report_debug ("background_run run %s (%s) [%d jobs left in queue]\n", |
functionp (task[0]) ? |
sprintf ("%s: %s", Function.defined (task[0]), |
master()->describe_function (task[0])) : |
programp (task[0]) ? |
sprintf ("%s: %s", Program.defined (task[0]), |
master()->describe_program (task[0])) : |
sprintf ("%O", task[0]), |
map (task[1], lambda (mixed arg) |
{return sprintf ("%O", arg);}) * ", ", |
bg_queue->size()); |
float task_time = gauge { |
#endif |
if (task[0]) |
|
|
task[0] (@task[1]); |
#ifdef DEBUG_BACKGROUND_RUN |
}; |
report_debug ("background_run done, took %f sec\n", task_time); |
#endif |
|
if (busy_threads > 1) bg_last_busy = time(); |
} |
}) { |
bg_process_running = 0; |
handle (bg_process_queue); |
throw (err); |
} |
bg_process_running = 0; |
} |
#endif |
|
void background_run (int|float delay, function func, mixed... args) |
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
#ifdef DEBUG_BACKGROUND_RUN |
report_debug ("background_run enqueue %s (%s) [%d jobs in queue]\n", |
functionp (func) ? |
sprintf ("%s: %s", Function.defined (func), |
master()->describe_function (func)) : |
programp (func) ? |
sprintf ("%s: %s", Program.defined (func), |
master()->describe_program (func)) : |
sprintf ("%O", func), |
map (args, lambda (mixed arg) |
{return sprintf ("%O", arg);}) * ", ", |
bg_queue->size()); |
#endif |
|
#ifdef THREADS |
if (!hold_wakeup_cond) |
|
return; |
|
function enqueue = lambda() |
{ |
bg_queue->write (({func, args})); |
if (!bg_process_running) |
handle (bg_process_queue); |
}; |
|
if (delay) |
call_out (enqueue, delay); |
else |
enqueue(); |
|
enqueue = 0; |
|
#else |
|
call_out (func, delay, @args); |
#endif |
} |
|
class BackgroundProcess |
|
|
|
|
|
|
{ |
int|float period; |
int stopping = 0; |
|
static void repeat (function func, mixed args) |
{ |
|
|
|
|
|
int self_refs = _refs (this); |
#ifdef DEBUG |
if (self_refs < 4) |
error ("Minimum ref calculation wrong - have only %d refs.\n", self_refs); |
#endif |
if (stopping || self_refs <= 4) return; |
func (@args); |
background_run (period, repeat, func, args); |
} |
|
|
|
|
void set_period (int|float period_) |
{ |
period = period_; |
} |
|
|
|
|
|
|
|
|
|
static void create (int|float period_, function func, mixed... args) |
{ |
period = period_; |
background_run (period, repeat, func, args); |
} |
|
void stop() |
|
{ |
stopping = 1; |
} |
|
string _sprintf() {return "BackgroundProcess()";} |
} |
|
|
mapping get_port_options( string key ) |
|
|
{ |
return (query( "port_options" )[ key ] || ([])); |
} |
|
void set_port_options( string key, mapping value ) |
|
|
{ |
mapping q = query("port_options"); |
q[ key ] = value; |
set( "port_options" , q ); |
save( ); |
} |
|
#ifdef DEBUG_URL2CONF |
#define URL2CONF_MSG(X...) report_debug (X) |
#else |
#define URL2CONF_MSG(X...) |
#endif |
|
static mapping(string:int(0..1)) host_is_local_cache = ([]); |
|
|
int(0..1) host_is_local(string hostname) |
{ |
int(0..1) res; |
if (!zero_type(res = host_is_local_cache[hostname])) return res; |
|
|
string ip = blocking_host_to_ip(hostname); |
|
|
Stdio.Port port = Stdio.Port(); |
|
catch { |
res = port->bind(0, 0, ip); |
}; |
|
destruct(port); |
return host_is_local_cache[hostname] = res; |
} |
|
Configuration find_configuration_for_url(Standards.URI url, |
void|Configuration only_this_conf, |
void|array(Protocol) return_port) |
|
|
|
|
|
{ |
Configuration c; |
Protocol c_portobj; |
|
string url_with_port = sprintf("%s://%s:%d%s", url->scheme, url->host, |
url->port, |
(sizeof(url->path)?url->path:"/")); |
|
URL2CONF_MSG("URL with port: %s\n", url_with_port); |
|
foreach( indices(urls), string u ) |
{ |
mixed q = urls[u]; |
URL2CONF_MSG("Trying %O:%O\n", u, q); |
if( glob( u+"*", url_with_port ) ) |
{ |
URL2CONF_MSG("glob match\n"); |
if( q->port && |
(c = q->port->find_configuration_for_url(url_with_port, 0, 1 )) ) |
{ |
URL2CONF_MSG("Found config: %O\n", c); |
|
if ((only_this_conf && (c != only_this_conf)) || |
((search(u, "*") != -1 || search(u, "?") != -1) && |
|
(!host_is_local(url->host)))) { |
|
URL2CONF_MSG("Bad match: only_this_conf:%O, host_is_local:%O\n", |
(only_this_conf && (c == only_this_conf)), |
(!host_is_local(url->host))); |
c = 0; |
continue; |
} |
c_portobj = q->port; |
break; |
} |
} |
} |
URL2CONF_MSG("Result: %O\n", c); |
if (return_port) |
return_port[0] = c_portobj; |
return c; |
} |
|
class InternalRequestID |
|
{ |
inherit RequestID; |
|
this_program set_path( string f ) |
{ |
raw_url = Roxen.http_encode_invalids( f ); |
|
if( strlen( f ) > 5 ) |
{ |
string a; |
switch( f[1] ) |
{ |
case '<': |
if (sscanf(f, "/<%s>/%s", a, f)==2) |
{ |
config = (multiset)(a/","); |
f = "/"+f; |
} |
|
case '(': |
if(strlen(f) && sscanf(f, "/(%s)/%s", a, f)==2) |
{ |
prestate = (multiset)( a/","-({""}) ); |
f = "/"+f; |
} |
} |
} |
not_query = Roxen.simplify_path( scan_for_query( f ) ); |
return this; |
} |
|
this_program set_url( string url ) |
{ |
object uri = Standards.URI(url); |
prot = upper_case(uri->scheme); |
misc->host = uri->host; |
if ((prot == "HTTP" && uri->port != 80) || |
(prot == "HTTPS" && uri->port != 443)) |
misc->host += ":" + uri->port; |
string path = uri->path; |
raw_url = path; |
method = "GET"; |
raw = "GET " + raw_url + " HTTP/1.1\r\n\r\n"; |
array(Protocol) port_array = ({ 0 }); |
conf = find_configuration_for_url(uri, 0, port_array); |
port_obj = port_array[0]; |
return set_path( raw_url ); |
} |
|
static string _sprintf() |
{ |
return sprintf("RequestID(conf=%O; not_query=%O)", conf, not_query ); |
} |
|
static void create() |
{ |
client = ({ "Roxen" }); |
prot = "INTERNAL"; |
method = "GET"; |
real_variables = ([]); |
variables = FakedVariables( real_variables ); |
root_id = this_object(); |
|
misc = ([ "pref_languages": PrefLanguages(), |
"cacheable": INITIAL_CACHEABLE, |
]); |
connection_misc = ([]); |
cookies = CookieJar(); |
throttle = ([]); |
client_var = ([]); |
request_headers = ([]); |
prestate = (<>); |
config = (<>); |
supports = (<>); |
pragma = (<>); |
rest_query = ""; |
extra_extension = ""; |
remoteaddr = "127.0.0.1"; |
} |
} |
|
class Protocol |
|
|
|
{ |
static Stdio.Port port_obj; |
|
inherit "basic_defvar"; |
int bound; |
|
string path; |
constant name = "unknown"; |
constant supports_ipless = 0; |
|
|
constant requesthandlerfile = ""; |
|
|
|
|
constant default_port = 4711; |
|
|
string url_prefix = name + "://"; |
|
int port; |
|
|
string ip; |
|
|
|
|
|
int refs; |
|
|
program requesthandler; |
|
|
array(string) sorted_urls = ({}); |
|
|
mapping(string:mapping) urls = ([]); |
|
|
mapping(Configuration:mapping) conf_data = ([]); |
|
|
void ref(string name, mapping data) |
|
{ |
if(urls[name]) |
{ |
conf_data[urls[name]->conf] = urls[name] = data; |
return; |
} |
if (!refs) path = data->path; |
else if (path != (data->path || "")) path = 0; |
refs++; |
conf_data[data->conf] = urls[name] = data; |
sorted_urls = Array.sort_array(indices(urls), |
lambda(string a, string b) { |
return sizeof(a)<sizeof(b); |
}); |
} |
|
void unref(string _name) |
|
{ |
|
|
|
m_delete(conf_data, urls[_name]->conf); |
m_delete(urls, _name); |
if (!path && sizeof (Array.uniq (values (urls)->path)) == 1) |
path = values (urls)[0]->path; |
sorted_urls -= ({_name}); |
#ifdef PORT_DEBUG |
report_debug("Protocol(%s)->unref(%O): refs:%d\n", |
get_url(), _name, refs); |
#endif /* PORT_DEBUG */ |
if( !--refs ) { |
if (retries) { |
remove_call_out(bind); |
} |
if (port_obj) { |
destruct(port_obj); |
} |
port_obj = 0; |
if (open_ports[name]) { |
if (open_ports[name][ip]) { |
m_delete(open_ports[name][ip], port); |
if(!sizeof(open_ports[name][ip])) { |
|
if (ip && ip != "::") |
m_delete(open_ports[name], ip); |
} |
} |
if (sizeof(open_ports[name]) <= 2) { |
|
int empty = 1; |
foreach(open_ports[name]; string ip; mapping m) { |
if (sizeof(m)) { |
empty = 0; |
break; |
} |
} |
if (empty) |
m_delete(open_ports, name); |
} |
} |
|
} |
} |
|
Stdio.File accept() |
{ |
return port_obj->accept(); |
} |
|
string query_address() |
{ |
return port_obj && port_obj->query_address(); |
} |
|
mapping mu; |
string rrhf; |
static void got_connection() |
{ |
Stdio.File q = accept( ); |
if( q ) |
{ |
if( !requesthandler ) |
{ |
requesthandler = (program)(rrhf); |
} |
Configuration c; |
if( refs < 2 ) |
{ |
if(!mu) |
{ |
mu = urls[sorted_urls[0]]; |
if(!(c=mu->conf)->inited ) |
c->enable_all_modules(); |
} else |
c = mu->conf; |
} |
requesthandler( q, this_object(), c ); |
} |
} |
|
local function sp_fcfu; |
|
|
|
#define INIT(X) do{ \ |
mapping _=(X); \ |
string __=_->path; \ |
c=_->conf; \ |
if(__&&id->adjust_for_config_path) \ |
id->adjust_for_config_path(__); \ |
if(!c->inited) \ |
c->enable_all_modules(); \ |
} while(0) |
|
Configuration find_configuration_for_url( string url, RequestID id, |
int|void no_default ) |
|
|
|
|
{ |
Configuration c; |
if( sizeof( urls ) == 1 && !no_default) |
{ |
if(!mu) mu=urls[sorted_urls[0]]; |
INIT( mu ); |
URL2CONF_MSG ("%O %O Only one configuration: %O\n", this_object(), url, c); |
return c; |
} else if (!sizeof(sorted_urls)) { |
URL2CONF_MSG("%O %O No active URLS!\n", this_object(), url); |
return 0; |
} |
|
url = lower_case( url ); |
URL2CONF_MSG("sorted_urls: %O\n" |
"url: %O\n", sorted_urls, url); |
|
|
|
foreach( sorted_urls, string in ) |
{ |
if( glob( in+"*", url ) ) |
{ |
INIT( urls[in] ); |
URL2CONF_MSG ("%O %O sorted_urls: %O\n", this_object(), url, c); |
return c; |
} |
} |
|
if( no_default ) { |
URL2CONF_MSG ("%O %O no default\n", this_object(), url); |
return 0; |
} |
|
|
|
mixed i; |
if( !functionp(sp_fcfu) && ( i=open_ports[ name ][ 0 ][ port ] ) ) |
sp_fcfu = i->find_configuration_for_url; |
|
if( sp_fcfu && (sp_fcfu != find_configuration_for_url) |
&& (i = sp_fcfu( url, id, 1 ))) { |
URL2CONF_MSG ("%O %O sp_fcfu: %O\n", this_object(), url, i); |
return i; |
} |
|
|
|
multiset choices = (< >); |
foreach( configurations, Configuration c ) |
if( c->query( "default_server" ) ) |
choices |= (< c >); |
|
if( sizeof( choices ) ) |
{ |
|
foreach( values(urls), mapping cc ) |
if( choices[ cc->conf ] ) |
{ |
INIT( cc ); |
URL2CONF_MSG ("%O %O conf in choices: %O\n", this_object(), url, c); |
return c; |
} |
|
|
|
|
|
c = ((array)choices)[0]; |
if(!c->inited) c->enable_all_modules(); |
URL2CONF_MSG ("%O %O any in choices: %O\n", this_object(), url, c); |
return c; |
} |
|
|
|
|
INIT( urls[sorted_urls[0]] ); |
id->misc->defaulted=1; |
URL2CONF_MSG ("%O %O first in sorted_urls: %O\n", this_object(), url, c); |
return c; |
} |
|
mixed query_option( string x ) |
|
{ |
return query( x ); |
} |
|
string get_key() |
|
{ |
if (ip == "::") |
return name + ":0:" + port; |
else |
return name+":"+ip+":"+port; |
} |
|
string get_url() |
|
{ |
return (string) name + "://" + |
(!ip ? "*" : has_value (ip, ":") ? "[" + ip + "]" : ip) + |
":" + port + "/"; |
} |
|
void save() |
|
{ |
set_port_options( get_key(), |
mkmapping( indices(variables), |
map(indices(variables),query))); |
} |
|
void restore() |
|
{ |
foreach( (array)get_port_options( get_key() ), array kv ) |
set( kv[0], kv[1] ); |
} |
|
static int retries; |
static void bind() |
{ |
if (bound) return; |
if (!port_obj) port_obj = Stdio.Port(); |
Privs privs = Privs (sprintf ("Binding %s", get_url())); |
if (port_obj->bind(port, got_connection, ip)) |
{ |
privs = 0; |
bound = 1; |
return; |
} |
privs = 0; |
#if constant(System.EAFNOSUPPORT) |
if (port_obj->errno() == System.EAFNOSUPPORT) { |
|
error("Invalid address " + ip); |
} |
#endif /* System.EAFNOSUPPORT */ |
report_error(LOC_M(6, "Failed to bind %s (%s)")+"\n", |
get_url(), strerror(port_obj->errno())); |
#if 0 |
werror (describe_backtrace (backtrace())); |
#endif |
#if constant(System.EADDRINUSE) || constant(system.EADDRINUSE) |
if ( |
#if constant(System.EADDRINUSE) |
(port_obj->errno() == System.EADDRINUSE) && |
#else /* !constant(System.EADDRINUSE) */ |
(port_obj->errno() == system.EADDRINUSE) && |
#endif /* constant(System.EADDRINUSE) */ |
(retries++ < 10)) { |
|
|
report_notice(LOC_M(62, "Attempt %d. Retrying in 1 minute.")+"\n", |
retries); |
call_out(bind, 60); |
} |
#endif /* constant(System.EADDRINUSE) || constant(system.EADDRINUSE) */ |
} |
|
static array(int) get_ipv6_sequence(string partition) |
{ |
array(int) segments = ({}); |
foreach(partition/":", string part) { |
if (has_value(part, ".")) { |
array(int) sub_segs = array_sscanf(part, "%d.%d.%d.%d"); |
switch(sizeof(sub_segs)) { |
default: |
case 4: |
segments += ({ sub_segs[0]*256+sub_segs[1], |
sub_segs[2]*256+sub_segs[3] }); |
break; |
case 3: |
segments += ({ sub_segs[0]*256+sub_segs[1], |
sub_segs[2] }); |
break; |
case 2: |
segments += ({ sub_segs[0]*256 + sub_segs[1]>>16, |
sub_segs[1]&0xffff }); |
break; |
} |
} else { |
segments += array_sscanf(part, "%x"); |
} |
} |
return segments; |
} |
|
string canonical_ip(string i) |
{ |
if (!i) return 0; |
if (has_value(i, ":")) { |
|
if (i == "::") return "::"; |
array(string) partitions = i/"::"; |
array(int) sections = get_ipv6_sequence(partitions[0]); |
if (sizeof(partitions) > 1) { |
array(int) tail = get_ipv6_sequence(partitions[1]); |
sections += allocate(8 - sizeof(sections) - sizeof(tail)) + tail; |
} else if (sizeof(sections) < 8) { |
sections += allocate(8 - sizeof(sections)); |
} |
i = sprintf("%04.4x:%04.4x:%04.4x:%04.4x:" |
"%04.4x:%04.4x:%04.4x:%04.4x", |
@sections); |
|
if (i == "0000:0000:0000:0000:0000:0000:0000:0000") return "::"; |
|
|
partitions = i/":"; |
int start; |
int max; |
int best; |
foreach(partitions + ({ "SENTINEL" }); int ind; string part) { |
if (part != "0000") { |
if ((ind - start) > max) { |
best = start; |
max = ind - start; |
} |
start = ind + 1; |
} |
} |
if (max) { |
i = (partitions[..best-1] + ({""}) + partitions[best+max..])*":"; |
if (!best) i = ":" + i; |
if (best + max == 8) i += ":"; |
} |
return i; |
} else { |
|
array(int) segments = array_sscanf(i, "%d.%d.%d.%d"); |
string bytes; |
switch(sizeof(segments)) { |
default: |
case 4: |
bytes = sprintf("%1c%1c%1c%1c", @segments); |
break; |
case 0: return 0; |
case 1: |
|
|
|
bytes = sprintf("%4c", @segments); |
break; |
case 2: |
|
|
|
|
|
|
bytes = sprintf("%1c%3c", @segments); |
break; |
case 3: |
|
|
|
|
|
|
bytes = sprintf("%1c%1c%2c", @segments); |
break; |
} |
if (bytes == "\0\0\0\0") return 0; |
return sprintf("%d.%d.%d.%d", @((array(int))bytes)); |
} |
} |
|
static void setup (int pn, string i) |
{ |
port = pn; |
ip = canonical_ip(i); |
|
restore(); |
if( file_stat( "../local/"+requesthandlerfile ) ) |
rrhf = "../local/"+requesthandlerfile; |
else |
rrhf = requesthandlerfile; |
DDUMP( rrhf ); |
#ifdef DEBUG |
if( !requesthandler ) |
requesthandler = (program)(rrhf); |
#endif |
bound = 0; |
port_obj = 0; |
retries = 0; |
} |
|
static void create( int pn, string i ) |
|
{ |
setup (pn, i); |
bind(); |
} |
|
static string _sprintf( ) |
{ |
return "Protocol(" + get_url() + ")"; |
} |
} |
|
#if constant(SSL.sslfile) |
class SSLProtocol |
|
{ |
inherit Protocol; |
|
|
SSL.context ctx = SSL.context(); |
|
int cert_failure; |
|
static void cert_err_unbind() |
{ |
if (bound) { |
port_obj->close(); |
report_warning ("TLS port %s closed.\n", get_url()); |
bound = 0; |
} |
} |
|
#define CERT_WARNING(VAR, MSG, ARGS...) do { \ |
string msg = (MSG); \ |
array args = ({ARGS}); \ |
if (sizeof (args)) msg = sprintf (msg, @args); \ |
report_warning ("TLS port %s: %s", get_url(), msg); \ |
(VAR)->add_warning (msg); \ |
} while (0) |
|
#define CERT_ERROR(VAR, MSG, ARGS...) do { \ |
string msg = (MSG); \ |
array args = ({ARGS}); \ |
if (sizeof (args)) msg = sprintf (msg, @args); \ |
report_error ("TLS port %s: %s", get_url(), msg); \ |
(VAR)->add_warning (msg); \ |
cert_err_unbind(); \ |
cert_failure = 1; \ |
return; \ |
} while (0) |
|
void certificates_changed(Variable|void ignored) |
{ |
int old_cert_failure = cert_failure; |
|
string raw_keydata; |
array(string) certificates = ({}); |
array(object) decoded_certs = ({}); |
Variable Certificates = getvar("ssl_cert_file"); |
|
object privs = Privs("Reading cert file"); |
|
foreach(map(Certificates->query(), String.trim_whites), string cert_file) { |
string raw_cert; |
SSL3_WERR (sprintf ("Reading cert file %O", cert_file)); |
if( catch{ raw_cert = lopen(cert_file, "r")->read(); } ) |
{ |
CERT_WARNING (Certificates, |
LOC_M(8, "Reading certificate file %O failed: %s\n"), |
cert_file, strerror (errno())); |
continue; |
} |
|
object msg = Tools.PEM.pem_msg()->init( raw_cert ); |
object part = msg->parts["CERTIFICATE"] || |
msg->parts["X509 CERTIFICATE"]; |
string cert; |
|
if (msg->parts["RSA PRIVATE KEY"] || |
msg->parts["DSA PRIVATE KEY"]) { |
raw_keydata = raw_cert; |
} |
|
if (!part || !(cert = part->decoded_body())) |
{ |
CERT_WARNING (Certificates, |
LOC_M(10, "No certificate found in %O.\n"), |
cert_file); |
continue; |
} |
certificates += ({ cert }); |
|
|
object tbs = Tools.X509.decode_certificate (cert); |
if (!tbs) { |
CERT_WARNING (Certificates, |
LOC_M(13, "Certificate not valid (DER).\n")); |
continue; |
} |
decoded_certs += ({tbs}); |
} |
|
if (!sizeof(decoded_certs)) { |
report_error ("TLS port %s: %s", get_url(), |
LOC_M(63,"No certificates found.\n")); |
cert_err_unbind(); |
cert_failure = 1; |
return; |
} |
|
Variable KeyFile = getvar("ssl_key_file"); |
|
if( strlen(KeyFile->query())) { |
SSL3_WERR (sprintf ("Reading key file %O", KeyFile->query())); |
if (catch{ raw_keydata = lopen(KeyFile->query(), "r")->read(); } ) |
CERT_ERROR (KeyFile, |
LOC_M(9, "Reading key file %O failed: %s\n"), |
KeyFile->query(), strerror (errno())); |
} |
else |
KeyFile = Certificates; |
|
privs = 0; |
|
if (!raw_keydata) |
CERT_ERROR (KeyFile, LOC_M (17,"No private key found.\n")); |
|
object msg = Tools.PEM.pem_msg()->init( raw_keydata ); |
|
SSL3_WERR(sprintf("key file contains: %O", indices(msg->parts))); |
|
object part; |
if (part = msg->parts["RSA PRIVATE KEY"]) |
{ |
string key; |
|
if (!(key = part->decoded_body())) |
CERT_ERROR (KeyFile, |
LOC_M(11,"Private rsa key not valid")+" (PEM).\n"); |
|
object rsa = Standards.PKCS.RSA.parse_private_key(key); |
if (!rsa) |
CERT_ERROR (KeyFile, |
LOC_M(11,"Private rsa key not valid")+" (DER).\n"); |
|
ctx->rsa = rsa; |
|
SSL3_WERR(sprintf("RSA key size: %d bits", rsa->rsa_size())); |
|
if (rsa->rsa_size() > 512) |
{ |
|
#if constant(Crypto.RSA) |
ctx->short_rsa = Crypto.RSA()->generate_key(512, ctx->random); |
#else |
ctx->short_rsa = Crypto.rsa()->generate_key(512, ctx->random); |
#endif |
|
|
} |
ctx->rsa_mode(); |
|
array(int) key_matches = |
map(decoded_certs, |
lambda (object tbs) { |
return tbs->public_key->rsa->public_key_equal (rsa); |
}); |
|
int num_key_matches; |
|
array(string) new_certificates = allocate(sizeof(certificates)); |
int i,j; |
for (i=0; i < sizeof(certificates); i++) { |
if (key_matches[i]) { |
new_certificates[j++] = certificates[i]; |
num_key_matches++; |
} |
} |
for (i=0; i < sizeof(certificates); i++) { |
if (!key_matches[i]) { |
new_certificates[j++] = certificates[i]; |
} |
} |
if( !num_key_matches ) |
CERT_ERROR (KeyFile, |
LOC_M(14, "Certificate and private key do not match.\n")); |
ctx->certificates = new_certificates; |
} |
else if (part = msg->parts["DSA PRIVATE KEY"]) |
{ |
string key; |
|
if (!(key = part->decoded_body())) |
CERT_ERROR (KeyFile, |
LOC_M(15,"Private dsa key not valid")+" (PEM).\n"); |
|
object dsa = Standards.PKCS.DSA.parse_private_key(key); |
if (!dsa) |
CERT_ERROR (KeyFile, |
LOC_M(15,"Private dsa key not valid")+" (DER).\n"); |
|
SSL3_WERR(sprintf("Using DSA key.")); |
|
|
ctx->dsa = dsa; |
|
#if constant(SSL.Cipher) |
ctx->dh_params = SSL.Cipher.DHParameters(); |
#else |
ctx->dh_params = SSL.cipher()->dh_parameters(); |
#endif |
|
ctx->dhe_dss_mode(); |
|
|
|
ctx->certificates = certificates; |
} |
else |
CERT_ERROR (KeyFile, LOC_M(17,"No private key found.\n")); |
|
#if EXPORT |
ctx->export_mode(); |
#endif |
|
if (!bound) { |
bind(); |
if (old_cert_failure && bound) |
report_notice (LOC_M(64, "TLS port %s opened.\n"), get_url()); |
} |
} |
|
class CertificateListVariable |
{ |
inherit Variable.FileList; |
|
string doc() |
{ |
return sprintf(::doc() + "\n", |
combine_path(getcwd(), "../local"), |
getcwd()); |
} |
} |
|
class KeyFileVariable |
{ |
inherit Variable.String; |
|
string doc() |
{ |
return sprintf(::doc() + "\n", |
combine_path(getcwd(), "../local"), |
getcwd()); |
} |
} |
|
RoxenSSLFile accept() |
{ |
Stdio.File q = ::accept(); |
if (q) |
return RoxenSSLFile (q, ctx); |
return 0; |
} |
|
static void bind() |
{ |
|
if (!ctx->certificates) return; |
::bind(); |
} |
|
void create(int pn, string i) |
{ |
#if constant(Crypto.Random.random_string) |
ctx->random = Crypto.Random.random_string; |
#else |
ctx->random = Crypto.randomness.reasonably_random()->read; |
#endif |
|
set_up_ssl_variables( this_object() ); |
|
::setup(pn, i); |
|
certificates_changed(); |
|
|
|
|
|
|
|
getvar ("ssl_cert_file")->set_changed_callback (certificates_changed); |
getvar ("ssl_key_file")->set_changed_callback (certificates_changed); |
} |
|
string _sprintf( ) |
{ |
return "SSLProtocol(" + get_url() + ")"; |
} |
} |
#endif |
|
mapping(string:Protocol) build_protocols_mapping() |
{ |
mapping protocols = ([]); |
int st = gethrtime(); |
report_debug("Protocol handlers ... \b"); |
#ifndef DEBUG |
class lazy_load( string prog, string name ) |
{ |
program real; |
static void realize() |
{ |
if( catch { |
DDUMP( prog ); |
real = (program)prog; |
protocols[name] = real; |
} ) |
report_error("Failed to compile protocol handler for "+name+"\n"); |
} |
|
Protocol `()(mixed ... x) |
{ |
if(!real) realize(); |
return real(@x); |
}; |
mixed `->( string x ) |
{ |
if(!real) realize(); |
return predef::`->(real, x); |
} |
}; |
#endif |
foreach( glob( "prot_*.pike", get_dir("protocols") ), string s ) |
{ |
sscanf( s, "prot_%s.pike", s ); |
#if !constant(SSL.sslfile) |
switch( s ) |
{ |
case "https": |
case "ftps": |
continue; |
} |
#endif |
report_debug( "\b%s \b", s ); |
|
catch |
{ |
#ifdef DEBUG |
protocols[ s ] = (program)("protocols/prot_"+s+".pike"); |
#else |
protocols[ s ] = lazy_load( ("protocols/prot_"+s+".pike"),s ); |
#endif |
}; |
} |
foreach( glob("prot_*.pike",get_dir("../local/protocols")||({})), string s ) |
{ |
sscanf( s, "prot_%s.pike", s ); |
#if !constant(SSL.sslfile) |
switch( s ) |
{ |
case "https": |
case "ftps": |
continue; |
} |
#endif |
report_debug( "\b%s \b", s ); |
catch { |
#ifdef DEBUG |
protocols[ s ] = (program)("../local/protocols/prot_"+s+".pike"); |
#else |
protocols[ s ] = lazy_load( ("../local/protocols/prot_"+s+".pike"),s ); |
#endif |
}; |
} |
report_debug("\bDone [%.1fms]\n", (gethrtime()-st)/1000.0 ); |
return protocols; |
} |
|
|
mapping protocols; |
|
|
mapping(string:mapping(string:mapping(int:Protocol))) open_ports = ([ ]); |
|
|
mapping(string:mapping(string:Configuration)) urls = ([]); |
array sorted_urls = ({}); |
|
array(string) find_ips_for( string what ) |
{ |
if( what == "*" || lower_case(what) == "any" ) |
return ({ 0, |
#if constant(__ROXEN_SUPPORTS_IPV6__) |
"::", |
#endif /* __ROXEN_SUPPORTS_IPV6__ */ |
}); |
|
if( is_ip( what ) ) |
return ({ what }); |
else if (what[0] == '[' && what[-1] == ']') { |
|
|
|
|
|
|
|
|
return ({ what[1..sizeof(what)-2] }); |
} else if (has_suffix(lower_case(what), ".ipv6")) { |
|
|
|
|
return ({ replace(what[..sizeof(what)-6], "-", ":") }); |
} |
array res = gethostbyname( what ); |
if( res && sizeof( res[1] ) ) |
return Array.uniq(res[1]); |
|
report_error(LOC_M(46, "Cannot possibly bind to %O, that host is " |
"unknown. Substituting with ANY")+"\n", what); |
return 0; |
} |
|
string normalize_url(string url) |
{ |
if (!sizeof (url - " " - "\t")) return ""; |
|
url = lower_case( url ); |
Standards.URI ui = Standards.URI(url); |
ui->fragment = 0; |
url = (string)ui; |
url = replace( url, "/ANY", "/*" ); |
url = replace( url, "/any", "/*" ); |
|
string host, path, protocol; |
|
sscanf( url, "%[^:]://%[^/]%s", protocol, host, path ); |
|
if (!host || !stringp(host)) return ""; |
if (!protocols[ protocol ]) return ""; |
|
int port; |
sscanf(host, "%[^:]:%d", host, port); |
|
if( !port ) |
{ |
port = protocols[ protocol ]->default_port; |
url = protocol+"://"+host+":"+port+path; |
} |
return url; |
} |
|
void unregister_url(string url, Configuration conf) |
{ |
string ourl = url; |
if (!sizeof(url = normalize_url(url))) return; |
|
report_debug("Unregister "+url+"\n"); |
|
if (urls[url] && (!conf || !urls[url]->conf || (urls[url]->conf == conf)) && |
urls[url]->port) |
{ |
urls[ url ]->port->unref(url); |
m_delete( urls, url ); |
m_delete( urls, ourl ); |
sort_urls(); |
} |
} |
|
array all_ports( ) |
{ |
return Array.uniq( values( urls )->port )-({0}); |
} |
|
Protocol find_port( string name ) |
{ |
foreach( all_ports(), Protocol p ) |
if( p->get_key() == name ) |
return p; |
} |
|
void sort_urls() |
{ |
sorted_urls = indices( urls ); |
sort( map( map( sorted_urls, strlen ), `-), sorted_urls ); |
} |
|
int register_url( string url, Configuration conf ) |
{ |
string ourl = url; |
url = lower_case( url ); |
if (!sizeof (url - " " - "\t")) return 1; |
string protocol; |
string host; |
int port; |
string path; |
|
Standards.URI ui = Standards.URI(url); |
mapping opts = ([]); |
string a, b; |
foreach( (ui->fragment||"")/";", string x ) |
{ |
sscanf( x, "%s=%s", a, b ); |
opts[a]=b; |
} |
ui->fragment = 0; |
url = (string)ui; |
|
if( (int)opts->nobind ) |
{ |
report_warning( |
LOC_M(61,"Not binding the port %O, disabled in configuration")+"\n", |
url ); |
return 0; |
} |
url = replace( url, "/ANY", "/*" ); |
url = replace( url, "/any", "/*" ); |
|
sscanf( url, "%[^:]://%[^/]%s", protocol, host, path ); |
if (!host || !stringp(host)) |
{ |
report_error(LOC_M(19,"Bad URL '%s' for server `%s'")+"\n", |
url, conf->query_name()); |
return 0; |
} |
|
if( !protocols[ protocol ] ) |
{ |
report_error(LOC_M(7,"The protocol '%s' is not available")+"\n", protocol); |
return 0; |
} |
|
sscanf(host, "%[^:]:%d", host, port); |
|
if( !port ) |
{ |
port = protocols[ protocol ]->default_port; |
url = protocol+"://"+host+":"+port+path; |
} |
|
if( strlen( path ) && ( path[-1] == '/' ) ) |
path = path[..strlen(path)-2]; |
if( !strlen( path ) ) |
path = 0; |
|
if( urls[ url ] ) |
{ |
if( !urls[ url ]->port ) |
m_delete( urls, url ); |
else if( urls[ url ]->conf ) |
{ |
if( urls[ url ]->conf != conf ) |
{ |
report_error(LOC_M(20, |
"Cannot register URL %s, " |
"already registered by %s!")+"\n", |
url, urls[ url ]->conf->name); |
return 0; |
} |
urls[ url ]->port->ref(url, urls[url]); |
return 1; |
} |
else |
urls[ url ]->port->unref( url ); |
} |
|
Protocol prot; |
|
if( !( prot = protocols[ protocol ] ) ) |
{ |
report_error(LOC_M(21, "Cannot register URL %s, " |
"cannot find the protocol %s!")+"\n", |
url, protocol); |
return 0; |
} |
|
if( !port ) |
port = prot->default_port; |
|
urls[ url ] = ([ "conf":conf, "path":path, "hostname": host ]); |
urls[ ourl ] = urls[url] + ([]); |
sorted_urls += ({ url }); |
|
array(string)|int(-1..0) required_hosts; |
|
if (is_ip(host)) |
required_hosts = ({ host }); |
else if(!sizeof(required_hosts = |
filter(replace(opts->ip||"", " ","")/",", is_ip)) ) |
required_hosts = find_ips_for( host ); |
|
if (!required_hosts) { |
|
|
report_error(LOC_M(23, "Cannot register URL %s!")+"\n", url); |
return 0; |
} |
|
mapping m; |
if( !( m = open_ports[ protocol ] ) ) |
|
|
|
|
|
m = open_ports[ protocol ] = ([ 0:([]), "::":([]) ]); |
|
if (prot->supports_ipless ) { |
|
|
|
|
|
array(string) ipv6 = filter(required_hosts - ({ 0 }), has_value, ":"); |
array(string) ipv4 = required_hosts - ipv6; |
if (m[0][port] && sizeof(ipv4 - ({ 0 }))) { |
|
ipv4 = ({ 0 }); |
} |
#if constant(__ROXEN_SUPPORTS_IPV6__) |
if (m["::"][port] && sizeof(ipv6 - ({ "::" }))) { |
|
ipv6 = ({ "::" }); |
} |
required_hosts = ipv4 + ipv6; |
#else |
if (sizeof(ipv6)) { |
foreach(ipv6, string p) { |
report_warning(LOC_M(65, "IPv6 port for URL %s disabled: %s\n"), |
url, p); |
} |
} |
required_hosts = ipv4; |
#endif /* __ROXEN_SUPPORTS_IPV6__ */ |
} |
|
int failures; |
|
foreach(required_hosts, string required_host) |
{ |
if( m[ required_host ] && m[ required_host ][ port ] ) |
{ |
m[required_host][port]->ref(url, urls[url]); |
|
urls[url]->port = m[required_host][port]; |
urls[ourl]->port = m[required_host][port]; |
continue; |
} |
|
if( !m[ required_host ] ) |
m[ required_host ] = ([ ]); |
|
mixed err; |
if (err = catch { |
m[ required_host ][ port ] = prot( port, required_host ); |
}) { |
failures++; |
if (has_prefix(describe_error(err), "Invalid address") && |
required_host && has_value(required_host, ":")) { |
report_error(sprintf("Failed to initialize IPv6 port for URL %s" |
" (ip %s). Not supported?\n", |
url, required_host)); |
} else { |
report_error(sprintf("Initializing the port handler for URL %s" |
" failed! (ip %s)\n" |
"%s\n", |
url, |
required_host||"ANY", |
describe_backtrace(err))); |
} |
continue; |
} |
|
if( !( m[ required_host ][ port ] ) ) |
{ |
m_delete( m[ required_host ], port ); |
failures++; |
if (required_host) { |
report_warning(LOC_M(22, "Binding the port on IP %s failed\n" |
" for URL %s!\n"), |
required_host, url); |
} |
continue; |
} |
|
|
urls[ url ]->port = m[ required_host ][ port ]; |
urls[ ourl ]->port = m[ required_host ][ port ]; |
m[ required_host ][ port ]->ref(url, urls[url]); |
|
if( !m[ required_host ][ port ]->bound ) |
failures++; |
} |
if (failures == sizeof(required_hosts)) |
{ |
report_error(LOC_M(23, "Cannot register URL %s!")+"\n", url); |
return 0; |
} |
sort_urls(); |
report_notice(" "+LOC_S(3, "Registered %s for %s")+"\n", |
url, conf->query_name() ); |
return 1; |
} |
|
|
Configuration find_configuration( string name ) |
|
|
{ |
|
if( Configuration o = get_configuration( name ) ) |
return o; |
|
name = replace( lower_case( replace(name,"-"," ") )-" ", "/", "-" ); |
foreach( configurations, Configuration o ) |
{ |
if( (lower_case( replace( replace(o->name, "-"," ") - " " , |
"/", "-" ) ) == name) || |
(lower_case( replace( replace(o->query_name(), "-", " ") - " " , |
"/", "-" ) ) == name) ) |
return o; |
} |
return 0; |
} |
|
static int last_hrtime = gethrtime(1)/100; |
static int clock_sequence = random(0x4000); |
static string hex_mac_address = |
Crypto.string_to_hex(Crypto.randomness.reasonably_random()->read(6)| |
"\1\0\0\0\0\0"); |
|
string new_uuid_string() |
{ |
int now = gethrtime(1)/100; |
if (now != last_hrtime) { |
clock_sequence = random(0x4000); |
last_hrtime = now; |
} |
int seq = clock_sequence++; |
|
|
|
|
#if 0 |
now -= Calendar.parse("%Y-%M-%D %h:%m:%s.%f %z", |
"1582-10-15 00:00:00.00 GMT")->unix_time() * 10000000; |
#else /* !0 */ |
now += 0x01b21dd213814000; |
#endif /* 0 */ |
now &= 0x0fffffffffffffff; |
now |= 0x1000000000000000; |
clock_sequence &= 0x3fff; |
clock_sequence |= 0x8000; |
return sprintf("%08x-%04x-%04x-%04x-%s", |
now & 0xffffffff, |
(now >> 32) & 0xffff, |
(now >> 48) & 0xffff, |
clock_sequence, |
hex_mac_address); |
} |
|
mapping(string:array(int)) error_log=([]); |
|
|
void nwrite(string s, int|void perr, int|void errtype, |
object|void mod, object|void conf) |
{ |
int log_time = time(1); |
string reference = (mod ? Roxen.get_modname(mod) : conf && conf->name) || ""; |
string log_index = sprintf("%d,%s,%s", errtype, reference, s); |
if(!error_log[log_index]) |
error_log[log_index] = ({ log_time }); |
else |
error_log[log_index] += ({ log_time }); |
|
if( mod ) |
{ |
if( mod->error_log ) |
mod->error_log[log_index] += ({ log_time }); |
} |
if( conf ) |
{ |
if( conf->error_log ) |
conf->error_log[log_index] += ({ log_time }); |
} |
|
if(errtype >= 1) |
report_debug( s ); |
} |
|
|
int boot_time =time(); |
int start_time =time(); |
|
string version() |
{ |
#ifndef NSERIOUS |
return query("default_ident")?real_version:query("ident"); |
#else |
multiset choices=(<>); |
string version=query("default_ident")?real_version:query("ident"); |
return version+", "+ ({ |
"Applier of Templates", |
"Beautifier of Layouts", |
"Conqueror of Comdex", |
"Deliverer of Documents", |
"Enhancer of Abilities", |
"Freer of Webmasters", |
"Generator of Logs", |
"Helper of Users", |
"Interpreter of Scripts", |
"Juggler of Java-code", |
"Keeper of Databases", |
"Locator of Keywords", |
"Manipulator of Data", |
"Negatiator of Protocols", |
"Operator of Sites", |
"Provider of Contents", |
"Quintessence of Quality", |
"Responder to Connections", |
"Server of Webs", |
"Translator of Texts", |
"Unifier of Interfaces", |
"Valet of Visitors", |
"Watcher for Requests", |
"Xylem of Services", |
"Yielder of Information", |
"Zenith of Extensibility" |
})[random(26)]; |
#endif |
} |
|
public void log(mapping file, RequestID request_id) |
{ |
if(!request_id->conf) return; |
request_id->conf->log(file, request_id); |
} |
|
#if ROXEN_COMPAT < 2.2 |
|
private Stdio.File current_user_id_file; |
private int current_user_id_number, current_user_id_file_last_mod; |
|
private void restore_current_user_id_number() |
{ |
if(!current_user_id_file) |
current_user_id_file = open(configuration_dir + "LASTUSER~", "rwc"); |
if(!current_user_id_file) |
{ |
call_out(restore_current_user_id_number, 2); |
return; |
} |
current_user_id_number = (int)current_user_id_file->read(100); |
current_user_id_file_last_mod = current_user_id_file->stat()[2]; |
report_debug("Restoring unique user ID information. (" + current_user_id_number |
+ ")\n"); |
#ifdef FD_DEBUG |
mark_fd(current_user_id_file->query_fd(), "Unique user ID logfile.\n"); |
#endif |
} |
|
int increase_id() |
{ |
if(!current_user_id_file) |
{ |
restore_current_user_id_number(); |
return current_user_id_number+time(1); |
} |
if(current_user_id_file->stat()[2] != current_user_id_file_last_mod) |
restore_current_user_id_number(); |
current_user_id_number++; |
current_user_id_file->seek(0); |
current_user_id_file->write((string)current_user_id_number); |
current_user_id_file_last_mod = current_user_id_file->stat()[2]; |
return current_user_id_number; |
} |
#endif // ROXEN_COMPAT < 2.2 |
|
private int unique_id_counter; |
string create_unique_id() |
{ |
object md5 = Crypto.md5(); |
md5->update(query("server_salt") + start_time + "|" + |
(unique_id_counter++) + "|" + time(1)); |
return Crypto.string_to_hex(md5->digest()); |
} |
|
#ifndef __NT__ |
static int abs_started; |
|
void restart_if_stuck (int force) |
|
|
{ |
remove_call_out(restart_if_stuck); |
if (!(query("abs_engage") || force)) |
return; |
if(!abs_started) |
{ |
abs_started = 1; |
report_debug("Anti-Block System Enabled.\n"); |
} |
call_out (restart_if_stuck,10); |
|
|
signal(signum("SIGALRM"), |
lambda( int n ) { |
if (!query("abs_engage")) { |
abs_started = 0; |
report_debug("Anti-Block System Disabled.\n"); |
return; |
} |
report_debug("**** %s: ABS engaged!\n" |
"Waited more than %d minute(s).\n" |
"Trying to dump backlog: \n", |
ctime(time()) - "\n", |
query("abs_timeout")); |
catch { |
|
describe_all_threads(); |
}; |
report_debug("**** %s: ABS exiting roxen!\n\n", |
ctime(time())); |
_exit(1); |
|
}); |
int t = alarm (60*query("abs_timeout")+20); |
|
} |
#endif |
|
#if constant(ROXEN_MYSQL_SUPPORTS_UNICODE) |
|
|
|
#define MYSQL__BINARY "_binary" |
#else |
#define MYSQL__BINARY "" |
#endif |
|
function(string:Sql.Sql) dbm_cached_get; |
|
class ImageCache |
|
|
|
|
{ |
#define QUERY(X,Y...) get_db()->query(X,Y) |
string name; |
string dir; |
function draw_function; |
mapping(string:array(mapping|int)) meta_cache = ([]); |
|
string documentation(void|string tag_n_args) |
{ |
string doc = Stdio.read_file("base_server/image_cache.xml"); |
if(!doc) return ""; |
if(!tag_n_args) |
return Parser.HTML()->add_container("ex", "")-> |
add_quote_tag("!--","","--")->finish(doc)->read(); |
return replace(doc, "###", tag_n_args); |
} |
|
static mapping meta_cache_insert( string i, mapping what ) |
{ |
#ifdef ARG_CACHE_DEBUG |
werror("MD insert for %O: %O\n", i, what ); |
#endif |
if( sizeof( meta_cache ) > 1000 ) |
sync_meta(); |
if( what ) { |
meta_cache[i] = ({ what, 0 }); |
return what; |
} |
else |
m_delete( meta_cache, i ); |
return 0; |
} |
|
static mixed frommapp( mapping what ) |
{ |
if( !what ) |
error( "Got invalid argcache-entry\n" ); |
if( !zero_type(what[""]) ) return what[""]; |
return what; |
} |
|
static void|mapping draw( string name, RequestID id ) |
{ |
#ifdef ARG_CACHE_DEBUG |
werror("draw %O\n", name ); |
#endif |
mixed args = Array.map( Array.map( name/"$", argcache->lookup, |
id->client ), frommapp); |
|
mapping meta; |
string data; |
array guides; |
mixed reply = draw_function( @copy_value(args), id ); |
|
if( !reply ) { |
#ifdef ARG_CACHE_DEBUG |
werror("%O(%{%O, %}%O) ==> 0\n", |
draw_function, args, id); |
#endif |
return; |
} |
|
if( arrayp( args ) ) |
args = args[0]; |
|
if( arrayp( reply ) ) |
{ |
guides = reply->get_misc_value( "image_guides" )-({}); |
if( sizeof( guides ) ) |
guides = guides[0]; |
reply = Image.lay( reply ); |
} |
if( objectp( reply ) && reply->image ) |
{ |
if( !guides ) |
guides = reply->get_misc_value( "image_guides" ); |
reply = ([ |
"img":reply->image(), |
"alpha":reply->alpha(), |
]); |
} |
|
|
if( objectp( reply ) || (mappingp(reply) && reply->img) ) |
{ |
int quant = (int)args->quant; |
string format = lower_case(args->format || "gif"); |
string dither = args->dither; |
Image.Colortable ct; |
Image.Color.Color bgcolor; |
Image.Image alpha; |
int true_alpha; |
|
if( args->fs || dither == "fs" ) |
dither = "floyd_steinberg"; |
|
if( dither == "random" ) |
dither = "random_dither"; |
|
if( format == "jpg" ) |
format = "jpeg"; |
|
if( dither ) |
dither = replace( dither, "-", "_" ); |
|
if(mappingp(reply)) |
{ |
alpha = reply->alpha; |
reply = reply->img; |
} |
|
if( args["true-alpha"] ) |
true_alpha = 1; |
|
if( args["background"] || args["background-color"]) |
bgcolor = Image.Color( args["background"]||args["background-color"] ); |
|
if( args["opaque-value"] ) |
{ |
if( !bgcolor ) |
true_alpha = 1; |
int ov = (int)(((float)args["opaque-value"])*2.55); |
if( ov < 0 ) |
ov = 0; |
else if( ov > 255 ) |
ov = 255; |
if( alpha ) |
alpha *= ov; |
else |
alpha = Image.Image( reply->xsize(), reply->ysize(), ov,ov,ov ); |
} |
|
if( args->gamma ) |
reply = reply->gamma( (float)args->gamma ); |
|
|
if( bgcolor && alpha && !true_alpha ) |
{ |
reply = Image.Image( reply->xsize(), |
reply->ysize(), bgcolor ) |
->paste_mask( reply, alpha ); |
alpha = alpha->threshold( 4 ); |
} |
|
int x0, y0, x1=reply->xsize(), y1=reply->ysize(), xc, yc; |
if( args["x-offset"] || args["xoffset"] ) |
x0 = (int)(args["x-offset"]||args["xoffset"]); |
if( args["y-offset"] || args["yoffset"] ) |
y0 = (int)(args["y-offset"]||args["yoffset"]); |
if( args["width"] || args["x-size"] ) |
x1 = (int)(args["x-size"]||args["width"]); |
if( args["height"] || args["y-size"] ) |
y1 = (int)(args["y-size"]||args["height"]); |
|
array xguides, yguides; |
function sort_guides = lambda() |
{ |
xguides = ({}); yguides = ({}); |
if( guides ) |
{ |
foreach( guides, object g ) |
if( g->pos > 0 ) |
if( g->vertical ) |
{ |
if( g->pos < reply->xsize() ) |
xguides += ({ g->pos }); |
} |
else |
if( g->pos < reply->ysize() ) |
yguides += ({ g->pos }); |
sort( xguides ); sort( yguides ); |
} |
}; |
|
if( args->crop ) |
{ |
int gx=1, gy=1, gx2, gy2; |
if( sscanf( args["guides-index"]||"", "%d,%d", gx, gy ) == 2 ) |
{ |
gx2 = gx+1; |
gy2 = gy+1; |
sscanf( args["guides-index"]||"", "%d,%d-%d,%d", gx, gy, gx2, gy2 ); |
} |
|
|
switch( args->crop ) |
{ |
case "guides-cross": |
sort_guides(); |
if( sizeof(xguides) && sizeof(yguides) ) |
{ |
xc = xguides[ min(sizeof(xguides),gx) - 1 ]; |
yc = yguides[ min(sizeof(yguides),gy) - 1 ]; |
break; |
} |
guides=0; |
case "guides-region": |
sort_guides(); |
if( (sizeof(xguides)>1) && (sizeof(yguides)>1) ) |
{ |
gx = min(sizeof(xguides)-1,gx) - 1; |
gy = min(sizeof(yguides)-1,gy) - 1; |
gx2 = min(sizeof(xguides),gx2) - 1; |
gy2 = min(sizeof(yguides),gy2) - 1; |
|
x0 = xguides[gx]; x1 = xguides[gx2] - x0; |
y0 = yguides[gy]; y1 = yguides[gy2] - y0; |
break; |
} |
default: |
if( sscanf( args->crop, "%d,%d-%d,%d", x0, y0, x1, y1 ) == 4) |
{ |
x1 -= x0; |
y1 -= y0; |
} |
break; |
case "auto": |
[ x0, y0, x1, y1 ] = reply->find_autocrop(); |
x1 = x1 - x0 + 1; |
y1 = y1 - y0 + 1; |
} |
} |
sort_guides = 0; |
|
#define SCALEI 1 |
#define SCALEF 2 |
#define SCALEA 4 |
#define CROP 8 |
|
function do_scale_and_crop = lambda ( int x0, int y0, |
int x1, int y1, |
int|float w, int|float h, |
int type ) |
{ |
if( (type & CROP) && x1 && y1 |
&& ((x1 != reply->xsize()) || (y1 != reply->ysize()) |
|| x0 || y0 ) ) |
{ |
reply = reply->copy( x0, y0, x0+x1-1, y0+y1-1, |
(bgcolor?bgcolor->rgb():0) ); |
if( alpha ) |
alpha = alpha->copy( x0, y0, x0+x1-1,y0+y1-1, Image.Color.black); |
} |
|
if( type & SCALEI ) |
{ |
if( xc || yc ) |
{ |
if( h && !w ) |
w = (reply->xsize() * h) / reply->ysize(); |
|
if( w && !h ) |
h = (reply->ysize() * w) / reply->xsize(); |
|
x0 = max( xc - w/2, 0 ); |
y0 = max( yc - h/2, 0 ); |
|
x1 = w; y1 = h; |
if( x0 + x1 > reply->xsize() ) |
{ |
x0 = reply->xsize()-w; |
if( x0 < 0 ) |
{ |
x0 = 0; |
x1 = reply->xsize(); |
} |
} |
if( y0 + y1 > reply->ysize() ) |
{ |
y0 = reply->ysize()-h; |
if( y0 < 0 ) |
{ |
y0 = 0; |
y1 = reply->ysize(); |
} |
} |
reply = reply->copy( x0, y0, x0+x1-1, y0+y1-1, |
(bgcolor?bgcolor->rgb():0) ); |
|
if( alpha ) |
alpha = alpha->copy( x0, y0, x0+x1-1,y0+y1-1, Image.Color.black); |
} |
} |
|
|
if( (type & SCALEF) && (w != 1.0) ) |
{ |
reply = reply->scale( w ); |
if( alpha ) |
alpha = alpha->scale( w ); |
} |
else if( (type & SCALEA) && |
((reply->xsize() != w) || (reply->ysize() != h)) ) |
{ |
reply = reply->scale( w,h ); |
if( alpha ) |
alpha = alpha->scale( w,h ); |
} |
else if( (type & SCALEI) && |
((reply->xsize() != w) || (reply->ysize() != h)) ) |
{ |
if( w && h ) |
{ |
if( (w * (float)reply->ysize()) < (h * (float)reply->xsize()) ) |
h = 0; |
else |
w = 0; |
} |
w = min( w, reply->xsize() ); |
h = min( h, reply->ysize() ); |
reply = reply->scale( w,h ); |
if( alpha ) |
alpha = alpha->scale( w,h ); |
} |
}; |
|
if( sizeof((string) (args->scale || "")) ) |
{ |
int x, y; |
if( sscanf( args->scale, "%d,%d", x, y ) == 2) |
do_scale_and_crop( x0, y0, x1, y1, x, y, SCALEA|CROP ); |
else if( (float)args->scale < 3.0) |
do_scale_and_crop( x0, y0, x1, y1, |
((float)args->scale), ((float)args->scale), |
SCALEF|CROP ); |
} |
else |
if( sizeof( (string) (args->maxwidth || args->maxheight || |
args["max-width"] || args["max-height"] || "")) ) |
{ |
int x = (int)args->maxwidth|| (int)args["max-width"]; |
int y = (int)args->maxheight||(int)args["max-height"]; |
do_scale_and_crop( x0, y0, x1, y1, x, y, SCALEI|CROP ); |
} |
else |
do_scale_and_crop( x0, y0, x1, y1, 0, 0, CROP ); |
do_scale_and_crop = 0; |
|
if( args["span-width"] || args["span-height"] ) |
{ |
int width = (int)args["span-width"]; |
int height = (int)args["span-height"]; |
|
if( (width && reply->xsize() > width) || |
(height && reply->ysize() > height) ) |
{ |
if( (width && height && (reply->xsize() / (float)width > |
reply->ysize() / (float)height)) || |
!height ) |
{ |
reply = reply->scale( width, 0 ); |
if( alpha ) |
alpha = alpha->scale( width, 0 ); |
} |
else if( height ) |
{ |
reply = reply->scale( 0, height ); |
if( alpha ) |
alpha = alpha->scale( 0, height ); |
} |
} |
|
int x1,x2,y1,y2; |
if( width ) |
{ |
x1 = -((width - reply->xsize()) / 2); |
x2 = x1 + width - 1; |
} |
if( height ) |
{ |
y1 = -((height - reply->ysize()) / 2); |
y2 = y1 + height - 1; |
} |
|
if( width && height ) |
{ |
reply = reply->copy(x1,y1,x2,y2,(bgcolor?bgcolor->rgb():0)); |
if( alpha ) alpha = alpha->copy(x1,y1,x2,y2); |
} |
else if( width ) |
{ |
reply = reply->copy(x1,0,x2,reply->ysize(),(bgcolor?bgcolor->rgb():0)); |
if ( alpha ) alpha = alpha->copy(x1,0,x2,alpha->ysize()); |
} |
else |
{ |
reply = reply->copy(0,y1,reply->xsize(),y2,(bgcolor?bgcolor->rgb():0)); |
if( alpha ) alpha = alpha->copy(0,y1,alpha->xsize(),y2); |
} |
} |
|
if( args["rotate-cw"] || args["rotate-ccw"]) |
{ |
float degree = (float)(args["rotate-cw"] || args["rotate-ccw"]); |
switch( args["rotate-unit"] && args["rotate-unit"][0..0] ) |
{ |
case "r": degree = (degree / (2*3.1415)) * 360; break; |
case "d": break; |
case "n": degree = (degree / 400) * 360; break; |
case "p": degree = (degree / 1.0) * 360; break; |
} |
if( args["rotate-cw"] ) |
degree = -degree; |
if(!alpha) |
alpha = reply->copy()->clear(255,255,255); |
reply = reply->rotate_expand( degree ); |
alpha = alpha->rotate( degree, 0,0,0 ); |
} |
|
|
if( args["mirror-x"] ) |
{ |
if( alpha ) |
alpha = alpha->mirrorx(); |
reply = reply->mirrorx(); |
} |
|
if( args["mirror-y"] ) |
{ |
if( alpha ) |
alpha = alpha->mirrory(); |
reply = reply->mirrory(); |
} |
|
if( bgcolor && alpha && !true_alpha ) |
{ |
reply = Image.Image( reply->xsize(), |
reply->ysize(), bgcolor ) |
->paste_mask( reply, alpha ); |
} |
|
if( args["cs-rgb-hsv"] )reply = reply->rgb_to_hsv(); |
if( args["cs-grey"] ) reply = reply->grey(); |
if( args["cs-invert"] ) reply = reply->invert(); |
if( args["cs-hsv-rgb"] )reply = reply->hsv_to_rgb(); |
|
if( !true_alpha && alpha ) |
alpha = alpha->threshold( 4 ); |
|
if( quant || (format=="gif") ) |
{ |
int ncols = quant; |
if( format=="gif" ) { |
ncols = ncols||id->misc->defquant||32; |
if( ncols > 254 ) |
ncols = 254; |
} |
ct = Image.Colortable( reply, ncols ); |
if( dither ) |
{ |
if( dither == "random" ) |
dither = "random_grey"; |
if( ct[ dither ] ) |
ct[ dither ](); |
else |
ct->ordered(); |
|
} |
} |
|
mapping enc_args = ([]); |
if( ct ) |
enc_args->colortable = ct; |
|
if( alpha ) |
enc_args->alpha = alpha; |
|
foreach( glob( "*-*", indices(args)), string n ) |
if(sscanf(n, "%*[^-]-%s", string opt ) == 2) |
if( opt != "alpha" ) |
enc_args[opt] = (int)args[n]; |
|
switch(format) |
{ |
case "wbf": |
format = "wbmp"; |
case "wbmp": |
Image.Colortable bw=Image.Colortable( ({ ({ 0,0,0 }), |
({ 255,255,255 }) }) ); |
bw->floyd_steinberg(); |
data = Image.WBF.encode( bw->map( reply ), enc_args ); |
break; |
case "gif": |
#if constant(Image.GIF) && constant(Image.GIF.encode) |
if( alpha && true_alpha ) |
{ |
Image.Colortable bw=Image.Colortable( ({ ({ 0,0,0 }), |
({ 255,255,255 }) }) ); |
bw->floyd_steinberg(); |
alpha = bw->map( alpha ); |
} |
if( catch { |
if( alpha ) |
data = Image.GIF.encode_trans( reply, ct, alpha ); |
else |
data = Image.GIF.encode( reply, ct ); |
}) |
data = Image.GIF.encode( reply ); |
break; |
|
#else |
|
|
format = "png"; |
#endif |
|
case "png": |
if( ct ) enc_args->palette = ct; |
m_delete( enc_args, "colortable" ); |
if( !(args["png-use-alpha"] || args["true-alpha"]) ) |
m_delete( enc_args, "alpha" ); |
else if( enc_args->alpha ) |
|
|
|
m_delete( enc_args, "palette"); |
else |
m_delete( enc_args, "alpha" ); |
|
default: |
if(!Image[upper_case( format )] |
|| !Image[upper_case( format )]->encode ) |
error("Image format "+format+" not supported\n"); |
data = Image[upper_case( format )]->encode( reply, enc_args ); |
} |
|
meta = |
([ |
"xsize":reply->xsize(), |
"ysize":reply->ysize(), |
"type":(format == "wbmp" ? "image/vnd.wap.wbmp" : "image/"+format ), |
]); |
} |
else if( mappingp(reply) ) |
{ |
|
if(reply->error) |
return reply; |
meta = reply->meta; |
data = reply->data; |
if( !meta || !data ) |
error("Invalid reply mapping.\n" |
"Expected ([ \"meta\": ([metadata]), \"data\":\"data\" ])\n" |
"Got %O\n", reply); |
} |
#ifdef ARG_CACHE_DEBUG |
werror("draw %O done\n", name ); |
#endif |
|
mixed err = catch(store_data( name, data, meta )); |
#ifdef ARG_CACHE_DEBUG |
if (err) { |
werror("store_data failed with:\n" |
"%s\n", describe_backtrace(err)); |
} |
#endif |
} |
|
static void store_data( string id, string data, mapping meta ) |
{ |
if(!stringp(data)) return; |
#ifdef ARG_CACHE_DEBUG |
werror("store %O (%d bytes)\n", id, strlen(data) ); |
#endif |
meta_cache_insert( id, meta ); |
string meta_data = encode_value( meta ); |
#ifdef ARG_CACHE_DEBUG |
werror("Replacing entry for %O\n", id ); |
#endif |
QUERY("REPLACE INTO "+name+ |
" (id,size,atime,meta,data) VALUES" |
" (%s,%d,UNIX_TIMESTAMP()," MYSQL__BINARY "%s," MYSQL__BINARY "%s)", |
id, strlen(data)+strlen(meta_data), meta_data, data ); |
#ifdef ARG_CACHE_DEBUG |
array(mapping(string:string)) q = |
QUERY("SELECT meta, data FROM " + name + |
" WHERE id = %s", id); |
if (!q || sizeof(q) != 1) { |
werror("Unexpected result size: %d\n", |
q && sizeof(q)); |
} else { |
if (q[0]->meta != meta_data) { |
werror("Meta data differs: %O != %O\n", |
meta_data, q[0]->meta); |
} |
if (q[0]->data != data) { |
werror("Data differs: %O != %O\n", |
data, q[0]->data); |
} |
} |
#endif |
} |
|
static mapping restore_meta( string id, RequestID rid ) |
{ |
if( meta_cache[ id ] ) |
{ |
meta_cache[ id ][ 1 ] = time(1); |
return meta_cache[ id ][ 0 ]; |
} |
|
#ifdef ARG_CACHE_DEBUG |
werror("restore meta %O\n", id ); |
#endif |
array(mapping(string:string)) q = |
QUERY("SELECT meta FROM "+name+" WHERE id=%s", id ); |
|
string s; |
if(!sizeof(q) || !strlen(s = q[0]->meta)) |
return 0; |
|
mapping m; |
if (catch (m = decode_value (s))) |
{ |
report_error( "Corrupt data in cache-entry "+id+".\n" ); |
QUERY( "DELETE FROM "+name+" WHERE id=%s", id); |
return 0; |
} |
|
QUERY("UPDATE "+name+" SET atime=UNIX_TIMESTAMP() WHERE id=%s",id ); |
return meta_cache_insert( id, m ); |
} |
|
static void sync_meta() |
{ |
|
foreach(meta_cache; string id; array value) { |
if (value[1]) |
QUERY("UPDATE "+name+" SET atime=%d WHERE id=%s", |
value[1], id); |
} |
meta_cache = ([]); |
} |
|
void flush(int|void age) |
|
|
|
{ |
int num; |
#ifdef DEBUG |
int t = gethrtime(); |
report_debug("Cleaning "+name+" image cache ... "); |
#endif |
sync_meta(); |
uid_cache = ([]); |
rst_cache = ([]); |
if( !age ) |
{ |
#ifdef DEBUG |
report_debug("cleared\n"); |
#endif |
QUERY( "DELETE FROM "+name ); |
num = -1; |
return; |
} |
|
array(string) ids = |
QUERY( "SELECT id FROM "+name+" WHERE atime < "+age)->id; |
|
num = sizeof( ids ); |
|
int q; |
while(q<sizeof(ids)) { |
string list = map(ids[q..q+99], get_db()->quote) * "','"; |
q+=100; |
QUERY( "DELETE FROM "+name+" WHERE id in ('"+list+"')" ); |
} |
|
if( num ) |
catch |
{ |
|
|
|
QUERY( "OPTIMIZE TABLE "+name ); |
}; |
|
#ifdef DEBUG |
report_debug("%s removed, %dms\n", |
(num==-1?"all":num?(string)num:"none"), |
(gethrtime()-t)/1000); |
#endif |
} |
|
array(int) status(int|void age) |
|
|
|
|
|
{ |
int imgs=0, size=0, aged=0; |
array(mapping(string:string)) q; |
|
q=QUERY("SHOW TABLE STATUS"); |
foreach(q, mapping qq) |
if(has_prefix(qq->Name, name)) { |
imgs = (int)qq->Rows; |
size += (int)qq->Data_length; |
} |
|
if(age) { |
q=QUERY("select SUM(1) as num from "+name+" where atime < "+age); |
aged = (int)q[0]->num; |
} |
return ({ imgs, size, aged }); |
} |
|
static mapping(string:mapping) rst_cache = ([ ]); |
static mapping(string:string) uid_cache = ([ ]); |
|
static mapping restore( string id, RequestID rid ) |
{ |
array q; |
string uid; |
if( zero_type(uid = uid_cache[id]) ) |
{ |
q = QUERY( "SELECT uid FROM "+name+" WHERE id=%s",id); |
if( sizeof(q) ) |
uid = q[0]->uid; |
else |
uid = 0; |
uid_cache[id] = uid; |
} |
|
if( uid && strlen(uid) ) |
{ |
User u; |
if( !(u=rid->conf->authenticate(rid)) || (u->name() != uid ) ) |
return rid->conf->authenticate_throw(rid, "User"); |
} |
|
if( rst_cache[ id ] ) |
return rst_cache[ id ] + ([]); |
|
#ifdef ARG_CACHE_DEBUG |
werror("restore %O\n", id ); |
#endif |
q = QUERY( "SELECT meta,atime,data FROM "+name+" WHERE id=%s",id); |
if( sizeof(q) ) |
{ |
if( sizeof(q[0]->data) ) |
{ |
|
string f = q[0]->data; |
mapping m; |
catch( m = decode_value( q[0]->meta ) ); |
if( !m ) return 0; |
|
m = Roxen.http_string_answer( f, m->type||("image/gif") ); |
|
if( strlen( f ) > 6000 ) |
return m; |
rst_cache[ id ] = m; |
if( sizeof( rst_cache ) > 100 ) |
rst_cache = ([ id : m ]); |
return rst_cache[ id ] + |