static int _losetup ( const char *loop, // Loop device const char *file, // Target file (or block device) int fatal ) { int r, loopfd = -1, filefd = -1, mode; r = filefd = open(file, mode = O_RDWR); if (r < 0) { r = filefd = open(file, mode = O_RDONLY); if (r < 0) { if (fatal) _fatal("cannot open %s", file); goto exit; } } r = loopfd = open(loop, mode); if (r < 0) { r = loopfd = open(loop, mode = O_RDONLY); if (r < 0) { if (fatal) _fatal("cannot open %s", loop); goto exit; } } r = losetup(loopfd, filefd, file); if (r < 0 && fatal) _fatal("cannot setup loop device %s", loop); exit: close(loopfd); close(filefd); return r; }
mword *bpdl_const_to_bstruct(bvm_cache *this_bvm, mword *entry, mword entry_type){ // bpdl_const_to_bstruct# #ifdef BPDL_TRACE _trace; #endif switch(entry_type){ case BPDL_LIST_ENTRY_DNUMBER: #ifdef BPDL_TRACE _trace; #endif return bpdl_dnumber_to_bstruct(this_bvm, entry); case BPDL_LIST_ENTRY_HNUMBER: #ifdef BPDL_TRACE _trace; #endif return bpdl_hnumber_to_bstruct(this_bvm, entry); case BPDL_LIST_ENTRY_DQUOTE: case BPDL_LIST_ENTRY_SQUOTE: #ifdef BPDL_TRACE _trace; #endif return bpdl_quote_to_bstruct(this_bvm, entry); default: _fatal("Something went wrong"); //FIXME Throw exception } _fatal("Something went wrong"); //FIXME Throw exception return nil; }
void *be_pg_init() { struct pg_backend *conf; char *host, *user, *pass, *dbname, *p, *port; char *userquery; _log(LOG_DEBUG, "}}}} POSTGRES"); host = p_stab("host"); p = p_stab("port"); user = p_stab("user"); pass = p_stab("pass"); dbname = p_stab("dbname"); host = (host) ? host : strdup("localhost"); port = (p) ? p : strdup("5432"); userquery = p_stab("userquery"); if (!userquery) { _fatal("Mandatory option 'userquery' is missing"); return (NULL); } if ((conf = (struct pg_backend *)malloc(sizeof(struct pg_backend))) == NULL) return (NULL); conf->conn = NULL; conf->host = host; conf->port = port; conf->user = user; conf->pass = pass; conf->dbname = dbname; conf->userquery = userquery; conf->superquery = p_stab("superquery"); conf->aclquery = p_stab("aclquery"); _log( LOG_DEBUG, "HERE: %s", conf->superquery ); _log( LOG_DEBUG, "HERE: %s", conf->aclquery ); char *connect_string = NULL; conf->conn = PQsetdbLogin(conf->host, conf->port, NULL, NULL, conf->dbname, conf->user, conf->pass ); if (PQstatus(conf->conn) == CONNECTION_BAD) { free(conf); free(connect_string); _fatal("We were unable to connect to the database"); return (NULL); } free(connect_string); return ((void *)conf); }
static int get_string_envs(CURL *curl, const char *required_env, char *querystring) { char *data = NULL; char *escaped_key = NULL; char *escaped_val = NULL; char *env_string = NULL; char *params_key[MAXPARAMSNUM]; char *env_names[MAXPARAMSNUM]; char *env_value[MAXPARAMSNUM]; int i, num = 0; //_log(LOG_DEBUG, "sys_envs=%s", sys_envs); env_string = (char *)malloc( strlen(required_env) + 20); if (env_string == NULL) { _fatal("ENOMEM"); return (-1); } sprintf(env_string, "%s", required_env); //_log(LOG_DEBUG, "env_string=%s", env_string); num = get_sys_envs(env_string, ",", "=", params_key, env_names, env_value); //sprintf(querystring, ""); for( i = 0; i < num; i++ ){ escaped_key = curl_easy_escape(curl, params_key[i], 0); escaped_val = curl_easy_escape(curl, env_value[i], 0); //_log(LOG_DEBUG, "key=%s", params_key[i]); //_log(LOG_DEBUG, "escaped_key=%s", escaped_key); //_log(LOG_DEBUG, "escaped_val=%s", escaped_envvalue); data = (char *)malloc(strlen(escaped_key) + strlen(escaped_val) + 1); if ( data == NULL ) { _fatal("ENOMEM"); return (-1); } sprintf(data, "%s=%s&", escaped_key, escaped_val); if ( i == 0 ) { sprintf(querystring, "%s", data); } else { strcat(querystring, data); } } if (data) free(data); if (escaped_key) free(escaped_key); if (escaped_val) free(escaped_val); free(env_string); return (num); }
void *be_http_init() { struct http_backend *conf; char *ip; char *getuser_uri; char *superuser_uri; char *aclcheck_uri; if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { _fatal("init curl fail"); return (NULL); } if ((ip = p_stab("http_ip")) == NULL) { _fatal("Mandatory parameter `http_ip' missing"); return (NULL); } if ((getuser_uri = p_stab("http_getuser_uri")) == NULL) { _fatal("Mandatory parameter `http_getuser_uri' missing"); return (NULL); } if ((superuser_uri = p_stab("http_superuser_uri")) == NULL) { _fatal("Mandatory parameter `http_superuser_uri' missing"); return (NULL); } if ((aclcheck_uri = p_stab("http_aclcheck_uri")) == NULL) { _fatal("Mandatory parameter `http_aclcheck_uri' missing"); return (NULL); } conf = (struct http_backend *)malloc(sizeof(struct http_backend)); conf->ip = ip; conf->port = p_stab("http_port") == NULL ? 80 : atoi(p_stab("http_port")); if (p_stab("http_hostname") != NULL) { conf->hostheader = (char *)malloc(128); sprintf(conf->hostheader, "Host: %s", p_stab("http_hostname")); } else { conf->hostheader = NULL; } conf->getuser_uri = getuser_uri; conf->superuser_uri = superuser_uri; conf->aclcheck_uri = aclcheck_uri; _log(LOG_DEBUG, "getuser_uri=%s", getuser_uri); _log(LOG_DEBUG, "superuser_uri=%s", superuser_uri); _log(LOG_DEBUG, "aclcheck_uri=%s", aclcheck_uri); return (conf); };
static void _preload_init(void) { void *libc; #ifndef DL_LAZY # define DL_LAZY RTLD_LAZY #endif if (!(libc = dlopen(_PATH_LIBC, DL_LAZY))) { _fatal("couldn't dlopen " _PATH_LIBC); } else if (!(_sys_open = dlsym(libc, "open"))) { _fatal("couldn't dlsym 'open'"); } else if (!(_sys_fopen = dlsym(libc, "fopen"))) { _fatal("couldn't dlsym 'fopen'"); } }
void *be_mongo_init() { struct mongo_backend *conf; conf = (struct mongo_backend *)malloc(sizeof(struct mongo_backend)); conf->database = strdup(be_mongo_get_option("mongo_database", NULL, "mqGate")); conf->user_coll = strdup(be_mongo_get_option("mongo_user_coll", "mongo_collection_users", "users")); conf->topiclist_coll = strdup(be_mongo_get_option("mongo_topiclist_coll", "mongo_collection_topics", "topics")); conf->user_username_prop = strdup(be_mongo_get_option("mongo_user_username_prop", NULL, "username")); conf->user_password_prop = strdup(be_mongo_get_option("mongo_user_password_prop", "mongo_location_password", "password")); conf->user_superuser_prop = strdup(be_mongo_get_option("mongo_user_superuser_prop", "mongo_location_superuser", "superuser")); conf->user_topics_prop = strdup(be_mongo_get_option("mongo_user_topics_prop", NULL, "topics")); conf->user_topiclist_fk_prop = strdup(be_mongo_get_option("mongo_user_topiclist_fk_prop", "mongo_location_topic", "topics")); conf->topiclist_key_prop = strdup(be_mongo_get_option("mongo_topiclist_key_prop", "mongo_location_superuser", "_id")); conf->topiclist_topics_prop = strdup(be_mongo_get_option("mongo_topiclist_topics_prop", "mongo_location_topic", "topics")); mongoc_init(); mongoc_uri_t *uri = be_mongo_new_uri_from_options(); if (!uri) { _fatal("MongoDB connection options invalid"); } conf->client = mongoc_client_new_from_uri(uri); mongoc_uri_destroy(uri); return (conf); }
static void * _xmalloc(size_t s) { void *p = malloc(s); if (!p) _fatal("libggcov: out of memory\n"); return p; }
void _fatal_perror(const char *file, int line, const char *func, const char *fmt, ...) { va_list ap; char buf[1024]; va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); _fatal(file, line, func, true, "%s: %s", buf, strerror(errno)); }
static int _mkdir (const char *path, int mode, int fatal) { int r; r = _mkpath(path, mode, fatal); if (r < 0) return r; r = mkdir(path, mode); if (r < 0 && errno != EEXIST) { if (fatal) _fatal("cannot create directory %s", path); return r; } return 0; }
/* Create standard Window */ extern "C" HWND APIENTRY Win32CreateStdWindow(HWND hwndParent, ULONG flStyle, PULONG pflCreateFlags, PCSZ pszClientClass, PCSZ pszTitle, ULONG styleClient, HMODULE hmod, ULONG idResources, PHWND phwndClient) { HWND hwndFrame=NULL, hwndClient=NULL; int rc,data,len; HAB iHab; STD_Window *pStdW; iHab = _hab.GetCurrentHAB(); if(iHab < 0) { //_hab.SetError(ihab - bad! , PMERR_INVALID_HAB); debug(3, 0)("WARNING: WinCreateStdWindow: bad ihab %x\n",iHab); _fatal("Thread don't initialized to FreePM"); return NULL; } pStdW = new STD_Window(); pStdW->CreateSTD_Window(hwndParent, /* Parent-window handle. */ flStyle, /* Frame Window style. */ pflCreateFlags, pszClientClass, /* Client Class */ pszTitle, /* Title */ styleClient, /* Client Window style. */ hmod, /* resource module */ idResources, /* Window resource identifier. */ phwndClient); /* return: client window hwnd */ debug(3, 2)( __FUNCTION__ "is not yet full implemented\n"); //create hwndFrame hwndFrame = pStdW->GetHandle(); ////create hwndClient // rc = F_SendCmdToServer(client_obj, F_CMD_WINCREATE_HWND, iHab); ////todo check rc // rc = F_RecvDataFromServer(client_obj, &hwndFrame, &len, sizeof(HWND)); ////todo check rc ////create hwndFrame // rc = F_SendCmdToServer(client_obj, F_CMD_WINCREATE_HWND, iHab); ////todo check rc // rc = F_RecvDataFromServer(client_obj, &hwndClient, &len, sizeof(HWND)); ////todo check rc // *phwndClient = hwndClient; ////todo: ////make hwndFrame frame window, hwndClient - client window return hwndFrame; }
static int _read_line (const char *path, char *buf, int len, int fatal) { int r, f; char *s; f = open(path, O_RDONLY); if (f < 0) { if (fatal) _fatal("cannot open %s", path); return f; } r = read(f, buf, len - 1); close(f); if (r < 0) { if (fatal) _fatal("cannot read %s", path); return r; } buf[r] = '\0'; if ((s = strchr(buf, '\n')) != NULL) *s = '\0'; return 0; }
EXTERN_C char* C_make_string(const char* src,size_t src_len) { assert(src); #ifdef __cplusplus char* const tmp = zaimoni::_new_buffer_nonNULL_throws<char>(ZAIMONI_LEN_WITH_NULL(src_len)); #else char* const tmp = (char*)calloc(1,ZAIMONI_LEN_WITH_NULL(src_len)); if (!tmp) _fatal("FATAL: RAM exhaustion"); #endif memmove(tmp,src,src_len); return tmp; }
static int _mkpath (const char *path, int mode, int fatal) { int i, r; char buf [256]; if (!path[0]) return 0; for (buf[0] = path[0], i = 1; buf[i] = '\0', path[i]; buf[i] = path[i], i++) { if (path[i] != '/') continue; r = mkdir(buf, mode); if (r < 0 && errno != EEXIST) { if (fatal) _fatal("cannot create directory %s", buf); return r; } } return 0; }
/* Linux-specific tweak to call the real open() */ static int real_open(const char *filename, int flags, int mode) { static int (*func)(const char *, int, int) = 0; if (!func) { func = (int (*)(const char *, int, int))object_to_function(dlsym(RTLD_NEXT, "open")); if (!func) _fatal("libggcov: cannot get real open() " "call address, exiting\n"); } return func(filename, flags, mode); }
static int _mount ( const char *source, // Source device const char *target, // Destination directory (will be made if necessary) const char *type, // Filesystem type (NULL to guess) unsigned long flags, // Mount flags const void *data, // Special data (depens on filesystem) int fatal ) { int r = _mkdir(target, 0755, fatal); if (r < 0) return r; if (type == NULL && !(flags & MS_MOVE)) { // Don't find out if moving type = fstype(source); if (type == NULL) { if (fatal) _fatal("cannot not guess %s filesystem type", source); return -1; } } r = mount(source, target, type, flags, data); if (r < 0 && fatal) _fatal("mounting %s on %s failed", source, target); return r; }
_export struct io_chunk_t _impl_os_path_chunk(enum os_path_e id) { return (struct io_chunk_t){ path_proc, (void *)id }; } /** * Processing callback for OS paths. * @output: The output stream. * @arg: The argument. */ static void path_proc(struct io_output_t output, void *arg) { enum os_path_e id = (enum os_path_e)arg; switch(id) { case os_path_lib_e: #ifdef BMAKE__PATH_LIB io_print_str(output, BMAKE__PATH_LIB); #else io_print_str(output, "/usr/local/lib"); #endif break; case os_path_share_e: #ifdef BMAKE__PATH_SHARE io_print_str(output, BMAKE__PATH_SHARE); #else io_print_str(output, "/usr/local/share"); #endif break; case os_path_user_config_e: io_printf(output, "%s/.config", os_env_get("HOME")); break; default: _fatal("Invalid OS path lookup. ID not recognized."); } }
// Safely, recursively de-references a tag mword *tptr_detag(bvm_cache *this_bvm, mword *tptr){ // tptr_detag# static int livelock_detect=0; if(is_nil(tptr)){ return nil; } if(is_tptr(tptr)){ if(livelock_detect++ > MAX_DETAG_DEPTH){ //cat_except(this_bvm); _fatal("FIXME: this should have been a cat_except..."); } return tptr_detag(this_bvm, get_tptr(tptr)); } else{ livelock_detect=0; return tptr; } }
mword *inline_bpdl_label_list(bvm_cache *this_bvm, mword *sexpr){ // inline_bpdl_label_list# mword *sexpr_label = (mword*)icar(sexpr); if(bpdl_is_label_nil(sexpr_label)){ return nil; } mword *macro_code_list; mword *bpdl_list; mword *result = nil; mword *bpdl_label = _lf2by(this_bvm, sexpr_label); mword *bpdl_label_hash = _hash8(this_bvm,bpdl_label); if(trie_exists(this_bvm, bpdl_macro_table, bpdl_label_hash, nil)){ bpdl_list = inline_bpdl_list_list(this_bvm, (mword *)icdr(sexpr)); macro_code_list = _cons( this_bvm, _cp( this_bvm, _ith( this_bvm, trie_lookup_hash(this_bvm, bpdl_macro_table, bpdl_label_hash, nil), 2) ), nil); // result = lib_babelc(this_bvm, (mword*)icar(macro_code_list), bpdl_list); result = lib_babelcs(this_bvm, (mword*)icar(macro_code_list), bpdl_list, this_bvm->soft_root); } else{ _fatal("Unrecognized label-list"); //FIXME Throw exception } return result; }
void ReplSetImpl::loadConfig() { startupStatus = LOADINGCONFIG; startupStatusMsg.set("loading " + rsConfigNs + " config (LOADINGCONFIG)"); LOG(1) << "loadConfig() " << rsConfigNs << endl; while (1) { try { OwnedPointerVector<ReplSetConfig> configs; try { configs.mutableVector().push_back(ReplSetConfig::makeDirect()); } catch (DBException& e) { log() << "replSet exception loading our local replset configuration object : " << e.toString() << rsLog; } for (vector<HostAndPort>::const_iterator i = _seeds->begin(); i != _seeds->end(); i++) { try { configs.mutableVector().push_back(ReplSetConfig::make(*i)); } catch (DBException& e) { log() << "replSet exception trying to load config from " << *i << " : " << e.toString() << rsLog; } } { scoped_lock lck(replSettings.discoveredSeeds_mx); if (replSettings.discoveredSeeds.size() > 0) { for (set<string>::iterator i = replSettings.discoveredSeeds.begin(); i != replSettings.discoveredSeeds.end(); i++) { try { configs.mutableVector().push_back( ReplSetConfig::make(HostAndPort(*i))); } catch (DBException&) { LOG(1) << "replSet exception trying to load config from discovered " "seed " << *i << rsLog; replSettings.discoveredSeeds.erase(*i); } } } } if (!replSettings.reconfig.isEmpty()) { try { configs.mutableVector().push_back(ReplSetConfig::make(replSettings.reconfig, true)); } catch (DBException& re) { log() << "replSet couldn't load reconfig: " << re.what() << rsLog; replSettings.reconfig = BSONObj(); } } int nok = 0; int nempty = 0; for (vector<ReplSetConfig*>::iterator i = configs.mutableVector().begin(); i != configs.mutableVector().end(); i++) { if ((*i)->ok()) nok++; if ((*i)->empty()) nempty++; } if (nok == 0) { if (nempty == (int) configs.mutableVector().size()) { startupStatus = EMPTYCONFIG; startupStatusMsg.set("can't get " + rsConfigNs + " config from self or any seed (EMPTYCONFIG)"); log() << "replSet can't get " << rsConfigNs << " config from self or any seed (EMPTYCONFIG)" << rsLog; static unsigned once; if (++once == 1) { log() << "replSet info you may need to run replSetInitiate -- rs.initia" "te() in the shell -- if that is not already done" << rsLog; } if (_seeds->size() == 0) { LOG(1) << "replSet info no seed hosts were specified on the --replSet " "command line" << rsLog; } } else { startupStatus = EMPTYUNREACHABLE; startupStatusMsg.set("can't currently get " + rsConfigNs + " config from self or any seed (EMPTYUNREACHABLE)"); log() << "replSet can't get " << rsConfigNs << " config from self or any seed (yet)" << rsLog; } sleepsecs(1); continue; } if (!_loadConfigFinish(configs.mutableVector())) { log() << "replSet info Couldn't load config yet. Sleeping 20sec and will try " "again." << rsLog; sleepsecs(20); continue; } } catch (DBException& e) { startupStatus = BADCONFIG; startupStatusMsg.set("replSet error loading set config (BADCONFIG)"); log() << "replSet error loading configurations " << e.toString() << rsLog; log() << "replSet error replication will not start" << rsLog; sethbmsg("error loading set config"); _fatal(); throw; } break; } startupStatusMsg.set("? started"); startupStatus = STARTED; }
mword *inline_bpdl_code_list(bvm_cache *this_bvm, mword *sexpr){ // inline_bpdl_code_list# #ifdef BPDL_TRACE _trace; #endif // 1. string // 2. number // 3. built-in (opcodes) // 3. label (lookup in sym-table) // 4. recurse inline_bpdl mword entry_type; mword *list_head = nil; mword *list_curr = nil; mword *new_entry; mword *bpdl_label; mword *bpdl_label_hash; while(!is_nil(sexpr)){ entry_type = get_bpdl_list_entry_type((mword*)icar(sexpr)); switch(entry_type){ case BPDL_LIST_ENTRY_DNUMBER: case BPDL_LIST_ENTRY_HNUMBER: case BPDL_LIST_ENTRY_DQUOTE: case BPDL_LIST_ENTRY_SQUOTE: new_entry = _cons( this_bvm, _cons( this_bvm, bpdl_const_to_bstruct( this_bvm, (mword*)icar(sexpr), entry_type), nil), nil); if(is_nil(list_head)){ list_head = list_curr = new_entry; } else{ _append_direct(this_bvm, list_curr, new_entry); list_curr = (mword*)icdr(list_curr); // Save unnecessary traversal } break; case BPDL_LIST_ENTRY_LABEL: if(_arcmp((mword*)icar(sexpr), SEXPR_NIL_SYMBOL) == 0){ new_entry = _cons( this_bvm, _cons( this_bvm, nil, nil), nil); } else{ // if built-in: // substitute opcode-value bpdl_label = _lf2by(this_bvm, (mword*)icar(sexpr)); bpdl_label_hash = _hash8(this_bvm,bpdl_label); if(trie_exists(this_bvm, bpdl_opcode_table, bpdl_label_hash, nil)){ new_entry = _cons( this_bvm, _cp( this_bvm, _ith( this_bvm, trie_lookup_hash(this_bvm, bpdl_opcode_table, bpdl_label_hash, nil), 2) ), nil); } else{ new_entry = _cons( this_bvm, tptr_new(this_bvm, bpdl_label_hash, nil), nil); } } if(is_nil(list_head)){ list_head = list_curr = new_entry; } else{ _append_direct(this_bvm, list_curr, new_entry); list_curr = (mword*)icdr(list_curr); // Save unnecessary traversal } break; case BPDL_LIST_ENTRY_INTE: new_entry = _cons( this_bvm, _cons( this_bvm, inline_bpdl(this_bvm, (mword*)icar(sexpr)), nil), nil); if(is_nil(list_head)){ list_head = list_curr = new_entry; } else{ _append_direct(this_bvm, list_curr, new_entry); list_curr = (mword*)icdr(list_curr); // Save unnecessary traversal } break; default: _d(entry_type); _fatal("Unrecognized bpdl list entry"); //FIXME Throw exception } sexpr = (mword*)icdr(sexpr); } return list_head; }
int SSgetSymbol (Setsetp ss, int index) { if (ss->nsets <= index) _fatal ("SSgetSymbol() index out of range."); return ss->onsymbols[index]; }
Intsetp SSget (Setsetp ss, int index) { if (ss->nsets <= index) _fatal ("SSget() index out of range."); return ss->sets[index]; }
static int http_post(void *handle, char *uri, const char *clientid, const char *token, const char *topic, int acc, int method) { struct jwt_backend *conf = (struct jwt_backend *)handle; CURL *curl; struct curl_slist *headerlist=NULL; int re; int respCode = 0; int ok = FALSE; char *url; char *data; if (token == NULL) { return (FALSE); } clientid = (clientid && *clientid) ? clientid : ""; topic = (topic && *topic) ? topic : ""; if ((curl = curl_easy_init()) == NULL) { _fatal("create curl_easy_handle fails"); return (FALSE); } if (conf->hostheader != NULL) headerlist = curl_slist_append(headerlist, conf->hostheader); headerlist = curl_slist_append(headerlist, "Expect:"); //_log(LOG_NOTICE, "u=%s p=%s t=%s acc=%d", username, password, topic, acc); url = (char *)malloc(strlen(conf->ip) + strlen(uri) + 20); if (url == NULL) { _fatal("ENOMEM"); return (FALSE); } // enable the https if (strcmp(conf->with_tls, "true") == 0){ sprintf(url, "https://%s:%d%s", conf->ip, conf->port, uri); }else{ sprintf(url, "http://%s:%d%s", conf->ip, conf->port, uri); } char* escaped_token = curl_easy_escape(curl, token, 0); char* escaped_topic = curl_easy_escape(curl, topic, 0); char* escaped_clientid = curl_easy_escape(curl, clientid, 0); char string_acc[20]; snprintf(string_acc, 20, "%d", acc); char *string_envs = (char *)malloc(MAXPARAMSLEN); if (string_envs == NULL) { _fatal("ENOMEM"); return (FALSE); } memset(string_envs, 0, MAXPARAMSLEN); //get the sys_env from here int env_num = 0; if ( method == METHOD_GETUSER && conf->getuser_envs != NULL ){ env_num = get_string_envs(curl, conf->getuser_envs, string_envs); }else if ( method == METHOD_SUPERUSER && conf->superuser_envs != NULL ){ env_num = get_string_envs(curl, conf->superuser_envs, string_envs); } else if ( method == METHOD_ACLCHECK && conf->aclcheck_envs != NULL ){ env_num = get_string_envs(curl, conf->aclcheck_envs, string_envs); } if( env_num == -1 ){ return (FALSE); } //---- over ---- data = (char *)malloc(strlen(string_envs) + strlen(escaped_topic) + strlen(string_acc) + strlen(escaped_clientid) + 30); if (data == NULL) { _fatal("ENOMEM"); return (FALSE); } sprintf(data, "%stopic=%s&acc=%s&clientid=%s", string_envs, escaped_topic, string_acc, clientid); _log(LOG_DEBUG, "url=%s", url); _log(LOG_DEBUG, "data=%s", data); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); char *token_header = (char *)malloc(strlen(escaped_token) + 22); if (token_header == NULL) { _fatal("ENOMEM"); return (FALSE); } sprintf(token_header, "Authorization: Bearer %s", escaped_token); headerlist = curl_slist_append(headerlist, token_header); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); re = curl_easy_perform(curl); if (re == CURLE_OK) { re = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respCode); if (re == CURLE_OK && respCode >= 200 && respCode < 300) { ok = TRUE; } else if (re == CURLE_OK && respCode >= 500) { ok = BACKEND_ERROR; } else { //_log(LOG_NOTICE, "http auth fail re=%d respCode=%d", re, respCode); } } else { _log(LOG_DEBUG, "http req fail url=%s re=%s", url, curl_easy_strerror(re)); ok = BACKEND_ERROR; } curl_easy_cleanup(curl); curl_slist_free_all (headerlist); free(url); free(data); free(string_envs); free(escaped_token); free(token_header); free(escaped_topic); free(escaped_clientid); return (ok); }
void *be_ldap_init() { struct ldap_backend *conf; char *uri; char *binddn, *bindpw; char *opt_flag; int rc, opt, len; _log(LOG_DEBUG, "}}}} LDAP"); uri = p_stab("ldap_uri"); binddn = p_stab("binddn"); bindpw = p_stab("bindpw"); if (!uri) { _fatal("Mandatory option 'ldap_uri' is missing"); return (NULL); } if (!ldap_is_ldap_url(uri)) { _fatal("Mandatory option 'ldap_uri' doesn't look like an LDAP URI"); return (NULL); } if ((conf = (struct ldap_backend *)malloc(sizeof(struct ldap_backend))) == NULL) return (NULL); conf->ldap_uri = NULL; conf->connstr = NULL; conf->lud = NULL; conf->ld = NULL; conf->user_uri = NULL; conf->superquery = NULL; conf->aclquery = NULL; conf->acldeny = 0; conf->ldap_uri = strdup(uri); if (ldap_url_parse(uri, &conf->lud) != 0) { _fatal("Cannot parse ldap_uri"); return (NULL); } /* ldap_initialize() allows schema://host:port only; build * an appropriate string from what we have, to use later also. */ len = strlen(conf->lud->lud_scheme) + strlen(conf->lud->lud_host) + 15; if ((conf->connstr = malloc(len)) == NULL) { _fatal("Out of memory"); return (NULL); } sprintf(conf->connstr, "%s://%s:%d", conf->lud->lud_scheme, conf->lud->lud_host, conf->lud->lud_port); if (ldap_initialize(&conf->ld, conf->connstr) != LDAP_SUCCESS) { ldap_free_urldesc(conf->lud); free(conf->connstr); free(conf->ldap_uri); _fatal("Cannot ldap_initialize"); return (NULL); } opt = LDAP_VERSION3; ldap_set_option(conf->ld, LDAP_OPT_PROTOCOL_VERSION, &opt); if ((rc = ldap_simple_bind_s(conf->ld, binddn, bindpw)) != LDAP_SUCCESS) { _fatal("Cannot bind to LDAP: %s", ldap_err2string(rc)); return (NULL); } // conf->superquery = p_stab("superquery"); // conf->aclquery = p_stab("aclquery"); opt_flag = get_bool("ldap_acl_deny", "false"); if (!strcmp("true", opt_flag)) conf->acldeny = 1; return ((void *)conf); }
int be_ldap_getuser(void *handle, const char *username, const char *password, char **phash, const char *clientid) { struct ldap_backend *conf = (struct ldap_backend *)handle; LDAPMessage *msg,*entry; int rc, len; char *filter, *bp, *fp, *up, *dn; // printf("+++++++++++ GET %s USERNAME [%s] (%s)\n", conf->ldap_uri, username, password); /* * Replace '@' in filter with `username' */ len = strlen(conf->lud->lud_filter) + strlen(username) + 10; filter = (char *)malloc(len); for (fp = filter, bp = conf->lud->lud_filter; bp && *bp;) { if (*bp == '@') { ++bp; for (up = (char *)username; up && *up; up++) { *fp++ = *up; } } else { *fp++ = *bp++; } *fp = 0; } rc = ldap_search_s(conf->ld, conf->lud->lud_dn, conf->lud->lud_scope, filter, conf->lud->lud_attrs, 0, &msg); if (rc != LDAP_SUCCESS) { _fatal("Cannot search LDAP for user %s: %s", username, ldap_err2string(rc)); return BACKEND_ERROR; } free(filter); if (ldap_count_entries(conf->ld, msg) != 1) { _log(1, "LDAP search for %s returns != 1 entry", username); return BACKEND_DEFER; } rc = BACKEND_DEFER; if ((entry = ldap_first_entry(conf->ld, msg)) != NULL) { dn = ldap_get_dn(conf->ld, entry); _log(1, "Attempt to bind as %s\n", dn); if (user_bind(conf->connstr, dn, password)) { rc = BACKEND_ALLOW; } ldap_memfree(dn); } return rc; }
mword *rinline_bpdl_ptr_list(bvm_cache *this_bvm, mword *sexpr){ // rinline_bpdl_ptr_list# #ifdef BPDL_TRACE _trace; #endif // 1. string // 2. number // 3. label (lookup in sym-table) // 4. recurse inline_bpdl mword entry_type; mword *list_head = nil; mword *list_curr = nil; mword *new_entry; mword *bpdl_label; mword *bpdl_label_hash; while(!is_nil(sexpr)){ entry_type = get_bpdl_list_entry_type((mword*)icar(sexpr)); switch(entry_type){ case BPDL_LIST_ENTRY_DNUMBER: case BPDL_LIST_ENTRY_HNUMBER: case BPDL_LIST_ENTRY_DQUOTE: case BPDL_LIST_ENTRY_SQUOTE: new_entry = _cons( this_bvm, bpdl_const_to_bstruct( this_bvm, (mword*)icar(sexpr), entry_type), nil); if(is_nil(list_head)){ list_head = list_curr = new_entry; } else{ _append_direct(this_bvm, list_curr, new_entry); list_curr = (mword*)icdr(list_curr); // Save unnecessary traversal } break; case BPDL_LIST_ENTRY_LABEL: bpdl_label = _lf2by(this_bvm, (mword*)icar(sexpr)); bpdl_label_hash = _hash8(this_bvm,bpdl_label); if(_arcmp((mword*)icar(sexpr), SEXPR_NIL_SYMBOL) == 0){ new_entry = _cons( this_bvm, lusym(this_bvm, _hash8( this_bvm, bpdl_label )), nil); } else{ if(exsym(this_bvm, bpdl_label_hash)){ // XXX PERF (double-lookup) new_entry = _cons( this_bvm, lusym(this_bvm, bpdl_label_hash), nil); } else{ new_entry = _cons( this_bvm, tptr_new(this_bvm, BABEL_TAG_REF_SYM_LOCAL, tptr_new(this_bvm, bpdl_label_hash, nil)), nil); } } if(is_nil(list_head)){ list_head = list_curr = new_entry; } else{ _append_direct(this_bvm, list_curr, new_entry); list_curr = (mword*)icdr(list_curr); // Save unnecessary traversal } break; case BPDL_LIST_ENTRY_INTE: new_entry = _cons( this_bvm, inline_bpdl(this_bvm, (mword*)icar(sexpr)), nil); if(is_nil(list_head)){ list_head = list_curr = new_entry; } else{ _append_direct(this_bvm, list_curr, new_entry); list_curr = (mword*)icdr(list_curr); // Save unnecessary traversal } break; default: _fatal("Unrecognized bpdl ptr list entry"); //FIXME Throw exception } sexpr = (mword*)icdr(sexpr); } return list_head; }
static int http_post(void *handle, char *uri, const char *clientid, const char *username,const char *password, const char *topic, int acc) { struct http_backend *conf = (struct http_backend *)handle; CURL *curl; struct curl_slist *headerlist=NULL; int re; int respCode = 0; int ok = FALSE; char *url; char *data; if (username == NULL) { return (FALSE); } clientid = (clientid && *clientid) ? clientid : ""; password = (password && *password) ? password : ""; topic = (topic && *topic) ? topic : ""; if ((curl = curl_easy_init()) == NULL) { _fatal("create curl_easy_handle fails"); return (FALSE); } if (conf->hostheader != NULL) curl_slist_append(headerlist, conf->hostheader); curl_slist_append(headerlist, "Expect:"); //_log(LOG_NOTICE, "u=%s p=%s t=%s acc=%d", username, password, topic, acc); url = (char *)malloc(strlen(conf->ip) + strlen(uri) + 20); if (url == NULL) { _fatal("ENOMEM"); return (FALSE); } sprintf(url, "http://%s:%d%s", conf->ip, conf->port, uri); /* Hoping the 1024 is sufficient for curl_easy_escapes ... */ data = (char *)malloc(strlen(username) + strlen(password) + strlen(topic) + strlen(clientid) + 1024); if (data == NULL) { _fatal("ENOMEM"); return (FALSE); } sprintf(data, "username=%s&password=%s&topic=%s&acc=%d&clientid=%s", curl_easy_escape(curl, username, 0), curl_easy_escape(curl, password, 0), curl_easy_escape(curl, topic, 0), acc, curl_easy_escape(curl, clientid, 0)); _log(LOG_DEBUG, "url=%s", url); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); re = curl_easy_perform(curl); if (re == CURLE_OK) { re = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respCode); if (re == CURLE_OK && respCode == 200) { ok = TRUE; } else { //_log(LOG_NOTICE, "http auth fail re=%d respCode=%d", re, respCode); } } else { _log(LOG_DEBUG, "http req fail url=%s re=%s", url, curl_easy_strerror(re)); } curl_easy_cleanup(curl); curl_slist_free_all (headerlist); free(url); free(data); return (ok); }
/* Create general Window */ extern "C" HWND APIENTRY Win32CreateWindow(HWND hwndParent, PCSZ pszClass, PCSZ pszName, ULONG flStyle, LONG x, LONG y, LONG nx, LONG ny, HWND hwndOwner, HWND hwndInsertBehind, ULONG id, PVOID pCtlData, PVOID pPresParams) { HAB iHab; HWND hwnd=NULL; FPM_Window *pw; int len,rc; iHab = _hab.GetCurrentHAB(); if(iHab < 0) { //_hab.SetError(ihab - bad! , PMERR_INVALID_HAB); debug(3, 0)("WARNING: __FUNCTION__: bad ihab %x\n",iHab); _fatal("Thread don't initialized to FreePM"); return NULL; } //create hwndClient rc = F_SendCmdToServer(client_obj, F_CMD_WINCREATE_HWND, iHab); if(rc) { if(rc == ERROR_BROKEN_PIPE) { /* todo: attempt to reconnect till timeout */ } debug(3, 0)("WARNING:__FUNCTION__:SendCmdToServer Error: %s\n",rc); _fatal("SendCmdToServer Error\n"); } rc = F_RecvDataFromServer(client_obj, &hwnd, &len, sizeof(HWND)); if(rc) { if(rc == ERROR_BROKEN_PIPE) { /* todo: attempt to reconnect till timeout */ } debug(3, 0)("WARNING:__FUNCTION__:RecvDataFromServer Error: %s\n",rc); _fatal("RecvDataFromServer Error\n"); } pw = new FPM_Window(); pw->CreateFPM_Window(hwndParent, /* Parent-window handle. */ pszClass, /* Registered-class name. */ pszName, /* Window text. */ flStyle, /* Window style. */ x, /* x-coordinate of window position. */ y, /* y-coordinate of window position. */ nx, /* Width of window, in window coordinates. */ ny, /* Height of window, in window coordinates. */ hwndOwner, /* Owner-window handle. */ hwndInsertBehind, /* Sibling-window handle. */ id, /* Window resource identifier. */ pCtlData, /* Pointer to control data. */ pPresParams);/* Presentation parameters. */ // _hab.AddHwnd(hwnd,iHab, pw); return hwnd; }
mword *rinline_bpdl_val_list(bvm_cache *this_bvm, mword *sexpr){ // rinline_bpdl_val_list# #ifdef BPDL_TRACE _trace; #endif // 1. string // 2. number // 3. label (lookup in sym-table) // 4. recurse inline_bpdl mword entry_type; mword *list_head = nil; mword *list_curr = nil; mword *new_entry; while(!is_nil(sexpr)){ entry_type = get_bpdl_list_entry_type((mword*)icar(sexpr)); switch(entry_type){ case BPDL_LIST_ENTRY_DNUMBER: case BPDL_LIST_ENTRY_HNUMBER: case BPDL_LIST_ENTRY_DQUOTE: case BPDL_LIST_ENTRY_SQUOTE: new_entry = _cons( this_bvm, bpdl_const_to_bstruct( this_bvm, (mword*)icar(sexpr), entry_type), nil); if(is_nil(list_head)){ list_head = list_curr = new_entry; } else{ _append_direct(this_bvm, list_curr, new_entry); list_curr = (mword*)icdr(list_curr); // Save unnecessary traversal } break; case BPDL_LIST_ENTRY_LABEL: new_entry = _cons( this_bvm, lusym2(this_bvm, _lf2by(this_bvm, (mword*)icar(sexpr))), nil); if(is_nil(list_head)){ list_head = list_curr = new_entry; } else{ _append_direct(this_bvm, list_curr, new_entry); list_curr = (mword*)icdr(list_curr); // Save unnecessary traversal } break; case BPDL_LIST_ENTRY_INTE: _fatal("Can't nest inside val/leaf array"); break; default: _fatal("Unrecognized bpdl val list entry"); //FIXME Throw exception } sexpr = (mword*)icdr(sexpr); } return list_head; }