int glswSetPath(const char* pathPrefix, const char* pathSuffix) { glswContext* gc = glswGetCurrentContext(); if (!gc) { return 0; } if (!gc->PathPrefix) { gc->PathPrefix = bfromcstr(pathPrefix); } else { bstring prefix = bfromcstr(pathPrefix); if (bstrcmp(gc->PathPrefix, prefix) != 0) { bassign(gc->PathPrefix, prefix); } else { bdestroy(prefix); } } if (!gc->PathSuffix) { gc->PathSuffix = bfromcstr(pathSuffix); } else { bstring suffix = bfromcstr(pathSuffix); if (bstrcmp(gc->PathSuffix, suffix) != 0) { bassign(gc->PathSuffix, suffix); } else { bdestroy(suffix); } } return 1; }
int attempt_chroot_drop(Server *srv) { int rc = 0; if(Unixy_chroot(srv->chroot) == 0) { log_info("All loaded up, time to turn into a server."); log_info("-- Starting " VERSION ". Copyright (C) Zed A. Shaw. Licensed BSD.\n"); log_info("-- Look in %s for startup messages and errors.", bdata(srv->error_log)); check(access("/run", F_OK) == 0, "/run directory doesn't exist in %s or isn't owned right.", bdata(srv->chroot)); check(access("/tmp", F_OK) == 0, "/tmp directory doesn't exist in %s or isn't owned right.", bdata(srv->chroot)); rc = Unixy_daemonize(); check(rc == 0, "Failed to daemonize, looks like you're hosed."); FILE *log = fopen(bdata(srv->error_log), "a+"); check(log, "Couldn't open %s log file.", bdata(srv->error_log)); setbuf(log, NULL); dbg_set_log(log); rc = Unixy_pid_file(srv->pid_file); check(rc == 0, "Failed to make the PID file %s", bdata(srv->pid_file)); rc = Unixy_drop_priv(&PRIV_DIR); check(rc == 0, "Failed to drop priv to the owner of %s", bdata(&PRIV_DIR)); } else { log_warn("Couldn't chroot too %s, assuming running in test mode.", bdata(srv->chroot)); // rewrite the access log to be in the right location bstring temp = bformat("%s%s", bdata(srv->chroot), bdata(srv->access_log)); bassign(srv->access_log, temp); bdestroy(temp); temp = bformat(".%s", bdata(srv->pid_file)); bassign(srv->pid_file, temp); bdestroy(temp); rc = Unixy_pid_file(srv->pid_file); check(rc == 0, "Failed to make the PID file %s", bdata(srv->pid_file)); } return 0; error: return -1; }
char *test_different_functions() { // create + length bstring string1 = bfromcstr("string"); mu_assert_equal(6, blength(string1)); // bassign, biseq bstring string2 = bfromcstr("foo"); bassign(string2, string1); mu_assert(strcmp(bdata(string1), bdata(string2)) == 0, "both strings should be equal"); mu_assert(biseq(string1, string2) == 1, "both strings should be equal"); // bconcat bstring string2 = bfromcstr("foo"); string1 = bfromcstr("Hello"); string2 = bfromcstr(", World!"); bconcat(string1, string2); mu_assert(strcmp("Hello, World!", bdata(string1)) == 0, "returned string not as expected"); // bsplit string1 = bfromcstr("foo, bar, baz"); struct bstrList *words; words = bsplit(string1, ','); mu_assert(strcmp("foo", bdata(words->entry[0])) == 0, "returned string not as expected"); return NULL; }
void rparse_do(char* yytext, int* out_line, bstring* out_filename) { bstring str = bfromcstr(yytext); bstring space = bfromcstr(" "); // Determine the space positions. int firstSpace = binstr(str, 0, space); int secondSpace = binstr(str, firstSpace + 1, space); // Create substrings. bstring bline = bmidstr(str, firstSpace + 1, secondSpace - firstSpace); bstring file = bmidstr(str, secondSpace + 1, blength(str) - secondSpace); // Convert the line number and trim file name. int line = strtoul((const char*)bline->data, NULL, 10); btrimws(file); // Set variables. if (*out_filename == NULL) *out_filename = bfromcstr(""); bassign(*out_filename, file); *out_line = line; // Free resources. bdestroy(file); bdestroy(bline); bdestroy(space); bdestroy(str); }
struct dbg_sym_payload_label* dbgfmt_create_symbol_label(bstring label, uint16_t address) { struct dbg_sym_payload_label* payload = malloc(sizeof(struct dbg_sym_payload_label)); payload->label = bfromcstr(""); bassign(payload->label, label); payload->address = address; return payload; }
struct dbg_sym_payload_string* dbgfmt_create_symbol_string(bstring data, uint16_t address) { struct dbg_sym_payload_string* payload = malloc(sizeof(struct dbg_sym_payload_string)); payload->data = bfromcstr(""); bassign(payload->data, data); payload->address = address; return payload; }
struct dbg_sym_payload_line* dbgfmt_create_symbol_line(bstring path, uint16_t lineno, uint16_t address) { struct dbg_sym_payload_line* payload = malloc(sizeof(struct dbg_sym_payload_line)); payload->path = bfromcstr(""); bassign(payload->path, path); payload->lineno = lineno; payload->address = address; return payload; }
char *test_bassign() { bstring b = bfromcstr("hello"); bstring a = bfromcstr("a much longer string"); int rc = bassign(a, b); mu_assert(rc == BSTR_OK, "bassign failed"); mu_assert(blength(a) == blength(b), "bassign length diff"); mu_assert(biseq(a, b) == 1, "bassign content diff"); bdestroy(a); bdestroy(b); return NULL; }
char *test_bassign(void) { bstring b = bfromcstr(test); bstring assigned = bfromcstr("peter&&"); mu_assert(bassign(assigned, b) == BSTR_OK, "Failed to bassign()."); mu_assert(strcmp((const char *)assigned->data, test) == 0, "Failed to string compare after bassign()."); mu_assert(assigned->slen == (int)strlen(test), "Wrong string length after bassign()."); mu_assert(bdestroy(b) == BSTR_OK, "Failed to bdestroy() after bassign()"); mu_assert(bdestroy(assigned) == BSTR_OK, "Failed to bdestroy() after bassign()."); return NULL; }
static int redir_handle_url(struct redir_t *redir, struct redir_conn_t *conn, struct redir_httpreq_t *httpreq, struct redir_socket_t *socket, struct sockaddr_in *peer, redir_request *req) { int port = 80; int matches = 1; int i = -1; char *p = 0; #ifdef ENABLE_REDIRINJECT char hasInject = 0; if (conn->s_params.flags & UAM_INJECT_URL) { safe_strncpy((char *) req->inject_url, (char *) conn->s_params.url, REDIRINJECT_MAX); hasInject = 1; } else if (_options.inject && *_options.inject) { safe_strncpy((char *) req->inject_url, (char *) _options.inject, REDIRINJECT_MAX); hasInject = 1; } else { #endif for (i=0; i < MAX_REGEX_PASS_THROUGHS; i++) { if ( ! _options.regex_pass_throughs[i].inuse ) break; /* if ( ! _options.regex_pass_throughs[i].regex_host[0] && ! _options.regex_pass_throughs[i].regex_path[0] && ! _options.regex_pass_throughs[i].regex_qs[0] ) break; */ #if(_debug_) log_dbg("REGEX host=[%s] path=[%s] qs=[%s]", _options.regex_pass_throughs[i].regex_host, _options.regex_pass_throughs[i].regex_path, _options.regex_pass_throughs[i].regex_qs); log_dbg("Host %s", httpreq->host); #endif if (_options.regex_pass_throughs[i].regex_host[0]) { switch(check_regex(&_options.regex_pass_throughs[i].re_host, _options.regex_pass_throughs[i].regex_host, httpreq->host)) { case -1: return -1; case 1: matches = _options.regex_pass_throughs[i].neg_host; break; case 0: matches = !_options.regex_pass_throughs[i].neg_host; break; } } if (matches && _options.regex_pass_throughs[i].regex_path[0]) { switch(check_regex(&_options.regex_pass_throughs[i].re_path, _options.regex_pass_throughs[i].regex_path, httpreq->path)) { case -1: return -1; case 1: matches = _options.regex_pass_throughs[i].neg_path; break; case 0: matches = !_options.regex_pass_throughs[i].neg_path; break; } } if (matches && _options.regex_pass_throughs[i].regex_qs[0]) { switch(check_regex(&_options.regex_pass_throughs[i].re_qs, _options.regex_pass_throughs[i].regex_qs, httpreq->qs)) { case -1: return -1; case 1: matches = _options.regex_pass_throughs[i].neg_qs; break; case 0: matches = !_options.regex_pass_throughs[i].neg_qs; break; } } if (matches) break; } #ifdef ENABLE_REDIRINJECT } #endif if (i == 0) matches = 0; if (matches) { log_dbg("Matched for Host %s", httpreq->host); req->proxy = 1; #ifdef ENABLE_REDIRINJECT /* XXX */ /* Check for headers we wish to filter out */ if (hasInject) { bstring newhdr = bfromcstr(""); char *hdr = (char *)req->wbuf->data; if (_options.inject_wispr) (void) inject_fmt(req, conn); while (hdr && *hdr) { char *p = strstr(hdr, "\r\n"); int skip = 0; int l; if (p) { l = (p - hdr); } else { l = req->wbuf->slen - (hdr - (char*)req->wbuf->data); } if (!strncasecmp(hdr, "accept-encoding:", 16)) { bcatcstr(newhdr, "Accept-Encoding: identity\r\n"); skip = 1; } else if (!strncasecmp(hdr, "connection:", 11)) { bcatcstr(newhdr, "Connection: close\r\n"); skip = 1; } else if (!strncasecmp(hdr, "keep-alive:", 11)) { skip = 1; } if (!skip) bcatblk(newhdr, hdr, l); if (p) { if (!skip) bcatblk(newhdr, p, 2); hdr = p + 2; } else { hdr = 0; } } if (req->wbuf->slen != newhdr->slen) { log_dbg("Changed HTTP Headers"); } bassign(req->wbuf, newhdr); bdestroy(newhdr); } /* XXX */ #endif if ((p = strchr(httpreq->host, ':'))) { *p++ = 0; port = atoi(p); } if (conn_setup(&req->conn, httpreq->host, port, req->wbuf, req->dbuf)) { log_err(errno, "conn_setup()"); return -1; } req->state |= REDIR_CONN_FD; net_select_addfd(&sctx, req->conn.sock, SELECT_READ); return 0; } return 1; }
int attempt_chroot_drop(Server *srv) { int rc = 0; log_info("All loaded up, time to turn into a server."); log_info("-- Starting " VERSION ". Copyright (C) Zed A. Shaw. Licensed BSD.\n"); log_info("-- Look in %s for startup messages and errors.", bdata(srv->error_log)); int testmode = 0; if(srv->chroot != NULL) { if(Unixy_chroot(srv->chroot) == 0) { if(Setting_get_int("server.daemonize", 1)) { rc = Unixy_daemonize(1); // 1 == chdir / check(rc == 0, "Failed to daemonize, looks like you're hosed."); } else { rc = chdir("/"); check(rc == 0, "Failed to change working directory to '/'."); } } else { log_warn("Couldn't chroot to %s, assuming running in test mode.", bdata(srv->chroot)); // rewrite the access log to be in the right location bstring temp = bformat("%s%s", bdata(srv->chroot), bdata(srv->access_log)); bassign(srv->access_log, temp); bdestroy(temp); temp = bformat(".%s", bdata(srv->pid_file)); bassign(srv->pid_file, temp); bdestroy(temp); testmode = 1; } } else { if(Setting_get_int("server.daemonize", 1)) { rc = Unixy_daemonize(0); // 0 == don't chdir check(rc == 0, "Failed to daemonize, looks like you're hosed."); } } rc = Unixy_pid_file(srv->pid_file); check(rc == 0, "Failed to make the PID file %s", bdata(srv->pid_file)); if(srv->chroot != NULL && ! testmode) { rc = Unixy_drop_priv(&PRIV_DIR); check(rc == 0, "Failed to drop priv to the owner of %s", bdata(&PRIV_DIR)); } else { rc = Unixy_drop_priv(NULL); check(rc == 0, "Failed to drop priv"); } if(!testmode) { FILE *log = fopen(bdata(srv->error_log), "a+"); check(log, "Couldn't open %s log file.", bdata(srv->error_log)); setbuf(log, NULL); check(0==add_log_to_rotate_list(srv->error_log,log),"Unable to add error log to list of files to rotate."); dbg_set_log(log); } return 0; error: return -1; }
/* ---------------------------------------------------------- * FUNCTION : parse_line * DESCRIPTION : This function will process a line of data * : from a configuration file. * INPUT : 0 - Line (bstring) * ---------------------------------------------------------- */ void parse_line (bstring line) { bstring param, value; struct bstrList *list; int i; /* Check to see if this line has something to read. */ if (line->data[0] == '\0' || line->data[0] == '#') return; /* Check to see if this line has a comment in it. */ if ((list = bsplit(line, '#')) != NULL) { if ((bassign(line, list->entry[0])) == -1) { log_message("warning: 'bassign' in function 'parse_line' failed."); } if (list != NULL) bstrListDestroy(list); } /* Seperate line into a parameter and a value. */ if ((i = bstrchr(line, ' ')) == BSTR_ERR) return; if ((param = bmidstr(line, 0, i)) == NULL) return; if ((value = bmidstr(line, i + 1, line->slen - i)) == NULL) return; /* Normalize Strings */ if ((btolower(param)) != 0) log_message("warning: 'btolower' in function 'parse_line' failed."); if ((bltrim(value)) != 0) log_message("warning: 'bltrim' in function 'parse_line' failed."); if ((brtrim(value)) != 0) log_message("warning: 'brtrim' in function 'parse_line' failed."); /* Do something based upon value. */ if ((biseqcstr(param, "daemon")) == 1) { /* DAEMON */ if (!gc.daemon_mode) { if (value->data[0] == '1') gc.daemon_mode = 1; else gc.daemon_mode = 0; } } else if ((biseqcstr(param, "pid_file")) == 1) { /* PID FILE */ gc.pid_file = bstrcpy(value); } else if ((biseqcstr(param, "sig_file")) == 1) { /* SIGNATURE FILE */ gc.sig_file = bstrcpy(value); } else if ((biseqcstr(param, "mac_file")) == 1) { /* MAC / VENDOR RESOLUTION FILE */ gc.mac_file = bstrcpy(value); } else if ((biseqcstr(param, "output")) == 1) { /* OUTPUT */ conf_module_plugin(value, &activate_output_plugin); } else if ((biseqcstr(param, "user")) == 1) { /* USER */ gc.priv_user = bstrcpy(value); } else if ((biseqcstr(param, "group")) == 1) { /* GROUP */ gc.priv_group = bstrcpy(value); } else if ((biseqcstr(param, "interface")) == 1) { /* INTERFACE */ gc.dev = bstr2cstr(value, '-'); } else if ((biseqcstr(param, "filter")) == 1) { /* FILTER */ gc.pcap_filter = bstr2cstr(value, '-'); } else if ((biseqcstr(param, "network")) == 1) { /* NETWORK */ parse_networks(bdata(value)); } verbose_message("config - PARAM: |%s| / VALUE: |%s|", bdata(param), bdata(value)); /* Clean Up */ if (param != NULL) bdestroy(param); if (value != NULL) bdestroy(value); }
int main (int argc, char * argv[]) { bstring b = bfromcstr ("Hello WORLDDDD"); if (!b) { fprintf (stderr, "Out of memory"); } else { puts ((char *) b->data); } bdestroy (b); b = bfromcstralloc (64, "123456789012345678901234567890123456789012345678901234567890123"); if (b) { b->data[63] = 'x'; puts ((char *) b->data); printf("dump is %s\n", dumpBstring(b)); } else { puts("bfromcstralloc failed"); } bdestroy (b); b = blk2bstr ("AWHOLEnewworld", sizeof("wholenewworld")-3); puts ((char *) b->data); char *cstr = bstr2cstr(b, '\0'); puts(cstr); free(cstr); cstr = bstr2cstr(b, '0'); puts(cstr); free(cstr); bstring copy = bstrcpy(b); printf("copy is %s\n", (char *) copy->data); bdestroy (b); b = bfromcstralloc (64, "123456789012345678901234567890123456789012345678901234567890123"); bassign(copy, b); printf("copy is %s\n", (char *) copy->data); printf("b is %s\n", (char *) b->data); bdestroy (b); b = blk2bstr ("AWHOLEnewworld", sizeof("wholenewworld")); bassign(copy, b); printf("copy is %s\n", (char *) copy->data); printf("b is %s\n", (char *) b->data); bdestroy(b); bdestroy(copy); int i = 0; b = bfromcstralloc (64, "123456789012345678901234567890123456789012345678901234567890123"); struct bstrList *blist = bsplit(b, '0'); printf("made blist. qty is %d, mlen is %d. entry[0] is %s\n", blist->qty, blist->mlen, blist->entry[0]->data); for(i=0; i<blist->qty; i++) { printf("entry[%d] is %s\n", i, blist->entry[i]->data); } bstrListDestroy(blist); blist = bsplit(b, '\0'); printf("made blist. qty is %d, mlen is %d. entry[0] is %s\n", blist->qty, blist->mlen, blist->entry[0]->data); for(i=0; i<blist->qty; i++) { printf("entry[%d] is %s\n", i, blist->entry[i]->data); } bstrListDestroy(blist); struct tagbstring split = bsStatic ("123"); blist = bsplitstr(b, &split); printf("made blist. qty is %d, mlen is %d. entry[0] is %s\n", blist->qty, blist->mlen, blist->entry[0]->data); for(i=0; i<blist->qty; i++) { printf("entry[%d] is %s\n", i, blist->entry[i]->data); } bdestroy(b); /* * *#define CHUNK 1024 [> read 1024 bytes at a time <] * char buf[CHUNK]; * FILE *file; * size_t nread; * * file = fopen("./LICENSE", "r"); * if (file) { * while ((nread = fread(buf, 1, sizeof buf, file)) > 0) * fwrite(buf, 1, nread, stdout); * if (ferror(file)) { * [> deal with error <] * } * fclose(file); * } */ FILE *lic_fd = fopen("./LICENSE", "r"); struct bStream *bslic_fd = bsopen((bNread) fread, lic_fd); bstring lic_bstr = bread( (bNread) fread, lic_fd); printf("bstring is %s", lic_bstr->data); return 0; }