END_TEST static void test_manifest_tell_seek(enum protocol protocol, int phase) { struct slist *slist; struct manio *manio; struct sbuf *sb=NULL; man_off_t *offset=NULL; int entries=1000; prng_init(0); base64_init(); hexmap_init(); recursive_delete(path); slist=build_manifest(path, protocol, entries, phase); fail_unless(slist!=NULL); sb=slist->head; fail_unless((manio=do_manio_open(path, "rb", protocol, phase))!=NULL); read_manifest(&sb, manio, 0, entries/2, protocol, phase); fail_unless((offset=manio_tell(manio))!=NULL); fail_unless(sb!=NULL); fail_unless(!manio_close(&manio)); fail_unless((manio=do_manio_open(path, "rb", protocol, phase))!=NULL); fail_unless(!manio_seek(manio, offset)); read_manifest(&sb, manio, entries/2, entries, protocol, phase); fail_unless(sb==NULL); fail_unless(!manio_close(&manio)); fail_unless(!manio); slist_free(&slist); man_off_t_free(&offset); tear_down(); }
int decode_context (char *dst, int *dstlen, const void *src, int srclen) { base64_ctx x; int i; int n; int m; const unsigned char *p = src; if (base64_init (&x) < 0) return (-1); for (i = 0, n = 0; i < srclen; i++) { if (base64_decode_update (&x, dst, &m, p + i, 1) < 0) return (-1); dst += m; n += m; } if (base64_decode_final (&x, dst, &m) < 0) return (-1); if (base64_cleanup (&x) < 0) return (-1); n += m; *dstlen = n; return (0); }
string decryptPassword( const string& strUser, const string& strPassword ) { base64_t b64; base64_init(&b64, (const char*)&url_base64.alphabet[0]); char buf[512] = {0}; int buflen = base64_decode(&b64, buf, strPassword.c_str(), strPassword.length(), BASE64_IGNORE_SPACE); unsigned char keys[8] = {'r', 'z', 'r', 'k', '.', 'k', 'e', 'y'}; for(size_t i=0; i<strUser.length() && i<sizeof(keys); i++) { keys[i] = keys[i] + strUser.at(i); } AESCipher* pAesCipher = new AESCipher(); pAesCipher->init2(keys, sizeof(keys)); unsigned char out[512] = {0}; size_t outlen = sizeof(out); pAesCipher->decrypt((const unsigned char *)&buf[0], buflen, out, &outlen); if(outlen != buflen) { delete pAesCipher; return strPassword; } delete pAesCipher; string strResult = string((char *)out, outlen); return strResult; }
int encode_context ( char *dst, int *dstlen, const char *src, int srclen) { base64_ctx x; int i; int n; int m; if (base64_init (&x) < 0) return (-1); for (i=0, n=0; i<srclen; i++) { if (base64_encode_update (&x, dst, &m, src + i, 1) < 0) return (-1); dst += m; n += m; } if (base64_encode_final (&x, dst, &m) < 0) return (-1); if (base64_cleanup (&x) < 0) return (-1); n += m; *dstlen = n; return (0); }
int run_bsigs(int argc, char *argv[]) { int ret=1; struct fzp *fzp=NULL; struct iobuf rbuf; struct blk blk; memset(&rbuf, 0, sizeof(struct iobuf)); base64_init(); if(argc!=2) return usage(); path=argv[1]; if(!(fzp=fzp_gzopen(path, "rb"))) goto end; while(1) { iobuf_free_content(&rbuf); switch(iobuf_fill_from_fzp(&rbuf, fzp)) { case 1: ret=0; // Finished OK. case -1: goto end; // Error. } if(parse_cmd(&rbuf, &blk)) goto end; } end: iobuf_free_content(&rbuf); fzp_close(&fzp); return ret; }
//----------------------------------------------------------------------------- // Decode a sequence of 4 byte base64 blocks into a 3 byte binary block stream. // the base64 encoded stream should be a null-terminated string. int base64_decode(char *source, unsigned char *output, int *buflen) { int len; int newlen; int i, j, used; char *in; unsigned char *out; unsigned char ch; assert(source && output && buflen); assert(buflen[0] > 0); base64_init(); len = strlen(source); assert(len > 0); newlen = 0; for(i=0,j=0,in=source,out=output; j<len; i+=3,j+=4,in+=4,out+=3) { while(*in == '\n' || *in == '\r' || *in == ' ' || *in == '\t') { j++; in++; } assert(j < len); assert((i+3)<buflen[0]); assert(newlen == i); assert(in && out); assert(in[0] != '=' && in[0] != 0); assert(in[1] != '=' && in[1] != 0); used = 1; out[0] = (_reverse[(int)in[0]]) << 2; ch = _reverse[(int)in[1]]; out[0] |= ((ch >> 4) & 0x03); if (in[2] != '=') { used ++; out[1] = ((ch & 0x0f) << 4); ch = _reverse[(int)in[2]]; out[1] |= (ch >> 2); if (in[3] != '=') { used ++; out[2] = (ch & 0x03) << 6; out[2] |= _reverse[(int)in[3]]; } } assert(used > 0 && used <= 3); newlen += used; assert(newlen < len); assert(newlen < *buflen); }
/** * gsf_init : * * Initializes the GSF library **/ void gsf_init (void) { static gboolean libgsf_initialized = FALSE; if (libgsf_initialized) return; #ifdef ENABLE_NLS #ifdef G_OS_WIN32 { char *pkg_dir = g_win32_get_package_installation_directory_of_module (gsf_dll_hmodule); gchar *locale_dir = g_build_filename (pkg_dir, "lib/locale", NULL); bindtextdomain (GETTEXT_PACKAGE, locale_dir); g_free (locale_dir); g_free (pkg_dir); } #else bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); #endif bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); #endif g_type_init (); #ifndef HAVE_G_BASE64_ENCODE base64_init (); #endif #ifdef _GSF_GTYPE_THREADING_FIXED if (NULL == static_type_module) { static_type_module = g_object_new (gsf_dummy_type_module_get_type(), NULL); g_assert (static_type_module != NULL); g_type_module_use (static_type_module); g_type_module_set_name (static_type_module, "libgsf-builtin"); } #else gsf_init_dynamic (NULL); #endif { /* Little-endian representation of M_PI. */ static guint8 pibytes[8] = { 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40 }; /* * If this fails, see * http://bugzilla.gnome.org/show_bug.cgi?id=350973 */ double pi = gsf_le_get_double (pibytes); if (!(pi > 3.14 && pi < 3.15)) g_error ("Compilation trouble with endianess."); } }
int main(int argc, char *argv[]) { if(argc < 2) { printf("Usage: raftd <id> <basedir>\n"); return 1; } base64_init(); int id = atoi(argv[1]); if(id < 1 || id > 3) { printf("Usage: raftd <id>\n"); printf("provided <id> > 0 && <id> <= 3\n"); return 1; } srandom(id); verbosity = LOG_DEBUG; struct server_context_t *s = init_raft_server(id, argv[2], "0.0.0.0", ports[id - 1], HTTP, 1000000l, 500000l); int peers[2]; int peer_ports[2]; for(int i = 1, j = 0; i <= 3; i++) { if(id != i) { peers[j] = i; peer_ports[j] = ports[i - 1]; j++; } } printf("Server port = %d\n", ports[id - 1]); printf("Peer ports = %d %d\n", peer_ports[0], peer_ports[1]); const char *hosts[] = { "0.0.0.0", "0.0.0.0" }; if(s) { if(add_pairs(s, 2, peers, hosts, peer_ports)) { DBG_LOG(LOG_FATAL, "[%s][%d] Error while adding peers to server", ss[s->state], s->current_term); deinit_raft_server(s); return 1; } } else { DBG_LOG(LOG_FATAL, "[?][?] Error while creating server instance"); exit(1); } int clientport = ports[id - 1] + 10; printf("Client port = %d\n", clientport); start_server(s, clientport); deinit_raft_server(s); base64_cleanup(); return 0; }
static void run_test(int expected_ret, int manio_entries, int async_read_write_callback(struct async *as), void setup_asfds_callback(struct asfd *asfd, struct asfd *chfd, struct slist *slist)) { struct asfd *asfd; struct asfd *chfd; struct async *as; struct sdirs *sdirs; struct conf **confs; struct slist *slist=NULL; prng_init(0); base64_init(); hexmap_init(); setup(&as, &sdirs, &confs); asfd=asfd_mock_setup(&areads, &awrites); chfd=asfd_mock_setup(&creads, &cwrites); fail_unless((asfd->desc=strdup_w("a", __func__))!=NULL); fail_unless((chfd->desc=strdup_w("c", __func__))!=NULL); as->asfd_add(as, asfd); as->asfd_add(as, chfd); as->read_write=async_read_write_callback; if(manio_entries) slist=build_manifest(sdirs->phase1data, PROTO_2, manio_entries, 1 /*phase*/); setup_asfds_callback(asfd, chfd, slist); fail_unless(do_backup_phase2_server_protocol2( as, chfd, sdirs, 0, // resume confs )==expected_ret); if(!expected_ret) { // FIX THIS: Should check for the presence and correctness of // changed and unchanged manios. } asfd_free(&asfd); asfd_free(&chfd); asfd_mock_teardown(&areads, &awrites); asfd_mock_teardown(&creads, &cwrites); slist_free(&slist); tear_down(&as, &sdirs, &confs); }
int inline_data_receive(char *data,int length) { if(processing_png == true) { if(file_end==1) {processing_png=false; return 1;} char decoded_buffer[10240]; // should be malloc'd based on length. bool failflag; int decoded_buffer_size = base64_decode(data,length,decoded_buffer,&failflag); if(decoded_buffer_size != 0) { inlinepng_process_data(decoded_buffer,decoded_buffer_size); if(file_end==1) { processing_png=false; return 1; } } if(failflag == true) { file_end =1; processing_png=false; return 2; } if(file_end==1) processing_png=false; return 1; } file_end=0; buffer_push(data,length); int pos = buffer_search(inline_magic); if(pos < 0) return 0; processing_png=true; base64_init(); initialize_png_reader(); char decoded_buffer[4096]; // should be malloc'd based on length. bool failflag; int decoded_buffer_size = base64_decode(buffer+pos+strlen(inline_magic),buffer_size-pos-strlen(inline_magic),decoded_buffer,&failflag); if(decoded_buffer_size != 0) { inlinepng_process_data(decoded_buffer,decoded_buffer_size); } buffer_clear(); if(failflag) { processing_png=false; return 0; } return 2; }
int main(int argc, char **argv) { struct agent_core_t core; struct agent_plugin_t *plug; struct pidfh *pfh = NULL; core.config = calloc(1,sizeof(struct agent_config_t)); assert(core.config); core.plugins = NULL; core_alloc_plugins(&core); core_opt(&core, argc, argv); base64_init(); core_plugins(&core); if (core.config->P_arg) p_open(&pfh, core.config->P_arg); sandbox(&core); if (core.config->d_arg) logger(-1, "Plugins initialized. -d argument given, so not forking."); else v_daemon(&pfh); if (pfh) pidfile_write(pfh); ipc_sanity(&core); threads_started = 1; for (plug = core.plugins; plug != NULL; plug = plug->next) { if (plug->start != NULL) plug->thread = plug->start(&core, plug->name); } threads_started = 2; for (plug = core.plugins; plug; plug = plug->next) { if (plug->thread) { pthread_join(*(pthread_t *)plug->thread, NULL); free(plug->thread); } } /* * XXX: Might want to do this on SIGTERM too I suppose. */ if (pfh) pidfile_remove(pfh); return 0; }
int main(int argc, char *argv[]) { base64_init(); START(argc, argv, "rpmemd_obc"); out_init("rpmemd_obc", "RPMEM_LOG_LEVEL", "RPMEM_LOG_FILE", 0, 0); rpmemd_log_init("rpmemd", getenv("RPMEMD_LOG_FILE"), 0); rpmemd_log_level = rpmemd_log_level_from_str( getenv("RPMEMD_LOG_LEVEL")); TEST_CASE_PROCESS(argc, argv, test_cases, NTESTS); rpmemd_log_close(); out_fini(); DONE(NULL); }
static void run_test(int expected_ret, int slist_entries, enum protocol protocol, void setup_callback(struct asfd *asfd, struct slist *slist)) { int result; struct slist *slist=NULL; const char *conffile=BASE "/burp.conf"; struct asfd *asfd; struct conf **confs; const char *buf; if(protocol==PROTO_1) buf=MIN_CLIENT_CONF "protocol=1\n"; else buf=MIN_CLIENT_CONF "protocol=2\n"; base64_init(); asfd=asfd_mock_setup(&reads, &writes); confs=setup_conf(); fail_unless(recursive_delete(BASE)==0); build_file(conffile, buf); fail_unless(!conf_load_global_only(conffile, confs)); if(slist_entries) slist=build_slist_phase1(BASE, protocol, slist_entries); setup_callback(asfd, slist); result=do_restore_client(asfd, confs, ACTION_RESTORE, 0 /* vss_restore */); fail_unless(result==expected_ret); slist_free(&slist); tear_down(&asfd, &confs); }
void test_base64(void **state) { (void) state; /* unused */ /* * Test the base64 routines by encoding and decoding * lstat() packets. */ char where[500]; int i; char *fname; struct stat statp; struct stat statn; int debug_level = 0; char *p; int32_t j; time_t t = 1028712799; fname = BINARYNAME; base64_init(); if (lstat(fname, &statp) < 0) { berrno be; printf("Cannot stat %s: %s\n", fname, be.bstrerror(errno)); } encode_stat(where, &statp, sizeof(statp), 0, 0); //printf("Encoded stat=%s\n", where); #ifdef xxx p = where; p += to_base64((int64_t)(statp.st_atime), p); *p++ = ' '; p += to_base64((int64_t)t, p); printf("%s %s\n", fname, where); printf("%s %lld\n", "st_dev", (int64_t)statp.st_dev); printf("%s %lld\n", "st_ino", (int64_t)statp.st_ino); printf("%s %lld\n", "st_mode", (int64_t)statp.st_mode); printf("%s %lld\n", "st_nlink", (int64_t)statp.st_nlink); printf("%s %lld\n", "st_uid", (int64_t)statp.st_uid); printf("%s %lld\n", "st_gid", (int64_t)statp.st_gid); printf("%s %lld\n", "st_rdev", (int64_t)statp.st_rdev); printf("%s %lld\n", "st_size", (int64_t)statp.st_size); printf("%s %lld\n", "st_blksize", (int64_t)statp.st_blksize); printf("%s %lld\n", "st_blocks", (int64_t)statp.st_blocks); printf("%s %lld\n", "st_atime", (int64_t)statp.st_atime); printf("%s %lld\n", "st_mtime", (int64_t)statp.st_mtime); printf("%s %lld\n", "st_ctime", (int64_t)statp.st_ctime); #endif //printf("%s: len=%d val=%s\n", fname, strlen(where), where); decode_stat(where, &statn, sizeof(statn), &j); assert_false(statp.st_dev != statn.st_dev || statp.st_ino != statn.st_ino || statp.st_mode != statn.st_mode || statp.st_nlink != statn.st_nlink || statp.st_uid != statn.st_uid || statp.st_gid != statn.st_gid || statp.st_rdev != statn.st_rdev || statp.st_size != statn.st_size || statp.st_blksize != statn.st_blksize || statp.st_blocks != statn.st_blocks || statp.st_atime != statn.st_atime || statp.st_mtime != statn.st_mtime || statp.st_ctime != statn.st_ctime); /* { printf("%s: %s\n", fname, where); encode_stat(where, &statn, sizeof(statn), 0, 0); printf("%s: %s\n", fname, where); printf("NOT EQAL\n"); } */ //printf("%d files examined\n", i); to_base64(UINT32_MAX, where); //printf("UINT32_MAX=%s\n", where); int xx = 0; int len; char buf[100]; char junk[100]; // int i; #ifdef xxxx for (i=0; i < 1000; i++) { bin_to_base64(buf, sizeof(buf), (char *)&xx, 4, true); printf("xx=%s\n", buf); xx++; } #endif junk[0] = 0xFF; for (i=1; i<100; i++) { junk[i] = junk[i-1]-1; } len = bin_to_base64(buf, sizeof(buf), junk, 16, true); //printf("len=%d junk=%s\n", len, buf); strcpy(junk, "This is a sample string"); len = bin_to_base64(buf, sizeof(buf), junk, strlen(junk), true); buf[len] = 0; base64_to_bin(junk, sizeof(junk), buf, len); //printf("buf=<%s>\n", junk); }
void inline_data_init(int swidth,int sheight) { base64_init(); inline_data_layer = SDL_CreateRGBSurface(SDL_SWSURFACE,swidth,sheight,32,0x000000FF,0x0000FF00,0x00FF0000,0xFF000000); }
int main() { { struct request_vote_input_t in = {41, 42, 43, 44}; struct method_t *rv = make_request_vote_rpc_method(&in); if(rv) { if(strcmp(rv->name, REQUEST_VOTE_RPC)) { printf("failed = expected %s actual %s\n", REQUEST_VOTE_RPC, rv->name); } else { if(rv->nparams != REQUEST_VOTE_PARAM_COUNT) { printf("failed = expected %d actual %d\n", REQUEST_VOTE_PARAM_COUNT, rv->nparams); } else { if(*(json_int_t *)rv->params[0]->value != 41 || *(json_int_t *)rv->params[1]->value != 42 || *(json_int_t *)rv->params[2]->value != 43 || *(json_int_t *)rv->params[3]->value != 44) { printf("failed = incorrect values in params\n"); } } } free_method_t(rv); } else { printf("failed = null method\n"); } } { base64_init(); unsigned char buf[] = {0, 41, 12, 33}; struct log_entry_t le1 = { buf, 4, 42, 43 }; struct log_entry_t *les[] = { &le1 }; struct append_entries_input_t in = {41, 42, 43, 44, 45, les, 1}; struct method_t *rv = make_append_entries_rpc_method(&in); if(rv) { if(strcmp(rv->name, APPEND_ENTRIES_RPC)) { printf("failed = expected %s actual %s\n", APPEND_ENTRIES_RPC, rv->name); } else { if(rv->nparams != APPEND_ENTRIES_PARAM_COUNT) { printf("failed = expected %d actual %d\n", APPEND_ENTRIES_PARAM_COUNT, rv->nparams); } else { if(*(json_int_t *)rv->params[0]->value != 41 || *(json_int_t *)rv->params[1]->value != 42 || *(json_int_t *)rv->params[2]->value != 43 || *(json_int_t *)rv->params[3]->value != 44 || *(json_int_t *)rv->params[4]->value != 45) { printf("failed = incorrect values in params\n"); } else { if(rv->params[5]->length == 1) { struct data_t *dx = rv->params[5]->child[0]; char *d = (char *)dx->child[0]->value; uint64_t term = *(json_int_t *)dx->child[1]->value; uint64_t index = *(json_int_t *)dx->child[2]->value; size_t outlen = 0; unsigned char *buf = base64_decode(d, strlen(d), &outlen); if(outlen == 4) { if(buf[0] != 0 || buf[1] != 41 || buf[2] != 12 || buf[3] != 33 || term != 42 || index != 43) { printf("failed = incorrect values in decoded buf\n"); } } else { printf("failed = incorrect values in decoded params\n"); } if(buf) free(buf); } else { printf("failed = incorrect values in array params\n"); } } } } free_method_t(rv); } else { printf("failed = null method\n"); } base64_cleanup(); } { struct request_vote_output_t o = {41, 42}; struct data_t *r = make_request_vote_rpc_response(&o); if(r) { if(r->type != RPC_VECTOR || r->length != 2) { printf("failed = incorrect values in output\n"); } free_data_t(r); } else { printf("failed = null output\n"); } } { struct append_entries_output_t o = {41, 42}; struct data_t *r = make_append_entries_rpc_response(&o); if(r) { if(r->type != RPC_VECTOR || r->length != 2) { printf("failed = incorrect values in output\n"); } free_data_t(r); } else { printf("failed = null output\n"); } } return 0; }
int udpxy_main( int argc, char* const argv[] ) { int rc = 0, ch = 0, port = -1, custom_log = 0, no_daemon = 0; char ipaddr[IPADDR_STR_SIZE] = "\0", mcast_addr[IPADDR_STR_SIZE] = "\0"; char pidfile[ MAXPATHLEN ] = "\0"; u_short MIN_MCAST_REFRESH = 0, MAX_MCAST_REFRESH = 0; /* support for -r -w (file read/write) option is disabled by default; * those features are experimental and for dev debugging ONLY * */ #ifdef UDPXY_FILEIO static const char UDPXY_OPTMASK[] = "TvSa:l:p:m:c:B:n:R:r:w:H:M:U:"; #else static const char UDPXY_OPTMASK[] = "TvSa:l:p:m:c:B:n:R:H:M:U:"; #endif struct sigaction qact, iact, cact, oldact; mk_app_info(g_udpxy_app, g_app_info, sizeof(g_app_info) - 1); (void) get_pidstr( PID_RESET, "S" ); rc = init_uopt( &g_uopt ); while( (0 == rc) && (-1 != (ch = getopt(argc, argv, UDPXY_OPTMASK))) ) { switch( ch ) { case 'v': set_verbose( &g_uopt.is_verbose ); break; case 'T': no_daemon = 1; break; case 'S': g_uopt.cl_tpstat = uf_TRUE; break; case 'a': rc = get_ipv4_address( optarg, ipaddr, sizeof(ipaddr) ); if( 0 != rc ) { (void) fprintf( stderr, "Invalid address: [%s]\n", optarg ); rc = ERR_PARAM; } break; case 'p': port = atoi( optarg ); if( port <= 0 ) { (void) fprintf( stderr, "Invalid port number: [%d]\n", port ); rc = ERR_PARAM; } break; case 'm': rc = get_ipv4_address( optarg, mcast_addr, sizeof(mcast_addr) ); if( 0 != rc ) { (void) fprintf( stderr, "Invalid multicast address: " "[%s]\n", optarg ); rc = ERR_PARAM; } break; case 'c': g_uopt.max_clients = atoi( optarg ); if( (g_uopt.max_clients < MIN_CLIENT_COUNT) || (g_uopt.max_clients > MAX_CLIENT_COUNT) ) { (void) fprintf( stderr, "Client count should be between %d and %d\n", MIN_CLIENT_COUNT, MAX_CLIENT_COUNT ); rc = ERR_PARAM; } break; case 'l': g_flog = fopen( optarg, "a" ); if( NULL == g_flog ) { rc = errno; (void) fprintf( stderr, "Error opening logfile " "[%s]: %s\n", optarg, strerror(rc) ); rc = ERR_PARAM; break; } Setlinebuf( g_flog ); custom_log = 1; break; case 'U': base64_init(); rc = read_authfile(optarg); /* ..fills valid_users[] */ if (rc < 0) { (void) fprintf( stderr, "Error reading authfile " "[%s]: %d, %s\n", optarg, rc, strerror(errno) ); rc = ERR_PARAM; } break; case 'B': rc = a2size(optarg, &g_uopt.rbuf_len); if( 0 != rc ) { (void) fprintf( stderr, "Invalid buffer size: [%s]\n", optarg ); exit( ERR_PARAM ); } else if( (g_uopt.rbuf_len < MIN_MCACHE_LEN) || (g_uopt.rbuf_len > MAX_MCACHE_LEN) ) { fprintf(stderr, "Buffer size " "must be within [%ld-%ld] bytes\n", (long)MIN_MCACHE_LEN, (long)MAX_MCACHE_LEN ); rc = ERR_PARAM; } break; case 'n': g_uopt.nice_incr = atoi( optarg ); if( 0 == g_uopt.nice_incr ) { (void) fprintf( stderr, "Invalid nice-value increment: [%s]\n", optarg ); rc = ERR_PARAM; break; } break; case 'R': g_uopt.rbuf_msgs = atoi( optarg ); if( (g_uopt.rbuf_msgs <= 0) && (-1 != g_uopt.rbuf_msgs) ) { (void) fprintf( stderr, "Invalid Rmsgs size: [%s]\n", optarg ); rc = ERR_PARAM; break; } break; case 'H': g_uopt.dhold_tmout = (time_t)atoi( optarg ); if( (0 == g_uopt.dhold_tmout) || ((g_uopt.dhold_tmout) < 0 && (-1 != g_uopt.dhold_tmout)) ) { (void) fprintf( stderr, "Invalid value for max time " "to hold buffered data: [%s]\n", optarg ); rc = ERR_PARAM; break; } break; #ifdef UDPXY_FILEIO case 'r': if( 0 != access(optarg, R_OK) ) { perror("source file - access"); rc = ERR_PARAM; break; } g_uopt.srcfile = strdup( optarg ); break; case 'w': g_uopt.dstfile = strdup( optarg ); break; #endif /* UDPXY_FILEIO */ case 'M': g_uopt.mcast_refresh = (u_short)atoi( optarg ); MIN_MCAST_REFRESH = 30; MAX_MCAST_REFRESH = 64000; if( g_uopt.mcast_refresh && (g_uopt.mcast_refresh < MIN_MCAST_REFRESH || g_uopt.mcast_refresh > MAX_MCAST_REFRESH )) { (void) fprintf( stderr, "Invalid multicast refresh period [%d] seconds, " "min=[%d] sec, max=[%d] sec\n", (int)g_uopt.mcast_refresh, (int)MIN_MCAST_REFRESH, (int)MAX_MCAST_REFRESH ); rc = ERR_PARAM; break; } break; case ':': (void) fprintf( stderr, "Option [-%c] requires an argument\n", optopt ); rc = ERR_PARAM; break; case '?': (void) fprintf( stderr, "Unrecognized option: [-%c]\n", optopt ); rc = ERR_PARAM; break; default: usage( argv[0], stderr ); rc = ERR_PARAM; break; } } /* while getopt */ if (rc) { free_uopt( &g_uopt ); return rc; } openlog( g_udpxy_app, LOG_CONS | LOG_PID, LOG_LOCAL0 ); do { if( (argc < 2) || (port <= 0) || (rc != 0) ) { usage( argv[0], stderr ); rc = ERR_PARAM; break; } if( '\0' == mcast_addr[0] ) { (void) strncpy( mcast_addr, IPv4_ALL, sizeof(mcast_addr) - 1 ); } if( !custom_log ) { /* in debug mode output goes to stderr, otherwise to /dev/null */ g_flog = ((uf_TRUE == g_uopt.is_verbose) ? stderr : fopen( "/dev/null", "a" )); if( NULL == g_flog ) { perror("fopen"); rc = ERR_INTERNAL; break; } } if( 0 == geteuid() ) { if( !no_daemon ) { if( stderr == g_flog ) { (void) fprintf( stderr, "Logfile must be specified to run " "in verbose mode in background\n" ); rc = ERR_PARAM; break; } if( 0 != (rc = daemonize(0, g_flog)) ) { rc = ERR_INTERNAL; break; } } rc = set_pidfile( g_udpxy_app, port, pidfile, sizeof(pidfile) ); if( 0 != rc ) { mperror( g_flog, errno, "set_pidfile" ); rc = ERR_INTERNAL; break; } if( 0 != (rc = make_pidfile( pidfile, getpid(), g_flog )) ) break; } qact.sa_handler = handle_quitsigs; sigemptyset(&qact.sa_mask); qact.sa_flags = 0; if( (sigaction(SIGTERM, &qact, &oldact) < 0) || (sigaction(SIGQUIT, &qact, &oldact) < 0) || (sigaction(SIGINT, &qact, &oldact) < 0)) { perror("sigaction-quit"); rc = ERR_INTERNAL; break; } iact.sa_handler = SIG_IGN; sigemptyset(&iact.sa_mask); iact.sa_flags = 0; if( (sigaction(SIGPIPE, &iact, &oldact) < 0) ) { perror("sigaction-ignore"); rc = ERR_INTERNAL; break; } cact.sa_handler = handle_sigchld; sigemptyset(&cact.sa_mask); cact.sa_flags = 0; if( sigaction(SIGCHLD, &cact, &oldact) < 0 ) { perror("sigaction-sigchld"); rc = ERR_INTERNAL; break; } syslog( LOG_NOTICE, "%s is starting\n", g_app_info ); TRACE( printcmdln( g_flog, g_app_info, argc, argv ) ); rc = srv_loop( ipaddr, port, mcast_addr ); syslog( LOG_NOTICE, "%s is exiting with rc=[%d]\n", g_app_info, rc); TRACE( tmfprintf( g_flog, "%s is exiting with rc=[%d]\n", g_udpxy_app, rc ) ); TRACE( printcmdln( g_flog, g_app_info, argc, argv ) ); } while(0); if( '\0' != pidfile[0] ) { if( -1 == unlink(pidfile) ) { mperror( g_flog, errno, "unlink [%s]", pidfile ); } } if( g_flog && (stderr != g_flog) ) { (void) fclose(g_flog); } closelog(); free_uopt( &g_uopt ); return rc; }
static void run_test(int expected_ret, enum protocol protocol, int manio_entries, int blocks_per_file, void setup_asfds_callback(struct asfd *asfd, struct slist *slist)) { struct async *as; struct asfd *asfd; struct sdirs *sdirs; struct conf **confs; struct slist *slist=NULL; char *dir_for_notify=NULL; prng_init(0); base64_init(); hexmap_init(); setup(protocol, &as, &sdirs, &confs); set_string(confs[OPT_BACKUP], "1"); set_protocol(confs, protocol); asfd=asfd_mock_setup(&reads, &writes); as->asfd_add(as, asfd); as->read_write=async_rw_simple; as->read_quick=async_rw_simple; asfd->as=as; build_storage_dirs(sdirs, sd1, ARR_LEN(sd1)); if(manio_entries) { struct sbuf *s; if(protocol==PROTO_2) slist=build_manifest_with_data_files(sdirs->cmanifest, sdirs->data, manio_entries, blocks_per_file); else { slist=build_manifest(sdirs->cmanifest, protocol, manio_entries, 0 /*phase*/); for(s=slist->head; s; s=s->next) { char path[256]; if(!sbuf_is_filedata(s)) continue; snprintf(path, sizeof(path), "%s/%s%s", sdirs->currentdata, TREE_DIR, s->path.buf); build_file(path, "data"); } } } setup_asfds_callback(asfd, slist); fail_unless(do_restore_server( asfd, sdirs, ACTION_RESTORE, 0, // srestore &dir_for_notify, confs )==expected_ret); if(!expected_ret) { // FIX THIS: Should check for the presence and correctness of // changed and unchanged manios. } slist_free(&slist); free_w(&dir_for_notify); tear_down(&as, &asfd, &sdirs, &confs); }
static #endif int real_main(int argc, char *argv[]) { int ret=1; int option=0; int daemon=1; int forking=1; int strip=0; int randomise=0; struct lock *lock=NULL; struct conf **confs=NULL; int forceoverwrite=0; enum action act=ACTION_LIST; const char *backup=NULL; const char *backup2=NULL; char *restoreprefix=NULL; char *stripfrompath=NULL; const char *regex=NULL; const char *browsefile=NULL; char *browsedir=NULL; const char *conffile=get_conf_path(); const char *orig_client=NULL; const char *logfile=NULL; // The orig_client is the original client that the normal client // would like to restore from. #ifndef HAVE_WIN32 int generate_ca_only=0; #endif int vss_restore=1; int test_confs=0; enum burp_mode mode; log_init(argv[0]); #ifndef HAVE_WIN32 if(!strcmp(prog, "bedup")) return run_bedup(argc, argv); if(!strcmp(prog, "bsigs")) return run_bsigs(argc, argv); #endif while((option=getopt(argc, argv, "a:b:c:C:d:fFghil:nq:Qr:s:tvxjz:?"))!=-1) { switch(option) { case 'a': if(parse_action(&act, optarg)) goto end; break; case 'b': // The diff command may have two backups // specified. if(!backup2 && backup) backup2=optarg; if(!backup) backup=optarg; break; case 'c': conffile=optarg; break; case 'C': orig_client=optarg; break; case 'd': restoreprefix=optarg; // for restores browsedir=optarg; // for lists break; case 'f': forceoverwrite=1; break; case 'F': daemon=0; break; case 'g': #ifndef HAVE_WIN32 generate_ca_only=1; #endif break; case 'i': cmd_print_all(); ret=0; goto end; case 'l': logfile=optarg; break; case 'n': forking=0; break; case 'q': randomise=atoi(optarg); break; case 'Q': log_force_quiet(); break; case 'r': regex=optarg; break; case 's': strip=atoi(optarg); break; case 'v': printf("%s-%s\n", progname(), VERSION); ret=0; goto end; case 'x': vss_restore=0; break; case 't': test_confs=1; break; case 'z': browsefile=optarg; break; case 'h': case '?': default: usage(); goto end; } } if(optind<argc) { usage(); goto end; } if(act==ACTION_MONITOR) { // Try to output everything in JSON. log_set_json(1); #ifndef HAVE_WIN32 // Need to do this so that processes reading stdout get the // result of the printfs of logp straight away. setlinebuf(stdout); #endif } if(!(confs=confs_alloc())) goto end; if(reload(confs, conffile, 1)) goto end; // Dry run to test config file syntax. if(test_confs) { ret=run_test_confs(confs, orig_client); goto end; } if(!backup) switch(act) { case ACTION_DELETE: logp("No backup specified for deletion.\n"); goto end; case ACTION_RESTORE: case ACTION_VERIFY: case ACTION_DIFF: case ACTION_DIFF_LONG: logp("No backup specified. Using the most recent.\n"); backup="0"; default: break; } if(!backup2) switch(act) { case ACTION_DIFF: case ACTION_DIFF_LONG: logp("No second backup specified. Using file system scan.\n"); backup2="n"; // For 'next'. default: break; } // The logfile option is only used for the status client stuff. if(logfile && (act!=ACTION_STATUS && act!=ACTION_STATUS_SNAPSHOT)) logp("-l <logfile> option obsoleted\n"); if(orig_client && *orig_client && set_string(confs[OPT_ORIG_CLIENT], orig_client)) goto end; // The random delay needs to happen before the lock is got, otherwise // you would never be able to use burp by hand. if(randomise) set_int(confs[OPT_RANDOMISE], randomise); mode=get_e_burp_mode(confs[OPT_BURP_MODE]); if(mode==BURP_MODE_CLIENT && (act==ACTION_BACKUP_TIMED || act==ACTION_TIMER_CHECK)) random_delay(confs); if(mode==BURP_MODE_SERVER && act==ACTION_CHAMP_CHOOSER) { // These server modes need to run without getting the lock. } else if(mode==BURP_MODE_CLIENT && (act==ACTION_LIST || act==ACTION_LIST_LONG || act==ACTION_DIFF || act==ACTION_DIFF_LONG || act==ACTION_STATUS || act==ACTION_STATUS_SNAPSHOT || act==ACTION_MONITOR)) { // These client modes need to run without getting the lock. } else { const char *lockfile=confs_get_lockfile(confs); if(!(lock=lock_alloc_and_init(lockfile))) goto end; lock_get(lock); switch(lock->status) { case GET_LOCK_GOT: break; case GET_LOCK_NOT_GOT: logp("Could not get lockfile.\n"); logp("Another process is probably running,\n"); goto end; case GET_LOCK_ERROR: default: logp("Could not get lockfile.\n"); logp("Maybe you do not have permissions to write to %s.\n", lockfile); goto end; } } set_int(confs[OPT_OVERWRITE], forceoverwrite); set_int(confs[OPT_STRIP], strip); set_int(confs[OPT_FORK], forking); set_int(confs[OPT_DAEMON], daemon); strip_trailing_slashes(&restoreprefix); strip_trailing_slashes(&browsedir); if(replace_conf_str(confs[OPT_BACKUP], backup) || replace_conf_str(confs[OPT_BACKUP2], backup2) || replace_conf_str(confs[OPT_RESTOREPREFIX], restoreprefix) || replace_conf_str(confs[OPT_STRIP_FROM_PATH], stripfrompath) || replace_conf_str(confs[OPT_REGEX], regex) || replace_conf_str(confs[OPT_BROWSEFILE], browsefile) || replace_conf_str(confs[OPT_BROWSEDIR], browsedir) || replace_conf_str(confs[OPT_MONITOR_LOGFILE], logfile)) goto end; base64_init(); hexmap_init(); if(mode==BURP_MODE_SERVER) { #ifdef HAVE_WIN32 logp("Sorry, server mode is not implemented for Windows.\n"); #else ret=server_modes(act, conffile, lock, generate_ca_only, confs); #endif } else { ret=client(confs, act, vss_restore); } end: lock_release(lock); lock_free(&lock); confs_free(&confs); return ret; }
rsapem_t * rsapem_init(const char *pemstr) { rsapem_t *rsapem=NULL; const char *header; const char *footer; base64_t *b64dec; unsigned char *data; int datalen; header = strstr(pemstr, RSAPRIVHEADER); footer = strstr(pemstr, RSAPRIVFOOTER); if (!header || !footer) { return NULL; } /* Base64 decode the whole input excluding header and footer */ b64dec = base64_init(NULL, 0, 1); datalen = base64_decode(b64dec, &data, pemstr+sizeof(RSAPRIVHEADER), (footer-header)-sizeof(RSAPRIVHEADER)); base64_destroy(b64dec); b64dec = NULL; if (datalen < 0) { return NULL; } #ifdef RSAPEM_DEBUG { int i; printf("Decoded output:\n"); for (i=0; i<datalen; i++) { printf("%02x", data[i]); } printf("\n"); } #endif /* Check that first 4 bytes are all valid */ if (datalen < 4 || data[0] != 0x30 || data[1] != 0x82) { free(data); return NULL; } else if (((data[2] << 8) | data[3]) != datalen-4) { free(data); return NULL; } rsapem = calloc(1, sizeof(rsapem_t)); if (!rsapem) { free(data); return NULL; } /* Initialize the data */ rsapem->data = data; rsapem->datalen = datalen; rsapem->datapos = 4; data = NULL; datalen = rsapem_read_vector(rsapem, &data); if (datalen != 1 && data[0] != 0x00) { free(data); rsapem_destroy(rsapem); return NULL; } free(data); return rsapem; }