/* Given a pointer to some noise, and a desired length, open up the * random_seed_file and get between 16 bytes and the desired length from * said file, putting the entropy in the noise pointer */ void get_entropy_from_seedfile(uint8_t *noise,int len) { char *filename = 0; int zap = 0; int seed = -1; if(key_s[DWM_S_random_seed_file] == 0) { filename = "/dev/urandom"; /* Default filename */ } else { filename = (char *)dw_to_cstr(key_s[DWM_S_random_seed_file]); zap = 1; } seed = open(filename, O_RDONLY); if(seed == -1) { dw_log_3strings("Fatal error opening random seed file ", filename,"",1); exit(1); } if(read(seed,(void *)noise,len) < 16) { dw_log_3strings("Unable to get 128 bits of entropy; file ", filename, " must be\n at least 16 bytes (128 bits) long",1); exit(1); } if(zap == 1) { free(filename); filename = 0; } close(seed); }
/* This function converts a dw_str object in to a null-terminated * C-string with the last item in the comma-separated list in the * dw_str, with any leading whitespace in the last item removed */ char *pop_last_item(dw_str *list) { dw_str *a = 0, *b = 0; char *ret = 0; a = dw_qspop(list); if(a == 0) { goto catch_pop_last_item; } b = dw_zap_lws(a); if(b == 0) { goto catch_pop_last_item; } ret = (char *)dw_to_cstr(b); catch_pop_last_item: if(a != 0) { dw_destroy(a); a = 0; } if(b != 0) { dw_destroy(b); b = 0; } return ret; }
/* Process a signal received */ void process_signal(int number) { dw_str *filename = 0; char *fname_convert = 0; #ifndef MINGW /* Clear the signal flag */ got_signal = 0; #endif /* MINGW */ /* Write the cache contents to disk */ filename = key_s[DWM_S_cache_file]; if(cache != 0 && filename != 0) { dw_filename_sanitize(filename); fname_convert = (char *)dw_to_cstr(filename); dwh_write_hash(cache,fname_convert); free(fname_convert); } #ifndef MINGW /* Exit if they requested it (*NIX only) */ if(number == 1) { /* TERM */ exit(0); } else if(number == 2) { /* HUP */ exit(8); /* Use by Duende to indicate we exited with HUP */ } #endif /* MINGW */ }
/* Initialize the cache */ void init_cache() { dw_str *filename = 0; char *fname_convert = 0; struct stat cache_st; dwh_process_mararc_params(); /* Get the cache size */ if(cache != 0) { /* Don't init cache twice */ return; } /* See if we can read the cache from a file */ filename = key_s[DWM_S_cache_file]; if(filename != 0 && do_read_cache == 1) { dw_filename_sanitize(filename); fname_convert = (char *)dw_to_cstr(filename); if(stat(fname_convert,&cache_st) == 0 && cache_st.st_mtime < mararc_st.st_mtime) { dw_log_string( "Cache older than rc file; not reading cache",0); } else { cache = dwh_read_hash(fname_convert); } free(fname_convert); } if(cache == 0) { /* Just in case read from file failed */ cache = dwh_hash_init(0); /* Size comes from dwood2rc */ } }
/* Drop privileges and become unprivileged user */ void sandbox() { #ifndef MINGW #ifndef __CYGWIN__ unsigned char *c = 0; gid_t g = DW_UID; if(key_s[DWM_S_chroot_dir] == 0) { dw_fatal("chroot_dir not set"); } c = dw_to_cstr(key_s[DWM_S_chroot_dir]); if(c == 0) { dw_fatal("Converting chroot_dir to string failed"); } if(chdir((char *)c) != 0) { printf("There is no directory %s\n",(char *)c); dw_fatal("chdir() failed"); } #ifndef QNX if(chroot((char *)c) == -1) { dw_fatal("chroot() failed"); } #endif /* QNX */ if(setgroups(1,&g) == -1) { dw_fatal("setgroups() failed"); } if(setgid(maradns_gid) != 0) { dw_fatal("setgid() failed"); } if(setuid(maradns_uid) != 0) { dw_fatal("setuid() failed"); } if(setuid(0) == 0) { dw_fatal("Your kernel\'s setuid() is broken"); } if(c != 0) { free(c); c = 0; } return; #endif /* __CYGWIN__ */ #endif /* MINGW */ }
/* Drop privileges and become unprivileged user */ void sandbox() { #if ! (defined MINGW || defined __CYGWIN__) unsigned char *c = 0; gid_t g = DW_UID; if(key_s[DWM_S_chroot_dir] == 0) { dw_fatal("chroot_dir not set"); } c = dw_to_cstr(key_s[DWM_S_chroot_dir]); if(c == 0) { dw_fatal("Converting chroot_dir to string failed"); } if(chdir((char *)c) != 0) { dw_fatal("chdir() failed"); } #ifndef QNX if(chroot((char *)c) == -1) { dw_fatal("chroot() failed"); } #endif if(setgroups(1,&g) == -1) { dw_fatal("setgroups() failed"); } if(setgid(maradns_gid) != 0) { dw_fatal("setgid() failed"); } if(setuid(maradns_uid) != 0) { dw_fatal("setuid() failed"); } if(setuid(0) == 0) { dw_fatal("Your kernel\'s setuid() is broken"); } if(c != 0) { free(c); c = 0; } return; #endif /* MINGW */ }
/* Initialize the cache */ void init_cache() { dw_str *filename = 0; char *fname_convert = 0; dwh_process_mararc_params(); /* Get the cache size */ if(cache != 0) { /* Don't init cache twice */ return; } /* See if we can read the cache from a file */ filename = key_s[DWM_S_cache_file]; if(filename != 0) { dw_filename_sanitize(filename); fname_convert = (char *)dw_to_cstr(filename); cache = dwh_read_hash(fname_convert); free(fname_convert); } if(cache == 0) { /* Just in case read from file failed */ cache = dwh_hash_init(0); /* Size comes from dwood2rc */ } }