Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
bb_http.c File Reference
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include "gwlib/gwlib.h"
#include "bearerbox.h"

Go to the source code of this file.

Data Structures

struct  httpd_command
 

Functions

static Octstrhttpd_check_authorization (List *cgivars, int status)
 
static Octstrhttpd_check_status (void)
 
static Octstrhttpd_status (List *cgivars, int status_type)
 
static Octstrhttpd_store_status (List *cgivars, int status_type)
 
static Octstrhttpd_loglevel (List *cgivars, int status_type)
 
static Octstrhttpd_shutdown (List *cgivars, int status_type)
 
static Octstrhttpd_isolate (List *cgivars, int status_type)
 
static Octstrhttpd_suspend (List *cgivars, int status_type)
 
static Octstrhttpd_resume (List *cgivars, int status_type)
 
static Octstrhttpd_restart (List *cgivars, int status_type)
 
static Octstrhttpd_graceful_restart (List *cgivars, int status_type)
 
static Octstrhttpd_flush_dlr (List *cgivars, int status_type)
 
static Octstrhttpd_stop_smsc (List *cgivars, int status_type)
 
static Octstrhttpd_remove_smsc (List *cgivars, int status_type)
 
static Octstrhttpd_add_smsc (List *cgivars, int status_type)
 
static Octstrhttpd_restart_smsc (List *cgivars, int status_type)
 
static Octstrhttpd_reload_lists (List *cgivars, int status_type)
 
static Octstrhttpd_remove_message (List *cgivars, int status_type)
 
static void httpd_serve (HTTPClient *client, Octstr *ourl, List *headers, Octstr *body, List *cgivars)
 
static void httpadmin_run (void *arg)
 
int httpadmin_start (Cfg *cfg)
 
void httpadmin_stop (void)
 

Variables

volatile sig_atomic_t bb_status
 
static volatile sig_atomic_t httpadmin_running
 
static long ha_port
 
static Octstrha_interface
 
static Octstrha_password
 
static Octstrha_status_pw
 
static Octstrha_allow_ip
 
static Octstrha_deny_ip
 
static struct httpd_command httpd_commands []
 

Function Documentation

static void httpadmin_run ( void *  arg)
static

Definition at line 510 of file bb_http.c.

References BB_DEAD, BB_SHUTDOWN, bb_shutdown(), bb_status, client(), ha_port, http_accept_request(), http_close_client(), httpadmin_running, httpd_serve(), info(), is_allowed_ip(), octstr_destroy(), octstr_get_cstr, and url.

Referenced by httpadmin_start(), and httpadmin_stop().

511 {
513  Octstr *ip, *url, *body;
514  List *headers, *cgivars;
515 
516  while(bb_status != BB_DEAD) {
517  if (bb_status == BB_SHUTDOWN)
518  bb_shutdown();
519  client = http_accept_request(ha_port, &ip, &url, &headers, &body,
520  &cgivars);
521  if (client == NULL)
522  break;
523  if (is_allowed_ip(ha_allow_ip, ha_deny_ip, ip) == 0) {
524  info(0, "HTTP admin tried from denied host <%s>, disconnected",
525  octstr_get_cstr(ip));
526  http_close_client(client);
527  continue;
528  }
529  httpd_serve(client, url, headers, body, cgivars);
530  octstr_destroy(ip);
531  }
532 
533  httpadmin_running = 0;
534 }
void info(int err, const char *fmt,...)
Definition: log.c:636
static long ha_port
Definition: bb_http.c:82
static Octstr * ha_deny_ip
Definition: bb_http.c:87
void http_close_client(HTTPClient *client)
Definition: http.c:2735
static void client(int port)
Definition: test_udp.c:77
static Octstr * ha_allow_ip
Definition: bb_http.c:86
static volatile sig_atomic_t httpadmin_running
Definition: bb_http.c:80
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
int is_allowed_ip(Octstr *allow_ip, Octstr *deny_ip, Octstr *ip)
Definition: utils.c:815
HTTPClient * http_accept_request(int port, Octstr **client_ip, Octstr **url, List **headers, Octstr **body, List **cgivars)
Definition: http.c:2574
volatile sig_atomic_t bb_status
Definition: bearerbox.c:132
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
static void httpd_serve(HTTPClient *client, Octstr *ourl, List *headers, Octstr *body, List *cgivars)
Definition: bb_http.c:394
Definition: octstr.c:118
int bb_shutdown(void)
Definition: bearerbox.c:832
static Octstr * url
Definition: test_xmlrpc.c:84
Definition: list.c:102
int httpadmin_start ( Cfg cfg)

Definition at line 542 of file bb_http.c.

References cfg_get, cfg_get_bool(), cfg_get_integer(), cfg_get_single_group(), gwthread_create, ha_port, http_open_port_if(), httpadmin_run(), httpadmin_running, octstr_destroy(), octstr_imm(), panic, and ssl.

Referenced by init_bearerbox().

543 {
544  CfgGroup *grp;
545  int ssl = 0;
546 #ifdef HAVE_LIBSSL
547  Octstr *ssl_server_cert_file;
548  Octstr *ssl_server_key_file;
549 #endif /* HAVE_LIBSSL */
550 
551  if (httpadmin_running) return -1;
552 
553 
554  grp = cfg_get_single_group(cfg, octstr_imm("core"));
555  if (cfg_get_integer(&ha_port, grp, octstr_imm("admin-port")) == -1)
556  panic(0, "Missing admin-port variable, cannot start HTTP admin");
557 
558  ha_interface = cfg_get(grp, octstr_imm("admin-interface"));
559  ha_password = cfg_get(grp, octstr_imm("admin-password"));
560  if (ha_password == NULL)
561  panic(0, "You MUST set HTTP admin-password");
562 
563  ha_status_pw = cfg_get(grp, octstr_imm("status-password"));
564 
565  ha_allow_ip = cfg_get(grp, octstr_imm("admin-allow-ip"));
566  ha_deny_ip = cfg_get(grp, octstr_imm("admin-deny-ip"));
567 
568 #ifdef HAVE_LIBSSL
569  cfg_get_bool(&ssl, grp, octstr_imm("admin-port-ssl"));
570 
571  /*
572  * check if SSL is desired for HTTP servers and then
573  * load SSL client and SSL server public certificates
574  * and private keys
575  */
576  ssl_server_cert_file = cfg_get(grp, octstr_imm("ssl-server-cert-file"));
577  ssl_server_key_file = cfg_get(grp, octstr_imm("ssl-server-key-file"));
578  if (ssl_server_cert_file != NULL && ssl_server_key_file != NULL) {
579  /* we are fine here, the following call is now in conn_config_ssl(),
580  * so there is no reason to do this twice.
581 
582  use_global_server_certkey_file(ssl_server_cert_file,
583  ssl_server_key_file);
584  */
585  } else if (ssl) {
586  panic(0, "You MUST specify cert and key files within core group for SSL-enabled HTTP servers!");
587  }
588 
589  octstr_destroy(ssl_server_cert_file);
590  octstr_destroy(ssl_server_key_file);
591 #endif /* HAVE_LIBSSL */
592 
594 
595  if (gwthread_create(httpadmin_run, NULL) == -1)
596  panic(0, "Failed to start a new thread for HTTP admin");
597 
598  httpadmin_running = 1;
599  return 0;
600 }
static long ha_port
Definition: bb_http.c:82
static Octstr * ha_deny_ip
Definition: bb_http.c:87
int ssl
static Octstr * ha_allow_ip
Definition: bb_http.c:86
static Octstr * ha_status_pw
Definition: bb_http.c:85
#define cfg_get(grp, varname)
Definition: cfg.h:86
static volatile sig_atomic_t httpadmin_running
Definition: bb_http.c:80
static Octstr * ha_password
Definition: bb_http.c:84
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define gwthread_create(func, arg)
Definition: gwthread.h:90
static Octstr * ha_interface
Definition: bb_http.c:83
int http_open_port_if(int port, int ssl, Octstr *interface)
Definition: http.c:2472
int cfg_get_bool(int *n, CfgGroup *grp, Octstr *varname)
Definition: cfg.c:756
Definition: octstr.c:118
int cfg_get_integer(long *n, CfgGroup *grp, Octstr *varname)
Definition: cfg.c:739
#define panic
Definition: log.h:87
Definition: cfg.c:73
CfgGroup * cfg_get_single_group(Cfg *cfg, Octstr *name)
Definition: cfg.c:636
static void httpadmin_run(void *arg)
Definition: bb_http.c:510
void httpadmin_stop ( void  )

Definition at line 603 of file bb_http.c.

References gwthread_join_every(), http_close_all_ports(), httpadmin_run(), and octstr_destroy().

Referenced by main().

604 {
612  ha_password = NULL;
613  ha_status_pw = NULL;
614  ha_allow_ip = NULL;
615  ha_deny_ip = NULL;
616 }
static Octstr * ha_deny_ip
Definition: bb_http.c:87
static Octstr * ha_allow_ip
Definition: bb_http.c:86
static Octstr * ha_status_pw
Definition: bb_http.c:85
void gwthread_join_every(gwthread_func_t *func)
static Octstr * ha_password
Definition: bb_http.c:84
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
static Octstr * ha_interface
Definition: bb_http.c:83
void http_close_all_ports(void)
Definition: http.c:2515
static void httpadmin_run(void *arg)
Definition: bb_http.c:510
static Octstr* httpd_add_smsc ( List cgivars,
int  status_type 
)
static

Definition at line 302 of file bb_http.c.

References bb_add_smsc(), http_cgi_variable(), httpd_check_authorization(), httpd_check_status(), octstr_create, octstr_format(), octstr_get_cstr, and reply().

303 {
304  Octstr *reply;
305  Octstr *smsc;
306  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
307  if ((reply = httpd_check_status())!= NULL) return reply;
308 
309  /* check if the smsc id is given */
310  smsc = http_cgi_variable(cgivars, "smsc");
311  if (smsc) {
312  if (bb_add_smsc(smsc) == -1)
313  return octstr_format("Could not add smsc-id `%s'", octstr_get_cstr(smsc));
314  else
315  return octstr_format("SMSC `%s' added", octstr_get_cstr(smsc));
316  } else
317  return octstr_create("SMSC id not given");
318 }
int bb_add_smsc(Octstr *id)
Definition: bearerbox.c:938
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2813
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_check_authorization ( List cgivars,
int  status 
)
static

Definition at line 98 of file bb_http.c.

References gwthread_sleep(), http_cgi_variable(), octstr_compare(), octstr_create, and password.

Referenced by httpd_add_smsc(), httpd_flush_dlr(), httpd_graceful_restart(), httpd_isolate(), httpd_loglevel(), httpd_reload_lists(), httpd_remove_message(), httpd_remove_smsc(), httpd_restart(), httpd_restart_smsc(), httpd_resume(), httpd_shutdown(), httpd_status(), httpd_stop_smsc(), httpd_store_status(), and httpd_suspend().

99 {
100  Octstr *password;
101  static double sleep = 0.01;
102 
103  password = http_cgi_variable(cgivars, "password");
104 
105  if (status) {
106  if (ha_status_pw == NULL)
107  return NULL;
108 
109  if (password == NULL)
110  goto denied;
111 
112  if (octstr_compare(password, ha_password)!=0
113  && octstr_compare(password, ha_status_pw)!=0)
114  goto denied;
115  }
116  else {
117  if (password == NULL || octstr_compare(password, ha_password)!=0)
118  goto denied;
119  }
120  sleep = 0.0;
121  return NULL; /* allowed */
122 denied:
123  gwthread_sleep(sleep);
124  sleep += 1.0; /* little protection against brute force
125  * password cracking */
126  return octstr_create("Denied");
127 }
static Octstr * ha_status_pw
Definition: bb_http.c:85
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2813
unsigned char * password
Definition: test_cimd2.c:100
static Octstr * ha_password
Definition: bb_http.c:84
#define octstr_create(cstr)
Definition: octstr.h:125
void gwthread_sleep(double seconds)
Definition: octstr.c:118
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:869
static Octstr* httpd_check_status ( void  )
static

Definition at line 132 of file bb_http.c.

References BB_DEAD, BB_SHUTDOWN, bb_status, and octstr_create.

Referenced by httpd_add_smsc(), httpd_flush_dlr(), httpd_graceful_restart(), httpd_isolate(), httpd_loglevel(), httpd_reload_lists(), httpd_remove_message(), httpd_remove_smsc(), httpd_restart(), httpd_restart_smsc(), httpd_resume(), httpd_stop_smsc(), and httpd_suspend().

133 {
134  if (bb_status == BB_SHUTDOWN || bb_status == BB_DEAD)
135  return octstr_create("Avalanche has already started, too late to "
136  "save the sheeps");
137  return NULL;
138 }
volatile sig_atomic_t bb_status
Definition: bearerbox.c:132
#define octstr_create(cstr)
Definition: octstr.h:125
static Octstr* httpd_flush_dlr ( List cgivars,
int  status_type 
)
static

Definition at line 254 of file bb_http.c.

References bb_flush_dlr(), httpd_check_authorization(), httpd_check_status(), octstr_create, and reply().

255 {
256  Octstr *reply;
257  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
258  if ((reply = httpd_check_status())!= NULL) return reply;
259 
260  if (bb_flush_dlr() == -1)
261  return octstr_create("Suspend " GW_NAME " before trying to flush DLR queue");
262  else
263  return octstr_create("DLR queue flushed");
264 }
int bb_flush_dlr(void)
Definition: bearerbox.c:916
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_graceful_restart ( List cgivars,
int  status_type 
)
static

Definition at line 239 of file bb_http.c.

References BB_DEAD, bb_graceful_restart(), bb_restart(), BB_SHUTDOWN, bb_status, httpd_check_authorization(), httpd_check_status(), octstr_create, and reply().

240 {
241  Octstr *reply;
242  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
243  if ((reply = httpd_check_status())!= NULL) return reply;
244 
245  if (bb_status == BB_SHUTDOWN) {
246  bb_status = BB_DEAD;
247  bb_restart();
248  return octstr_create("Already in shutdown phase, restarting hard...");
249  }
251  return octstr_create("Restarting gracefully.....");
252 }
volatile sig_atomic_t bb_status
Definition: bearerbox.c:132
int bb_graceful_restart(void)
Definition: bearerbox.c:954
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
int bb_restart(void)
Definition: bearerbox.c:948
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_isolate ( List cgivars,
int  status_type 
)
static

Definition at line 188 of file bb_http.c.

References bb_isolate(), httpd_check_authorization(), httpd_check_status(), octstr_create, and reply().

189 {
190  Octstr *reply;
191  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
192  if ((reply = httpd_check_status())!= NULL) return reply;
193 
194  if (bb_isolate() == -1)
195  return octstr_create("Already isolated");
196  else
197  return octstr_create(GW_NAME " isolated from message providers");
198 }
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
int bb_isolate(void)
Definition: bearerbox.c:861
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_loglevel ( List cgivars,
int  status_type 
)
static

Definition at line 154 of file bb_http.c.

References http_cgi_variable(), httpd_check_authorization(), httpd_check_status(), log_set_log_level(), octstr_create, octstr_format(), octstr_get_cstr, and reply().

155 {
156  Octstr *reply;
157  Octstr *level;
158  int new_loglevel;
159 
160  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
161  if ((reply = httpd_check_status())!= NULL) return reply;
162 
163  /* check if new loglevel is given */
164  level = http_cgi_variable(cgivars, "level");
165  if (level) {
166  new_loglevel = atoi(octstr_get_cstr(level));
167  log_set_log_level(new_loglevel);
168  return octstr_format("log-level set to %d", new_loglevel);
169  }
170  else {
171  return octstr_create("New level not given");
172  }
173 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2813
void log_set_log_level(enum output_level level)
Definition: log.c:229
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_reload_lists ( List cgivars,
int  status_type 
)
static

Definition at line 338 of file bb_http.c.

References bb_reload_lists(), httpd_check_authorization(), httpd_check_status(), octstr_create, and reply().

339 {
340  Octstr *reply;
341  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
342  if ((reply = httpd_check_status())!= NULL) return reply;
343 
344  if (bb_reload_lists() == -1)
345  return octstr_create("Could not re-load lists");
346  else
347  return octstr_create("Black/white lists re-loaded");
348 }
int bb_reload_lists(void)
Definition: bearerbox.c:959
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_remove_message ( List cgivars,
int  status_type 
)
static

Definition at line 350 of file bb_http.c.

References bb_remove_message(), http_cgi_variable(), httpd_check_authorization(), httpd_check_status(), octstr_create, octstr_format(), octstr_get_cstr, octstr_len(), reply(), and UUID_STR_LEN.

351 {
352  Octstr *reply;
353  Octstr *message_id;
354  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
355  if ((reply = httpd_check_status())!= NULL) return reply;
356 
357  /* check if the smsc id is given */
358  message_id = http_cgi_variable(cgivars, "id");
359  if (message_id) {
360  if (octstr_len(message_id) != UUID_STR_LEN)
361  return octstr_format("Message id should be %d characters long", UUID_STR_LEN);
362  if (bb_remove_message(message_id) == -1)
363  return octstr_format("Could not remove message id `%s'", octstr_get_cstr(message_id));
364  else
365  return octstr_format("Message id `%s' removed", octstr_get_cstr(message_id));
366  } else
367  return octstr_create("Message id not given");
368 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2813
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
#define octstr_create(cstr)
Definition: octstr.h:125
#define UUID_STR_LEN
Definition: gw_uuid.h:19
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
int bb_remove_message(Octstr *message_id)
Definition: bearerbox.c:964
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_remove_smsc ( List cgivars,
int  status_type 
)
static

Definition at line 284 of file bb_http.c.

References bb_remove_smsc(), http_cgi_variable(), httpd_check_authorization(), httpd_check_status(), octstr_create, octstr_format(), octstr_get_cstr, and reply().

285 {
286  Octstr *reply;
287  Octstr *smsc;
288  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
289  if ((reply = httpd_check_status())!= NULL) return reply;
290 
291  /* check if the smsc id is given */
292  smsc = http_cgi_variable(cgivars, "smsc");
293  if (smsc) {
294  if (bb_remove_smsc(smsc) == -1)
295  return octstr_format("Could not remove smsc-id `%s'", octstr_get_cstr(smsc));
296  else
297  return octstr_format("SMSC `%s' removed", octstr_get_cstr(smsc));
298  } else
299  return octstr_create("SMSC id not given");
300 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2813
int bb_remove_smsc(Octstr *id)
Definition: bearerbox.c:943
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_restart ( List cgivars,
int  status_type 
)
static

Definition at line 224 of file bb_http.c.

References BB_DEAD, bb_restart(), BB_SHUTDOWN, bb_status, gwthread_wakeup_all(), httpd_check_authorization(), httpd_check_status(), octstr_create, and reply().

225 {
226  Octstr *reply;
227  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
228  if ((reply = httpd_check_status())!= NULL) return reply;
229 
230  if (bb_status == BB_SHUTDOWN) {
231  bb_status = BB_DEAD;
233  return octstr_create("Trying harder to restart");
234  }
235  bb_restart();
236  return octstr_create("Restarting.....");
237 }
volatile sig_atomic_t bb_status
Definition: bearerbox.c:132
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
int bb_restart(void)
Definition: bearerbox.c:948
void gwthread_wakeup_all(void)
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_restart_smsc ( List cgivars,
int  status_type 
)
static

Definition at line 320 of file bb_http.c.

References bb_restart_smsc(), http_cgi_variable(), httpd_check_authorization(), httpd_check_status(), octstr_create, octstr_format(), octstr_get_cstr, and reply().

321 {
322  Octstr *reply;
323  Octstr *smsc;
324  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
325  if ((reply = httpd_check_status())!= NULL) return reply;
326 
327  /* check if the smsc id is given */
328  smsc = http_cgi_variable(cgivars, "smsc");
329  if (smsc) {
330  if (bb_restart_smsc(smsc) == -1)
331  return octstr_format("Could not re-start smsc-id `%s'", octstr_get_cstr(smsc));
332  else
333  return octstr_format("SMSC `%s' re-started", octstr_get_cstr(smsc));
334  } else
335  return octstr_create("SMSC id not given");
336 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
int bb_restart_smsc(Octstr *id)
Definition: bearerbox.c:933
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2813
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_resume ( List cgivars,
int  status_type 
)
static

Definition at line 212 of file bb_http.c.

References bb_resume(), httpd_check_authorization(), httpd_check_status(), octstr_create, and reply().

213 {
214  Octstr *reply;
215  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
216  if ((reply = httpd_check_status())!= NULL) return reply;
217 
218  if (bb_resume() == -1)
219  return octstr_create("Already running");
220  else
221  return octstr_create("Running resumed");
222 }
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
int bb_resume(void)
Definition: bearerbox.c:898
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
static void reply(HTTPClient *c, List *push_headers)
static void httpd_serve ( HTTPClient client,
Octstr ourl,
List headers,
Octstr body,
List cgivars 
)
static

Definition at line 394 of file bb_http.c.

References bb_status_linebreak(), BBSTATUS_HTML, BBSTATUS_TEXT, BBSTATUS_WML, BBSTATUS_XML, httpd_command::command, content_type, httpd_command::function, gw_assert(), gwlist_create, http_destroy_cgiargs(), http_destroy_headers(), http_header_add(), HTTP_OK, http_send_reply(), http_type_accepted(), httpd_commands, octstr_append(), octstr_append_cstr(), octstr_copy, octstr_create, octstr_delete(), octstr_destroy(), octstr_duplicate, octstr_format(), octstr_format_append(), octstr_get_char(), octstr_imm(), octstr_len(), octstr_search(), octstr_search_char(), octstr_str_compare(), reply(), and url.

Referenced by httpadmin_run().

396 {
397  Octstr *reply, *final_reply, *url;
398  char *content_type;
399  char *header, *footer;
400  int status_type;
401  int i;
402  long pos;
403 
404  reply = final_reply = NULL; /* for compiler please */
405  url = octstr_duplicate(ourl);
406 
407  /* Set default reply format according to client
408  * Accept: header */
409  if (http_type_accepted(headers, "text/vnd.wap.wml")) {
410  status_type = BBSTATUS_WML;
411  content_type = "text/vnd.wap.wml";
412  }
413  else if (http_type_accepted(headers, "text/html")) {
414  status_type = BBSTATUS_HTML;
415  content_type = "text/html";
416  }
417  else if (http_type_accepted(headers, "text/xml")) {
418  status_type = BBSTATUS_XML;
419  content_type = "text/xml";
420  } else {
421  status_type = BBSTATUS_TEXT;
422  content_type = "text/plain";
423  }
424 
425  /* kill '/cgi-bin' prefix */
426  pos = octstr_search(url, octstr_imm("/cgi-bin/"), 0);
427  if (pos != -1)
428  octstr_delete(url, pos, 9);
429  else if (octstr_get_char(url, 0) == '/')
430  octstr_delete(url, 0, 1);
431 
432  /* look for type and kill it */
433  pos = octstr_search_char(url, '.', 0);
434  if (pos != -1) {
435  Octstr *tmp = octstr_copy(url, pos+1, octstr_len(url) - pos - 1);
436  octstr_delete(url, pos, octstr_len(url) - pos);
437 
438  if (octstr_str_compare(tmp, "txt") == 0)
439  status_type = BBSTATUS_TEXT;
440  else if (octstr_str_compare(tmp, "html") == 0)
441  status_type = BBSTATUS_HTML;
442  else if (octstr_str_compare(tmp, "xml") == 0)
443  status_type = BBSTATUS_XML;
444  else if (octstr_str_compare(tmp, "wml") == 0)
445  status_type = BBSTATUS_WML;
446 
447  octstr_destroy(tmp);
448  }
449 
450  for (i=0; httpd_commands[i].command != NULL; i++) {
451  if (octstr_str_compare(url, httpd_commands[i].command) == 0) {
452  reply = httpd_commands[i].function(cgivars, status_type);
453  break;
454  }
455  }
456 
457  /* check if command found */
458  if (httpd_commands[i].command == NULL) {
459  char *lb = bb_status_linebreak(status_type);
460  reply = octstr_format("Unknown command `%S'.%sPossible commands are:%s",
461  ourl, lb, lb);
462  for (i=0; httpd_commands[i].command != NULL; i++)
463  octstr_format_append(reply, "%s%s", httpd_commands[i].command, lb);
464  }
465 
466  gw_assert(reply != NULL);
467 
468  if (status_type == BBSTATUS_HTML) {
469  header = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n"
470  "<html>\n<title>" GW_NAME "</title>\n<body>\n<p>";
471  footer = "</p>\n</body></html>\n";
472  content_type = "text/html";
473  } else if (status_type == BBSTATUS_WML) {
474  header = "<?xml version=\"1.0\"?>\n"
475  "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" "
476  "\"http://www.wapforum.org/DTD/wml_1.1.xml\">\n"
477  "\n<wml>\n <card>\n <p>";
478  footer = " </p>\n </card>\n</wml>\n";
479  content_type = "text/vnd.wap.wml";
480  } else if (status_type == BBSTATUS_XML) {
481  header = "<?xml version=\"1.0\"?>\n"
482  "<gateway>\n";
483  footer = "</gateway>\n";
484  } else {
485  header = "";
486  footer = "";
487  content_type = "text/plain";
488  }
489  final_reply = octstr_create(header);
490  octstr_append(final_reply, reply);
491  octstr_append_cstr(final_reply, footer);
492 
493  /* debug("bb.http", 0, "Result: '%s'", octstr_get_cstr(final_reply));
494  */
495  http_destroy_headers(headers);
496  headers = gwlist_create();
497  http_header_add(headers, "Content-Type", content_type);
498 
499  http_send_reply(client, HTTP_OK, headers, final_reply);
500 
501  octstr_destroy(url);
502  octstr_destroy(ourl);
503  octstr_destroy(body);
504  octstr_destroy(reply);
505  octstr_destroy(final_reply);
506  http_destroy_headers(headers);
507  http_destroy_cgiargs(cgivars);
508 }
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2863
char * bb_status_linebreak(int status_type)
Definition: bearerbox.c:1097
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1502
long octstr_search(const Octstr *haystack, const Octstr *needle, long pos)
Definition: octstr.c:1068
static struct httpd_command httpd_commands[]
void octstr_append_cstr(Octstr *ostr, const char *cstr)
Definition: octstr.c:1509
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1010
void http_destroy_headers(List *headers)
Definition: http.c:2856
void http_destroy_cgiargs(List *args)
Definition: http.c:2795
void http_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2671
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
Definition: http.h:142
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1525
const char * command
Definition: bb_http.c:372
#define octstr_duplicate(ostr)
Definition: octstr.h:187
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
gw_assert(wtls_machine->packet_to_send!=NULL)
const char * content_type
Definition: fakewap.c:249
Octstr *(* function)(List *cgivars, int status_type)
Definition: bb_http.c:373
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
int octstr_str_compare(const Octstr *ostr, const char *str)
Definition: octstr.c:971
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2505
#define gwlist_create()
Definition: list.h:136
int http_type_accepted(List *headers, char *type)
Definition: http.c:3480
static Octstr * url
Definition: test_xmlrpc.c:84
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:404
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_shutdown ( List cgivars,
int  status_type 
)
static

Definition at line 175 of file bb_http.c.

References BB_DEAD, BB_SHUTDOWN, bb_shutdown(), bb_status, gwthread_wakeup(), httpd_check_authorization(), MAIN_THREAD_ID, octstr_create, and reply().

176 {
177  Octstr *reply;
178  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
179  if (bb_status == BB_SHUTDOWN)
180  bb_status = BB_DEAD;
181  else {
182  bb_shutdown();
184  }
185  return octstr_create("Bringing system down");
186 }
volatile sig_atomic_t bb_status
Definition: bearerbox.c:132
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
int bb_shutdown(void)
Definition: bearerbox.c:832
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
void gwthread_wakeup(long thread)
#define MAIN_THREAD_ID
Definition: gwthread.h:77
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_status ( List cgivars,
int  status_type 
)
static

Definition at line 140 of file bb_http.c.

References bb_print_status(), httpd_check_authorization(), and reply().

141 {
142  Octstr *reply;
143  if ((reply = httpd_check_authorization(cgivars, 1))!= NULL) return reply;
144  return bb_print_status(status_type);
145 }
Definition: octstr.c:118
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
Octstr * bb_print_status(int status_type)
Definition: bearerbox.c:986
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_stop_smsc ( List cgivars,
int  status_type 
)
static

Definition at line 266 of file bb_http.c.

References bb_stop_smsc(), http_cgi_variable(), httpd_check_authorization(), httpd_check_status(), octstr_create, octstr_format(), octstr_get_cstr, and reply().

267 {
268  Octstr *reply;
269  Octstr *smsc;
270  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
271  if ((reply = httpd_check_status())!= NULL) return reply;
272 
273  /* check if the smsc id is given */
274  smsc = http_cgi_variable(cgivars, "smsc");
275  if (smsc) {
276  if (bb_stop_smsc(smsc) == -1)
277  return octstr_format("Could not shut down smsc-id `%s'", octstr_get_cstr(smsc));
278  else
279  return octstr_format("SMSC `%s' shut down", octstr_get_cstr(smsc));
280  } else
281  return octstr_create("SMSC id not given");
282 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2813
int bb_stop_smsc(Octstr *id)
Definition: bearerbox.c:927
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_store_status ( List cgivars,
int  status_type 
)
static

Definition at line 147 of file bb_http.c.

References httpd_check_authorization(), reply(), and store_status().

148 {
149  Octstr *reply;
150  if ((reply = httpd_check_authorization(cgivars, 1))!= NULL) return reply;
151  return store_status(status_type);
152 }
Octstr * store_status(int status_type)
Definition: bb_store.c:148
Definition: octstr.c:118
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
static void reply(HTTPClient *c, List *push_headers)
static Octstr* httpd_suspend ( List cgivars,
int  status_type 
)
static

Definition at line 200 of file bb_http.c.

References bb_suspend(), httpd_check_authorization(), httpd_check_status(), octstr_create, and reply().

201 {
202  Octstr *reply;
203  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
204  if ((reply = httpd_check_status())!= NULL) return reply;
205 
206  if (bb_suspend() == -1)
207  return octstr_create("Already suspended");
208  else
209  return octstr_create(GW_NAME " suspended");
210 }
int bb_suspend(void)
Definition: bearerbox.c:880
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
static Octstr * httpd_check_status(void)
Definition: bb_http.c:132
static Octstr * httpd_check_authorization(List *cgivars, int status)
Definition: bb_http.c:98
static void reply(HTTPClient *c, List *push_headers)

Variable Documentation

volatile sig_atomic_t bb_status
Octstr* ha_allow_ip
static

Definition at line 86 of file bb_http.c.

Octstr* ha_deny_ip
static

Definition at line 87 of file bb_http.c.

Octstr* ha_interface
static

Definition at line 83 of file bb_http.c.

Octstr* ha_password
static

Definition at line 84 of file bb_http.c.

long ha_port
static

Definition at line 82 of file bb_http.c.

Referenced by httpadmin_run(), and httpadmin_start().

Octstr* ha_status_pw
static

Definition at line 85 of file bb_http.c.

volatile sig_atomic_t httpadmin_running
static

Definition at line 80 of file bb_http.c.

Referenced by httpadmin_run(), and httpadmin_start().

struct httpd_command httpd_commands[]
static
Initial value:
= {
{ "status", httpd_status },
{ "store-status", httpd_store_status },
{ "log-level", httpd_loglevel },
{ "shutdown", httpd_shutdown },
{ "suspend", httpd_suspend },
{ "isolate", httpd_isolate },
{ "resume", httpd_resume },
{ "restart", httpd_restart },
{ "graceful-restart", httpd_graceful_restart },
{ "flush-dlr", httpd_flush_dlr },
{ "stop-smsc", httpd_stop_smsc },
{ "start-smsc", httpd_restart_smsc },
{ "add-smsc", httpd_add_smsc },
{ "remove-smsc", httpd_remove_smsc },
{ "reload-lists", httpd_reload_lists },
{ "remove-message", httpd_remove_message },
{ NULL , NULL }
}
static Octstr * httpd_resume(List *cgivars, int status_type)
Definition: bb_http.c:212
static Octstr * httpd_reload_lists(List *cgivars, int status_type)
Definition: bb_http.c:338
static Octstr * httpd_flush_dlr(List *cgivars, int status_type)
Definition: bb_http.c:254
static Octstr * httpd_isolate(List *cgivars, int status_type)
Definition: bb_http.c:188
static Octstr * httpd_graceful_restart(List *cgivars, int status_type)
Definition: bb_http.c:239
static Octstr * httpd_store_status(List *cgivars, int status_type)
Definition: bb_http.c:147
static Octstr * httpd_status(List *cgivars, int status_type)
Definition: bb_http.c:140
static Octstr * httpd_loglevel(List *cgivars, int status_type)
Definition: bb_http.c:154
static Octstr * httpd_restart_smsc(List *cgivars, int status_type)
Definition: bb_http.c:320
static Octstr * httpd_remove_message(List *cgivars, int status_type)
Definition: bb_http.c:350
static Octstr * httpd_suspend(List *cgivars, int status_type)
Definition: bb_http.c:200
static Octstr * httpd_shutdown(List *cgivars, int status_type)
Definition: bb_http.c:175
static Octstr * httpd_restart(List *cgivars, int status_type)
Definition: bb_http.c:224
static Octstr * httpd_add_smsc(List *cgivars, int status_type)
Definition: bb_http.c:302
static Octstr * httpd_stop_smsc(List *cgivars, int status_type)
Definition: bb_http.c:266
static Octstr * httpd_remove_smsc(List *cgivars, int status_type)
Definition: bb_http.c:284

Referenced by httpd_serve().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.