1720541998-10-04Henrik Grubbström (Grubba) /*
b870ac1999-09-03Henrik Grubbström (Grubba)  * $Id: roxen.pike,v 1.321 1999/09/03 11:21:20 grubba Exp $
1720541998-10-04Henrik Grubbström (Grubba)  * * The Roxen Challenger main program. * * Per Hedbor, Henrik Grubbström, Pontus Hagland, David Hedbor and others. */
edc9af1998-07-11David Hedbor  // ABS and suicide systems contributed freely by Francesco Chemolli
b870ac1999-09-03Henrik Grubbström (Grubba) constant cvs_version="$Id: roxen.pike,v 1.321 1999/09/03 11:21:20 grubba Exp $";
34d3fa1999-04-22Per Hedbor  object backend_thread;
a22f6f1999-05-12Per Hedbor object argcache;
1720541998-10-04Henrik Grubbström (Grubba)  // Some headerfiles
4dd97c1996-12-04Per Hedbor #define IN_ROXEN
8540e11997-06-14Francesco Chemolli #include <roxen.h> #include <config.h>
b1fca01996-11-12Per Hedbor #include <module.h> #include <variables.h>
753a831999-08-30Per Hedbor #include <stat.h>
14179b1997-01-29Per Hedbor 
1720541998-10-04Henrik Grubbström (Grubba) // Inherits
753a831999-08-30Per Hedbor inherit "global_variables";
b1fca01996-11-12Per Hedbor inherit "hosts"; inherit "disk_cache"; inherit "language";
2ff8461999-09-02Per Hedbor inherit "supports";
7c92b91998-11-19Per Hedbor 
0f8b2f1999-03-27Henrik Grubbström (Grubba) /* * Version information */ constant __roxen_version__ = "1.4";
f7d22d1999-08-27Henrik Grubbström (Grubba) constant __roxen_build__ = "38";
0f8b2f1999-03-27Henrik Grubbström (Grubba)  #ifdef __NT__
753a831999-08-30Per Hedbor string real_version= "Roxen Challenger/"+__roxen_version__+"."+__roxen_build__+" NT";
0f8b2f1999-03-27Henrik Grubbström (Grubba) #else
753a831999-08-30Per Hedbor string real_version= "Roxen Challenger/"+__roxen_version__+"."+__roxen_build__;
0f8b2f1999-03-27Henrik Grubbström (Grubba) #endif
7c92b91998-11-19Per Hedbor // Prototypes for other parts of roxen. class RequestID { object conf; // Really Configuration, but that's sort of recursive. int time; string raw_url; int do_not_disconnect; mapping (string:string) variables; mapping (string:mixed) misc; mapping (string:string) cookies; mapping (string:string) request_headers; multiset(string) prestate; multiset(string) config; multiset(string) supports; multiset(string) pragma; array(string) client; array(string) referer; Stdio.File my_fd; string prot; string clientprot; string method; string realfile; string virtfile; string rest_query; string raw; string query; string not_query; string extra_extension; string data; string leftovers; array (int|string) auth; string rawauth; string realauth; string since;
e682c11998-11-22Per Hedbor  string remoteaddr; string host; void create(object|void master_request_id);
7c92b91998-11-19Per Hedbor  void send(string|object what, int|void len); string scan_for_query( string in ); void end(string|void s, int|void keepit); void ready_to_receive(); void send_result(mapping|void result); RequestID clone_me(); };
34d3fa1999-04-22Per Hedbor constant pipe = (program)"smartpipe";
0f8b2f1999-03-27Henrik Grubbström (Grubba) /* * The privilege changer. * * Based on privs.pike,v 1.36. */ // Some variables used by Privs #ifdef THREADS // This mutex is used by Privs object 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 && GLOBVAR(audit)) #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
b84a161999-06-07Martin Stjernholm  static mixed mutex_key; // Only one thread may modify the euid/egid at a time.
d3a71c1999-04-20Martin Stjernholm  static object threads_disabled;
0f8b2f1999-03-27Henrik Grubbström (Grubba) #endif /* THREADS */ int p_level; void create(string reason, int|string|void uid, int|string|void gid) { #ifdef PRIVS_DEBUG werror(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(); }; }
d3a71c1999-04-20Martin Stjernholm  threads_disabled = _disable_threads();
0f8b2f1999-03-27Henrik Grubbström (Grubba) #endif /* THREADS */ p_level = privs_level++; if(getuid()) return; /* Needs to be here since root-priviliges may be needed to * use getpw{uid,nam}. */ saved_uid = geteuid(); saved_gid = getegid(); seteuid(0); /* A string of digits? */ if (stringp(uid) && ((int)uid) && (replace(uid, ({ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }), ({ "", "", "", "", "", "", "", "", "", "" })) == "")) { uid = (int)uid; } if (stringp(gid) && ((int)gid) && (replace(gid, ({ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }), ({ "", "", "", "", "", "", "", "", "", "" })) == "")) { 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)) {
b84a161999-06-07Martin Stjernholm  report_warning(sprintf("Privs: User %d is not in the password database.\n" "Assuming nobody.\n", uid));
0f8b2f1999-03-27Henrik Grubbström (Grubba)  // Nobody. gid = gid || uid; // Fake a gid also. 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(sprintf("Change to %s(%d):%d privs wanted (%s), from %s", (string)u[0], (int)uid, (int)gid, (string)reason, (string)dbt(backtrace()[-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_debug(sprintf("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) { /* HPUX has doesn't like groups higher than 60000, * but has assigned nobody to group 60001 (which isn't even * in /etc/group!). * * HPUX's libc also insists on filling numeric fields it doesn't like * with the value 60001! */ perror("Privs: WARNING: Assuming nobody-group.\n" "Trying some alternatives...\n"); // Assume we want the nobody group, and try a couple of alternatives foreach(({ 60001, 65534, -2 }), gid2) { perror("%d... ", gid2); if (initgroups(u[0], gid2) >= 0) { if ((err = setegid(new_gid = gid2)) >= 0) { perror("Success!\n"); break; } } } } #endif /* HPUX_KLUDGE */ if (err < 0) { perror("Privs: Failed\n"); throw(({ sprintf("Failed to set EGID to %d\n", gid), backtrace() })); } perror("Privs: WARNING: Set egid to %d instead of %d.\n", gid2, gid); gid = gid2; } if(getgid()!=gid) setgid(gid||getgid()); seteuid(new_uid = uid); #endif /* HAVE_EFFECTIVE_USER */ } void destroy() { #ifdef PRIVS_DEBUG werror(sprintf("Privs->destroy()\n" "privs_level: %O\n", privs_level)); #endif /* PRIVS_DEBUG */ #ifdef HAVE_EFFECTIVE_USER /* Check that we don't increase the privs level */ 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) {
d3a71c1999-04-20Martin Stjernholm  report_notice(sprintf("Change back to uid#%d gid#%d, from %s\n", saved_uid, saved_gid, dbt(bt[-2])));
0f8b2f1999-03-27Henrik Grubbström (Grubba)  } else {
d3a71c1999-04-20Martin Stjernholm  report_notice(sprintf("Change back to uid#%d gid#%d, from backend\n", saved_uid, saved_gid));
0f8b2f1999-03-27Henrik Grubbström (Grubba)  } }; } if(getuid()) return; #ifdef DEBUG int uid = geteuid(); if (uid != new_uid) { report_warning(sprintf("Privs: UID #%d differs from expected #%d\n" "%s\n", uid, new_uid, describe_backtrace(backtrace()))); } int gid = getegid(); if (gid != new_gid) { report_warning(sprintf("Privs: GID #%d differs from expected #%d\n" "%s\n", gid, new_gid, describe_backtrace(backtrace()))); } #endif /* 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); #endif /* HAVE_EFFECTIVE_USER */ } #endif /* efun(seteuid) */ }; /* Used by read_config.pike, since there seems to be problems with * overloading otherwise. */ static object PRIVS(string r, int|string|void u, int|string|void g) { return Privs(r, u, g); }
3e646f1997-05-15David Hedbor #if _DEBUG_HTTP_OBJECTS mapping httpobjects = ([]); static int idcount; int new_id(){ return idcount++; } #endif
0f8c871997-06-01Henrik Grubbström (Grubba) #ifdef MODULE_DEBUG
753a831999-08-30Per Hedbor #define MD_PERROR(X) werror X;
0f8c871997-06-01Henrik Grubbström (Grubba) #else #define MD_PERROR(X) #endif /* MODULE_DEBUG */
1720541998-10-04Henrik Grubbström (Grubba) // pids of the start-script and ourselves.
b642a21998-09-05Henrik Grubbström (Grubba) int startpid, roxenpid;
b1fca01996-11-12Per Hedbor 
2ff8461999-09-02Per Hedbor #ifndef THREADS
b796b51998-11-18Per Hedbor class container { mixed value; mixed set(mixed to) { return value=to; } mixed get() { return value; } }
2ff8461999-09-02Per Hedbor #endif
b796b51998-11-18Per Hedbor 
c31f7b1998-10-10Henrik Grubbström (Grubba) // Locale support
86a7891999-05-01Henrik Grubbström (Grubba) object(Locale.Roxen.standard) default_locale=Locale.Roxen.standard /* nihongo */;
fb7ef91998-11-02Per Hedbor object fonts;
59912f1998-10-15Henrik Grubbström (Grubba) #ifdef THREADS object locale = thread_local(); #else
df022f1998-11-22Per Hedbor object locale = container();
59912f1998-10-15Henrik Grubbström (Grubba) #endif /* THREADS */ #define LOCALE LOW_LOCALE->base_server
c31f7b1998-10-10Henrik Grubbström (Grubba) 
dca5dc1998-09-12Per Hedbor program Configuration; /*set in create*/
b1fca01996-11-12Per Hedbor 
48fa361997-04-05Per Hedbor array configurations = ({});
b1fca01996-11-12Per Hedbor object main_configuration_port;
e493e81997-07-11Per Hedbor mapping allmodules, somemodules=([]);
b1fca01996-11-12Per Hedbor  // A mapping from ports (objects, that is) to an array of information
e5bad21998-02-10Per Hedbor // about that port. This will hopefully be moved to objects cloned // from the configuration object in the future.
b1fca01996-11-12Per Hedbor mapping portno=([]);
beaca01998-02-20Per Hedbor // Function pointer and the root of the configuration interface
b1fca01996-11-12Per Hedbor // object.
d4a3a51999-06-21Martin Stjernholm function build_root; object root;
b1fca01996-11-12Per Hedbor 
4f4bc11998-02-04Per Hedbor int die_die_die;
506ede1997-11-11Henrik Grubbström (Grubba) 
a9d8111998-09-01Henrik Grubbström (Grubba) void stop_all_modules()
b1fca01996-11-12Per Hedbor {
a9d8111998-09-01Henrik Grubbström (Grubba)  foreach(configurations, object conf) conf->stop(); }
1720541998-10-04Henrik Grubbström (Grubba) // Function that actually shuts down Roxen. (see low_shutdown).
a9d8111998-09-01Henrik Grubbström (Grubba) private static void really_low_shutdown(int exit_code) { // Die nicely.
3107101998-09-01Marcus Comstedt #ifdef THREADS
c974c91999-06-27Per Hedbor  catch( stop_handler_threads() );
3107101998-09-01Marcus Comstedt #endif /* THREADS */
a9d8111998-09-01Henrik Grubbström (Grubba)  exit(exit_code); // Now we die... } // Shutdown Roxen // exit_code = 0 True shutdown // exit_code = -1 Restart private static void low_shutdown(int exit_code) {
c974c91999-06-27Per Hedbor  catch( stop_all_modules() );
a9d8111998-09-01Henrik Grubbström (Grubba) 
c974c91999-06-27Per Hedbor  int pid; if (exit_code) { roxen_perror("Restarting Roxen.\n"); } else { roxen_perror("Shutting down Roxen.\n");
297dd91999-08-12Henrik Grubbström (Grubba)  // exit(0);
a9d8111998-09-01Henrik Grubbström (Grubba)  }
c974c91999-06-27Per Hedbor  call_out(really_low_shutdown, 0.1, exit_code);
b1fca01996-11-12Per Hedbor }
a9d8111998-09-01Henrik Grubbström (Grubba) // Perhaps somewhat misnamed, really... This function will close all
c974c91999-06-27Per Hedbor // listen ports and then quit. The 'start' script should then start a // new copy of roxen automatically.
a9d8111998-09-01Henrik Grubbström (Grubba) mapping restart() { low_shutdown(-1); return ([ "data": replace(Stdio.read_bytes("etc/restart.html"),
6ca8f61998-10-13Per Hedbor  ({"$docurl", "$PWD"}), ({docurl, getcwd()})),
a9d8111998-09-01Henrik Grubbström (Grubba)  "type":"text/html" ]); } mapping shutdown() { low_shutdown(0); return ([ "data":replace(Stdio.read_bytes("etc/shutdown.html"),
6ca8f61998-10-13Per Hedbor  ({"$docurl", "$PWD"}), ({docurl, getcwd()})),
a9d8111998-09-01Henrik Grubbström (Grubba)  "type":"text/html" ]); }
b1fca01996-11-12Per Hedbor // This is called for each incoming connection. private static void accept_callback( object port ) { object file;
06583f1997-09-03Per Hedbor  int q=QUERY(NumAccept);
14179b1997-01-29Per Hedbor  array pn=portno[port];
b1fca01996-11-12Per Hedbor 
14179b1997-01-29Per Hedbor #ifdef DEBUG if(!pn)
b1fca01996-11-12Per Hedbor  { destruct(port->accept()); perror("$&$$& Garbage Collector bug!!\n"); return; }
14179b1997-01-29Per Hedbor #endif
b1fca01996-11-12Per Hedbor  while(q--) { catch { file = port->accept(); };
14179b1997-01-29Per Hedbor #ifdef SOCKET_DEBUG if(!pn[-1])
b1fca01996-11-12Per Hedbor  { report_error("In accept: Illegal protocol handler for port.\n"); if(file) destruct(file); return; } perror(sprintf("SOCKETS: accept_callback(CONF(%s))\n",
14179b1997-01-29Per Hedbor  pn[1]&&pn[1]->name||"Configuration"));
b1fca01996-11-12Per Hedbor #endif if(!file) { switch(port->errno()) {
c3a53d1999-06-25Per Hedbor  case 0:
4859501999-05-08Henrik Grubbström (Grubba) #if constant(system.EAGAIN) case system.EAGAIN: #endif /* constant(system.EAGAIN) */
b1fca01996-11-12Per Hedbor  return;
4859501999-05-08Henrik Grubbström (Grubba) #if constant(system.EMFILE) case system.EMFILE: #endif /* constant(system.EMFILE) */ #if constant(system.EBADF) case system.EBADF: #endif /* constant(system.EBADF) */ report_fatal(LOCALE->out_of_sockets()); low_shutdown(-1); return; default:
b1fca01996-11-12Per Hedbor #ifdef DEBUG perror("Accept failed.\n");
4f4bc11998-02-04Per Hedbor #if constant(real_perror)
b1fca01996-11-12Per Hedbor  real_perror();
4f4bc11998-02-04Per Hedbor #endif
95e2b41997-05-25Wilhelm Köhler #endif /* DEBUG */
b1fca01996-11-12Per Hedbor  return; } }
3235691998-03-26Per Hedbor #ifdef FD_DEBUG
c31f7b1998-10-10Henrik Grubbström (Grubba)  mark_fd( file->query_fd(), LOCALE->out_of_sockets(file->query_address()));
14179b1997-01-29Per Hedbor #endif
ff31841999-06-08Martin Stjernholm  pn[-1](file,pn[1],pn);
b1fca01996-11-12Per Hedbor #ifdef SOCKET_DEBUG perror(sprintf("SOCKETS: Ok. Connect on %O:%O from %O\n",
14179b1997-01-29Per Hedbor  pn[2], pn[0], file->query_address())); #endif } }
4820e01999-07-27Henrik Grubbström (Grubba) /* * handle() stuff */
2ff8461999-09-02Per Hedbor #ifndef THREADS
1720541998-10-04Henrik Grubbström (Grubba) // handle function used when THREADS is not enabled.
3aaaa71997-06-12Wilhelm Köhler void unthreaded_handle(function f, mixed ... args) { f(@args); }
34fbbc1998-02-05Henrik Grubbström (Grubba) function handle = unthreaded_handle;
2ff8461999-09-02Per Hedbor #else
7277a11999-09-02Per Hedbor function handle = threaded_handle;
2ff8461999-09-02Per Hedbor #endif
34fbbc1998-02-05Henrik Grubbström (Grubba) 
1720541998-10-04Henrik Grubbström (Grubba) /* * THREADS code starts here */
34fbbc1998-02-05Henrik Grubbström (Grubba) #ifdef THREADS
6000961999-04-24Henrik Grubbström (Grubba) // #define THREAD_DEBUG
34fbbc1998-02-05Henrik Grubbström (Grubba) 
4f4bc11998-02-04Per Hedbor object do_thread_create(string id, function f, mixed ... args) { object t = thread_create(f, @args); catch(t->set_name( id ));
dca5dc1998-09-12Per Hedbor  roxen_perror(id+" started\n");
4f4bc11998-02-04Per Hedbor  return t; }
1720541998-10-04Henrik Grubbström (Grubba) // Queue of things to handle. // An entry consists of an array(function fp, array args)
3107101998-09-01Marcus Comstedt static object (Thread.Queue) handle_queue = Thread.Queue();
1720541998-10-04Henrik Grubbström (Grubba)  // Number of handler threads that are alive.
3107101998-09-01Marcus Comstedt static int thread_reap_cnt;
14179b1997-01-29Per Hedbor  void handler_thread(int id) {
4f4bc11998-02-04Per Hedbor  array (mixed) h, q;
34d3fa1999-04-22Per Hedbor  while(!die_die_die)
4f4bc11998-02-04Per Hedbor  { if(q=catch {
45cae31998-03-06Henrik Grubbström (Grubba)  do {
6000961999-04-24Henrik Grubbström (Grubba) #ifdef THREAD_DEBUG
75cdcb1999-03-29Henrik Grubbström (Grubba)  werror("Handle thread ["+id+"] waiting for next event\n");
6000961999-04-24Henrik Grubbström (Grubba) #endif /* THREAD_DEBUG */
3107101998-09-01Marcus Comstedt  if((h=handle_queue->read()) && h[0]) {
6000961999-04-24Henrik Grubbström (Grubba) #ifdef THREAD_DEBUG
75cdcb1999-03-29Henrik Grubbström (Grubba)  werror(sprintf("Handle thread [%O] calling %O(@%O)...\n",
6000961999-04-24Henrik Grubbström (Grubba)  id, h[0], h[1..])); #endif /* THREAD_DEBUG */
b796b51998-11-18Per Hedbor  SET_LOCALE(default_locale);
45cae31998-03-06Henrik Grubbström (Grubba)  h[0](@h[1]); h=0;
3107101998-09-01Marcus Comstedt  } else if(!h) { // Roxen is shutting down. werror("Handle thread ["+id+"] stopped\n"); thread_reap_cnt--; return;
45cae31998-03-06Henrik Grubbström (Grubba)  } } while(1); }) {
6000961999-04-24Henrik Grubbström (Grubba)  report_error(/* LOCALE->uncaught_error(*/describe_backtrace(q)/*)*/);
45cae31998-03-06Henrik Grubbström (Grubba)  if (q = catch {h = 0;}) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_error(LOCALE-> uncaught_error(describe_backtrace(q)));
45cae31998-03-06Henrik Grubbström (Grubba)  } }
4f4bc11998-02-04Per Hedbor  }
b1fca01996-11-12Per Hedbor }
3aaaa71997-06-12Wilhelm Köhler void threaded_handle(function f, mixed ... args) { handle_queue->write(({f, args })); }
14179b1997-01-29Per Hedbor int number_of_threads; void start_handler_threads() {
a60c4c1997-07-03Henrik Grubbström (Grubba)  if (QUERY(numthreads) <= 1) { QUERY(numthreads) = 1;
2ff8461999-09-02Per Hedbor  report_debug("Starting 1 thread to handle requests.\n");
a60c4c1997-07-03Henrik Grubbström (Grubba)  } else {
2ff8461999-09-02Per Hedbor  report_debug("Starting "+QUERY(numthreads) +" threads to handle requests.\n");
a60c4c1997-07-03Henrik Grubbström (Grubba)  }
14179b1997-01-29Per Hedbor  for(; number_of_threads < QUERY(numthreads); number_of_threads++)
4f4bc11998-02-04Per Hedbor  do_thread_create( "Handle thread ["+number_of_threads+"]", handler_thread, number_of_threads );
14179b1997-01-29Per Hedbor }
06583f1997-09-03Per Hedbor 
3107101998-09-01Marcus Comstedt void stop_handler_threads() { int timeout=30;
2ff8461999-09-02Per Hedbor  roxen_perror("Stopping all request handler threads.\n");
3107101998-09-01Marcus Comstedt  while(number_of_threads>0) { number_of_threads--; handle_queue->write(0); thread_reap_cnt++; } while(thread_reap_cnt) { if(--timeout<=0) {
2ff8461999-09-02Per Hedbor  roxen_perror("Giving up waiting on threads!\n");
3107101998-09-01Marcus Comstedt  return; }
34d3fa1999-04-22Per Hedbor  sleep(0.1);
3107101998-09-01Marcus Comstedt  } }
95e2b41997-05-25Wilhelm Köhler #endif /* THREADS */
14179b1997-01-29Per Hedbor 
06583f1997-09-03Per Hedbor 
b1fca01996-11-12Per Hedbor // Listen to a port, connected to the configuration 'conf', binding // only to the netinterface 'ether', using 'requestprogram' as a // protocol handled. // If you think that the argument order is quite unintuitive and odd, // you are right, the order is the same as the implementation order. // Old spinners only listened to a port number, then the // configurations came, then the need to bind to a specific // ethernetinterface, and then the need to have more than one concurrent // protocol (http, ftp, ssl, etc.)
14179b1997-01-29Per Hedbor 
b1fca01996-11-12Per Hedbor object create_listen_socket(mixed port_no, object conf,
9f46de1997-04-08Per Hedbor  string|void ether, program requestprogram, array prt)
b1fca01996-11-12Per Hedbor { object port; #ifdef SOCKET_DEBUG perror(sprintf("SOCKETS: create_listen_socket(%d,CONF(%s),%s)\n", port_no, conf?conf->name:"Configuration port", ether)); #endif if(!requestprogram) error("No request handling module passed to create_listen_socket()\n"); if(!port_no) {
7ed1021998-01-28Henrik Grubbström (Grubba)  port = Stdio.Port( "stdin", accept_callback );
c79b261998-02-05Johan Schön  port->set_id(port);
c31f7b1998-10-10Henrik Grubbström (Grubba)  if(port->errno()) { report_error(LOCALE->stdin_is_quiet(port->errno()));
b1fca01996-11-12Per Hedbor  } } else {
7ed1021998-01-28Henrik Grubbström (Grubba)  port = Stdio.Port();
c79b261998-02-05Johan Schön  port->set_id(port);
b1fca01996-11-12Per Hedbor  if(!stringp(ether) || (lower_case(ether) == "any")) ether=0; if(ether) sscanf(ether, "addr:%s", ether); if(!port->bind(port_no, accept_callback, ether)) { #ifdef SOCKET_DEBUG
581bf11997-09-06Per Hedbor  perror("SOCKETS: -> Failed.\n");
b1fca01996-11-12Per Hedbor #endif
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_warning(LOCALE-> socket_already_bound_retry(ether, port_no, port->errno()));
4ba6fc1998-04-03Henrik Grubbström (Grubba)  sleep(1); if(!port->bind(port_no, accept_callback, ether)) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_warning(LOCALE-> socket_already_bound(ether, port_no, port->errno()));
4ba6fc1998-04-03Henrik Grubbström (Grubba)  return 0; }
b1fca01996-11-12Per Hedbor  } }
14179b1997-01-29Per Hedbor  portno[port]=({ port_no, conf, ether||"Any", 0, requestprogram });
b1fca01996-11-12Per Hedbor #ifdef SOCKET_DEBUG perror("SOCKETS: -> Ok.\n"); #endif return port; }
4820e01999-07-27Henrik Grubbström (Grubba) /* * Port DB stuff. */ // ([ "prot" : ([ "ip" : ([ port : protocol_handler, ]), ]), ]) static mapping(string:mapping(string:mapping(int:object))) handler_db = ([]); // ([ "prot" : protocol_program, ]) static mapping(string:program) port_db = ([]); // Is there a handler for this port? object low_find_handler(string prot, string ip, int port) { mixed res; return((res = handler_db[prot]) && (res = res[ip]) && res[port]); } // Register a handler for a port. void register_handler(string prot, string ip, int port, object handler) { mapping m; if (m = handler_db[prot]) { mapping mm; if (mm = m[ip]) { // FIXME: What if mm[port] already exists? mm[port] = handler; } else { m[ip] = ([ port : handler ]); } } else { handler_db[prot] = ([ ip : ([ port : handler ]) ]); } } object find_handler(string prot, string ip, int port) { object handler = low_find_handler(prot, ip, port); if (!handler) { program prog = port_db[prot]; if (!prog) { return 0; } mixed err = catch { handler = prog(prot, ip, port); }; if (err) { report_error(LOCALE->failed_to_open_port("?", sprintf("%s://%s:%d/", prot, ip, port), describe_backtrace(err))); } else { register_handler(prot, ip, port, handler); } } return handler; }
b1fca01996-11-12Per Hedbor // The configuration interface is loaded dynamically for faster // startup-time, and easier coding in the configuration interface (the // Roxen environment is already finished when it is loaded) object configuration_interface_obj; int loading_config_interface; int enabling_configurations; object configuration_interface() { if(enabling_configurations) return 0; if(loading_config_interface) perror("Recursive calls to configuration_interface()\n" + describe_backtrace(backtrace())+"\n"); if(!configuration_interface_obj) { perror("Loading configuration interface.\n");
039ce41997-08-21Henrik Grubbström (Grubba)  loading_config_interface = 1;
2740711997-08-21Henrik Grubbström (Grubba)  array err = catch {
0f28da1997-08-13Per Hedbor  configuration_interface_obj=((program)"mainconfig")();
d24d291997-08-19Per Hedbor  root = configuration_interface_obj->root;
d4a3a51999-06-21Martin Stjernholm  build_root = configuration_interface_obj->build_root;
0f28da1997-08-13Per Hedbor  };
039ce41997-08-21Henrik Grubbström (Grubba)  loading_config_interface = 0;
2740711997-08-21Henrik Grubbström (Grubba)  if(!configuration_interface_obj) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_error(LOCALE-> configuration_interface_failed(describe_backtrace(err)));
2740711997-08-21Henrik Grubbström (Grubba)  }
b1fca01996-11-12Per Hedbor  } return configuration_interface_obj; }
1668b21998-04-23Henrik Grubbström (Grubba) // Unload the configuration interface void unload_configuration_interface() {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_notice(LOCALE->unload_configuration_interface());
1668b21998-04-23Henrik Grubbström (Grubba)  configuration_interface_obj = 0; loading_config_interface = 0; enabling_configurations = 0; build_root = 0; catch{root->dest();}; root = 0; }
b1fca01996-11-12Per Hedbor  // Create a new configuration from scratch. // 'type' is as in the form. 'none' for a empty configuration. int add_new_configuration(string name, string type) { return configuration_interface()->low_enable_configuration(name, type); } // Call the configuration interface function. This is more or less // equivalent to a virtual configuration with the configurationinterface // mounted on '/'. This will probably be the case in future versions
fb74971997-09-16Per Hedbor #ifdef THREADS object configuration_lock = Thread.Mutex(); #endif
b1fca01996-11-12Per Hedbor mixed configuration_parse(mixed ... args) {
fb74971997-09-16Per Hedbor #ifdef THREADS object key; catch(key = configuration_lock->lock()); #endif
b1fca01996-11-12Per Hedbor  if(args) return configuration_interface()->configuration_parse(@args); }
025d221997-06-01Henrik Grubbström (Grubba) mapping(string:array(int)) error_log=([]);
ec2fe11997-06-09Henrik Grubbström (Grubba) string last_error="";
b1fca01996-11-12Per Hedbor // Write a string to the configuration interface error log and to stderr.
9b9f701997-08-12Per Hedbor void nwrite(string s, int|void perr, int|void type)
b1fca01996-11-12Per Hedbor {
ec2fe11997-06-09Henrik Grubbström (Grubba)  last_error = s;
9b9f701997-08-12Per Hedbor  if (!error_log[type+","+s]) { error_log[type+","+s] = ({ time() });
025d221997-06-01Henrik Grubbström (Grubba)  } else {
9b9f701997-08-12Per Hedbor  error_log[type+","+s] += ({ time() });
025d221997-06-01Henrik Grubbström (Grubba)  }
17834a1998-04-09Henrik Grubbström (Grubba)  if(type>=1) roxen_perror(s);
b1fca01996-11-12Per Hedbor } // When was Roxen started?
2ff8461999-09-02Per Hedbor int boot_time =time(); int start_time =time();
b1fca01996-11-12Per Hedbor  string version() {
71a11e1997-08-13Henrik Grubbström (Grubba)  return QUERY(default_ident)?real_version:QUERY(ident);
b1fca01996-11-12Per Hedbor } public void log(mapping file, object request_id) {
14179b1997-01-29Per Hedbor  if(!request_id->conf) return; request_id->conf->log(file, request_id);
b1fca01996-11-12Per Hedbor } // Support for unique user id's private object 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]; perror("Restoring unique user ID information. (" + current_user_id_number + ")\n");
3235691998-03-26Per Hedbor #ifdef FD_DEBUG
c31f7b1998-10-10Henrik Grubbström (Grubba)  mark_fd(current_user_id_file->query_fd(), LOCALE->unique_uid_logfile());
3235691998-03-26Per Hedbor #endif
b1fca01996-11-12Per Hedbor }
f6d62d1997-03-26Per Hedbor 
b1fca01996-11-12Per Hedbor int increase_id() { if(!current_user_id_file) { restore_current_user_id_number(); return current_user_id_number+time(); } if(current_user_id_file->stat()[2] != current_user_id_file_last_mod) restore_current_user_id_number(); current_user_id_number++; //perror("New unique id: "+current_user_id_number+"\n"); 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; } public string full_status() { int tmp; string res=""; array foo = ({0.0, 0.0, 0.0, 0.0, 0}); if(!sizeof(configurations))
c31f7b1998-10-10Henrik Grubbström (Grubba)  return LOCALE->no_servers_enabled();
b1fca01996-11-12Per Hedbor 
5281751997-11-26Henrik Grubbström (Grubba)  foreach(configurations, object conf)
b1fca01996-11-12Per Hedbor  {
88e1cb1996-12-07David Hedbor  if(!conf->sent ||!conf->received ||!conf->hsent) continue;
5281751997-11-26Henrik Grubbström (Grubba)  foo[0] += conf->sent->mb()/(float)(time(1)-start_time+1); foo[1] += conf->sent->mb(); foo[2] += conf->hsent->mb(); foo[3] += conf->received->mb(); foo[4] += conf->requests;
b1fca01996-11-12Per Hedbor  }
5281751997-11-26Henrik Grubbström (Grubba) 
b1fca01996-11-12Per Hedbor  for(tmp = 1; tmp < 4; tmp ++) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  // FIXME: LOCALE?
b1fca01996-11-12Per Hedbor  if(foo[tmp] < 1024.0)
27b0e11996-11-26Per Hedbor  foo[tmp] = sprintf("%.2f MB", foo[tmp]);
b1fca01996-11-12Per Hedbor  else
27b0e11996-11-26Per Hedbor  foo[tmp] = sprintf("%.2f GB", foo[tmp]/1024.0);
b1fca01996-11-12Per Hedbor  }
60ecef1998-06-13Henrik Grubbström (Grubba)  int uptime = time()-start_time; int days = uptime/(24*60*60); int hrs = uptime/(60*60); int min = uptime/60 - hrs*60; hrs -= days*24;
053e9a1998-08-20Per Hedbor  tmp=(int)((foo[4]*600.0)/(uptime+1));
b1fca01996-11-12Per Hedbor 
c31f7b1998-10-10Henrik Grubbström (Grubba)  return(LOCALE->full_status(real_version, boot_time, start_time-boot_time, days, hrs, min, uptime%60, foo[1], foo[0] * 8192.0, foo[2], foo[4], (float)tmp/(float)10, foo[3]));
b1fca01996-11-12Per Hedbor }
4f4bc11998-02-04Per Hedbor int config_ports_changed = 0;
b1fca01996-11-12Per Hedbor 
1c30601998-07-07Henrik Grubbström (Grubba) static string MKPORTKEY(array(string) p) { if (sizeof(p[3])) { return(sprintf("%s://%s:%s/(%s)", p[1], p[2], (string)p[0], replace(p[3], ({"\n", "\r"}), ({ " ", " " })))); } else { return(sprintf("%s://%s:%s/", p[1], p[2], (string)p[0])); } }
23620b1998-04-09Henrik Grubbström (Grubba) // Is this only used to hold the config-ports? // Seems like it. Changed to a mapping. private mapping(string:object) configuration_ports = ([]);
14179b1997-01-29Per Hedbor 
add03d1998-11-30Henrik Grubbström (Grubba) // Used by config_actions/openports.pike
efa11c1998-04-29Henrik Grubbström (Grubba) array(object) get_configuration_ports() { return(values(configuration_ports)); }
14179b1997-01-29Per Hedbor // I will remove this in a future version of roxen. private program __p;
08152b1998-04-24Per Hedbor mapping my_loaded = ([]);
14179b1997-01-29Per Hedbor program last_loaded() { return __p; }
b1fca01996-11-12Per Hedbor 
68a0a21997-06-12Henrik Grubbström (Grubba) string last_module_name;
08152b1998-04-24Per Hedbor string filename(object|program o)
14179b1997-01-29Per Hedbor {
08152b1998-04-24Per Hedbor  if(objectp(o)) o = object_program(o); return my_loaded[(program)o]||last_module_name;
14179b1997-01-29Per Hedbor }
b1fca01996-11-12Per Hedbor 
753a831999-08-30Per Hedbor class Codec { program p; string nameof(mixed x) { if(p!=x) if(mixed tmp=search(all_constants(),x)) return "efun:"+tmp; switch(sprintf("%t",x)) { case "program": if(p!=x) { mixed tmp; if(tmp=search(master()->programs,x)) return tmp; if((tmp=search(values(_static_modules), x))!=-1) return "_static_modules."+(indices(_static_modules)[tmp]); } break; case "object": if(mixed tmp=search(master()->objects,x)) { if(tmp=search(master()->programs,tmp)) { return tmp; } } break; } // werror("Did not find %O (%O)\n", x, indices(x) ); return ([])[0]; } function functionof(string x) { if(sscanf(x,"efun:%s",x)) return all_constants()[x]; werror("Failed to decode %s\n",x); return 0; } object objectof(string x) { if(sscanf(x,"efun:%s",x)) return all_constants()[x]; if(object tmp=(object)x) return tmp; werror("Failed to decode %s\n",x); return 0; } program programof(string x) { if(sscanf(x,"efun:%s",x)) return all_constants()[x]; if(sscanf(x,"_static_modules.%s",x)) { return (program)_static_modules[x]; } if(program tmp=(program)x) return tmp; werror("Failed to decode %s\n",x); return 0; } mixed encode_object(object x) { error("Cannot encode objects yet.\n"); } mixed decode_object(object x) { error("Cannot encode objects yet.\n"); } void create( program q ) { p = q; } }
34447f1998-03-20Per Hedbor program my_compile_file(string file) {
753a831999-08-30Per Hedbor  m_delete( master()->programs, file); program p = (program)( file );
3e94981999-08-30Per Hedbor  if( !file_stat( file+".o" ) || file_stat(file+".o")[ST_MTIME] < file_stat(file)[ST_MTIME] )
753a831999-08-30Per Hedbor  if( catch { string data = encode_value( p, Codec(p) ); if( strlen( data ) ) Stdio.File( file+".o", "wct" )->write( data ); } ) { #ifdef MODULE_DEBUG werror(" [nodump] "); #endif Stdio.File( file+".o", "wct" );
3e94981999-08-30Per Hedbor  } else { #ifdef MODULE_DEBUG werror(" [dump] "); #endif
753a831999-08-30Per Hedbor  } return p;
34447f1998-03-20Per Hedbor }
add03d1998-11-30Henrik Grubbström (Grubba) array compile_module( string file ) { array foo; object o; program p; MD_PERROR(("Compiling " + file + "...\n")); if (catch(p = my_compile_file(file)) || (!p)) { MD_PERROR((" compilation failed")); throw("MODULE: Compilation failed.\n"); } // Set the module-filename, so that create in the // new object can get it. last_module_name = file; array err = catch(o = p()); last_module_name = 0; if (err) { MD_PERROR((" load failed\n")); throw(err); } else if (!o) { MD_PERROR((" load failed\n")); throw("Failed to initialize module.\n"); } else { MD_PERROR((" load ok - ")); if (!o->register_module) { MD_PERROR(("register_module missing")); throw("No registration function in module.\n"); } } foo = o->register_module(); if (!foo) { MD_PERROR(("registration failed.\n")); return 0; } else { MD_PERROR(("registered.")); } return({ foo[1], foo[2]+"<p><i>"+ replace(o->file_name_and_stuff(), "0<br>", file+"<br>") +"</i>", foo[0] }); }
e382ed1997-07-16Henrik Grubbström (Grubba) // ([ filename:stat_array ]) mapping(string:array) module_stat_cache = ([]);
8c62221997-08-06Henrik Grubbström (Grubba) object load(string s, object conf) // Should perhaps be renamed to 'reload'.
14179b1997-01-29Per Hedbor {
08065d1997-05-27Per Hedbor  string cvs;
e493e81997-07-11Per Hedbor  array st;
08065d1997-05-27Per Hedbor  sscanf(s, "/cvs:%s", cvs);
753a831999-08-30Per Hedbor // perror("Module is "+s+"?");
e493e81997-07-11Per Hedbor  if(st=file_stat(s+".pike"))
5e89211997-02-13Per Hedbor  {
753a831999-08-30Per Hedbor // perror("Yes, compile "+s+"?");
08065d1997-05-27Per Hedbor  if((cvs?(__p=master()->cvs_load_file( cvs+".pike" ))
34447f1998-03-20Per Hedbor  :(__p=my_compile_file(s+".pike"))))
14179b1997-01-29Per Hedbor  {
82f5191997-03-02Per Hedbor // perror("Yes.");
14179b1997-01-29Per Hedbor  my_loaded[__p]=s+".pike";
e493e81997-07-11Per Hedbor  module_stat_cache[s-dirname(s)]=st;
8c62221997-08-06Henrik Grubbström (Grubba)  return __p(conf);
5e89211997-02-13Per Hedbor  } else perror(s+".pike exists, but compilation failed.\n"); }
753a831999-08-30Per Hedbor // #if 0 if(st=file_stat(s+".so")) if(__p=predef::load_module(s+".so"))
2a2a5b1996-12-01Per Hedbor  {
5e89211997-02-13Per Hedbor  my_loaded[__p]=s+".so";
e493e81997-07-11Per Hedbor  module_stat_cache[s-dirname(s)]=st;
8c62221997-08-06Henrik Grubbström (Grubba)  return __p(conf);
5e89211997-02-13Per Hedbor  } else perror(s+".so exists, but compilation failed.\n");
753a831999-08-30Per Hedbor // #endif
b1fca01996-11-12Per Hedbor  return 0; // FAILED.. }
ab2f671996-11-12Mirar (Pontus Hagland) array(string) expand_dir(string d)
5e4ede1996-11-12Per Hedbor { string nd;
ab2f671996-11-12Mirar (Pontus Hagland)  array(string) dirs=({d});
5e4ede1996-11-12Per Hedbor 
5e89211997-02-13Per Hedbor //perror("Expand dir "+d+"\n");
82f5191997-03-02Per Hedbor  catch { foreach((get_dir(d) || ({})) - ({"CVS"}) , nd) if(file_stat(d+nd)[1]==-2) dirs+=expand_dir(d+nd+"/"); }; // This catch is needed....
ab2f671996-11-12Mirar (Pontus Hagland)  return dirs;
5e4ede1996-11-12Per Hedbor }
ab2f671996-11-12Mirar (Pontus Hagland) array(string) last_dirs=0,last_dirs_expand;
8c62221997-08-06Henrik Grubbström (Grubba) object load_from_dirs(array dirs, string f, object conf)
14179b1997-01-29Per Hedbor { string dir; object o;
b1fca01996-11-12Per Hedbor 
14179b1997-01-29Per Hedbor  if (dirs!=last_dirs) {
5e89211997-02-13Per Hedbor  last_dirs_expand=({}); foreach(dirs, dir) last_dirs_expand+=expand_dir(dir);
14179b1997-01-29Per Hedbor  }
b1fca01996-11-12Per Hedbor 
14179b1997-01-29Per Hedbor  foreach (last_dirs_expand,dir)
753a831999-08-30Per Hedbor  if((o=load(dir+f, conf))) return o;
b1fca01996-11-12Per Hedbor 
14179b1997-01-29Per Hedbor  return 0; }
6ca8f61998-10-13Per Hedbor 
ee8b201998-07-13David Hedbor static int abs_started;
6ca8f61998-10-13Per Hedbor  void restart_if_stuck (int force) {
ee8b201998-07-13David Hedbor  remove_call_out(restart_if_stuck); if (!(QUERY(abs_engage) || force))
edc9af1998-07-11David Hedbor  return;
6ca8f61998-10-13Per Hedbor  if(!abs_started) {
ee8b201998-07-13David Hedbor  abs_started = 1;
dca5dc1998-09-12Per Hedbor  roxen_perror("Anti-Block System Enabled.\n");
ee8b201998-07-13David Hedbor  } call_out (restart_if_stuck,10);
6ca8f61998-10-13Per Hedbor  signal(signum("SIGALRM"), lambda( int n ) {
b0659e1999-07-20Henrik Grubbström (Grubba)  werror(sprintf("**** %s: ABS engaged!\n" "Trying to dump backlog: \n", ctime(time()) - "\n")); catch { // Catch for paranoia reasons. describe_all_threads(); }; werror(sprintf("**** %s: ABS exiting roxen!\n\n", ctime(time())));
6ca8f61998-10-13Per Hedbor  _exit(1); // It might now quit correctly otherwise, if it's // locked up });
ee8b201998-07-13David Hedbor  alarm (60*QUERY(abs_timeout)+10);
edc9af1998-07-11David Hedbor }
6ca8f61998-10-13Per Hedbor void post_create () {
ee8b201998-07-13David Hedbor  if (QUERY(abs_engage)) call_out (restart_if_stuck,10);
edc9af1998-07-11David Hedbor  if (QUERY(suicide_engage))
ee8b201998-07-13David Hedbor  call_out (restart,60*60*24*QUERY(suicide_timeout));
edc9af1998-07-11David Hedbor }
59912f1998-10-15Henrik Grubbström (Grubba) 
753a831999-08-30Per Hedbor 
2ff8461999-09-02Per Hedbor class ImageCache { string name; string dir; function draw_function; mapping data_cache = ([]); // not normally used. mapping meta_cache = ([]);
0f8b2f1999-03-27Henrik Grubbström (Grubba) 
34d3fa1999-04-22Per Hedbor 
2ff8461999-09-02Per Hedbor  static mapping meta_cache_insert( string i, mapping what ) { return meta_cache[i] = what; } static string data_cache_insert( string i, string what ) { return data_cache[i] = what; }
753a831999-08-30Per Hedbor 
2ff8461999-09-02Per Hedbor  static mixed frommapp( mapping what )
753a831999-08-30Per Hedbor  {
2ff8461999-09-02Per Hedbor  if( what[""] ) return what[""]; return what;
753a831999-08-30Per Hedbor  }
2ff8461999-09-02Per Hedbor  static void draw( string name, RequestID id ) { mixed args = Array.map( Array.map( name/"$", argcache->lookup, id->client ), frommapp); mapping meta; string data; mixed reply = draw_function( @copy_value(args), id );
b1fca01996-11-12Per Hedbor 
2ff8461999-09-02Per Hedbor  if( arrayp( args ) ) args = args[0];
b1fca01996-11-12Per Hedbor 
2ff8461999-09-02Per Hedbor  if( objectp( reply ) || (mappingp(reply) && reply->img) )
b1fca01996-11-12Per Hedbor  {
2ff8461999-09-02Per Hedbor  int quant = (int)args->quant; string format = lower_case(args->format || "gif"); string dither = args->dither; Image.Colortable ct; object alpha; int true_alpha;
b1fca01996-11-12Per Hedbor 
2ff8461999-09-02Per Hedbor  if( args->fs || dither == "fs" ) dither = "floyd_steinberg";
b1fca01996-11-12Per Hedbor 
2ff8461999-09-02Per Hedbor  if( dither == "random" ) dither = "random_dither";
b1fca01996-11-12Per Hedbor 
2ff8461999-09-02Per Hedbor  if( format == "jpg" ) format = "jpeg";
b1fca01996-11-12Per Hedbor 
2ff8461999-09-02Per Hedbor  if(mappingp(reply)) { alpha = reply->alpha; reply = reply->img;
93ebdd1999-03-11Martin Stjernholm  }
2ff8461999-09-02Per Hedbor  if( args->gamma ) reply = reply->gamma( (float)args->gamma );
b79be21999-06-11Per Hedbor 
c3a53d1999-06-25Per Hedbor  if( args["true-alpha"] ) true_alpha = 1;
b79be21999-06-11Per Hedbor  if( args["opaque-value"] ) {
c3a53d1999-06-25Per Hedbor  true_alpha = 1;
b79be21999-06-11Per Hedbor  int ov = (int)(((float)args["opaque-value"])*2.55); if( ov < 0 ) ov = 0; else if( ov > 255 ) ov = 255; if( alpha ) { object i = Image.image( reply->xsize(), reply->ysize(), ov,ov,ov ); i->paste_alpha( alpha, ov ); alpha = i; } else { alpha = Image.image( reply->xsize(), reply->ysize(), ov,ov,ov ); } }
c526921999-05-18Per Hedbor  if( args->scale ) { int x, y; if( sscanf( args->scale, "%d,%d", x, y ) == 2) { reply = reply->scale( x, y ); if( alpha ) alpha = alpha->scale( x, y ); }
3255b11999-05-19Per Hedbor  else if( (float)args->scale < 3.0)
c526921999-05-18Per Hedbor  {
3255b11999-05-19Per Hedbor  reply = reply->scale( ((float)args->scale) );
c526921999-05-18Per Hedbor  if( alpha )
3255b11999-05-19Per Hedbor  alpha = alpha->scale( ((float)args->scale) );
c526921999-05-18Per Hedbor  } } if( args->maxwidth || args->maxheight ) { int x = (int)args->maxwidth, y = (int)args->maxheight; if( x && reply->xsize() > x ) { reply = reply->scale( x, 0 ); if( alpha ) alpha = alpha->scale( x, 0 ); } if( y && reply->ysize() > y ) { reply = reply->scale( 0, y ); if( alpha ) alpha = alpha->scale( 0, y ); } }
69a8691999-05-18Per Hedbor  if( quant || (format=="gif") ) {
000c781999-05-25Per Hedbor  int ncols = quant||id->misc->defquant||16;
7ed2ef1999-05-25Peter Bortas  if( ncols > 250 )
000c781999-05-25Per Hedbor  ncols = 250; ct = Image.Colortable( reply, ncols );
69a8691999-05-18Per Hedbor  if( dither )
000c781999-05-25Per Hedbor  if( ct[ dither ] ) ct[ dither ]();
69a8691999-05-18Per Hedbor  else ct->ordered(); }
3760171999-06-27Per Hedbor  if(!Image[upper_case( format )] || !Image[upper_case( format )]->encode )
69a8691999-05-18Per Hedbor  error("Image format "+format+" unknown\n"); mapping enc_args = ([]); if( ct ) enc_args->colortable = ct; if( alpha ) enc_args->alpha = alpha;
c526921999-05-18Per Hedbor  foreach( glob( "*-*", indices(args)), string n ) if(sscanf(n, "%*[^-]-%s", string opt ) == 2) enc_args[opt] = (int)args[n];
69a8691999-05-18Per Hedbor  switch(format) { case "gif":
c3a53d1999-06-25Per Hedbor  if( alpha && true_alpha )
b79be21999-06-11Per Hedbor  {
c3a53d1999-06-25Per Hedbor  object ct=Image.Colortable( ({ ({ 0,0,0 }), ({ 255,255,255 }) }) );
b79be21999-06-11Per Hedbor  ct->floyd_steinberg(); alpha = ct->map( alpha ); }
000c781999-05-25Per Hedbor  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;
69a8691999-05-18Per Hedbor  case "png": if( ct ) enc_args->palette = ct; m_delete( enc_args, "colortable" ); default: data = Image[upper_case( format )]->encode( reply, enc_args ); } meta = ([ "xsize":reply->xsize(), "ysize":reply->ysize(), "type":"image/"+format, ]);
c526921999-05-18Per Hedbor  }
69a8691999-05-18Per Hedbor  else if( mappingp(reply) ) { meta = reply->meta; data = reply->data; if( !meta || !data ) error("Invalid reply mapping.\n" "Should be ([ \"meta\": ([metadata]), \"data\":\"data\" ])\n"); } store_meta( name, meta ); store_data( name, data ); } static void store_meta( string id, mapping meta ) { meta_cache_insert( id, meta ); string data = encode_value( meta );
b503621999-07-27Per Hedbor  Stdio.File f = Stdio.File( ); if(!f->open(dir+id+".i", "wct" ))
69a8691999-05-18Per Hedbor  { report_error( "Failed to open image cache persistant cache file "+ dir+id+".i: "+strerror( errno() )+ "\n" ); return; } f->write( data ); } static void store_data( string id, string data ) {
b503621999-07-27Per Hedbor  Stdio.File f = Stdio.File( ); if(!f->open(dir+id+".d", "wct" ))
69a8691999-05-18Per Hedbor  { data_cache_insert( id, data ); report_error( "Failed to open image cache persistant cache file "+
3760171999-06-27Per Hedbor  dir+id+".d: "+strerror( errno() )+ "\n" );
69a8691999-05-18Per Hedbor  return; } f->write( data ); } static mapping restore_meta( string id ) { Stdio.File f; if( meta_cache[ id ] ) return meta_cache[ id ]; f = Stdio.File( ); if( !f->open(dir+id+".i", "r" ) ) return 0; return meta_cache_insert( id, decode_value( f->read() ) ); } static mapping restore( string id ) { string|object(Stdio.File) f; mapping m; if( data_cache[ id ] ) f = data_cache[ id ]; else f = Stdio.File( );
3760171999-06-27Per Hedbor  if(!f->open(dir+id+".d", "r" ))
69a8691999-05-18Per Hedbor  return 0; m = restore_meta( id ); if(!m) return 0; if( stringp( f ) )
2ff8461999-09-02Per Hedbor  return roxenp()->http_string_answer( f, m->type||("image/gif") );
69a8691999-05-18Per Hedbor  return roxenp()->http_file_answer( f, m->type||("image/gif") ); } string data( string|mapping args, RequestID id, int|void nodraw ) { string na = store( args, id ); mixed res; if(!( res = restore( na )) ) { if(nodraw) return 0; draw( na, id ); res = restore( na ); } if( res->file ) return res->file->read(); return res->data; }
c3a53d1999-06-25Per Hedbor  mapping http_file_answer( string|mapping data, RequestID id, int|void nodraw )
69a8691999-05-18Per Hedbor  { string na = store( data,id ); mixed res; if(!( res = restore( na )) ) { if(nodraw) return 0; draw( na, id ); res = restore( na ); } return res; }
2ff8461999-09-02Per Hedbor  mapping metadata( string|mapping data, RequestID id, int|void nodraw ) { string na = store( data,id ); if(!restore_meta( na )) { if(nodraw) return 0; draw( na, id ); return restore_meta( na ); } return restore_meta( na ); } mapping tomapp( mixed what ) { if( mappingp( what )) return what; return ([ "":what ]); } string store( array|string|mapping data, RequestID id ) { string ci; if( mappingp( data ) ) ci = argcache->store( data ); else if( arrayp( data ) ) ci = Array.map( Array.map( data, tomapp ), argcache->store )*"$"; else ci = data; return ci; } void set_draw_function( function to ) { draw_function = to; } void create( string id, function draw_func, string|void d ) { if(!d) d = roxenp()->QUERY(argument_cache_dir); if( d[-1] != '/' ) d+="/"; d += id+"/"; mkdirhier( d+"foo"); dir = d; name = id; draw_function = draw_func; } } class ArgCache { static string name; static string path; static int is_db; static object db; #define CACHE_VALUE 0 #define CACHE_SKEY 1 #define CACHE_SIZE 600 #define CLEAN_SIZE 100 #ifdef THREADS static Thread.Mutex mutex = Thread.Mutex(); # define LOCK() object __key = mutex->lock() #else # define LOCK() #endif static mapping (string:mixed) cache = ([ ]);
b870ac1999-09-03Henrik Grubbström (Grubba)  static void setup_table()
2ff8461999-09-02Per Hedbor  { if(catch(db->query("select id from "+name+" where id=-1"))) if(catch(db->query("create table "+name+" (" "id int auto_increment primary key, " "lkey varchar(80) not null default '', " "contents blob not null default '', " "atime bigint not null default 0)"))) throw("Failed to create table in database\n"); } void create( string _name, string _path, int _is_db ) { name = _name; path = _path; is_db = _is_db; if(is_db) { db = Sql.sql( path ); if(!db) error("Failed to connect to database for argument cache\n"); setup_table( ); } else { if(path[-1] != '/' && path[-1] != '\\') path += "/"; path += replace(name, "/", "_")+"/"; mkdirhier( path + "/tmp" ); object test = Stdio.File(); if (!test->open (path + "/.testfile", "wc")) error ("Can't create files in the argument cache directory " + path + "\n"); else { test->close(); rm (path + "/.testfile"); } } } static string read_args( string id ) { if( is_db ) { mapping res = db->query("select contents from "+name+" where id='"+id+"'"); if( sizeof(res) ) { db->query("update "+name+" set atime='"+ time()+"' where id='"+id+"'"); return res[0]->contents; } return 0; } else { if( file_stat( path+id ) ) return Stdio.read_bytes(path+"/"+id); } return 0; } static string create_key( string long_key ) { if( is_db ) { mapping data = db->query(sprintf("select id,contents from %s where lkey='%s'", name,long_key[..79])); foreach( data, mapping m ) if( m->contents == long_key ) return m->id; db->query( sprintf("insert into %s (contents,lkey,atime) values " "('%s','%s','%d')", name, long_key, long_key[..79], time() )); return create_key( long_key ); } else { string _key=MIME.encode_base64(Crypto.md5()->update(long_key)->digest(),1); _key = replace(_key-"=","/","="); string short_key = _key[0..1]; while( file_stat( path+short_key ) ) { if( Stdio.read_bytes( path+short_key ) == long_key ) return short_key; short_key = _key[..strlen(short_key)]; if( strlen(short_key) >= strlen(_key) ) short_key += "."; // Not very likely... } object f = Stdio.File( path + short_key, "wct" ); f->write( long_key ); return short_key; } } int key_exists( string key ) { LOCK(); if( !is_db ) return !!file_stat( path+key ); return !!read_args( key ); } string store( mapping args )
69a8691999-05-18Per Hedbor  {
2ff8461999-09-02Per Hedbor  LOCK(); array b = values(args), a = sort(indices(args),b); string data = MIME.encode_base64(encode_value(({a,b})),1); if( cache[ data ] ) return cache[ data ][ CACHE_SKEY ]; string id = create_key( data ); cache[ data ] = ({ 0, 0 }); cache[ data ][ CACHE_VALUE ] = copy_value( args ); cache[ data ][ CACHE_SKEY ] = id; cache[ id ] = data; if( sizeof( cache ) > CACHE_SIZE )
69a8691999-05-18Per Hedbor  {
2ff8461999-09-02Per Hedbor  array i = indices(cache); while( sizeof(cache) > CACHE_SIZE-CLEAN_SIZE ) m_delete( cache, i[random(sizeof(i))] );
69a8691999-05-18Per Hedbor  }
2ff8461999-09-02Per Hedbor  return id;
69a8691999-05-18Per Hedbor  }
2ff8461999-09-02Per Hedbor  mapping lookup( string id, string|void client )
c3a53d1999-06-25Per Hedbor  {
2ff8461999-09-02Per Hedbor  LOCK(); if(cache[id]) return cache[cache[id]][CACHE_VALUE]; string q = read_args( id ); if(!q) error("Key does not exist! (Thinks "+ client +")\n"); mixed data = decode_value(MIME.decode_base64( q )); data = mkmapping( data[0],data[1] ); cache[ q ] = ({0,0}); cache[ q ][ CACHE_VALUE ] = data; cache[ q ][ CACHE_SKEY ] = id; cache[ id ] = q; return data;
c3a53d1999-06-25Per Hedbor  }
2ff8461999-09-02Per Hedbor  void delete( string id )
69a8691999-05-18Per Hedbor  {
2ff8461999-09-02Per Hedbor  LOCK(); if(cache[id]) { m_delete( cache, cache[id] ); m_delete( cache, id ); } if( is_db ) db->query( "delete from "+name+" where id='"+id+"'" );
69a8691999-05-18Per Hedbor  else
2ff8461999-09-02Per Hedbor  rm( path+id );
69a8691999-05-18Per Hedbor  }
2ff8461999-09-02Per Hedbor }
69a8691999-05-18Per Hedbor 
2ff8461999-09-02Per Hedbor void create() { SET_LOCALE(default_locale); // catch // { // module_stat_cache = decode_value(Stdio.read_bytes(".module_stat_cache")); // allmodules = decode_value(Stdio.read_bytes(".allmodules")); // }; #ifndef __NT__ if(!getuid()) { add_constant("Privs", Privs); } else #endif /* !__NT__ */ add_constant("Privs", class{});
69a8691999-05-18Per Hedbor 
2ff8461999-09-02Per Hedbor  // Dump some programs (for speed) dump( "base_server/newdecode.pike" ); dump( "base_server/read_config.pike" ); dump( "base_server/global_variables.pike" ); dump( "base_server/module_support.pike" ); dump( "base_server/http.pike" ); dump( "base_server/smartpipe.pike" ); dump( "base_server/socket.pike" ); dump( "base_server/cache.pike" ); dump( "base_server/supports.pike" ); dump( "base_server/fonts.pike"); dump( "base_server/hosts.pike"); dump( "base_server/language.pike");
69a8691999-05-18Per Hedbor 
2ff8461999-09-02Per Hedbor  // for module encoding stuff add_constant( "Image", Image ); add_constant( "Image.Image", Image.Image ); add_constant( "Image.Colortable", Image.Colortable ); add_constant("Fonts", fonts ); add_constant("_____argcache", argcache ); add_constant("ArgCache", ArgCache ); add_constant("Stdio_dot_File_indeed", Stdio.File ); #if constant(Thread.Mutex) add_constant( "_Thread_dot_Mutex_", Thread.Mutex ); add_constant( "_Thread_dot_Queue_", Thread.Queue ); #endif add_constant("roxen", this_object()); add_constant("RequestID", RequestID); add_constant("load", load); add_constant("_Roxen_dot_set_locale", set_locale ); add_constant("_Roxen_dot_locale", locale ); add_constant("_Locale_dot_Roxen", Locale.Roxen ); add_constant("_roxen_dot_ImageCache", ImageCache ); // compatibility add_constant("hsv_to_rgb", Colors.hsv_to_rgb ); add_constant("rgb_to_hsv", Colors.rgb_to_hsv ); add_constant("parse_color", Colors.parse_color ); add_constant("color_name", Colors.color_name ); add_constant("colors", Colors ); fonts = (object)"fonts.pike"; Configuration = (program)"configuration"; if(!file_stat( "base_server/configuration.pike.o" ) || file_stat("base_server/configuration.pike.o")[ST_MTIME] < file_stat("base_server/configuration.pike")[ST_MTIME]) { Stdio.write_file( "base_server/configuration.pike.o", encode_value( Configuration, Codec( Configuration) ) );
69a8691999-05-18Per Hedbor  }
2ff8461999-09-02Per Hedbor  add_constant("Configuration", Configuration ); call_out(post_create,1); //we just want to delay some things a little
69a8691999-05-18Per Hedbor }
a22f6f1999-05-12Per Hedbor 
2ff8461999-09-02Per Hedbor // Set the uid and gid to the ones requested by the user. If the sete* // functions are available, and the define SET_EFFECTIVE is enabled, // the euid and egid is set. This might be a minor security hole, but // it will enable roxen to start CGI scripts with the correct // permissions (the ones the owner of that script have).
a22f6f1999-05-12Per Hedbor 
2ff8461999-09-02Per Hedbor int set_u_and_gid() { #ifndef __NT__ string u, g; int uid, gid; array pw; u=QUERY(User); sscanf(u, "%s:%s", u, g); if(strlen(u)) { if(getuid()) { report_error ("It is only possible to change uid and gid if the server " "is running as root.\n"); } else { if (g) { #if constant(getgrnam) pw = getgrnam (g); if (!pw) if (sscanf (g, "%d", gid)) pw = getgrgid (gid), g = (string) gid; else report_error ("Couldn't resolve group " + g + ".\n"), g = 0; if (pw) g = pw[0], gid = pw[2];
a22f6f1999-05-12Per Hedbor #else
2ff8461999-09-02Per Hedbor  if (!sscanf (g, "%d", gid)) report_warning ("Can't resolve " + g + " to gid on this system; " "numeric gid required.\n");
a22f6f1999-05-12Per Hedbor #endif
2ff8461999-09-02Per Hedbor  }
a22f6f1999-05-12Per Hedbor 
2ff8461999-09-02Per Hedbor  pw = getpwnam (u); if (!pw) if (sscanf (u, "%d", uid)) pw = getpwuid (uid), u = (string) uid; else { report_error ("Couldn't resolve user " + u + ".\n"); return 0; } if (pw) { u = pw[0], uid = pw[2]; if (!g) gid = pw[3]; }
a22f6f1999-05-12Per Hedbor 
2ff8461999-09-02Per Hedbor #ifdef THREADS object mutex_key; catch { mutex_key = euid_egid_lock->lock(); }; object threads_disabled = _disable_threads(); #endif
a22f6f1999-05-12Per Hedbor 
2ff8461999-09-02Per Hedbor #if constant(seteuid) if (geteuid() != getuid()) seteuid (getuid()); #endif
a22f6f1999-05-12Per Hedbor 
2ff8461999-09-02Per Hedbor #if constant(initgroups) catch { initgroups(pw[0], gid); // Doesn't always work - David. }; #endif if (QUERY(permanent_uid)) { #if constant(setuid) if (g) { # if constant(setgid) setgid(gid); if (getgid() != gid) report_error ("Failed to set gid.\n"), g = 0; # else report_warning ("Setting gid not supported on this system.\n"); g = 0; # endif } setuid(uid); if (getuid() != uid) report_error ("Failed to set uid.\n"), u = 0; if (u) report_notice(LOCALE->setting_uid_gid_permanently (uid, gid, u, g)); #else report_warning ("Setting uid not supported on this system.\n"); u = g = 0; #endif }
b84a161999-06-07Martin Stjernholm  else {
2ff8461999-09-02Per Hedbor #if constant(seteuid) if (g) { # if constant(setegid) setegid(gid); if (getegid() != gid) report_error ("Failed to set effective gid.\n"), g = 0; # else report_warning ("Setting effective gid not supported on this system.\n"); g = 0; # endif } seteuid(uid); if (geteuid() != uid) report_error ("Failed to set effective uid.\n"), u = 0; if (u) report_notice(LOCALE->setting_uid_gid (uid, gid, u, g)); #else report_warning ("Setting effective uid not supported on this system.\n"); u = g = 0; #endif
b84a161999-06-07Martin Stjernholm  }
2ff8461999-09-02Per Hedbor  return !!u;
a22f6f1999-05-12Per Hedbor  } }
2ff8461999-09-02Per Hedbor #endif return 0; }
a22f6f1999-05-12Per Hedbor 
2ff8461999-09-02Per Hedbor void reload_all_configurations() { object conf; array (object) new_confs = ({}); mapping config_cache = ([]); int modified; report_notice(LOCALE->reloading_config_interface()); configs = ([]); setvars(retrieve("Variables", 0)); initiate_configuration_port( 0 ); foreach(list_all_configurations(), string config)
a22f6f1999-05-12Per Hedbor  {
2ff8461999-09-02Per Hedbor  array err, st; foreach(configurations, conf)
a22f6f1999-05-12Per Hedbor  {
2ff8461999-09-02Per Hedbor  if(lower_case(conf->name) == lower_case(config))
a22f6f1999-05-12Per Hedbor  {
2ff8461999-09-02Per Hedbor  break; } else conf = 0; } if(!(st = config_is_modified(config))) { if(conf) { config_cache[config] = config_stat_cache[config]; new_confs += ({ conf });
a22f6f1999-05-12Per Hedbor  }
2ff8461999-09-02Per Hedbor  continue;
a22f6f1999-05-12Per Hedbor  }
2ff8461999-09-02Per Hedbor  modified = 1; config_cache[config] = st; if(conf) { // Closing ports... if (conf->server_ports) { // Roxen 1.2.26 or later Array.map(values(conf->server_ports), destruct); } else { Array.map(indices(conf->open_ports), destruct); } conf->stop(); conf->invalidate_cache(); conf->modules = ([]); conf->create(conf->name);
a22f6f1999-05-12Per Hedbor  } else {
2ff8461999-09-02Per Hedbor  if(err = catch
a22f6f1999-05-12Per Hedbor  {
2ff8461999-09-02Per Hedbor  conf = enable_configuration(config); }) { report_error(LOCALE-> error_enabling_configuration(config, describe_backtrace(err))); continue;
a22f6f1999-05-12Per Hedbor  } }
2ff8461999-09-02Per Hedbor  if(err = catch { conf->start(); conf->enable_all_modules(); }) { report_error(LOCALE-> error_enabling_configuration(config, describe_backtrace(err))); continue; } new_confs += ({ conf });
a22f6f1999-05-12Per Hedbor  }
2ff8461999-09-02Per Hedbor  foreach(configurations - new_confs, conf)
a22f6f1999-05-12Per Hedbor  {
2ff8461999-09-02Per Hedbor  modified = 1; report_notice(LOCALE->disabling_configuration(conf->name)); if (conf->server_ports) { // Roxen 1.2.26 or later Array.map(values(conf->server_ports), destruct); } else { Array.map(indices(conf->open_ports), destruct);
a22f6f1999-05-12Per Hedbor  }
2ff8461999-09-02Per Hedbor  conf->stop(); destruct(conf); } if(modified) { configurations = new_confs; config_stat_cache = config_cache; unload_configuration_interface();
a22f6f1999-05-12Per Hedbor  }
2ff8461999-09-02Per Hedbor }
a22f6f1999-05-12Per Hedbor 
2ff8461999-09-02Per Hedbor object enable_configuration(string name) { object cf = Configuration(name); configurations += ({ cf }); report_notice(LOCALE->enabled_server(name)); return cf; }
a22f6f1999-05-12Per Hedbor 
2ff8461999-09-02Per Hedbor // Enable all configurations void enable_configurations() { array err;
f7adf51999-05-24Per Hedbor 
2ff8461999-09-02Per Hedbor  enabling_configurations = 1; configurations = ({}); foreach(list_all_configurations(), string config) { if(err=catch { enable_configuration(config)->start(); }) perror("Error while loading configuration "+config+":\n"+ describe_backtrace(err)+"\n"); }; enabling_configurations = 0; }
a22f6f1999-05-12Per Hedbor 
2ff8461999-09-02Per Hedbor void enable_configurations_modules() { foreach(configurations, object config)
a22f6f1999-05-12Per Hedbor  {
2ff8461999-09-02Per Hedbor  array err; if(err=catch { config->enable_all_modules(); }) perror("Error while loading modules in configuration "+config->name+":\n"+ describe_backtrace(err)+"\n"); };
a22f6f1999-05-12Per Hedbor } array(int) invert_color(array color ) { return ({ 255-color[0], 255-color[1], 255-color[2] }); } mapping low_decode_image(string data, void|array tocolor) { Image.image i, a; string format; if(!data) return 0; #if constant(Image.GIF._decode) // Use the low-level decode function to get the alpha channel. catch { array chunks = Image.GIF._decode( data ); // If there is more than one render chunk, the image is probably // an animation. Handling animations is left as an exercise for // the reader. :-) foreach(chunks, mixed chunk) if(arrayp(chunk) && chunk[0] == Image.GIF.RENDER ) [i,a] = chunk[3..4]; format = "GIF"; }; if(!i) catch { i = Image.GIF.decode( data ); format = "GIF"; }; #endif #if constant(Image.JPEG) && constant(Image.JPEG.decode) if(!i) catch { i = Image.JPEG.decode( data ); format = "JPEG"; }; #endif #if constant(Image.XCF) && constant(Image.XCF._decode) if(!i) catch {
c3a53d1999-06-25Per Hedbor  mixed q = Image.XCF._decode( data,(["background":tocolor,]) );
a22f6f1999-05-12Per Hedbor  tocolor=0; format = "XCF Gimp file"; i = q->image; a = q->alpha; }; #endif #if constant(Image.PSD) && constant(Image.PSD._decode) if(!i) catch { mixed q = Image.PSD._decode( data, ([ "background":tocolor, ])); tocolor=0; format = "PSD Photoshop file"; i = q->image; a = q->alpha; }; #endif #if constant(Image.PNG) && constant(Image.PNG._decode) if(!i) catch { mixed q = Image.PNG._decode( data ); format = "PNG"; i = q->image; a = q->alpha; }; #endif #if constant(Image.BMP) && constant(Image.BMP._decode) if(!i) catch { mixed q = Image.BMP._decode( data ); format = "Windows bitmap file"; i = q->image; a = q->alpha; }; #endif #if constant(Image.TGA) && constant(Image.TGA._decode) if(!i) catch { mixed q = Image.TGA._decode( data ); format = "Targa"; i = q->image; a = q->alpha; }; #endif #if constant(Image.PCX) && constant(Image.PCX._decode) if(!i) catch { mixed q = Image.PCX._decode( data ); format = "PCX"; i = q->image; a = q->alpha; }; #endif #if constant(Image.XBM) && constant(Image.XBM._decode) if(!i) catch { mixed q = Image.XBM._decode( data, (["bg":tocolor||({255,255,255}), "fg":invert_color(tocolor||({255,255,255})) ])); format = "XBM"; i = q->image; a = q->alpha; }; #endif #if constant(Image.XPM) && constant(Image.XPM._decode) if(!i) catch { mixed q = Image.XPM._decode( data ); format = "XPM"; i = q->image; a = q->alpha; }; #endif #if constant(Image.TIFF) && constant(Image.TIFF._decode) if(!i) catch { mixed q = Image.TIFF._decode( data ); format = "TIFF"; i = q->image; a = q->alpha; }; #endif #if constant(Image.ILBM) && constant(Image.ILBM._decode) if(!i) catch { mixed q = Image.ILBM._decode( data ); format = "ILBM"; i = q->image; a = q->alpha; }; #endif #if constant(Image.PS) && constant(Image.PS._decode) if(!i) catch { mixed q = Image.PS._decode( data ); format = "Postscript"; i = q->image; a = q->alpha; }; #endif #if constant(Image.XWD) && constant(Image.XWD.decode) if(!i) catch { i = Image.XWD.decode( data ); format = "XWD"; }; #endif #if constant(Image.HRZ) && constant(Image.HRZ._decode) if(!i) catch { mixed q = Image.HRZ._decode( data ); format = "HRZ"; i = q->image; a = q->alpha; }; #endif #if constant(Image.AVS) && constant(Image.AVS._decode) if(!i) catch { mixed q = Image.AVS._decode( data ); format = "AVS X"; i = q->image; a = q->alpha; }; #endif if(!i) catch{ i = Image.PNM.decode( data ); format = "PNM"; }; if(!i) // No image could be decoded at all. return 0; if( tocolor && i && a ) { object o = Image.image( i->xsize(), i->ysize(), @tocolor ); o->paste_mask( i,a ); i = o; } return ([ "format":format, "alpha":a, "img":i, ]); } mapping low_load_image(string f,object id) { string data; object file, img;
69a8691999-05-18Per Hedbor  if(id->misc->_load_image_called < 5) {
3ffe061999-05-16David Hedbor  // We were recursing very badly with the demo module here... id->misc->_load_image_called++;
c526921999-05-18Per Hedbor  if(!(data=id->conf->try_get_file(f, id))) {
3ffe061999-05-16David Hedbor  file=Stdio.File();
c526921999-05-18Per Hedbor  if(!file->open(f,"r") || !(data=file->read()))
3ffe061999-05-16David Hedbor  return 0; } } id->misc->_load_image_called = 0; if(!data) return 0;
a22f6f1999-05-12Per Hedbor  return low_decode_image( data ); } object load_image(string f,object id) { mapping q = low_load_image( f, id ); if( q ) return q->img; return 0; }
b1fca01996-11-12Per Hedbor // Somewhat misnamed, since there can be more then one // configuration-interface port nowdays. But, anyway, this function // opens and listens to all configuration interface ports. void initiate_configuration_port( int|void first ) { object o; array port;
4f4bc11998-02-04Per Hedbor  // Hm. if(!first && !config_ports_changed ) return 0; config_ports_changed = 0;
23620b1998-04-09Henrik Grubbström (Grubba)  // First find out if we have any new ports. mapping(string:array(string)) new_ports = ([]); foreach(QUERY(ConfigPorts), port) {
add03d1998-11-30Henrik Grubbström (Grubba)  if ((< "ssl", "ssleay", "ssl3" >)[port[1]]) {
72022f1998-07-07Henrik Grubbström (Grubba)  // Obsolete versions of the SSL protocol.
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_warning(LOCALE->obsolete_ssl(port[1]));
add03d1998-11-30Henrik Grubbström (Grubba)  port[1] = "https";
86a7891999-05-01Henrik Grubbström (Grubba)  } else if ((< "ftp2" >)[port[1]]) {
108b9f1999-05-01Henrik Grubbström (Grubba)  // ftp2.pike has replaced ftp.pike entirely.
0ad6061999-05-01Henrik Grubbström (Grubba)  report_warning(LOCALE->obsolete_ftp(port[1]));
86a7891999-05-01Henrik Grubbström (Grubba)  port[1] = "ftp";
72022f1998-07-07Henrik Grubbström (Grubba)  }
1c30601998-07-07Henrik Grubbström (Grubba)  string key = MKPORTKEY(port);
23620b1998-04-09Henrik Grubbström (Grubba)  if (!configuration_ports[key]) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_notice(LOCALE->new_config_port(key));
23620b1998-04-09Henrik Grubbström (Grubba)  new_ports[key] = port; } else { // This is needed not to delete old unchanged ports. new_ports[key] = 0; } } // Then disable the old ones that are no more. foreach(indices(configuration_ports), string key) { if (zero_type(new_ports[key])) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_notice(LOCALE->disable_config_port(key));
23620b1998-04-09Henrik Grubbström (Grubba)  object o = configuration_ports[key]; if (main_configuration_port == o) { main_configuration_port = 0; } m_delete(configuration_ports, key); mixed err; if (err = catch{ destruct(o); }) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_warning(LOCALE-> error_disabling_config_port(key, describe_backtrace(err)));
23620b1998-04-09Henrik Grubbström (Grubba)  } o = 0; // Be sure that there are no references left... } } // Now we can create the new ports. foreach(indices(new_ports), string key)
b1fca01996-11-12Per Hedbor  {
23620b1998-04-09Henrik Grubbström (Grubba)  port = new_ports[key]; if (port) {
39edcd1997-08-25Henrik Grubbström (Grubba)  array old = port; mixed erro; erro = catch { program requestprogram = (program)(getcwd()+"/protocols/"+port[1]); function rp; array tmp; if(!requestprogram) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_error(LOCALE->no_request_program(port[1]));
39edcd1997-08-25Henrik Grubbström (Grubba)  continue; } if(rp = requestprogram()->real_port) if(tmp = rp(port, 0)) port = tmp;
23620b1998-04-09Henrik Grubbström (Grubba)  // FIXME: For SSL3 we might need to be root to read our // secret files.
39edcd1997-08-25Henrik Grubbström (Grubba)  object privs; if(port[0] < 1024)
c31f7b1998-10-10Henrik Grubbström (Grubba)  privs = Privs(LOCALE->opening_low_port());
39edcd1997-08-25Henrik Grubbström (Grubba)  if(o=create_listen_socket(port[0],0,port[2],requestprogram,port)) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_notice(LOCALE->opening_config_port(key));
23620b1998-04-09Henrik Grubbström (Grubba)  if (!main_configuration_port) { main_configuration_port = o; } configuration_ports[key] = o;
39edcd1997-08-25Henrik Grubbström (Grubba)  } else {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_error(LOCALE->could_not_open_config_port(key));
39edcd1997-08-25Henrik Grubbström (Grubba)  } }; if (erro) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_error(LOCALE->open_config_port_failed(key,
23620b1998-04-09Henrik Grubbström (Grubba)  (stringp(erro)?erro:describe_backtrace(erro))));
b1fca01996-11-12Per Hedbor  } }
727f701998-06-13Henrik Grubbström (Grubba)  } if(!main_configuration_port) {
74dca61999-05-21Henrik Grubbström (Grubba)  if (sizeof(configuration_ports)) { // This case happens when you delete the main config port, // but still have others left.
10d1f21999-05-23Henrik Grubbström (Grubba)  main_configuration_port = values(configuration_ports)[0];
74dca61999-05-21Henrik Grubbström (Grubba)  } else { report_error(LOCALE->no_config_port()); if(first) exit( -1 ); // Restart. }
b1fca01996-11-12Per Hedbor  } }
e493e81997-07-11Per Hedbor #include <stat.h>
b1fca01996-11-12Per Hedbor // Find all modules, so a list of them can be presented to the // user. This is not needed when the server is started. void scan_module_dir(string d) {
6ad3161997-08-20Per Hedbor  if(sscanf(d, "%*s.pmod")!=0) return;
e493e81997-07-11Per Hedbor  MD_PERROR(("\n\nLooking for modules in "+d+" "));
01d0811996-11-12Mirar (Pontus Hagland)  string file,path=d; mixed err;
990b441997-07-18Per Hedbor  array q = (get_dir( d )||({})) - ({".","..","CVS","RCS" }); if(!sizeof(q)) {
3912ec1997-09-05Henrik Grubbström (Grubba)  MD_PERROR(("No modules in here. Continuing elsewhere\n"));
990b441997-07-18Per Hedbor  return; }
3bba751997-08-20Per Hedbor  if(search(q, ".no_modules")!=-1) {
3912ec1997-09-05Henrik Grubbström (Grubba)  MD_PERROR(("No modules in here. Continuing elsewhere\n"));
3bba751997-08-20Per Hedbor  return; }
e493e81997-07-11Per Hedbor  MD_PERROR(("There are "+language("en","number")(sizeof(q))+" files.\n"));
01d0811996-11-12Mirar (Pontus Hagland) 
e493e81997-07-11Per Hedbor  foreach( q, file )
b1fca01996-11-12Per Hedbor  {
b796b51998-11-18Per Hedbor  object e = ErrorContainer(); master()->set_inhibit_compile_errors(e->got_error);
fd0b6f1996-12-02Per Hedbor  if ( file[0]!='.' && !backup_extension(file) && (file[-1]!='z'))
b1fca01996-11-12Per Hedbor  {
e493e81997-07-11Per Hedbor  array stat = file_stat(path+file); if(!stat || (stat[ST_SIZE] < 0))
5e4ede1996-11-12Per Hedbor  {
e493e81997-07-11Per Hedbor  if(err = catch ( scan_module_dir(path+file+"/") )) MD_PERROR((sprintf("Error in module rescanning directory code:" " %s\n",describe_backtrace(err))));
0f8c871997-06-01Henrik Grubbström (Grubba)  } else {
e493e81997-07-11Per Hedbor  MD_PERROR(("Considering "+file+" - ")); if((module_stat_cache[path+file] && module_stat_cache[path+file][ST_MTIME])==stat[ST_MTIME]) { MD_PERROR(("Already tried this one.\n")); continue; } module_stat_cache[path+file]=stat;
28d38d1997-03-12Per Hedbor  switch(extension(file)) {
0f8c871997-06-01Henrik Grubbström (Grubba)  case "pike": case "lpc":
dd47911997-04-12Per Hedbor  if(catch{
0f8c871997-06-01Henrik Grubbström (Grubba)  if((open(path+file,"r")->read(4))=="#!NO") {
e382ed1997-07-16Henrik Grubbström (Grubba)  MD_PERROR(("Not a module\n"));
e493e81997-07-11Per Hedbor  file=0;
28d38d1997-03-12Per Hedbor  }
0f8c871997-06-01Henrik Grubbström (Grubba)  }) {
f1cee51997-06-06Henrik Grubbström (Grubba)  MD_PERROR(("Couldn't open file\n"));
e493e81997-07-11Per Hedbor  file=0;
0f8c871997-06-01Henrik Grubbström (Grubba)  }
e493e81997-07-11Per Hedbor  if(!file) break;
0f8c871997-06-01Henrik Grubbström (Grubba)  case "mod": case "so":
28d38d1997-03-12Per Hedbor  string *module_info;
add03d1998-11-30Henrik Grubbström (Grubba)  if (!(err = catch( module_info = compile_module(path + file)))) {
0f8c871997-06-01Henrik Grubbström (Grubba)  // Load OK
566fdf1998-08-18Henrik Grubbström (Grubba)  if (module_info) { // Module load OK. allmodules[ file-("."+extension(file)) ] = module_info; } else { // Disabled module.
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_notice(LOCALE->disabled_module(path+file));
566fdf1998-08-18Henrik Grubbström (Grubba)  }
28d38d1997-03-12Per Hedbor  } else {
0f8c871997-06-01Henrik Grubbström (Grubba)  // Load failed.
9b9f701997-08-12Per Hedbor  module_stat_cache[path+file]=0;
b796b51998-11-18Per Hedbor  e->errors += "\n"; // if (arrayp(err)) { // e->errors += path + file + ":" +describe_backtrace(err) + "\n"; // } else { // _master->errors += path + file + ": " + err; // }
28d38d1997-03-12Per Hedbor  }
b1fca01996-11-12Per Hedbor  }
0f8c871997-06-01Henrik Grubbström (Grubba)  MD_PERROR(("\n"));
b1fca01996-11-12Per Hedbor  } }
b796b51998-11-18Per Hedbor  master()->set_inhibit_compile_errors(0); if(strlen(e->get())) { report_debug(LOCALE->module_compilation_errors(d, e->get()));
0f8c871997-06-01Henrik Grubbström (Grubba)  }
b1fca01996-11-12Per Hedbor  } } void rescan_modules() { string file, path; mixed err;
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_notice(LOCALE->scanning_for_modules());
e382ed1997-07-16Henrik Grubbström (Grubba)  if (!allmodules) { allmodules=copy_value(somemodules); }
0f8c871997-06-01Henrik Grubbström (Grubba) 
b1fca01996-11-12Per Hedbor  foreach(QUERY(ModuleDirs), path) {
dd47911997-04-12Per Hedbor  array err; err = catch(scan_module_dir( path ));
6193221997-05-31Henrik Grubbström (Grubba)  if(err) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_error(LOCALE->module_scan_error(path, describe_backtrace(err)));
6193221997-05-31Henrik Grubbström (Grubba)  } }
9b9f701997-08-12Per Hedbor  catch { rm(".module_stat_cache"); rm(".allmodules"); Stdio.write_file(".module_stat_cache", encode_value(module_stat_cache)); Stdio.write_file(".allmodules", encode_value(allmodules)); };
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_notice(LOCALE->module_scan_done(sizeof(allmodules)));
b1fca01996-11-12Per Hedbor } // do the chroot() call. This is not currently recommended, since // roxen dynamically loads modules, all module files must be // available at the new location. private void fix_root(string to) {
c79b261998-02-05Johan Schön #ifndef __NT__
b1fca01996-11-12Per Hedbor  if(getuid()) { perror("It is impossible to chroot() if the server is not run as root.\n"); return; } if(!chroot(to)) { perror("Roxen: Cannot chroot to "+to+": "); #if efun(real_perror) real_perror(); #endif return; } perror("Root is now "+to+".\n");
c79b261998-02-05Johan Schön #endif
b1fca01996-11-12Per Hedbor } void create_pid_file(string where) {
c79b261998-02-05Johan Schön #ifndef __NT__
b1fca01996-11-12Per Hedbor  if(!where) return; where = replace(where, ({ "$pid", "$uid" }), ({ (string)getpid(), (string)getuid() })); rm(where);
5e89211997-02-13Per Hedbor  if(catch(Stdio.write_file(where, sprintf("%d\n%d", getpid(), getppid()))))
b1fca01996-11-12Per Hedbor  perror("I cannot create the pid file ("+where+").\n");
c79b261998-02-05Johan Schön #endif
b1fca01996-11-12Per Hedbor }
6ca8f61998-10-13Per Hedbor 
ccffe71999-03-05Wilhelm Köhler object shuffle(object from, object to, object|void to2, function(:void)|void callback)
14179b1997-01-29Per Hedbor {
beaca01998-02-20Per Hedbor #if efun(spider.shuffle) if(!to2)
5bc1991997-01-29Per Hedbor  {
beaca01998-02-20Per Hedbor  object p = pipe(); p->input(from);
95e2b41997-05-25Wilhelm Köhler  p->set_done_callback(callback);
beaca01998-02-20Per Hedbor  p->output(to);
ccffe71999-03-05Wilhelm Köhler  return p;
a60c4c1997-07-03Henrik Grubbström (Grubba)  } else {
beaca01998-02-20Per Hedbor #endif // 'smartpipe' does not support multiple outputs. object p = Pipe.pipe(); if (callback) p->set_done_callback(callback); p->output(to);
33b7701998-03-20Johan Schön  if(to2) p->output(to2);
beaca01998-02-20Per Hedbor  p->input(from);
ccffe71999-03-05Wilhelm Köhler  return p;
beaca01998-02-20Per Hedbor #if efun(spider.shuffle)
b1fca01996-11-12Per Hedbor  }
beaca01998-02-20Per Hedbor #endif
b1fca01996-11-12Per Hedbor }
3aaaa71997-06-12Wilhelm Köhler 
4f4bc11998-02-04Per Hedbor 
b1fca01996-11-12Per Hedbor static private int _recurse;
a9d8111998-09-01Henrik Grubbström (Grubba) // FIXME: Ought to use the shutdown code.
b1fca01996-11-12Per Hedbor void exit_when_done() { object o; int i;
c31f7b1998-10-10Henrik Grubbström (Grubba)  roxen_perror("Interrupt request received. Exiting,\n");
4f4bc11998-02-04Per Hedbor  die_die_die=1; // trace(9);
b1fca01996-11-12Per Hedbor  if(++_recurse > 4)
38dca81996-12-10Per Hedbor  {
dca5dc1998-09-12Per Hedbor  roxen_perror("Exiting roxen (spurious signals received).\n");
38dca81996-12-10Per Hedbor  stop_all_modules();
1f79ba1998-09-01Marcus Comstedt #ifdef THREADS stop_handler_threads(); #endif /* THREADS */
3835ca1998-01-16Henrik Grubbström (Grubba)  exit(-1); // Restart.
38dca81996-12-10Per Hedbor  }
14179b1997-01-29Per Hedbor 
b1fca01996-11-12Per Hedbor  // First kill off all listening sockets..
4f4bc11998-02-04Per Hedbor  foreach(indices(portno)||({}), o) { #ifdef THREADS
bfbd4d1998-04-07Henrik Grubbström (Grubba)  object fd = Stdio.File();
4f4bc11998-02-04Per Hedbor  fd->connect( portno[o][2]!="Any"?portno[o][2]:"127.0.0.1", portno[o][0] ); destruct(fd); #endif
6ca8f61998-10-13Per Hedbor  destruct(o);
4f4bc11998-02-04Per Hedbor  }
b1fca01996-11-12Per Hedbor  // Then wait for all sockets, but maximum 10 minutes.. call_out(lambda() {
aaef2a1997-03-02Henrik Grubbström (Grubba)  call_out(Simulate.this_function(), 5);
38dca81996-12-10Per Hedbor  if(!_pipe_debug()[0]) {
dca5dc1998-09-12Per Hedbor  roxen_perror("Exiting roxen (all connections closed).\n");
38dca81996-12-10Per Hedbor  stop_all_modules();
1f79ba1998-09-01Marcus Comstedt #ifdef THREADS stop_handler_threads(); #endif /* THREADS */
f6f0921998-05-08Henrik Grubbström (Grubba)  add_constant("roxen", 0); // Paranoia...
3835ca1998-01-16Henrik Grubbström (Grubba)  exit(-1); // Restart.
c31f7b1998-10-10Henrik Grubbström (Grubba)  roxen_perror("Odd. I am not dead yet.\n");
38dca81996-12-10Per Hedbor  } }, 0.1); call_out(lambda(){
dca5dc1998-09-12Per Hedbor  roxen_perror("Exiting roxen (timeout).\n");
38dca81996-12-10Per Hedbor  stop_all_modules();
1f79ba1998-09-01Marcus Comstedt #ifdef THREADS stop_handler_threads(); #endif /* THREADS */
f6f0921998-05-08Henrik Grubbström (Grubba)  add_constant("roxen", 0); // Paranoia...
a9d8111998-09-01Henrik Grubbström (Grubba)  exit(-1); // Restart.
38dca81996-12-10Per Hedbor  }, 600, 0); // Slow buggers..
b1fca01996-11-12Per Hedbor } void exit_it() { perror("Recursive signals.\n");
3835ca1998-01-16Henrik Grubbström (Grubba)  exit(-1); // Restart.
b1fca01996-11-12Per Hedbor }
7c92b91998-11-19Per Hedbor void set_locale( string to ) { if( to == "standard" ) SET_LOCALE( default_locale ); SET_LOCALE( Locale.Roxen[ to ] || default_locale ); }
a22f6f1999-05-12Per Hedbor 
1c9d081999-07-19Henrik Grubbström (Grubba) // Dump all threads to the debug log. void describe_all_threads() { array(mixed) all_backtraces; #if constant(all_threads) all_backtraces = all_threads()->backtrace(); #else /* !constant(all_threads) */ all_backtraces = ({ backtrace() }); #endif /* constant(all_threads) */ werror("Describing all threads:\n"); int i; for(i=0; i < sizeof(all_backtraces); i++) { werror(sprintf("Thread %d:\n" "%s\n", i+1, describe_backtrace(all_backtraces[i]))); } }
753a831999-08-30Per Hedbor  void dump( string file ) { program p = master()->programs[ replace(getcwd() +"/"+ file , "//", "/" ) ]; array q; if(!p)
2ff8461999-09-02Per Hedbor  { #ifdef DUMP_DEBUG report_debug(file+" not loaded, and thus cannot be dumped.\n"); #endif
753a831999-08-30Per Hedbor  return;
2ff8461999-09-02Per Hedbor  }
753a831999-08-30Per Hedbor  if(!file_stat( file+".o" ) || (file_stat(file+".o")[ST_MTIME] < file_stat(file)[ST_MTIME]))
2ff8461999-09-02Per Hedbor  {
753a831999-08-30Per Hedbor  if(q=catch{ Stdio.write_file(file+".o",encode_value(p,Codec(p)));
2ff8461999-09-02Per Hedbor #ifdef DUMP_DEBUG b report_debug( file+" dumped successfully to "+file+".o\n" ); #endif
753a831999-08-30Per Hedbor  })
2ff8461999-09-02Per Hedbor  report_debug("** Cannot encode "+file+": "+describe_backtrace(q)+"\n"); } #ifdef DUMP_DEBUG else report_debug(file+" already dumped (and up to date)\n"); #endif
753a831999-08-30Per Hedbor }
b1fca01996-11-12Per Hedbor // And then we have the main function, this is the oldest function in // Roxen :) It has not changed all that much since Spider 2.0.
c856841998-01-21Henrik Grubbström (Grubba) int main(int|void argc, array (string)|void argv)
b1fca01996-11-12Per Hedbor {
753a831999-08-30Per Hedbor // dump( "base_server/disk_cache.pike"); // cannot encode this one yet... call_out( lambda() { dump( "base_server/state.pike" );
2ff8461999-09-02Per Hedbor  dump( "base_server/persistent.pike"); dump( "base_server/restorable.pike"); dump( "base_server/highlight_pike.pike"); dump( "base_server/dates.pike");
753a831999-08-30Per Hedbor  dump( "base_server/wizard.pike" ); dump( "base_server/proxyauth.pike" ); dump( "base_server/html.pike" ); dump( "base_server/module.pike" ); }, 9); switch(getenv("LANG")) { case "sv": default_locale = Locale.Roxen["svenska"]; break; case "jp": default_locale = Locale.Roxen["nihongo"]; break; }
59912f1998-10-15Henrik Grubbström (Grubba)  SET_LOCALE(default_locale);
c245691997-10-25Per Hedbor  initiate_languages();
b1fca01996-11-12Per Hedbor  mixed tmp;
2ff8461999-09-02Per Hedbor 
b1fca01996-11-12Per Hedbor  mark_fd(0, "Stdin"); mark_fd(1, "Stdout"); mark_fd(2, "Stderr");
51643e1997-08-21Per Hedbor  configuration_dir =
6ca8f61998-10-13Per Hedbor  Getopt.find_option(argv, "d",({"config-dir","configuration-directory" }),
51643e1997-08-21Per Hedbor  ({ "ROXEN_CONFIGDIR", "CONFIGURATIONS" }), "../configurations");
b1fca01996-11-12Per Hedbor 
a92b951997-08-05Martin Stjernholm  if(configuration_dir[-1] != '/')
b1fca01996-11-12Per Hedbor  configuration_dir += "/";
0978ee1997-09-08David Hedbor  startpid = getppid();
e145221997-10-10David Hedbor  roxenpid = getpid();
b1fca01996-11-12Per Hedbor 
14179b1997-01-29Per Hedbor  // Dangerous...
6ca8f61998-10-13Per Hedbor  if(tmp = Getopt.find_option(argv, "r", "root")) fix_root(tmp);
b1fca01996-11-12Per Hedbor  argv -= ({ 0 });
51643e1997-08-21Per Hedbor  argc = sizeof(argv);
b1fca01996-11-12Per Hedbor  define_global_variables(argc, argv);
b796b51998-11-18Per Hedbor  object o; if(QUERY(locale) != "standard" && (o = Locale.Roxen[QUERY(locale)])) { default_locale = o; SET_LOCALE(default_locale); } report_notice(LOCALE->starting_roxen());
6420831997-08-21Per Hedbor 
b1fca01996-11-12Per Hedbor #if efun(syslog) init_logger(); #endif init_garber(); initiate_supports();
0f28da1997-08-13Per Hedbor  initiate_configuration_port( 1 );
b84a161999-06-07Martin Stjernholm  enable_configurations(); set_u_and_gid(); // Running with the right uid:gid from this point on. create_pid_file(Getopt.find_option(argv, "p", "pid-file", "ROXEN_PID_FILE") || QUERY(pidfile));
a22f6f1999-05-12Per Hedbor  roxen_perror("Initiating argument cache ... "); int id; string cp = QUERY(argument_cache_dir), na = "args"; if( QUERY(argument_cache_in_db) ) { id = 1; cp = QUERY(argument_cache_db_path); na = "argumentcache"; } mixed e; e = catch( argcache = ArgCache(na,cp,id) ); if( e ) { report_error( "Failed to initialize the global argument cache:\n" + (describe_backtrace( e )/"\n")[0]+"\n"); werror( describe_backtrace( e ) ); } roxen_perror( "\n" );
acd9e71999-05-12Per Hedbor  enable_configurations_modules();
b1fca01996-11-12Per Hedbor  call_out(update_supports_from_roxen_com, QUERY(next_supports_update)-time());
48fa361997-04-05Per Hedbor #ifdef THREADS start_handler_threads();
4f4bc11998-02-04Per Hedbor  catch( this_thread()->set_name("Backend") );
34d3fa1999-04-22Per Hedbor  backend_thread = this_thread();
3aaaa71997-06-12Wilhelm Köhler #if efun(thread_set_concurrency)
41d0f91998-02-20Per Hedbor  thread_set_concurrency(QUERY(numthreads)+1);
48fa361997-04-05Per Hedbor #endif
4f4bc11998-02-04Per Hedbor 
34fbbc1998-02-05Henrik Grubbström (Grubba) #endif /* THREADS */
990cbb1997-08-12David Hedbor 
3835ca1998-01-16Henrik Grubbström (Grubba)  // Signals which cause a restart (exitcode != 0)
297dd91999-08-12Henrik Grubbström (Grubba)  foreach( ({ "SIGINT" }), string sig) {
9b9f701997-08-12Per Hedbor  catch { signal(signum(sig), exit_when_done); };
b399651997-07-22Henrik Grubbström (Grubba)  }
65924d1998-07-24David Hedbor  catch { signal(signum("SIGHUP"), reload_all_configurations); };
3835ca1998-01-16Henrik Grubbström (Grubba)  // Signals which cause a shutdown (exitcode == 0)
297dd91999-08-12Henrik Grubbström (Grubba)  foreach( ({ "SIGTERM" }), string sig) {
a9d8111998-09-01Henrik Grubbström (Grubba)  catch { signal(signum(sig), shutdown); };
3835ca1998-01-16Henrik Grubbström (Grubba)  }
1c9d081999-07-19Henrik Grubbström (Grubba)  // Signals which cause Roxen to dump the thread state foreach( ({ "SIGUSR1", "SIGUSR2", "SIGTRAP" }), string sig) { catch { signal(signum(sig), describe_all_threads); }; }
0f28da1997-08-13Per Hedbor 
c31f7b1998-10-10Henrik Grubbström (Grubba)  report_notice(LOCALE->roxen_started(time()-start_time));
4f4bc11998-02-04Per Hedbor #ifdef __RUN_TRACE trace(1); #endif
08152b1998-04-24Per Hedbor  start_time=time(); // Used by the "uptime" info later on.
b1fca01996-11-12Per Hedbor  return -1; }
7a61de1998-03-26Per Hedbor  string diagnose_error(array from) { }
edc9af1998-07-11David Hedbor  // Called from the configuration interface.
ee8b201998-07-13David Hedbor string check_variable(string name, mixed value)
edc9af1998-07-11David Hedbor { switch(name) { case "ConfigPorts":
ee8b201998-07-13David Hedbor  config_ports_changed = 1; break;
edc9af1998-07-11David Hedbor  case "cachedir": if(!sscanf(value, "%*s/roxen_cache")) {
c31f7b1998-10-10Henrik Grubbström (Grubba)  // FIXME: LOCALE?
753a831999-08-30Per Hedbor  // We will skip this soon anyway.... // object node; // node = (configuration_interface()->root->descend("Globals", 1)-> // descend("Proxy disk cache: Base Cache Dir", 1)); // if(node && !node->changed) node->change(1);
edc9af1998-07-11David Hedbor  mkdirhier(value+"roxen_cache/foo"); call_out(set, 0, "cachedir", value+"roxen_cache/"); } break; case "ConfigurationURL": case "MyWorldLocation": if(strlen(value)<7 || value[-1] != '/' || !(sscanf(value,"%*s://%*s/")==2))
c31f7b1998-10-10Henrik Grubbström (Grubba)  return(LOCALE->url_format());
ee8b201998-07-13David Hedbor  break; case "abs_engage": if (value) restart_if_stuck(1); else remove_call_out(restart_if_stuck); break; case "suicide_engage": if (value) call_out(restart,60*60*24*QUERY(suicide_timeout)); else remove_call_out(restart); break;
b796b51998-11-18Per Hedbor  case "locale": object o; if(value != "standard" && (o = Locale.Roxen[value])) { default_locale = o; SET_LOCALE(default_locale); if(root) {
753a831999-08-30Per Hedbor  root->clear();
c3a53d1999-06-25Per Hedbor // destruct(root);
b796b51998-11-18Per Hedbor // configuration_interface()->root = configuration_interface()->Node(); configuration_interface()-> build_root(configuration_interface()->root); } } break;
edc9af1998-07-11David Hedbor  } }
9fd61c1999-02-16Per Hedbor  mapping config_cache = ([ ]); mapping host_accuracy_cache = ([]); int is_ip(string s) {
753a831999-08-30Per Hedbor  return (replace(s,"0123456789."/"",({""})*11) == "");
9fd61c1999-02-16Per Hedbor }
57e9621999-03-01Marcus Comstedt object find_server_for(object id, string host, string|void port)
9fd61c1999-02-16Per Hedbor { object old_conf = id->conf;
57e9621999-03-01Marcus Comstedt  int portno = ((int)port);
75cdcb1999-03-29Henrik Grubbström (Grubba) #ifdef REQUEST_DEBUG werror(sprintf("ROXEN: find_server_for(%O, %O, %O)\n", id, host, port)); #endif /* REQUEST_DEBUG */
57e9621999-03-01Marcus Comstedt  if(portno!=0 && portno!=21 && portno!=80 && portno!=443) // Not likely to be anything else than the current virtual server. return id->conf;
9fd61c1999-02-16Per Hedbor  host = lower_case(host); if(config_cache[host]) { id->conf=config_cache[host]; } else { if (is_ip(host)) { // Not likely to be anything else than the current virtual server. config_cache[host] = id->conf; return (id->conf); } int best; object c; string hn;
75cdcb1999-03-29Henrik Grubbström (Grubba) #if !constant(String.fuzzymatch) && constant(Array.diff_longest_sequence)
9fd61c1999-02-16Per Hedbor  array a = host/"";
75cdcb1999-03-29Henrik Grubbström (Grubba) #endif /* !String.fuzzymatch && Array.diff_longest_sequence */
9fd61c1999-02-16Per Hedbor  foreach(configurations, object s) { string h = lower_case(s->query("MyWorldLocation")); // Remove http:// et al here... // Would get interresting correlation problems with the "http" otherwise. int i = search(h, "://"); if (i != -1) { h = h[i+3..]; }
75cdcb1999-03-29Henrik Grubbström (Grubba)  if ((i = search(h, "/")) != -1) { h = h[..i-1]; }
9fd61c1999-02-16Per Hedbor 
75cdcb1999-03-29Henrik Grubbström (Grubba) #if constant(String.fuzzymatch) int corr = String.fuzzymatch(host, h); #elif constant(Array.diff_longest_sequence) int corr = sizeof(Array.diff_longest_sequence(a, h/"")); #endif /* constant(Array.diff_longest_sequence) */ /* The idea of the algorithm is to find the server-url with the longest * common sequence of characters with the host-string, and among those * with the same correlation take the one which is shortest (ie least * amount to throw away). */
9fd61c1999-02-16Per Hedbor  if ((corr > best) || ((corr == best) && hn && (sizeof(hn) > sizeof(h)))) { /* Either better correlation, * or the same, but a shorter hostname. */ best = corr; c = s; hn = h; } }
75cdcb1999-03-29Henrik Grubbström (Grubba)  #if !constant(String.fuzzymatch) && constant(Array.diff_longest_sequence)
9fd61c1999-02-16Per Hedbor  // Minmatch should be counted in percent best=best*100/strlen(host);
75cdcb1999-03-29Henrik Grubbström (Grubba) #endif /* !String.fuzzymatch && Array.diff_longest_sequence */
6000961999-04-24Henrik Grubbström (Grubba)  if(best >= 50 /* QUERY(minmatch) */)
9fd61c1999-02-16Per Hedbor  id->conf = config_cache[host] = (c || id->conf); else config_cache[host] = id->conf; host_accuracy_cache[host] = best; }
a22f6f1999-05-12Per Hedbor  if (id->conf != old_conf) {
9fd61c1999-02-16Per Hedbor  /* Need to re-authenticate with the new server */ if (id->rawauth) { array(string) y = id->rawauth / " "; id->realauth = 0; id->auth = 0; if (sizeof(y) >= 2) { y[1] = MIME.decode_base64(y[1]); id->realauth = y[1]; if (id->conf && id->conf->auth_module) { y = id->conf->auth_module->auth(y, id); } id->auth = y; } } } return id->conf; }
3cf0aa1999-07-31Johan Schön object find_site_for( object id )
9fd61c1999-02-16Per Hedbor { if(id->misc->host)
3cf0aa1999-07-31Johan Schön  return find_server_for(id,@lower_case(id->misc->host)/":"); else return id->conf;
9fd61c1999-02-16Per Hedbor }