/** * Indicates whether a given open file contains .darx data. * * @return true if the data pointed by the file pointer is a .darx file, * false otherwise. */ bool read_tensor(datatensor& tensor, darx& darx, FILE* file, data_type_info& dtinfo){ if(VERBOSE){ std::cout << "# file pos: " << ftell(file) << std::endl; } uint8_t namelen=0; fread(&namelen, sizeof(uint8_t), 1, file); if(namelen > 0){ char* tensor_name = new char[namelen+1]; tensor.name=tensor_name; fread(tensor_name, sizeof(uint8_t), namelen, file); tensor_name[namelen]=0; if(VERBOSE){ std::cout << "# name : " << tensor.name << std::endl; } } else { if(VERBOSE){ std::cout << "# (unnamed)" << std::endl; } } // write the tensor's rank fread(&(tensor.rank), sizeof(uint8_t), 1, file); if(VERBOSE){ std::cout << "# rank : " << ((int)tensor.rank) << std::endl; } // write the length of each dimmension tensor.lengths = new unsigned int[tensor.rank]; if(VERBOSE){ std::cout << "# lengths : "; } for(int dim_idx=0; dim_idx < tensor.rank; dim_idx++){ tensor.lengths[dim_idx]=0; fread(&(tensor.lengths[dim_idx]), dtinfo.int_size, 1, file); if(VERBOSE){ std::cout << tensor.lengths[dim_idx] << " "; } } if(VERBOSE){ std::cout << std::endl; } // write the element type stuct tensor.type = read_tensor_type(darx, file, dtinfo); if(!tensor.type){ return UNSUPPORTED_ELEMENT_TYPE; } // write the data (possibly compressed) { uint8_t ctype; fread(&ctype, sizeof(uint8_t), 1, file); tensor.compression = (CompressionType)ctype; if(VERBOSE){ std::cout << "# compression : " << ((int)ctype) << std::endl; } bool cdata_is_temp=false; unsigned int cdata_length; fread(&cdata_length, sizeof(unsigned int), 1, file); if(VERBOSE){ std::cout << "# cdata size: " << cdata_length << std::endl; } uint8_t* cdata = new uint8_t[cdata_length]; fread(cdata, cdata_length, 1, file); if(VERBOSE){ std::cout << "# cdata : " << ((void*)cdata) << std::endl; } if(!decompress_data(tensor, &cdata, &cdata_length, &cdata_is_temp)){ return UNSUPPORTED_COMPRESS_TYPE; } if(cdata_is_temp){ if(VERBOSE){ std::cout << "# deleting temp cdata... " << cdata << std::endl; } delete[] cdata; } } return SUCCESS; }
void extract_gids_from_encoded_data(xmlChar *value, uint32_t **gids, int *gid_count) { unsigned char *decoded = malloc(1024); size_t outlen = base64_decode((char *)value, &decoded, 1024); unsigned char *decompressed = calloc(1, 256); int unzip_len = decompress_data(decoded, outlen, &decompressed, 256); free(decoded); int i = 0; *gids = malloc(sizeof(uint32_t) * (unzip_len / 4)); int cell = 0; for (i = 0; i < unzip_len; i += 4) { uint32_t gid = (decompressed[i] | decompressed[i + 1] << 8 | decompressed[i + 2] << 16 | decompressed[i + 3] << 24); (*gids)[cell] = gid; cell += 1; } free(decompressed); *gid_count = cell; }
consume_data (j_decompress_ptr cinfo) { j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec; d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private; //JDIMENSION MCU_col_num; /* index of current MCU within row */ //JDIMENSION MCU_count; /* number of MCUs decoded */ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; int comp, ci; // yoffset, row, prev_row; JSAMPARRAY buffer[MAX_COMPS_IN_SCAN]; jpeg_component_info *compptr; /* Align the virtual buffers for the components used in this scan. */ for (comp = 0; comp < cinfo->comps_in_scan; comp++) { compptr = cinfo->cur_comp_info[comp]; ci = compptr->component_index; buffer[ci] = (*cinfo->mem->access_virt_sarray) ((j_common_ptr) cinfo, diff->whole_image[ci], cinfo->input_iMCU_row * compptr->v_samp_factor, (JDIMENSION) compptr->v_samp_factor, TRUE); } return decompress_data(cinfo, buffer); }
int main(int argc, char **argv) { int sock, preference; int len_inet, length, place, result; int soa_count = 0; struct sockaddr_in zone_server; /* AF_INET */ js_string *send, *zone, *get, *expand, *mxexpand; /* ( Sending and getting data from the client ) */ q_header header; q_rr rr; rr_soa soa; int qclass = 1; unsigned char len[2]; if(argv[1] == 0 || argv[2] == 0) { harderror(L_USAGE); /* "Usage: getzone zone_name zone_server_IP" */ } if(argc == 4 && argv[3] == 0) { harderror(L_USAGE); /* "Usage: getzone zone_name zone_server_IP" */ } /* Set up an option to change the query class */ if(argc == 4 && atoi(argv[3]) == 255) qclass = 255; /* Create a timeout alarm */ signal(SIGALRM,timeout); alarm(300); /* 5 minutes */ /* Create a socket to the zone server */ memset(&zone_server,0,sizeof zone_server); zone_server.sin_family = AF_INET; zone_server.sin_port = htons(53); zone_server.sin_addr.s_addr = inet_addr(argv[2]); if(zone_server.sin_addr.s_addr == INADDR_NONE) harderror(L_VALID_IP); /* "Please use a valid IP for the zone server" */ len_inet = sizeof zone_server; /* Create a TCP/IP socket */ sock = socket(PF_INET,SOCK_STREAM,0); if(sock == -1) harderror(L_NO_SOCK); /* "Unable to create TCP socket" */ /* Connect to the zone server */ if(connect(sock,(struct sockaddr *)&zone_server,len_inet) == -1) harderror(L_NO_CONNECT); /* "Unable to connect to zone server" */ /* OK, create a query to send over the connection */ if((send = js_create(256,1)) == 0) harderror(L_NO_SEND); /* "Unable to create send string object" */ /* The 12-byte header to send to the DNS server */ header.id = 45; header.qr = 0; header.opcode = 0; header.aa = 0; header.tc = 0; header.rd = 0; header.ra = 0; header.z = 0; header.rcode = 0; header.qdcount = 1; header.ancount = 0; header.nscount = 0; header.arcount = 0; if(make_hdr(&header,send) == JS_ERROR) harderror(L_NO_HEADER); /* "Unable to make DNS header" */ if((zone = js_create(128,1)) == 0) harderror(L_NO_ZSTRING); /* "Unable to create zone string object" */ if(js_qstr2js(zone,"A") == JS_ERROR) harderror(L_QSTR2JS_ERROR); /* "qstr2js" */ if(js_qappend(argv[1],zone) == JS_ERROR) harderror(L_APPEND_Z); /* "Unable to append zone string to zone object" */ /* Append trailing dot, if needed */ if(*(zone->string + zone->unit_count - 1) != '.') if(js_qappend(".",zone) == JS_ERROR) harderror(L_APPEND_D); /* "Unable to append dot at end of zone string object" */ /* Convert zone in to raw "over-the-wire" UDP data */ if(hname_2rfc1035(zone) == JS_ERROR) harderror(L_INVALID_NAME); /* "Invalid form of zone name" */ /* Append raw binary zone to data to send to server */ if(js_append(zone,send) == JS_ERROR) harderror(L_APPEND_ZS); /* "Can not append zone string to send string" */ /* Append query type and query class to data */ if(js_adduint16(send,252) == JS_ERROR) harderror(L_ADD_QT); /* "Could not add query type to send" */ if(js_adduint16(send,qclass) == JS_ERROR) harderror(L_ADD_QC); /* "Could not add query class to send" */ /* Question formed, now send question to server */ len[0] = (send->unit_count & 0xff00) >> 8; len[1] = send->unit_count & 0xff; if(write(sock,len,2) == -1) harderror(L_SEND_2BYTE); /* "Could not send 2-byte length header to zone server" */ if(write(sock,send->string,send->unit_count) == -1) harderror(L_SEND_QUERY); /* "Could not send query to zone server" */ if((rr.name = js_create(257,1)) == 0) harderror(L_C_RRNAME); /* "Could not create rr.name" */ if((soa.mname = js_create(257,1)) == 0) harderror(L_MNAME); /* "Could not create soa.mname" */ if((soa.rname = js_create(257,1)) == 0) harderror(L_RNAME); /* "Could not create soa.rname" */ while(recv(sock,len,2,MSG_WAITALL) == 2) { /* Get the length of the reply from the server */ length = ((len[0] << 8) & 0xff00) | (len[1] & 0xff); if(length < 12) harderror(L_NOT_MANLY); /* "Response from server is not long enough to hold header" */ /* Allocate the string "get" */ if((get = js_create(length + 7,1)) == 0) harderror(L_NO_GET); /* "Could not allocate memory for get string" */ if((expand = js_create((length + 7) * 4,1)) == 0) harderror(L_NO_EXPAND); /* "Could not allocate memory for expand string" */ if((length = recv(sock,get->string,length,MSG_WAITALL)) == -1) harderror(L_SERVER); /* "Could not get packet from server" */ get->unit_count = length; /* Decompress that */ decomp_init(0); if(decompress_data(get,expand) == JS_ERROR) { harderror(L_DECOMPRESS); /* "Fatal error performing decompression" */ } if(read_hdr(expand,&header) == JS_ERROR) harderror(L_RHEADER); /* "Could not read header from server" */ /* Stop on any error codes */ if(header.rcode != 0) { switch(header.rcode) { case 1: harderror(L_FORMAT); /* "Format error" */ case 2: harderror(L_SERVER_FAIL); /* "Server failure" */ case 3: harderror(L_NAME); /* "Name error" */ case 4: harderror(L_NOTIMPL); /* "Not implemented" */ case 5: harderror(L_REFUSE); /* "Refused" */ default: harderror(L_RCODE); /* "Rcode > 5" */ } } /* Move past any and all questions */ place = 12; if(header.qdcount > 0) { result = dlabel_length(expand,place); if(result == -1) harderror(L_DLABEL); /* "Invalid dlabel in question" */ place += result; place += 4; header.qdcount--; } /* Convert the answers to csv1-compatible lines in a MaraDNS Zone file */ while(header.ancount > 0) { result = read_rr_h(expand,&rr,place); if(result == -1) harderror(L_READ_RR_H); /* "Fatal error running read_rr_h" */ place += result; js_destroy(get); /* Make sure that rr.name is in baliwick. If not, then put in an "Out of baliwick" warning and comment out the offending line */ if((get = js_create(rr.name->unit_count + 7,1)) == 0) harderror(L_CGET); /* "Can not make get string" */ if(js_copy(rr.name,get) == JS_ERROR) harderror(L_GET_COPY); /* "Fatal error copying name to get" */ get->encoding = zone->encoding; result = 0; /* Out of baliwick */ if(js_issame(get,zone) == 1) result = 1; /* In baliwick */ while(result == 0 && get->unit_count > zone->unit_count) { bobbit_label(get); if(js_issame(get,zone) == 1) result = 1; /* In baliwick */ } if(result == 0) /* If out of baliwick */ printf("%s%s%s",L_BALIWICK,LF,L_HASH); /* "# Disabled out-of-baliwick record follows" */ if((get = js_create((int)(rr.rdlength) + 7,1)) == 0) harderror(L_CGET); /* "Can not make get string" */ if(js_substr(expand,get,place,rr.rdlength) == -1) harderror(L_RDDATA); /* "Problem getting rddata" */ switch(rr.type) { case RR_SOA: soa_count++; if(soa_count > 1) /* Then the zone has ended */ exit(0); /* Translate all the fields, going to failover mode (Make this an "Unsupported" data type) if needed */ if(read_soa(get,&soa,0) == JS_ERROR) goto failover; if(hname_translate(rr.name,RR_SOA) == JS_ERROR) goto failover; if(hname_translate(soa.mname,RR_A) == JS_ERROR) goto failover; if(soa.mname->unit_count < 1) goto failover; if(email_translate(soa.rname) == JS_ERROR) goto failover; show_esc_stdout(rr.name); printf("|%u",rr.ttl); *(soa.mname->string) = '|'; show_esc_stdout(soa.mname); show_esc_stdout(soa.rname); printf("|%u|%d|%d|%d|%u\n",soa.serial,(int)soa.refresh, (int)soa.retry,(int)soa.expire, soa.minimum); break; case RR_A: if(get->unit_count != 4) goto failover; if(hname_translate(rr.name,RR_A) == JS_ERROR) harderror(L_TRANS); /* "Problem translating A record name" */ show_esc_stdout(rr.name); printf("|%u|%d.%d.%d.%d\n",rr.ttl,*(get->string), *(get->string + 1),*(get->string + 2), *(get->string + 3)); break; case RR_MX: if(get->unit_count < 3) goto failover; mxexpand = js_create(512,1); if(mxexpand == 0) goto failover; preference = ((*(get->string) & 0xff) << 8) | (*(get->string + 1) & 0xff); if(js_substr(get,mxexpand,2,get->unit_count - 2) == JS_ERROR) goto failover; if(hname_translate(rr.name,RR_MX) == JS_ERROR) goto failover; if(hname_translate(mxexpand,RR_MX) == JS_ERROR) goto failover; if(expand->unit_count < 1) goto failover; *(expand->string) = '|'; *(mxexpand->string) = '|'; show_esc_stdout(rr.name); printf("|%u|%d",rr.ttl,preference); show_esc_stdout(mxexpand); printf("%s",LF); /* "\n" */ js_destroy(mxexpand); break; case RR_TXT: if(get->unit_count < 1) goto failover; if(*(get->string) != get->unit_count - 1) goto failover; *(get->string) = '|'; if(hname_translate(rr.name,RR_TXT) == JS_ERROR) goto failover; show_esc_stdout(rr.name); printf("|%u",rr.ttl); show_esc_stdout(get); printf("%s",LF); /* "\n" */ break; case RR_NS: case RR_PTR: case RR_CNAME: if(hname_translate(rr.name,rr.type) == JS_ERROR) harderror(L_HNAME); /* "Hname problem" */ if(hname_translate(get,RR_A) == JS_ERROR) goto failover; if(get->unit_count < 1) goto failover; *(get->string) = '|'; show_esc_stdout(rr.name); printf("|%u",rr.ttl); show_esc_stdout(get); printf("%s",LF); /* "\n" */ break; default: failover: if(hname_translate(rr.name,RR_A) == JS_ERROR) harderror(L_HNAME); /* "Hname problem" */ if(rr.name->unit_count < 1) harderror(L_ZERO); /* "No 0-length names!" */ *(rr.name->string) = 'U'; show_esc_stdout(rr.name); printf("|%u|%u|",rr.ttl,rr.type); show_esc_stdout(get); printf("%s",LF); /* "\n" */ } place += rr.rdlength; header.ancount--; } js_destroy(get); js_destroy(expand); } return 0; /* Success */ }
int main(int argc, char **argv) { js_string *mararc_loc, *errors, *chrootn, *uidstr, *maxpstr, *kvar_query, *bind_address, *incoming, *uncomp, *verbstr; unsigned char chroot_zt[255]; uid_t uid; gid_t gid; int errorn, value, sock, maxprocs, counter; int cache_size; int min_ttl_n = 300, min_ttl_c = 300; int max_glueless; /* Maximum allowed glueless level */ int max_q_total; /* Maximum total queries in attempt to resolve hostname */ int timeout; /* Maximum time to wait for a remote server when performing a recursive query */ struct sockaddr client; struct sockaddr_in *clin; /* So we can log the IP */ #ifndef DARWIN struct rlimit rlim; #endif clin = (struct sockaddr_in *)&client; /* Initialize the strings (allocate memory for them, etc.) */ if((mararc_loc = js_create(256,1)) == 0) harderror(L_MLC); /* "Could not create mararc_loc string" */ if(js_set_encode(mararc_loc,MARA_LOCALE) == JS_ERROR) harderror(L_MLL); /* "Could not set locale for mararc_loc string" */ if((errors = js_create(256,1)) == 0) harderror(L_EC); /* "Could not create errors string" */ if(js_set_encode(errors,MARA_LOCALE) == JS_ERROR) harderror(L_EL); /* "Could not set locale for errors string" */ if((uidstr = js_create(256,1)) == 0) harderror(L_UC); /* "Could not create uidstr string" */ if(js_set_encode(uidstr,MARA_LOCALE) == JS_ERROR) harderror(L_UL); /* "Could not set locale for uidstr string" */ if((verbstr = js_create(256,1)) == 0) harderror(L_VC); /* "Could not create verbstr string" */ if(js_set_encode(verbstr,MARA_LOCALE) == JS_ERROR) harderror(L_VL); /* "Could not set locale for verbstr string" */ if((maxpstr = js_create(256,1)) == 0) harderror(L_MC); /* "Could not create maxpstr string" */ if(js_set_encode(maxpstr,MARA_LOCALE) == JS_ERROR) harderror(L_ML); /* "Could not set locale for maxpstr string" */ if((chrootn = js_create(256,1)) == 0) harderror(L_CC); /* "Could not create chrootn string" */ if(js_set_encode(chrootn,MARA_LOCALE) == JS_ERROR) harderror(L_CL); /* "Could not set locale for chrootn string" */ if((kvar_query = js_create(256,1)) == 0) harderror(L_KQC); /* "Could not create kvar_query string" */ if(js_set_encode(kvar_query,MARA_LOCALE) == JS_ERROR) harderror(L_KQL); /* "Could not set locale for kvar_query string" */ if((bind_address = js_create(64,1)) == 0) harderror(L_BAC); /* "Could not create bins_address string" */ if(js_set_encode(bind_address,MARA_LOCALE) == JS_ERROR) harderror(L_BAL); /* "Could not set locale for bind_address string" */ if((incoming = js_create(768,1)) == 0) harderror(L_IC); /* "Could not create incoming string" */ if(js_set_encode(incoming,MARA_LOCALE) == JS_ERROR) harderror(L_IL); /* "Could not set locale for incoming string" */ if((uncomp = js_create(768,1)) == 0) harderror(L_UCC); /* "Could not create uncomp string" */ if(js_set_encode(uncomp,MARA_LOCALE) == JS_ERROR) harderror(L_UCL); /* "Could not set locale for uncomp string" */ /* First, find the mararc file */ if(argc == 1) { /* No arguments */ if(find_mararc(mararc_loc) == JS_ERROR) harderror(L_LOC_MARARC); /* "Error locating mararc file" */ } else if(argc==3) { /* maradns -f /wherever/mararc */ if(js_qstr2js(mararc_loc,argv[2]) == JS_ERROR) harderror(L_MARARC_ARG); /* "Could not get mararc from command line" */ } else harderror(L_USAGE); /* "Usage: mararc [-f mararc_location]" */ /* Then parse that file */ if(read_mararc(mararc_loc,errors,&errorn) == JS_ERROR) { harderror(L_MARARC_PARSE); /* "Error parsing contents of mararc file" */ } if(errorn != 0) { /* Print this out at log level 0 because it is a fatal error */ if(errorn != -1) /* "Error parsing contents of mararc file on line " */ printf("%s%d%s",L_MARARC_LINE,errorn,L_N); /* errorn, "\n" */ printf("%s",L_ERROR_CODE); /* "Error code: " */ js_show_stdout(errors); printf("%s",L_N); /* "\n" */ exit(2); } /* There are too many greedy lawyers in the US */ if(js_qstr2js(kvar_query,"hide_disclaimer") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,verbstr) != JS_SUCCESS) { printf("%s","THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS OR\n"); printf("%s","IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n"); printf("%s","OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n"); printf("%s","IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\n"); printf("%s","INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n"); printf("%s","(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n"); printf("%s","SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"); printf("%s","HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n"); printf("%s","STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n"); printf("%s","IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n"); printf("%s","POSSIBILITY OF SUCH DAMAGE.\n"); printf("\nTo not display this message, add the follwing to your "); printf("mararc file:\n\nhide_disclaimer = \"YES\"\n\n"); } /* Get in to a state of least privledge ASAP */ /* Limit the maximum number of processes */ if(js_qstr2js(kvar_query,"maxprocs") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,maxpstr) == JS_ERROR) harderror(L_MAXPROC); /* "Problem getting maxprocs value.\nmaxprocs must be set before starting the MaraDNS server" */ if((maxprocs = js_atoi(maxpstr,0)) == 0) harderror(L_MAXPROC_NUM); /* "Problem converting maxprocs to a number\nThis must be a non-zero number" */ /* MaraDNS has a known problem with hanging if maxprocs is too high */ if(maxprocs > 200) { maxprocs = 200; mlog(L_MAXPROC_MAX); /* "Maxprocs can not be greater than 200\nThere are known problems with MaraDNS hanging using higher numbers" */ } #ifndef DARWIN rlim.rlim_cur = rlim.rlim_max = maxprocs; /* If this OS supports setrlimit and if setrlimit fails, bail (the ENOSYS check is there so OSes w/o setrlimit support can still run MaraDNS) */ #ifndef SOLARIS if(setrlimit(RLIMIT_NPROC,&rlim) != 0 && errno != ENOSYS) sys_harderror(L_MAXPROC_SET); /* "Unable to set maximum number of processes" */ #endif /* SOLARIS */ #endif /* DARWIN */ /* Anything after this does not need recursion enabled for the kvar in question to be read */ /* Determine if we are root */ if(geteuid() == 0) { /* Change the root directory */ if(js_qstr2js(kvar_query,"chroot_dir") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,chrootn) == JS_ERROR) harderror(L_CHROOT_KVAR); /* "Problem getting chroot kvar.\nYou must have chroot_dir set if you start this as root" */ if(js_js2str(chrootn,chroot_zt,200) == JS_ERROR) harderror(L_CHROOT_NT); /* "Problem making chroot nt string.\nMake sure the chroot directory is 200 chars or less" */ if(chdir(chroot_zt) != 0) sys_harderror(L_CHROOT_CHANGE); /* "Problem changing to chroot dir.\nMake sure chroot_dir points to a valid directory" */ if(chroot(chroot_zt) != 0) sys_harderror(L_CHROOT_DO); /* "Problem changing the root directory." */ mlog(L_CHROOT_SUCCESS); /* "Root directory changed" */ /* Bind to port 53 To Do: use capset to give us privledged bind abilities without needing to be root. */ if(js_qstr2js(kvar_query,"bind_address") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,bind_address) == JS_ERROR) harderror(L_NOBIND); /* "Problem getting chroot kvar.\nYou must have bind_address set to the IP maradns will listen on" */ if(udpbind(&sock,bind_address) == JS_ERROR) sys_harderror(L_BINDFAIL); /* "Problem binding to port 53.\nMost likely, another process is already listening on port 53" */ zjlog(L_BIND2ADDR,bind_address); /* "Binding to address " */ mlog(L_BIND_SUCCESS); /* "Socket opened on UDP port 53" */ /* Drop the elevated privileges */ /* First, change the GID */ if(js_qstr2js(kvar_query,"maradns_gid") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,uidstr) == JS_SUCCESS) { gid = js_atoi(uidstr,0); /* Drop all supplemtal groups */ setgroups(1,&gid); /* Set the group ID */ setgid(gid); } /* Next, change the UID */ if(js_qstr2js(kvar_query,"maradns_uid") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,uidstr) == JS_ERROR) harderror(L_NOUID); /* "Problem getting maradns_uid kvar.\nYou must have maradns_uid set if you start this as root" */ if((uid = js_atoi(uidstr,0)) < 10) harderror(L_BADUID); /* "maradns_uid is less than 10 or not a number.\nThis uid must have a value of 10 or more" */ if(setuid(uid) != 0) sys_harderror(L_NODROP); /* "Could not drop root uid" */ /* Workaround for known Linux kernel security problem circa early 2000 */ if(setuid(0) == 0) sys_harderror(L_STILL_ROOT); /* "We seem to still be root" */ mlog(L_DROP_SUCCESS); /* "Root privileges dropped" */ } else { /* Bind to port 53 as a non-root user */ if(js_qstr2js(kvar_query,"bind_address") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,bind_address) == JS_ERROR) harderror(L_NOBIND); /* "Problem getting chroot kvar.\nYou must have bind_address set to the IP maradns will listen on" */ if(udpbind(&sock,bind_address) == JS_ERROR) sys_harderror(L_BEROOT); /* "Problem binding to port 53.\nYou should run this as root" */ mlog(L_BIND_SUCCESS); /* "Socket opened on UDP port 53" */ } /* Flush out any messages that have already appeared */ fflush(stdout); /* Listen for data on the UDP socket */ for(;;) { if(getudp(sock,(struct sockaddr *)&client,incoming,512) == JS_ERROR) continue; if(decompress_data(incoming,uncomp) == JS_ERROR) continue; proc_query(uncomp,(struct sockaddr_in *)&client,sock); } }
/* should be the first function */ void decompress_entry(unsigned long reg_a0, unsigned long reg_a1, unsigned long reg_a2, unsigned long reg_a3, unsigned long icache_size, unsigned long icache_lsize, unsigned long dcache_size, unsigned long dcache_lsize) { unsigned char props[LZMA_PROPERTIES_SIZE]; unsigned int i; /* temp value */ SizeT osize; /* uncompressed size */ int res; board_init(); printf("\n\nLZMA loader for " CONFIG_BOARD_NAME ", Copyright (C) 2007-2008 OpenWrt.org\n\n"); decompress_init(); /* lzma args */ for (i = 0; i < LZMA_PROPERTIES_SIZE; i++) props[i] = get_byte(); /* skip rest of the LZMA coder property */ /* read the lower half of uncompressed size in the header */ osize = ((SizeT)get_byte()) + ((SizeT)get_byte() << 8) + ((SizeT)get_byte() << 16) + ((SizeT)get_byte() << 24); /* skip rest of the header (upper half of uncompressed size) */ for (i = 0; i < 4; i++) get_byte(); res = LzmaDecodeProperties(&lzma_state.Properties, props, LZMA_PROPERTIES_SIZE); if (res != LZMA_RESULT_OK) { printf("Incorrect LZMA stream properties!\n"); halt(); } printf("decompressing kernel... "); lzma_state.Probs = (CProb *)workspace; res = decompress_data(&lzma_state, (unsigned char *)LOADADDR, osize); if (res != LZMA_RESULT_OK) { printf("failed, "); switch (res) { case LZMA_RESULT_DATA_ERROR: printf("data error!\n"); break; default: printf("unknown error %d!\n", res); } halt(); } else printf("done!\n"); blast_dcache(dcache_size, dcache_lsize); blast_icache(icache_size, icache_lsize); printf("launching kernel...\n\n"); #ifdef CONFIG_PASS_KARGS reg_a0 = 0; reg_a1 = 0; reg_a2 = (unsigned long)env_vars; reg_a3 = 0; #endif /* Jump to load address */ ((kernel_entry) LOADADDR)(reg_a0, reg_a1, reg_a2, reg_a3); }
/* * Inflate (uncompress) the content of a read record and return the data as an alternative datastream. */ static bool auto_inflate_record(DCR *dcr) { DEV_RECORD *rec, *nrec; bool retval = false; bool intermediate_value = false; /* * See what our starting point is. When dcr->after_rec is set we already have * a translated record by an other SD plugin. Then we use that translated record * as the starting point otherwise we start at dcr->before_rec. When an earlier * translation already happened we can free that record when we have a success * full translation here as that record is of no use anymore. */ if (dcr->after_rec) { rec = dcr->after_rec; intermediate_value = true; } else { rec = dcr->before_rec; } /* * We only do auto inflation for the following stream types: * * - STREAM_COMPRESSED_DATA * - STREAM_WIN32_COMPRESSED_DATA * - STREAM_SPARSE_COMPRESSED_DATA */ switch (rec->maskedStream) { case STREAM_COMPRESSED_DATA: case STREAM_WIN32_COMPRESSED_DATA: case STREAM_SPARSE_COMPRESSED_DATA: break; default: goto bail_out; } /* * Clone the data from the original DEV_RECORD to the converted one. * As we use the compression buffers for the data we need a new * DEV_RECORD without a new memory buffer so we call new_record here * with the with_data boolean set explicitly to false. */ nrec = bfuncs->new_record(false); bfuncs->copy_record_state(nrec, rec); /* * Setup the converted record to point to the original data. * The decompress_data function will decompress that data and * then update the pointers with the data in the compression buffer * and with the length of the decompressed data. */ nrec->data = rec->data; nrec->data_len = rec->data_len; if (!decompress_data(dcr->jcr, "Unknown", rec->maskedStream, &nrec->data, &nrec->data_len, true)) { bfuncs->free_record(nrec); goto bail_out; } /* * If we succeeded in decompressing the data update the stream type. */ switch (rec->maskedStream) { case STREAM_COMPRESSED_DATA: nrec->Stream = STREAM_FILE_DATA; nrec->maskedStream = STREAM_FILE_DATA; break; case STREAM_WIN32_COMPRESSED_DATA: nrec->Stream = STREAM_WIN32_DATA; nrec->maskedStream = STREAM_WIN32_DATA; break; case STREAM_SPARSE_COMPRESSED_DATA: nrec->Stream = STREAM_SPARSE_DATA; nrec->maskedStream = STREAM_SPARSE_DATA; break; default: break; } Dmsg4(400, "auto_inflate_record: From datastream %d to %d from original size %ld to %ld\n", rec->maskedStream, nrec->maskedStream, rec->data_len, nrec->data_len); /* * If the input is just an intermediate value free it now. */ if (intermediate_value) { bfuncs->free_record(dcr->after_rec); } dcr->after_rec = nrec; retval = true; bail_out: return retval; }