static void print_json (int what, const char *burl, const char *path, const char *name, time_t mtime, char **m, size_t *o, size_t *s, int *num) { switch(what) { case 1: { char *u1, *u2, *c1; u1 = url_escape(path, 0); u2 = url_escape(name, 0); c1 = str_escape(name, 0, '\\'); rprintf("%s{\"type\":\"file\", \"baseurl\":\"%s\", \"file\":\"%s%s%s\", \"name\":\"%s\", \"mtime\":%"PRIlld"}", (*num > 0) ? ", ":"", burl, u1, SL_SEP(path), u2, c1, (long long) mtime); free(u1); free(u2); free(c1); (*num)++; } break; case 0: { char *u2, *c1; u2 = url_escape(name, 0); c1 = str_escape(name, 0, '\\'); rprintf("%s{\"type\":\"dir\", \"indexurl\":\"%s%s%s/\", \"name\":\"%s\", \"mtime\":%"PRIlld"}", (*num > 0) ? ", ":"", burl, SL_SEP(path), u2, c1, (long long) mtime); free(u2); free(c1); (*num)++; } break; default: break; } }
static void print_csv (int what, const char *burl, const char *path, const char *name, time_t mtime, char **m, size_t *o, size_t *s, int *num) { switch(what) { case 1: { char *u1, *u2, *c1; u1 = url_escape(path, 0); u2 = url_escape(name, 0); c1 = str_escape(name, 0, '"'); rprintf("F,\"%s\",\"%s%s%s\",\"%s\",%"PRIlld"\n", burl, u1, SL_SEP(path), u2, c1, (long long) mtime); free(u1); free(u2); free(c1); (*num)++; } break; case 0: { char *u2, *c1; u2 = url_escape(name, 0); c1 = str_escape(name, 0, '"'); rprintf("D,\"%s%s%s/\",\"%s\",%"PRIlld"\n", burl, SL_SEP(path), u2, c1, (long long) mtime); free(u2); free(c1); (*num)++; } break; default: break; } }
/* Write the invitelists to a file. */ static int write_invites(FILE *f, int idx) { struct chanset_t *chan; maskrec *ir; char *mask; long expire, added; if (global_invites) if (fprintf(f, INVITE_NAME " - -\n") == EOF) /* Daemus */ return 0; for (ir = global_invites; ir; ir = ir->next) { mask = str_escape(ir->mask, ':', '\\'); expire = ir->expire; added = ir->added; if (!mask || fprintf(f, "@ %s:%s%lu%s:+%lu:%lu:%s:%s\n", mask, (ir->flags & MASKREC_PERM) ? "+" : "", expire, (ir->flags & MASKREC_STICKY) ? "*" : "", added, (long) ir->lastactive, ir->user ? ir->user : botnetnick, ir->desc ? ir->desc : "requested") == EOF) { if (mask) nfree(mask); return 0; } nfree(mask); } for (chan = chanset; chan; chan = chan->next) if ((idx < 0) || (chan->status & CHAN_SHARED)) { struct flag_record fr = { FR_CHAN | FR_GLOBAL | FR_BOT, 0, 0, 0, 0, 0 }; if (idx >= 0) get_user_flagrec(dcc[idx].user, &fr, chan->dname); else fr.chan = BOT_SHARE; if ((fr.chan & BOT_SHARE) || (fr.bot & BOT_GLOBAL)) { if (fprintf(f, "$$%s invites\n", chan->dname) == EOF) return 0; for (ir = chan->invites; ir; ir = ir->next) { mask = str_escape(ir->mask, ':', '\\'); expire = ir->expire; added = ir->added; if (!mask || fprintf(f, "@ %s:%s%lu%s:+%lu:%lu:%s:%s\n", mask, (ir->flags & MASKREC_PERM) ? "+" : "", expire, (ir->flags & MASKREC_STICKY) ? "*" : "", added, (long) ir->lastactive, ir->user ? ir->user : botnetnick, ir->desc ? ir->desc : "requested") == EOF) { if (mask) nfree(mask); return 0; } nfree(mask); } } } return 1; }
PLDHashOperator pref_savePref(PLDHashTable *table, PLDHashEntryHdr *heh, PRUint32 i, void *arg) { pref_saveArgs *argData = static_cast<pref_saveArgs *>(arg); PrefHashEntry *pref = static_cast<PrefHashEntry *>(heh); PR_ASSERT(pref); if (!pref) return PL_DHASH_NEXT; nsCAutoString prefValue; // where we're getting our pref from PrefValue* sourcePref; if (PREF_HAS_USER_VALUE(pref) && pref_ValueChanged(pref->defaultPref, pref->userPref, (PrefType) PREF_TYPE(pref))) sourcePref = &pref->userPref; else if (PREF_IS_LOCKED(pref)) sourcePref = &pref->defaultPref; else // do not save default prefs that haven't changed return PL_DHASH_NEXT; #if MOZ_PROFILESHARING if ((argData->saveTypes == SAVE_SHARED && !gSharedPrefHandler->IsPrefShared(pref->key)) || (argData->saveTypes == SAVE_NONSHARED && gSharedPrefHandler->IsPrefShared(pref->key))) return PL_DHASH_NEXT; #endif // strings are in quotes! if (pref->flags & PREF_STRING) { prefValue = '\"'; str_escape(sourcePref->stringVal, prefValue); prefValue += '\"'; } else if (pref->flags & PREF_INT) prefValue.AppendInt(sourcePref->intVal); else if (pref->flags & PREF_BOOL) prefValue = (sourcePref->boolVal) ? "true" : "false"; nsCAutoString prefName; str_escape(pref->key, prefName); argData->prefArray[i] = ToNewCString(NS_LITERAL_CSTRING("user_pref(\"") + prefName + NS_LITERAL_CSTRING("\", ") + prefValue + NS_LITERAL_CSTRING(");")); return PL_DHASH_NEXT; }
PLDHashOperator pref_savePref(PLDHashTable *table, PLDHashEntryHdr *heh, PRUint32 i, void *arg) { pref_saveArgs *argData = static_cast<pref_saveArgs *>(arg); PrefHashEntry *pref = static_cast<PrefHashEntry *>(heh); PR_ASSERT(pref); if (!pref) return PL_DHASH_NEXT; nsCAutoString prefValue; nsCAutoString prefPrefix; prefPrefix.Assign(NS_LITERAL_CSTRING("user_pref(\"")); // where we're getting our pref from PrefValue* sourcePref; if (PREF_HAS_USER_VALUE(pref) && (pref_ValueChanged(pref->defaultPref, pref->userPref, (PrefType) PREF_TYPE(pref)) || !(pref->flags & PREF_HAS_DEFAULT))) { sourcePref = &pref->userPref; } else { if (argData->saveTypes == SAVE_ALL_AND_DEFAULTS) { prefPrefix.Assign(NS_LITERAL_CSTRING("pref(\"")); sourcePref = &pref->defaultPref; } else // do not save default prefs that haven't changed return PL_DHASH_NEXT; } // strings are in quotes! if (pref->flags & PREF_STRING) { prefValue = '\"'; str_escape(sourcePref->stringVal, prefValue); prefValue += '\"'; } else if (pref->flags & PREF_INT) prefValue.AppendInt(sourcePref->intVal); else if (pref->flags & PREF_BOOL) prefValue = (sourcePref->boolVal) ? "true" : "false"; nsCAutoString prefName; str_escape(pref->key, prefName); argData->prefArray[i] = ToNewCString(prefPrefix + prefName + NS_LITERAL_CSTRING("\", ") + prefValue + NS_LITERAL_CSTRING(");")); return PL_DHASH_NEXT; }
/* This function will convert an attribute value, specified by the OID, * to a string. The result will be a null terminated string. * * res may be null. This will just return the res_size, needed to * hold the string. */ int _gnutls_x509_dn_to_string(const char *oid, void *value, int value_size, gnutls_datum_t * str) { const struct oid_to_string *oentry; int ret; gnutls_datum_t tmp; size_t size; if (value == NULL || value_size <= 0) { gnutls_assert(); return GNUTLS_E_INVALID_REQUEST; } oentry = get_oid_entry(oid); if (oentry == NULL) { /* unknown OID -> hex */ unknown_oid: str->size = value_size * 2 + 2; str->data = gnutls_malloc(str->size); if (str->data == NULL) return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); size = str->size; ret = data2hex(value, value_size, str->data, &size); if (ret < 0) { gnutls_assert(); gnutls_free(str->data); return ret; } str->size = size; return 0; } if (oentry->asn_desc != NULL) { /* complex */ ret = decode_complex_string(oentry, value, value_size, &tmp); if (ret < 0) { /* we failed decoding -> handle it as unknown OID */ goto unknown_oid; } } else { ret = _gnutls_x509_decode_string(oentry->etype, value, value_size, &tmp); if (ret < 0) { /* we failed decoding -> handle it as unknown OID */ goto unknown_oid; } } ret = str_escape(&tmp, str); _gnutls_free_datum(&tmp); if (ret < 0) return gnutls_assert_val(ret); return 0; }
void map_add(char c, const char *cmd) { struct map *m = umalloc(sizeof *m); m->c = c; m->cmd = ustrdup(cmd); str_escape(m->cmd); list_append(maps, m); }
static int u_delinvite(struct chanset_t *c, char *who, int doit) { int j, i = 0; maskrec *t; maskrec **u = c ? &(c->invites) : &global_invites; char temp[256]; if (!strchr(who, '!') && str_isdigit(who)) { j = atoi(who); j--; for (; (*u) && j; u = &((*u)->next), j--); if (*u) { strncpyz(temp, (*u)->mask, sizeof temp); i = 1; } else return -j - 1; } else { /* Find matching host, if there is one */ for (; *u && !i; u = &((*u)->next)) if (!rfc_casecmp((*u)->mask, who)) { strncpyz(temp, who, sizeof temp); i = 1; break; } if (!*u) return 0; } if (i && doit) { if (!noshare) { char *mask = str_escape(temp, ':', '\\'); if (mask) { /* Distribute chan invites differently */ if (c) shareout(c, "-invc %s %s\n", c->dname, mask); else shareout(NULL, "-inv %s\n", mask); nfree(mask); } } if (lastdeletedmask) nfree(lastdeletedmask); lastdeletedmask = nmalloc(strlen((*u)->mask) + 1); strcpy(lastdeletedmask, (*u)->mask); nfree((*u)->mask); if ((*u)->desc) nfree((*u)->desc); if ((*u)->user) nfree((*u)->user); t = *u; *u = (*u)->next; nfree(t); } return i; }
char *list_to_str(ADTList list, char *(*to_str)(void *)) { ADTList str_list = list_new(); for (ADTListItem it = list_head(list); it; it = list_next(it)) { char *str = to_str(list_value(it)); list_append(str_list, str_escape(str, (char *) ",")); free(str); } char *str = str_join(str_list, (char *) ","); list_full_release(str_list, free); return str; }
void test_strescape(void) { static struct strinput unesc[] = { { "foo", "foo" }, { "\\\\\\\\\\\"\\\"\\\'\\\'", "\\\\\"\"\'\'" }, { "\\a\\n\\r\\", "anr" } }; static struct strinput tabesc[] = { { "foo", "foo" }, { "\001", "\0011" }, { "\t", "\001t" }, { "\r", "\001r" }, { "\n", "\001n" }, { "\001\001\t\t\r\r\n\n", "\0011\0011\001t\001t\001r\001r\001n\001n" } }; unsigned char buf[1 << CHAR_BIT]; const char *escaped, *tabstr; string_t *str; unsigned int i; test_begin("str_escape"); for (i = 1; i < sizeof(buf); i++) buf[i-1] = i; buf[i-1] = '\0'; escaped = str_escape((char *)buf); test_assert(strlen(escaped) == (1 << CHAR_BIT) - 1 + 3); test_assert(escaped['\"'-1] == '\\'); /* 34 */ test_assert(escaped['\"'] == '\"'); test_assert(escaped['\''+1-1] == '\\'); /* 39 */ test_assert(escaped['\''+1] == '\''); test_assert(escaped['\\'+2-1] == '\\'); /* 92 */ test_assert(escaped['\\'+2] == '\\'); test_assert(strcmp(str_escape("\\\\\"\"\'\'"), "\\\\\\\\\\\"\\\"\\\'\\\'") == 0); test_end(); str = t_str_new(256); test_begin("str_unescape"); for (i = 0; i < N_ELEMENTS(unesc); i++) { test_assert(strcmp(str_unescape(t_strdup_noconst(unesc[i].input)), unesc[i].output) == 0); str_truncate(str, 0); str_append_unescaped(str, unesc[i].input, strlen(unesc[i].input)); test_assert(strcmp(str_c(str), unesc[i].output) == 0); } test_end(); test_begin("str_tabescape"); for (i = 0; i < N_ELEMENTS(tabesc); i++) { test_assert(strcmp(str_tabunescape(t_strdup_noconst(tabesc[i].output)), tabesc[i].input) == 0); test_assert(strcmp(str_tabescape(tabesc[i].input), tabesc[i].output) == 0); str_truncate(str, 0); str_append_tabunescaped(str, tabesc[i].output, strlen(tabesc[i].output)); test_assert(strcmp(str_c(str), tabesc[i].input) == 0); } str_truncate(str, 0); tabstr = "\0012\001l\001"; str_append_tabunescaped(str, tabstr, strlen(tabstr)); test_assert(strcmp(str_c(str), "2l") == 0); test_assert(strcmp(str_c(str), str_tabunescape(t_strdup_noconst(tabstr))) == 0); test_end(); }
void hdl_index_dir (int fd, const char *root, char *base_url, char *path, int fmt, int opt) { size_t off = 0; size_t ss = 1024; char *sm = malloc(ss * sizeof(char)); const int bo = 5 + strstr(base_url, "/index") - base_url; int bl = strlen(base_url) - 1; int num = 0; sm[0] = '\0'; switch (fmt) { case OUT_PLAIN: break; case OUT_JSON: { char *p1 = str_escape(path, 0, '\\'); raprintf(sm, off, ss, "{\"path\":\"%s\", \"index\":[", p1); free(p1); } break; case OUT_CSV: break; default: raprintf(sm, off, ss, DOCTYPE HTMLOPEN); raprintf(sm, off, ss, "<title>harvid Index</title>\n"); raprintf(sm, off, ss, "<style type=\"text/css\">\nli {float:left; margin:0 .5em .5em 0em; list-style-type:none;}\n</style>\n"); raprintf(sm, off, ss, "</head>\n"); raprintf(sm, off, ss, HTMLBODY); raprintf(sm, off, ss, "<h2>harvid - Index</h2>\n"); raprintf(sm, off, ss, "<p>Path: %s</p>\n<ul>\n", strlen(path) > 0 ? path : "<em>(docroot)</em>"); break; } if (bl > bo) { while (bl > bo && base_url[--bl] != '/') ; if (bl > bo) { base_url[bl] = 0; switch (fmt) { case OUT_PLAIN: case OUT_JSON: case OUT_CSV: break; default: raprintf(sm, off, ss, "<li>[D]<a href=\"%s/\">..</li>\n", base_url); break; } base_url[bl] = '/'; } } switch (fmt) { case OUT_PLAIN: parse_dir(fd, root, base_url, path, opt, &sm, &off, &ss, &num, print_plain); raprintf(sm, off, ss, "# total: %d\n", num); break; case OUT_JSON: parse_dir(fd, root, base_url, path, opt, &sm, &off, &ss, &num, print_json); raprintf(sm, off, ss, "]}"); break; case OUT_CSV: parse_dir(fd, root, base_url, path, opt, &sm, &off, &ss, &num, print_csv); break; default: parse_dir(fd, root, base_url, path, opt, &sm, &off, &ss, &num, print_html); raprintf(sm, off, ss, "</ul><div style=\"clear:both;\"></div>\n<p>Total Entries: %d</p>\n", num); raprintf(sm, off, ss, "<hr/><div style=\"text-align:center; color:#888;\">"SERVERVERSION"</div>"); raprintf(sm, off, ss, "</body>\n</html>"); break; } if (strlen(sm) > 0) CSEND(fd, sm); free(sm); }
int main(int argc, char** argv) { int c; char* sock_name; int sock_type; int s; struct binrpc_cmd cmd; struct id_list* sock_id; char* format; char* line; char* l; quit=0; format=0; line=0; s=-1; sock_name=0; sock_type=UNIXS_SOCK; opterr=0; while((c=getopt(argc, argv, "UVhs:D:R:vf:"))!=-1){ switch(c){ case 'V': printf("version: %s\n", version); printf("%s compiled on %s \n", __FILE__, compiled); exit(0); break; case 'h': printf("version: %s\n", version); printf("%s", help_msg); exit(0); break; case 's': sock_name=optarg; break; case 'R': reply_socket=optarg; break; case 'D': sock_dir=optarg; break; case 'U': sock_type=UDP_SOCK; break; case 'v': verbose++; break; case 'f': format=str_escape(optarg); if (format==0){ fprintf(stderr, "ERROR: memory allocation failure\n"); goto error; } break; case '?': if (isprint(optopt)) fprintf(stderr, "Unknown option `-%c'.\n", optopt); else fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); goto error; case ':': fprintf(stderr, "Option `-%c' requires an argument.\n", optopt); goto error; default: abort(); } } if (sock_name==0){ sock_name=DEFAULT_CTL_SOCKET; } /* init the random number generator */ srand(getpid()+time(0)); /* we don't need very strong random numbers */ if (sock_name==0){ fprintf(stderr, "ERROR: no server socket address specified\n"); goto error; } sock_id=parse_listen_id(sock_name, strlen(sock_name), sock_type); if (sock_id==0){ fprintf(stderr, "ERROR: error parsing server socket address %s\n", sock_name); goto error; } switch(sock_id->proto){ case UDP_SOCK: case TCP_SOCK: if (sock_id->port==0){ sock_id->port=DEFAULT_CTL_PORT; /* fprintf(stderr, "ERROR: no port specified: %s:<port>\n", sock_name); goto error; */ } if ((s=connect_tcpudp_socket(sock_id->name, sock_id->port, (sock_id->proto==UDP_SOCK)?SOCK_DGRAM: SOCK_STREAM))<0){ goto error; } break; case UNIXS_SOCK: case UNIXD_SOCK: if ((s=connect_unix_sock(sock_id->name, (sock_id->proto==UNIXD_SOCK)?SOCK_DGRAM: SOCK_STREAM))<0) goto error; break; case UNKNOWN_SOCK: fprintf(stderr, "ERROR: Bad socket type for %s\n", sock_name); goto error; } free(sock_id); /* not needed anymore */ sock_id=0; if (optind>=argc){ /*fprintf(stderr, "ERROR: no command specified\n"); goto error; */ }else{ if (parse_cmd(&cmd, &argv[optind], argc-optind)<0) goto error; if (run_cmd(s, &cmd, format)<0) goto error; goto end; } /* interactive mode */ if (get_kamcmd_list(s)==0){ #ifdef USE_CFG_VARS get_cfgvars_list(s); #endif /* USE_CFG_VARS */ #ifdef USE_COUNTERS get_counters_list(s); #endif /* USE_COUNTERS */ } /* banners */ printf("%s %s\n", NAME, VERSION); printf("%s\n", COPYRIGHT); printf("%s\n", DISCLAIMER); #ifdef USE_READLINE /* initialize readline */ /* allow conditional parsing of the ~/.inputrc file*/ rl_readline_name=NAME; rl_completion_entry_function=kamcmd_generator; rl_attempted_completion_function=kamcmd_completion; while(!quit){ line=readline(NAME "> "); if (line==0) /* EOF */ break; l=trim_ws(line); /* trim whitespace */ if (*l){ add_history(l); run_line(s, l, format); } free(line); line=0; } #else line=malloc(MAX_LINE_SIZE); if (line==0){ fprintf(stderr, "memory allocation error\n"); goto error; } printf(NAME "> "); fflush(stdout); /* prompt */ while(!quit && fgets(line, MAX_LINE_SIZE, stdin)){ l=trim_ws(line); if (*l){ run_line(s, l, format); } printf(NAME "> "); fflush(stdout); /* prompt */ }; free(line); line=0; #endif /* USE_READLINE */ end: /* normal exit */ if (line) free(line); if (format) free(format); if (rpc_array) free_rpc_array(rpc_array, rpc_no); #ifdef USE_CFG_VARS if (cfg_grp_lst) free_cfg_grp_lst(); if (cfg_vars_array){ free_rpc_array(cfg_vars_array, cfg_vars_no); cfg_vars_array=0; cfg_vars_no=0; } #endif /* USE_CFG_VARS */ #ifdef USE_COUNTERS if (cnt_grp_lst) free_cnt_grp_lst(); if (cnt_grps_array){ free_rpc_array(cnt_grps_array, cnt_grps_no); cnt_grps_array=0; cnt_grps_no=0; } #endif /* USE_COUNTERS */ cleanup(); exit(0); error: if (line) free(line); if (format) free(format); if (rpc_array) free_rpc_array(rpc_array, rpc_no); #ifdef USE_CFG_VARS if (cfg_grp_lst) free_cfg_grp_lst(); if (cfg_vars_array){ free_rpc_array(cfg_vars_array, cfg_vars_no); cfg_vars_array=0; cfg_vars_no=0; } #endif /* USE_CFG_VARS */ #ifdef USE_COUNTERS if (cnt_grp_lst) free_cnt_grp_lst(); if (cnt_grps_array){ free_rpc_array(cnt_grps_array, cnt_grps_no); cnt_grps_array=0; cnt_grps_no=0; } #endif /* USE_COUNTERS */ cleanup(); exit(-1); }
int proc_info(NetworkServer *net, Link *link, const Request &req, Response *resp){ SSDBServer *serv = (SSDBServer *)net->data; resp->push_back("ok"); resp->push_back("ssdb-server"); resp->push_back("version"); resp->push_back(SSDB_VERSION); { resp->push_back("links"); resp->add(net->link_count); } { int64_t calls = 0; proc_map_t::iterator it; for(it=net->proc_map.begin(); it!=net->proc_map.end(); it++){ Command *cmd = it->second; calls += cmd->calls; } resp->push_back("total_calls"); resp->add(calls); } { uint64_t size = serv->ssdb->size(); resp->push_back("dbsize"); resp->push_back(str(size)); } { std::string s = serv->ssdb->binlogs->stats(); resp->push_back("binlogs"); resp->push_back(s); } { std::vector<std::string> syncs = serv->backend_sync->stats(); std::vector<std::string>::iterator it; for(it = syncs.begin(); it != syncs.end(); it++){ std::string s = *it; resp->push_back("replication"); resp->push_back(s); } } { std::vector<Slave *>::iterator it; for(it = serv->slaves.begin(); it != serv->slaves.end(); it++){ Slave *slave = *it; std::string s = slave->stats(); resp->push_back("replication"); resp->push_back(s); } } { std::string val; std::string s, e; serv->get_kv_range(&s, &e); char buf[512]; { snprintf(buf, sizeof(buf), " kv : \"%s\" - \"%s\"", str_escape(s).c_str(), str_escape(e).c_str() ); val.append(buf); } { snprintf(buf, sizeof(buf), "\n hash: \"\" - \"\""); val.append(buf); } { snprintf(buf, sizeof(buf), "\n zset: \"\" - \"\""); val.append(buf); } { snprintf(buf, sizeof(buf), "\n list: \"\" - \"\""); val.append(buf); } resp->push_back("serv_key_range"); resp->push_back(val); } if(req.size() == 1 || req[1] == "range"){ std::string val; std::vector<std::string> tmp; int ret = serv->ssdb->key_range(&tmp); if(ret == 0){ char buf[512]; snprintf(buf, sizeof(buf), " kv : \"%s\" - \"%s\"", hexmem(tmp[0].data(), tmp[0].size()).c_str(), hexmem(tmp[1].data(), tmp[1].size()).c_str() ); val.append(buf); snprintf(buf, sizeof(buf), "\n hash: \"%s\" - \"%s\"", hexmem(tmp[2].data(), tmp[2].size()).c_str(), hexmem(tmp[3].data(), tmp[3].size()).c_str() ); val.append(buf); snprintf(buf, sizeof(buf), "\n zset: \"%s\" - \"%s\"", hexmem(tmp[4].data(), tmp[4].size()).c_str(), hexmem(tmp[5].data(), tmp[5].size()).c_str() ); val.append(buf); snprintf(buf, sizeof(buf), "\n list: \"%s\" - \"%s\"", hexmem(tmp[6].data(), tmp[6].size()).c_str(), hexmem(tmp[7].data(), tmp[7].size()).c_str() ); val.append(buf); } resp->push_back("data_key_range"); resp->push_back(val); } if(req.size() == 1 || req[1] == "leveldb"){ std::vector<std::string> tmp = serv->ssdb->info(); for(int i=0; i<(int)tmp.size(); i++){ std::string block = tmp[i]; resp->push_back(block); } } if(req.size() > 1 && req[1] == "cmd"){ proc_map_t::iterator it; for(it=net->proc_map.begin(); it!=net->proc_map.end(); it++){ Command *cmd = it->second; resp->push_back("cmd." + cmd->name); char buf[128]; snprintf(buf, sizeof(buf), "calls: %" PRIu64 "\ttime_wait: %.0f\ttime_proc: %.0f", cmd->calls, cmd->time_wait, cmd->time_proc); resp->push_back(buf); } } return 0; }
SSDBServer::SSDBServer(SSDB *ssdb, SSDB *meta, const Config &conf, NetworkServer *net){ this->ssdb = (SSDBImpl *)ssdb; this->meta = meta; net->data = this; this->reg_procs(net); int sync_speed = conf.get_num("replication.sync_speed"); backend_dump = new BackendDump(this->ssdb); backend_sync = new BackendSync(this->ssdb, sync_speed); expiration = new ExpirationHandler(this->ssdb); cluster = new Cluster(this->ssdb); if(cluster->init() == -1){ log_fatal("cluster init failed!"); exit(1); } { // slaves const Config *repl_conf = conf.get("replication"); if(repl_conf != NULL){ std::vector<Config *> children = repl_conf->children; for(std::vector<Config *>::iterator it = children.begin(); it != children.end(); it++){ Config *c = *it; if(c->key != "slaveof"){ continue; } std::string ip = c->get_str("ip"); int port = c->get_num("port"); if(ip == ""){ ip = c->get_str("host"); } if(ip == "" || port <= 0 || port > 65535){ continue; } bool is_mirror = false; std::string type = c->get_str("type"); if(type == "mirror"){ is_mirror = true; }else{ type = "sync"; is_mirror = false; } std::string id = c->get_str("id"); log_info("slaveof: %s:%d, type: %s", ip.c_str(), port, type.c_str()); Slave *slave = new Slave(ssdb, meta, ip.c_str(), port, is_mirror); if(!id.empty()){ slave->set_id(id); } slave->auth = c->get_str("auth"); slave->start(); slaves.push_back(slave); } } } // load kv_range int ret = this->get_kv_range(&this->kv_range_s, &this->kv_range_e); if(ret == -1){ log_fatal("load key_range failed!"); exit(1); } log_info("key_range.kv: \"%s\", \"%s\"", str_escape(this->kv_range_s).c_str(), str_escape(this->kv_range_e).c_str() ); }
static void solr_quote(string_t *dest, const char *str) { str_append_c(dest, '"'); str_append(dest, str_escape(str)); str_append_c(dest, '"'); }
std::string str(){ char buf[1024]; snprintf(buf, sizeof(buf), "(\"%s\", \"%s\"]", str_escape(start).c_str(), str_escape(end).c_str()); return std::string(buf); }
/* Parses an X509 DN in the asn1_struct, and puts the output into * the string buf. The output is an LDAP encoded DN. * * asn1_rdn_name must be a string in the form "tbsCertificate.issuer.rdnSequence". * That is to point in the rndSequence. */ int _gnutls_x509_parse_dn (ASN1_TYPE asn1_struct, const char *asn1_rdn_name, char *buf, size_t * sizeof_buf) { gnutls_string out_str; int k2, k1, result; char tmpbuffer1[MAX_NAME_SIZE]; char tmpbuffer2[MAX_NAME_SIZE]; char tmpbuffer3[MAX_NAME_SIZE]; opaque value[MAX_STRING_LEN], *value2 = NULL; char *escaped = NULL; const char *ldap_desc; char oid[128]; int len, printable; char *string = NULL; size_t sizeof_string, sizeof_escaped; if (sizeof_buf == NULL) { gnutls_assert (); return GNUTLS_E_INVALID_REQUEST; } if (buf) buf[0] = 0; else *sizeof_buf = 0; _gnutls_string_init (&out_str, gnutls_malloc, gnutls_realloc, gnutls_free); k1 = 0; do { k1++; /* create a string like "tbsCertList.issuer.rdnSequence.?1" */ if (asn1_rdn_name[0]!=0) snprintf( tmpbuffer1, sizeof (tmpbuffer1), "%s.?%u", asn1_rdn_name, k1); else snprintf( tmpbuffer1, sizeof (tmpbuffer1), "?%u", k1); len = sizeof (value) - 1; result = asn1_read_value (asn1_struct, tmpbuffer1, value, &len); if (result == ASN1_ELEMENT_NOT_FOUND) { break; } if (result != ASN1_VALUE_NOT_FOUND) { gnutls_assert (); result = _gnutls_asn2err (result); goto cleanup; } k2 = 0; do { /* Move to the attibute type and values */ k2++; if (tmpbuffer1[0] != 0) snprintf( tmpbuffer2, sizeof (tmpbuffer2), "%s.?%u", tmpbuffer1, k2); else snprintf( tmpbuffer2, sizeof (tmpbuffer2), "?%u", k2); /* Try to read the RelativeDistinguishedName attributes. */ len = sizeof (value) - 1; result = asn1_read_value (asn1_struct, tmpbuffer2, value, &len); if (result == ASN1_ELEMENT_NOT_FOUND) break; if (result != ASN1_VALUE_NOT_FOUND) { gnutls_assert (); result = _gnutls_asn2err (result); goto cleanup; } /* Read the OID */ _gnutls_str_cpy (tmpbuffer3, sizeof (tmpbuffer3), tmpbuffer2); _gnutls_str_cat (tmpbuffer3, sizeof (tmpbuffer3), ".type"); len = sizeof (oid) - 1; result = asn1_read_value (asn1_struct, tmpbuffer3, oid, &len); if (result == ASN1_ELEMENT_NOT_FOUND) break; else if (result != ASN1_SUCCESS) { gnutls_assert (); result = _gnutls_asn2err (result); goto cleanup; } /* Read the Value */ _gnutls_str_cpy (tmpbuffer3, sizeof (tmpbuffer3), tmpbuffer2); _gnutls_str_cat (tmpbuffer3, sizeof (tmpbuffer3), ".value"); len = 0; result = asn1_read_value (asn1_struct, tmpbuffer3, NULL, &len); value2 = gnutls_malloc (len); if (value2 == NULL) { gnutls_assert (); result = GNUTLS_E_MEMORY_ERROR; goto cleanup; } result = asn1_read_value (asn1_struct, tmpbuffer3, value2, &len); if (result != ASN1_SUCCESS) { gnutls_assert (); result = _gnutls_asn2err (result); goto cleanup; } #define STR_APPEND(y) if ((result=_gnutls_string_append_str( &out_str, y)) < 0) { \ gnutls_assert(); \ goto cleanup; \ } /* The encodings of adjoining RelativeDistinguishedNames are separated * by a comma character (',' ASCII 44). */ /* Where there is a multi-valued RDN, the outputs from adjoining * AttributeTypeAndValues are separated by a plus ('+' ASCII 43) * character. */ if (k1 != 1) { /* the first time do not append a comma */ if (k2 != 1) { /* adjoining multi-value RDN */ STR_APPEND ("+"); } else { STR_APPEND (","); } } ldap_desc = oid2ldap_string (oid); printable = _gnutls_x509_oid_data_printable (oid); sizeof_escaped = 2 * len + 1; escaped = gnutls_malloc (sizeof_escaped); if (escaped == NULL) { gnutls_assert (); result = GNUTLS_E_MEMORY_ERROR; goto cleanup; } sizeof_string = 2 * len + 2; /* in case it is not printable */ string = gnutls_malloc (sizeof_string); if (string == NULL) { gnutls_assert (); result = GNUTLS_E_MEMORY_ERROR; goto cleanup; } STR_APPEND (ldap_desc); STR_APPEND ("="); if (printable) result = _gnutls_x509_oid_data2string (oid, value2, len, string, &sizeof_string); else result = _gnutls_x509_data2hex (value2, len, string, &sizeof_string); if (result < 0) { gnutls_assert (); _gnutls_x509_log ("Found OID: '%s' with value '%s'\n", oid, _gnutls_bin2hex (value2, len, escaped, sizeof_escaped)); goto cleanup; } STR_APPEND (str_escape (string, escaped, sizeof_escaped)); gnutls_free (string); string = NULL; gnutls_free (escaped); escaped = NULL; gnutls_free (value2); value2 = NULL; } while (1); } while (1); if (out_str.length >= (unsigned int) *sizeof_buf) { gnutls_assert (); *sizeof_buf = out_str.length + 1; result = GNUTLS_E_SHORT_MEMORY_BUFFER; goto cleanup; } if (buf) { memcpy (buf, out_str.data, out_str.length); buf[out_str.length] = 0; } *sizeof_buf = out_str.length; result = 0; cleanup: gnutls_free (value2); gnutls_free (string); gnutls_free (escaped); _gnutls_string_clear (&out_str); return result; }
/* Note: If first char of note is '*' it's a sticky exempt. */ static int u_addexempt(struct chanset_t *chan, char *exempt, char *from, char *note, time_t expire_time, int flags) { char host[1024], s[1024]; maskrec *p = NULL, *l, **u = chan ? &chan->exempts : &global_exempts; strcpy(host, exempt); /* Choke check: fix broken exempts (must have '!' and '@') */ if ((strchr(host, '!') == NULL) && (strchr(host, '@') == NULL)) strcat(host, "!*@*"); else if (strchr(host, '@') == NULL) strcat(host, "@*"); else if (strchr(host, '!') == NULL) { char *i = strchr(host, '@'); strcpy(s, i); *i = 0; strcat(host, "!*"); strcat(host, s); } for (l = *u; l; l = l->next) if (!rfc_casecmp(l->mask, host)) { p = l; break; } /* It shouldn't expire and be sticky also */ if (note[0] == '*') { flags |= MASKREC_STICKY; note++; } if ((expire_time == 0L) || (flags & MASKREC_PERM)) { flags |= MASKREC_PERM; expire_time = 0L; } if (p == NULL) { p = user_malloc(sizeof(maskrec)); p->next = *u; *u = p; } else { nfree(p->mask); nfree(p->user); nfree(p->desc); } p->expire = expire_time; p->added = now; p->lastactive = 0; p->flags = flags; p->mask = user_malloc(strlen(host) + 1); strcpy(p->mask, host); p->user = user_malloc(strlen(from) + 1); strcpy(p->user, from); p->desc = user_malloc(strlen(note) + 1); strcpy(p->desc, note); if (!noshare) { char *mask = str_escape(host, ':', '\\'); if (mask) { if (!chan) shareout(NULL, "+e %s %li %s%s %s %s\n", mask, expire_time - now, (flags & MASKREC_STICKY) ? "s" : "", (flags & MASKREC_PERM) ? "p" : "-", from, note); else shareout(chan, "+ec %s %li %s %s%s %s %s\n", mask, expire_time - now, chan->dname, (flags & MASKREC_STICKY) ? "s" : "", (flags & MASKREC_PERM) ? "p" : "-", from, note); nfree(mask); } } return 1; }
/* Note: If first char of note is '*' it's a sticky ban. */ static int u_addban(struct chanset_t *chan, char *ban, char *from, char *note, time_t expire_time, int flags) { char host[1024], s[1024]; maskrec *p = NULL, *l, **u = chan ? &chan->bans : &global_bans; module_entry *me; strcpy(host, ban); /* Choke check: fix broken bans (must have '!' and '@') */ if ((strchr(host, '!') == NULL) && (strchr(host, '@') == NULL)) strcat(host, "!*@*"); else if (strchr(host, '@') == NULL) strcat(host, "@*"); else if (strchr(host, '!') == NULL) { char *i = strchr(host, '@'); strcpy(s, i); *i = 0; strcat(host, "!*"); strcat(host, s); } if ((me = module_find("server", 0, 0)) && me->funcs) { simple_sprintf(s, "%s!%s", me->funcs[SERVER_BOTNAME], me->funcs[SERVER_BOTUSERHOST]); if (match_addr(host, s)) { putlog(LOG_MISC, "*", IRC_IBANNEDME); return 0; } } if (expire_time == now) return 1; for (l = *u; l; l = l->next) if (!rfc_casecmp(l->mask, host)) { p = l; break; } /* It shouldn't expire and be sticky also */ if (note[0] == '*') { flags |= MASKREC_STICKY; note++; } if ((expire_time == 0L) || (flags & MASKREC_PERM)) { flags |= MASKREC_PERM; expire_time = 0L; } if (p == NULL) { p = user_malloc(sizeof(maskrec)); p->next = *u; *u = p; } else { nfree(p->mask); nfree(p->user); nfree(p->desc); } p->expire = expire_time; p->added = now; p->lastactive = 0; p->flags = flags; p->mask = user_malloc(strlen(host) + 1); strcpy(p->mask, host); p->user = user_malloc(strlen(from) + 1); strcpy(p->user, from); p->desc = user_malloc(strlen(note) + 1); strcpy(p->desc, note); if (!noshare) { char *mask = str_escape(host, ':', '\\'); if (mask) { if (!chan) shareout(NULL, "+b %s %li %s%s %s %s\n", mask, expire_time - now, (flags & MASKREC_STICKY) ? "s" : "", (flags & MASKREC_PERM) ? "p" : "-", from, note); else shareout(chan, "+bc %s %li %s %s%s %s %s\n", mask, expire_time - now, chan->dname, (flags & MASKREC_STICKY) ? "s" : "", (flags & MASKREC_PERM) ? "p" : "-", from, note); nfree(mask); } } return 1; }