//added by mj 20090420---------------- void MulLang_Load(void) { FAT_FILE *pf=Shell_FAT_fopen_Mul_Language_messages(); if(pf==NULL){ _consolePrintf("Fatal error: Not found message file.\n"); ShowLogHalt(); } u32 bufsize=FAT2_GetFileSize(pf); u8 *pbuf=(u8*)safemalloc(bufsize); FAT2_fread_fast(pbuf,1,bufsize,pf); FAT2_fclose(pf); MulLangDataCount=0; char linebuf[512+1]; u32 linelen=0; linebuf[linelen]=0; s32 ofs=0; while(ofs<bufsize){ if(pbuf[ofs]==0) break; if(pbuf[ofs]<0x20){ if((pbuf[ofs]==0x0d)||(pbuf[ofs]==0x0a)){ linebuf[linelen]=0; if((linelen!=0)&&(linebuf[0]!=';')&&(linebuf[0]!='#')){ u32 ofs=0; for(u32 idx=0;idx<linelen;idx++){ if(linebuf[idx]=='='){ ofs=idx; break; } } if(ofs!=0){ linebuf[ofs]=0; if(LangDataCount==LangDataMaxCount){ _consolePrintf("Fatal error: Language data buffer overflow.\n"); ShowLogHalt(); } TLangData *pld=&MulLangData[MulLangDataCount]; pld->pItem=str_AllocateCopy(&linebuf[0]); pld->ItemHash=CalcItemHash(pld->pItem); pld->pValue=str_AllocateCopy(&linebuf[ofs+1]); MulLangDataCount++; } } linelen=0; } }else{ if((linelen+1)<512){ linebuf[linelen++]=pbuf[ofs]; } } ofs++; } if(pbuf!=NULL){ safefree(pbuf); pbuf=NULL; } _consolePrintf("Language materials count= %d\n",LangDataCount); }
/* ---------------------------------------------------------------------------- */ void ringb_free(ringb_t *rb) { safefree(rb->buf); safefree((char *) rb); }
struct gp_sock_ctx *init_unix_socket(struct gssproxy_ctx *gpctx, const char *file_name) { struct sockaddr_un addr = {0}; struct gp_sock_ctx *sock_ctx; mode_t old_mode; int ret = 0; int fd = -1; sock_ctx = calloc(1, sizeof(struct gp_sock_ctx)); if (!sock_ctx) { return NULL; } /* can't bind if an old socket is around */ unlink(file_name); /* socket should be r/w by anyone */ old_mode = umask(0111); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, file_name, sizeof(addr.sun_path)-1); addr.sun_path[sizeof(addr.sun_path)-1] = '\0'; fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd == -1) { ret = errno; GPDEBUG("Failed to init socket! (%d: %s)\n", ret, gp_strerror(ret)); goto done; } ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); if (ret == -1) { ret = errno; GPDEBUG("Failed to bind socket %s! (%d: %s)\n", addr.sun_path, ret, gp_strerror(ret)); goto done; } ret = listen(fd, 10); if (ret == -1) { ret = errno; GPDEBUG("Failed to listen! (%d: %s)\n", ret, gp_strerror(ret)); goto done; } ret = set_status_flags(fd, O_NONBLOCK); if (ret != 0) { GPDEBUG("Failed to set O_NONBLOCK on %d!\n", fd); goto done; } ret = set_fd_flags(fd, FD_CLOEXEC); if (ret != 0) { GPDEBUG("Failed to set FD_CLOEXEC on %d!\n", fd); goto done; } done: if (ret) { GPERROR("Failed to create Unix Socket! (%d:%s)", ret, gp_strerror(ret)); if (fd != -1) { close(fd); fd = -1; } safefree(sock_ctx); } else { sock_ctx->gpctx = gpctx; sock_ctx->socket = file_name; sock_ctx->fd = fd; } umask(old_mode); return sock_ctx; }
int main(int argc, char *argv[]) { int maxlen; uint64_t size; unsigned char *buffer; char mode; std::string s, line; mtx_common_init("base64tool"); set_usage(); if (argc < 4) usage(0); mode = 0; if (!strcmp(argv[1], "encode")) mode = 'e'; else if (!strcmp(argv[1], "decode")) mode = 'd'; else mxerror(boost::format(Y("Invalid mode '%1%'.\n")) % argv[1]); maxlen = 72; if ((argc == 5) && (mode == 'e')) { if (!parse_number(argv[4], maxlen) || (maxlen < 4)) mxerror(Y("Max line length must be >= 4.\n\n")); } else if ((argc > 5) || ((argc > 4) && (mode == 'd'))) usage(2); maxlen = ((maxlen + 3) / 4) * 4; mm_io_cptr in, intext; try { in = mm_io_cptr(new mm_file_io_c(argv[2])); if (mode != 'e') intext = mm_io_cptr(new mm_text_io_c(in.get(), false)); } catch (mtx::mm_io::exception &ex) { mxerror(boost::format(Y("The file '%1%' could not be opened for reading: %2%.\n")) % argv[2] % ex); } mm_io_cptr out; try { out = mm_write_buffer_io_c::open(argv[3], 128 * 1024); } catch (mtx::mm_io::exception &ex) { mxerror(boost::format(Y("The file '%1%' could not be opened for writing: %2%.\n")) % argv[3] % ex); } in->save_pos(); in->setFilePointer(0, seek_end); size = in->getFilePointer(); in->restore_pos(); if (mode == 'e') { buffer = (unsigned char *)safemalloc(size); size = in->read(buffer, size); s = base64_encode(buffer, size, true, maxlen); safefree(buffer); out->write(s.c_str(), s.length()); } else { while (intext->getline2(line)) { strip(line); s += line; } buffer = (unsigned char *)safemalloc(s.length() / 4 * 3 + 100); try { size = base64_decode(s, buffer); } catch(...) { mxerror(Y("The Base64 encoded data could not be decoded.\n")); } out->write(buffer, size); safefree(buffer); } mxinfo(Y("Done.\n")); return 0; }
/* ---------------------------------------------------------------------------- */ void ringb_float_free(ringb_float_t *rb) { safefree((void *) rb->buf); safefree((char *) rb); }
static void free_config (struct config_s *conf) { safefree (conf->config_file); safefree (conf->logf_name); safefree (conf->stathost); safefree (conf->user); safefree (conf->group); safefree (conf->ipAddr); #ifdef FILTER_ENABLE safefree (conf->filter); #endif /* FILTER_ENABLE */ #ifdef REVERSE_SUPPORT free_reversepath_list(conf->reversepath_list); safefree (conf->reversebaseurl); #endif #ifdef UPSTREAM_SUPPORT free_upstream_list (conf->upstream_list); #endif /* UPSTREAM_SUPPORT */ safefree (conf->pidpath); safefree (conf->bind_address); safefree (conf->via_proxy_name); hashmap_delete (conf->errorpages); free_added_headers (conf->add_headers); safefree (conf->errorpage_undef); safefree (conf->statpage); flush_access_list (conf->access_list); free_connect_ports_list (conf->connect_ports); hashmap_delete (conf->anonymous_map); memset (conf, 0, sizeof(*conf)); }
int gp_config_get_string_array(struct gp_ini_context *ctx, const char *secname, const char *keyname, int *num_values, const char ***values) { struct ini_cfgobj *ini_config = (struct ini_cfgobj *)ctx->private_data; struct value_obj *vo = NULL; const char *value; int ret; int i, count = 0; const char **array = NULL; const char **t_array; if (!values || !num_values) { return EINVAL; } *num_values = 0; *values = NULL; ret = ini_get_config_valueobj(secname, keyname, ini_config, INI_GET_FIRST_VALUE, &vo); if (ret) { return ret; } if (!vo) { return ENOENT; } value = ini_get_const_string_config_value(vo, &ret); if (ret) { return ret; } array = calloc(1, sizeof(char *)); if (array == NULL) { ret = ENOMEM; goto done; } array[count] = strdup(value); if (array[count] == NULL) { ret = ENOMEM; goto done; } count++; do { ret = ini_get_config_valueobj(secname, keyname, ini_config, INI_GET_NEXT_VALUE, &vo); if (ret) { goto done; } if (!vo) { break; } value = ini_get_const_string_config_value(vo, &ret); if (ret) { goto done; } t_array = realloc(array, (count+1) * sizeof(char *)); if (t_array == NULL) { ret = ENOMEM; goto done; } array = t_array; array[count] = strdup(value); if (array[count] == NULL) { ret = ENOMEM; goto done; } count++; } while (1); *num_values = count; *values = array; ret = 0; done: if (ret && array) { for (i = 0; i < count; i++) { safefree(array[i]); } safefree(array); } return ret; }
static int load_services(struct gp_config *cfg, struct gp_ini_context *ctx) { int num_sec; char *secname = NULL; const char *value; char *vcopy; char *token; char *handle; int valnum; int ret; int i, n; num_sec = gp_config_get_nsec(ctx); /* allocate enough space for num_sec services, * we won't waste too much space by overallocating */ cfg->svcs = calloc(num_sec, sizeof(struct gp_service *)); if (!cfg->svcs) { ret = ENOMEM; goto done; } for (i = 0; i < num_sec; i++) { secname = gp_config_get_secname(ctx, i); ret = strncmp(secname, "service/", 8); if (ret == 0) { n = cfg->num_svcs; cfg->svcs[n] = calloc(1, sizeof(struct gp_service)); if (!cfg->svcs[n]) { ret = ENOMEM; goto done; } cfg->num_svcs++; /* by default allow both */ cfg->svcs[n]->cred_usage = GSS_C_BOTH; cfg->svcs[n]->name = strdup(secname + 8); if (!cfg->svcs[n]->name) { ret = ENOMEM; goto done; } /* euid can be a string or an int */ ret = gp_config_get_int(ctx, secname, "euid", &valnum); if (ret != 0) { ret = gp_config_get_string(ctx, secname, "euid", &value); if (ret == 0) { struct passwd *eu_passwd; /* static; do not free */ errno = 0; /* needs to be 0; otherwise it won't be set */ eu_passwd = getpwnam(value); if (!eu_passwd) { ret = errno; if (ret == 0) { /* not that it gets set anyway... */ ret = ENOENT; } } else { valnum = eu_passwd->pw_uid; } } if (ret != 0) { /* if euid is missing or there is an error retrieving it * return an error and end. This is a fatal condition. */ if (ret == ENOENT) { GPERROR("Option 'euid' is missing from [%s].\n", secname); ret = EINVAL; } gp_service_free(cfg->svcs[n]); cfg->num_svcs--; safefree(secname); goto done; } } cfg->svcs[n]->euid = valnum; ret = gp_config_get_string(ctx, secname, "allow_any_uid", &value); if (ret == 0) { if (gp_boolean_is_true(value)) { cfg->svcs[n]->any_uid = true; } } ret = gp_config_get_string(ctx, secname, "trusted", &value); if (ret == 0) { if (gp_boolean_is_true(value)) { cfg->svcs[n]->trusted = true; } } ret = gp_config_get_string(ctx, secname, "kernel_nfsd", &value); if (ret == 0) { if (gp_boolean_is_true(value)) { cfg->svcs[n]->kernel_nfsd = true; } } ret = gp_config_get_string(ctx, secname, "impersonate", &value); if (ret == 0) { if (gp_boolean_is_true(value)) { cfg->svcs[n]->impersonate = true; } } ret = gp_config_get_string(ctx, secname, "socket", &value); if (ret == 0) { cfg->svcs[n]->socket = strdup(value); if (!cfg->svcs[n]->socket) { ret = ENOMEM; goto done; } } ret = setup_service_creds_handle(cfg->svcs[n]); if (ret) { goto done; } ret = gp_config_get_string(ctx, secname, "mechs", &value); if (ret != 0) { /* if mechs is missing or there is an error retrieving it * return an error and end. This is a fatal condition. */ if (ret == ENOENT) { GPERROR("Option 'mechs' is missing from [%s].\n", secname); ret = EINVAL; } gp_service_free(cfg->svcs[n]); cfg->num_svcs--; safefree(secname); goto done; } vcopy = strdup(value); if (!vcopy) { ret = ENOMEM; goto done; } token = strtok_r(vcopy, ", ", &handle); do { ret = strcmp(value, "krb5"); if (ret == 0) { ret = get_krb5_mech_cfg(cfg->svcs[n], ctx, secname); if (ret == 0) { cfg->svcs[n]->mechs |= GP_CRED_KRB5; } else { GPERROR("Failed to read krb5 config for %s.\n", secname); safefree(vcopy); return ret; } } else { GPERROR("Unknown mech: %s in [%s], ignoring.\n", token, secname); } token = strtok_r(NULL, ", ", &handle); } while (token != NULL); safefree(vcopy); if (cfg->svcs[n]->mechs == 0) { GPDEBUG("No mechs found for [%s], ignoring.\n", secname); gp_service_free(cfg->svcs[n]); cfg->num_svcs--; safefree(secname); continue; } ret = gp_config_get_string(ctx, secname, "selinux_context", &value); if (ret == 0) { cfg->svcs[n]->selinux_ctx = SELINUX_context_new(value); if (!cfg->svcs[n]->selinux_ctx) { ret = EINVAL; goto done; } } ret = gp_config_get_string(ctx, secname, "cred_usage", &value); if (ret == 0) { if (strcasecmp(value, "initiate") == 0) { cfg->svcs[n]->cred_usage = GSS_C_INITIATE; } else if (strcasecmp(value, "accept") == 0) { cfg->svcs[n]->cred_usage = GSS_C_ACCEPT; } else if (strcasecmp(value, "both") == 0) { cfg->svcs[n]->cred_usage = GSS_C_BOTH; } else { GPDEBUG("Invalid value '%s' for cred_usage in [%s].\n", value, secname); ret = EINVAL; goto done; } } cfg->svcs[n]->filter_flags = DEFAULT_FILTERED_FLAGS; ret = gp_config_get_string(ctx, secname, "filter_flags", &value); if (ret == 0) { parse_flags(value, &cfg->svcs[n]->filter_flags); } cfg->svcs[n]->enforce_flags = DEFAULT_ENFORCED_FLAGS; ret = gp_config_get_string(ctx, secname, "enforce_flags", &value); if (ret == 0) { ret = parse_flags(value, &cfg->svcs[n]->enforce_flags); if (ret) goto done; } } safefree(secname); } if (cfg->num_svcs == 0) { GPERROR("No service sections configured!\n"); return ENOENT; } ret = check_services(cfg); done: safefree(secname); return ret; }
/* * Inserts a new access control into the list. The function will figure out * whether the location is an IP address (with optional netmask) or a * domain name. * * Returns: * -1 on failure * 0 otherwise. */ int insert_acl(char *location, acl_access_t access_type) { size_t i; struct acl_s **rev_acl_ptr, *acl_ptr, *new_acl_ptr; char *nptr; assert(location != NULL); /* * First check to see if the location is a string or numeric. */ for (i = 0; location[i] != '\0'; i++) { /* * Numeric strings can not contain letters, so test on it. */ if (isalpha((unsigned char) location[i])) { break; } } /* * Add a new ACL to the list. */ rev_acl_ptr = &access_list; acl_ptr = access_list; while (acl_ptr) { rev_acl_ptr = &acl_ptr->next; acl_ptr = acl_ptr->next; } new_acl_ptr = safemalloc(sizeof(struct acl_s)); if (!new_acl_ptr) { return -1; } new_acl_ptr->acl_access = access_type; if (location[i] == '\0') { DEBUG2("ACL \"%s\" is a number.", location); /* * We did not break early, so this a numeric location. * Check for a netmask. */ new_acl_ptr->type = ACL_NUMERIC; nptr = strchr(location, '/'); if (nptr) { *nptr++ = '\0'; new_acl_ptr->netmask = strtol(nptr, NULL, 10); if (new_acl_ptr->netmask < 0 || new_acl_ptr->netmask > 32) { safefree(new_acl_ptr); return -1; } } else { new_acl_ptr->netmask = 32; } } else { DEBUG2("ACL \"%s\" is a string.", location); new_acl_ptr->type = ACL_STRING; new_acl_ptr->netmask = 32; } new_acl_ptr->location = safestrdup(location); if (!new_acl_ptr->location) { safefree(new_acl_ptr); return -1; } *rev_acl_ptr = new_acl_ptr; new_acl_ptr->next = acl_ptr; return 0; }
/** Allocate a memory block. * * @internal * * Precondition: locked home * * @param home home to allocate * @param sub block structure used to allocate * @param size * @param zero if true, zero allocated block; * if > 1, allocate a subhome * */ static void *sub_alloc(su_home_t *home, su_block_t *sub, size_t size, enum sub_zero zero) { void *data, *preload = NULL; assert (size < (((size_t)1) << SIZEBITS)); #ifdef DEBUG SU_DEBUG_9(("sub_alloc: allocating size %ld from home: %p using block %p\n", size, home, sub)) ; #endif if (size >= ((size_t)1) << SIZEBITS) return (void)(errno = ENOMEM), NULL; if (!size) return NULL; if (sub == NULL || 3 * sub->sub_used > 2 * sub->sub_n) { /* Resize the hash table */ size_t i, n, n2; su_block_t *b2; if (sub) n = home->suh_blocks->sub_n, n2 = 4 * n + 3; //, used = sub->sub_used; else n = 0, n2 = SUB_N; //, used = 0; #ifdef DEBUG SU_DEBUG_9(("sub_alloc: realloc block hash of size %ld\n", n2)) ; #endif if (!(b2 = su_hash_alloc(n2))) return NULL; for (i = 0; i < n; i++) { if (sub->sub_nodes[i].sua_data) su_block_add(b2, sub->sub_nodes[i].sua_data)[0] = sub->sub_nodes[i]; } if (sub) { b2->sub_parent = sub->sub_parent; b2->sub_ref = sub->sub_ref; b2->sub_preload = sub->sub_preload; b2->sub_prsize = sub->sub_prsize; b2->sub_prused = sub->sub_prused; b2->sub_hauto = sub->sub_hauto; b2->sub_preauto = sub->sub_preauto; b2->sub_destructor = sub->sub_destructor; /* auto_all is not copied! */ b2->sub_stats = sub->sub_stats; } home->suh_blocks = b2; if (sub && !sub->sub_auto) free(sub); sub = b2; } if (sub && zero < do_clone && sub->sub_preload && size <= sub->sub_prsize) { /* Use preloaded memory */ size_t prused = sub->sub_prused + size + MEMCHECK_EXTRA; prused = __ALIGN(prused); if (prused <= sub->sub_prsize) { preload = (char *)sub->sub_preload + sub->sub_prused; sub->sub_prused = (unsigned)prused; } #ifdef DEBUG SU_DEBUG_9(("sub_alloc: using %s memory\n", "preloaded")) ; #endif } if (preload && zero) { data = memset(preload, 0, size); } else if (preload) { data = preload; } else if (zero) { data = calloc(1, size + MEMCHECK_EXTRA); } else { data = malloc(size + MEMCHECK_EXTRA); } if (data) { su_alloc_t *sua; #if MEMCHECK_EXTRA size_t term = 0 - size; memcpy((char *)data + size, &term, sizeof (term)); #endif #ifdef DEBUG SU_DEBUG_9(("sub_alloc: data will be located at %p\n", data)) ; #endif if (!preload) sub->sub_auto_all = 0; if (zero >= do_clone) { /* Prepare cloned home */ su_home_t *subhome = data; assert(preload == 0); subhome->suh_blocks = su_hash_alloc(SUB_N); if (!subhome->suh_blocks) return (void)safefree(data), NULL; subhome->suh_size = (unsigned)size; subhome->suh_blocks->sub_parent = home; subhome->suh_blocks->sub_hauto = 0; } /* OK, add the block to the hash table. */ sua = su_block_add(sub, data); assert(sua); sua->sua_size = (unsigned)size; sua->sua_home = zero > 1; if (sub->sub_stats) su_home_stats_alloc(sub, data, preload, size, zero); } return data; }