Kannel: Open Source WAP and SMS gateway  svn-r5285
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

◆ httpadmin_run()

static void httpadmin_run ( void *  arg)
static

Definition at line 512 of file bb_http.c.

References BB_DEAD, BB_SHUTDOWN, bb_shutdown(), bb_status, client(), ha_allow_ip, ha_deny_ip, 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().

513 {
515  Octstr *ip, *url, *body;
516  List *headers, *cgivars;
517 
518  while(bb_status != BB_DEAD) {
519  if (bb_status == BB_SHUTDOWN)
520  bb_shutdown();
521  client = http_accept_request(ha_port, &ip, &url, &headers, &body,
522  &cgivars);
523  if (client == NULL)
524  break;
525  if (is_allowed_ip(ha_allow_ip, ha_deny_ip, ip) == 0) {
526  info(0, "HTTP admin tried from denied host <%s>, disconnected",
527  octstr_get_cstr(ip));
529  continue;
530  }
531  httpd_serve(client, url, headers, body, cgivars);
532  octstr_destroy(ip);
533  }
534 
535  httpadmin_running = 0;
536 }
void info(int err, const char *fmt,...)
Definition: log.c:672
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:2757
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:2570
volatile sig_atomic_t bb_status
Definition: bearerbox.c:132
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
static void httpd_serve(HTTPClient *client, Octstr *ourl, List *headers, Octstr *body, List *cgivars)
Definition: bb_http.c:396
Definition: octstr.c:118
int bb_shutdown(void)
Definition: bearerbox.c:832
static Octstr * url
Definition: test_xmlrpc.c:84
Definition: list.c:102

◆ httpadmin_start()

int httpadmin_start ( Cfg cfg)

Definition at line 544 of file bb_http.c.

References cfg, cfg_get, cfg_get_bool(), cfg_get_integer(), cfg_get_single_group(), gwthread_create, ha_allow_ip, ha_deny_ip, ha_interface, ha_password, ha_port, ha_status_pw, http_open_port_if(), httpadmin_run(), httpadmin_running, octstr_destroy(), octstr_imm(), panic, and ssl.

Referenced by init_bearerbox().

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

◆ httpadmin_stop()

void httpadmin_stop ( void  )

Definition at line 605 of file bb_http.c.

References gwthread_join_every(), ha_allow_ip, ha_deny_ip, ha_interface, ha_password, ha_status_pw, http_close_all_ports(), httpadmin_run(), and octstr_destroy().

Referenced by main().

606 {
614  ha_password = NULL;
615  ha_status_pw = NULL;
616  ha_allow_ip = NULL;
617  ha_deny_ip = NULL;
618 }
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:324
static Octstr * ha_interface
Definition: bb_http.c:83
void http_close_all_ports(void)
Definition: http.c:2525
static void httpadmin_run(void *arg)
Definition: bb_http.c:512

◆ httpd_add_smsc()

static Octstr* httpd_add_smsc ( List cgivars,
int  status_type 
)
static

Definition at line 304 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().

305 {
306  Octstr *reply;
307  Octstr *smsc;
308  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
309  if ((reply = httpd_check_status())!= NULL) return reply;
310 
311  /* check if the smsc id is given */
312  smsc = http_cgi_variable(cgivars, "smsc");
313  if (smsc) {
314  if (bb_add_smsc(smsc) == -1)
315  return octstr_format("Could not add smsc-id `%s'", octstr_get_cstr(smsc));
316  else
317  return octstr_format("SMSC `%s' added", octstr_get_cstr(smsc));
318  } else
319  return octstr_create("SMSC id not given");
320 }
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:2835
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
#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)

◆ httpd_check_authorization()

static Octstr* httpd_check_authorization ( List cgivars,
int  status 
)
static

Definition at line 98 of file bb_http.c.

References gwthread_sleep(), ha_password, ha_status_pw, 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 
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:2835
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:871

◆ httpd_check_status()

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

◆ httpd_flush_dlr()

static Octstr* httpd_flush_dlr ( List cgivars,
int  status_type 
)
static

Definition at line 256 of file bb_http.c.

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

257 {
258  Octstr *reply;
259  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
260  if ((reply = httpd_check_status())!= NULL) return reply;
261 
262  if (bb_flush_dlr() == -1)
263  return octstr_create("Suspend " GW_NAME " before trying to flush DLR queue");
264  else
265  return octstr_create("DLR queue flushed");
266 }
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)

◆ httpd_graceful_restart()

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  }
250  if (bb_graceful_restart() == -1)
251  return octstr_create("Unable to restart gracefully! Please check log file.");
252  else
253  return octstr_create("Restarting gracefully.....");
254 }
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)

◆ httpd_isolate()

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)

◆ httpd_loglevel()

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:2835
void log_set_log_level(enum output_level level)
Definition: log.c:265
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
#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)

◆ httpd_reload_lists()

static Octstr* httpd_reload_lists ( List cgivars,
int  status_type 
)
static

Definition at line 340 of file bb_http.c.

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

341 {
342  Octstr *reply;
343  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
344  if ((reply = httpd_check_status())!= NULL) return reply;
345 
346  if (bb_reload_lists() == -1)
347  return octstr_create("Could not re-load lists");
348  else
349  return octstr_create("Black/white lists re-loaded");
350 }
int bb_reload_lists(void)
Definition: bearerbox.c:971
#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)

◆ httpd_remove_message()

static Octstr* httpd_remove_message ( List cgivars,
int  status_type 
)
static

Definition at line 352 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.

353 {
354  Octstr *reply;
355  Octstr *message_id;
356  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
357  if ((reply = httpd_check_status())!= NULL) return reply;
358 
359  /* check if the smsc id is given */
360  message_id = http_cgi_variable(cgivars, "id");
361  if (message_id) {
362  if (octstr_len(message_id) != UUID_STR_LEN)
363  return octstr_format("Message id should be %d characters long", UUID_STR_LEN);
364  if (bb_remove_message(message_id) == -1)
365  return octstr_format("Could not remove message id `%s'", octstr_get_cstr(message_id));
366  else
367  return octstr_format("Message id `%s' removed", octstr_get_cstr(message_id));
368  } else
369  return octstr_create("Message id not given");
370 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2835
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
#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:342
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:976
static void reply(HTTPClient *c, List *push_headers)

◆ httpd_remove_smsc()

static Octstr* httpd_remove_smsc ( List cgivars,
int  status_type 
)
static

Definition at line 286 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().

287 {
288  Octstr *reply;
289  Octstr *smsc;
290  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
291  if ((reply = httpd_check_status())!= NULL) return reply;
292 
293  /* check if the smsc id is given */
294  smsc = http_cgi_variable(cgivars, "smsc");
295  if (smsc) {
296  if (bb_remove_smsc(smsc) == -1)
297  return octstr_format("Could not remove smsc-id `%s'", octstr_get_cstr(smsc));
298  else
299  return octstr_format("SMSC `%s' removed", octstr_get_cstr(smsc));
300  } else
301  return octstr_create("SMSC id not given");
302 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2835
int bb_remove_smsc(Octstr *id)
Definition: bearerbox.c:943
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
#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)

◆ httpd_restart()

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)

◆ httpd_restart_smsc()

static Octstr* httpd_restart_smsc ( List cgivars,
int  status_type 
)
static

Definition at line 322 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().

323 {
324  Octstr *reply;
325  Octstr *smsc;
326  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
327  if ((reply = httpd_check_status())!= NULL) return reply;
328 
329  /* check if the smsc id is given */
330  smsc = http_cgi_variable(cgivars, "smsc");
331  if (smsc) {
332  if (bb_restart_smsc(smsc) == -1)
333  return octstr_format("Could not re-start smsc-id `%s'", octstr_get_cstr(smsc));
334  else
335  return octstr_format("SMSC `%s' re-started", octstr_get_cstr(smsc));
336  } else
337  return octstr_create("SMSC id not given");
338 }
#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:2835
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
#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)

◆ httpd_resume()

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)

◆ httpd_serve()

static void httpd_serve ( HTTPClient client,
Octstr ourl,
List headers,
Octstr body,
List cgivars 
)
static

Definition at line 396 of file bb_http.c.

References bb_status_linebreak(), BBSTATUS_HTML, BBSTATUS_TEXT, BBSTATUS_WML, BBSTATUS_XML, client(), 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().

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

◆ httpd_shutdown()

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)

◆ httpd_status()

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:998
static void reply(HTTPClient *c, List *push_headers)

◆ httpd_stop_smsc()

static Octstr* httpd_stop_smsc ( List cgivars,
int  status_type 
)
static

Definition at line 268 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().

269 {
270  Octstr *reply;
271  Octstr *smsc;
272  if ((reply = httpd_check_authorization(cgivars, 0))!= NULL) return reply;
273  if ((reply = httpd_check_status())!= NULL) return reply;
274 
275  /* check if the smsc id is given */
276  smsc = http_cgi_variable(cgivars, "smsc");
277  if (smsc) {
278  if (bb_stop_smsc(smsc) == -1)
279  return octstr_format("Could not shut down smsc-id `%s'", octstr_get_cstr(smsc));
280  else
281  return octstr_format("SMSC `%s' shut down", octstr_get_cstr(smsc));
282  } else
283  return octstr_create("SMSC id not given");
284 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2835
int bb_stop_smsc(Octstr *id)
Definition: bearerbox.c:927
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
#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)

◆ httpd_store_status()

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)

◆ httpd_suspend()

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

◆ bb_status

volatile sig_atomic_t bb_status

◆ ha_allow_ip

Octstr* ha_allow_ip
static

Definition at line 86 of file bb_http.c.

Referenced by httpadmin_run(), httpadmin_start(), and httpadmin_stop().

◆ ha_deny_ip

Octstr* ha_deny_ip
static

Definition at line 87 of file bb_http.c.

Referenced by httpadmin_run(), httpadmin_start(), and httpadmin_stop().

◆ ha_interface

Octstr* ha_interface
static

Definition at line 83 of file bb_http.c.

Referenced by httpadmin_start(), and httpadmin_stop().

◆ ha_password

Octstr* ha_password
static

Definition at line 84 of file bb_http.c.

Referenced by httpadmin_start(), httpadmin_stop(), and httpd_check_authorization().

◆ ha_port

long ha_port
static

Definition at line 82 of file bb_http.c.

Referenced by httpadmin_run(), and httpadmin_start().

◆ ha_status_pw

Octstr* ha_status_pw
static

Definition at line 85 of file bb_http.c.

Referenced by httpadmin_start(), httpadmin_stop(), and httpd_check_authorization().

◆ httpadmin_running

volatile sig_atomic_t httpadmin_running
static

Definition at line 80 of file bb_http.c.

Referenced by httpadmin_run(), and httpadmin_start().

◆ httpd_commands

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:340
static Octstr * httpd_flush_dlr(List *cgivars, int status_type)
Definition: bb_http.c:256
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:322
static Octstr * httpd_remove_message(List *cgivars, int status_type)
Definition: bb_http.c:352
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:304
static Octstr * httpd_stop_smsc(List *cgivars, int status_type)
Definition: bb_http.c:268
static Octstr * httpd_remove_smsc(List *cgivars, int status_type)
Definition: bb_http.c:286

Referenced by httpd_serve().

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