/*! * \brief [lua_CFunction asterisk:create_work_area] Create the work area. * \param L the lua state to use * * This function copies and symlinks files from the asterisk path to prepare * the work area for this instance. */ static int create_work_area(lua_State *L) { const char *work_area; const char *asterisk_path; char src_buf[PATH_MAX], dst_buf[PATH_MAX]; mode_t dir_mode = S_IRWXU | S_IRGRP| S_IXGRP| S_IROTH | S_IXOTH; int i; /* directories must end in '/' */ const char *copy_dirs[] = { "/etc/asterisk/", "/usr/lib/asterisk/modules/", "/usr/include/asterisk/", "/var/lib/asterisk/", "/var/log/asterisk/", "/var/spool/asterisk/", NULL, }; /* directories must end in '/' */ const char *create_dirs[] = { "/var/run/asterisk/", NULL, }; const char *asterisk_files[] = { "/usr/sbin/astcanary", "/usr/sbin/asterisk", "/usr/sbin/astgenkey", "/usr/sbin/autosupport", "/usr/sbin/rasterisk", "/usr/sbin/safe_asterisk", NULL, }; luaL_checktype(L, 1, LUA_TTABLE); /* get the work area for this instance */ lua_getfield(L, 1, "work_area"); work_area = lua_tostring(L, -1); /* get the asterisk path */ lua_getfield(L, LUA_REGISTRYINDEX, "astlib_path"); asterisk_path = lua_tostring(L, -1); /* copy directories */ for (i = 0; copy_dirs[i]; i++) { snprintf(src_buf, sizeof(src_buf), "%s%s", asterisk_path, copy_dirs[i]); snprintf(dst_buf, sizeof(dst_buf), "%s%s", work_area, copy_dirs[i]); if (mkdir_p(dst_buf, dir_mode)) { lua_pushstring(L, "unable to create directory in work area ("); lua_pushstring(L, dst_buf); lua_pushstring(L, "): "); lua_pushstring(L, strerror(errno)); lua_concat(L, 4); return lua_error(L); } if (symlink_copy_dir(L, src_buf, dst_buf)) { lua_pushstring(L, "\nerror initilizing work area"); lua_concat(L, 2); return lua_error(L); } } /* create directories */ for (i = 0; create_dirs[i]; i++) { snprintf(src_buf, sizeof(src_buf), "%s%s", asterisk_path, create_dirs[i]); snprintf(dst_buf, sizeof(dst_buf), "%s%s", work_area, create_dirs[i]); if (mkdir_p(dst_buf, dir_mode)) { lua_pushstring(L, "unable to create directory in work area ("); lua_pushstring(L, dst_buf); lua_pushstring(L, "): "); lua_pushstring(L, strerror(errno)); lua_concat(L, 4); return lua_error(L); } } /* copy files */ for (i = 0; asterisk_files[i]; i++) { snprintf(src_buf, sizeof(src_buf), "%s%s", asterisk_path, asterisk_files[i]); snprintf(dst_buf, sizeof(dst_buf), "%s%s", work_area, asterisk_files[i]); if (mkdir_p(dst_buf, dir_mode)) { lua_pushstring(L, "unable to create directory in work area ("); lua_pushstring(L, dst_buf); lua_pushstring(L, "): "); lua_pushstring(L, strerror(errno)); lua_concat(L, 4); return lua_error(L); } if (symlink_file(L, src_buf, dst_buf)) { lua_pushstring(L, "\nerror initilizing work area"); lua_concat(L, 2); return lua_error(L); } } return 0; }
static int burp_ca_init(struct conf *conf, const char *ca_dir) { int a=0; const char *args[15]; char linktarget[1024]=""; if(is_dir_lstat(ca_dir)) return 0; setup_stuff_done++; logp("Initialising %s\n", ca_dir); logp("Running '%s --init --ca %s --dir %s --config %s'\n", conf->ca_burp_ca, conf->ca_name, ca_dir, conf->ca_conf); args[a++]=conf->ca_burp_ca; args[a++]="--init"; args[a++]="--ca"; args[a++]=conf->ca_name; args[a++]="--dir"; args[a++]=ca_dir; args[a++]="--config"; args[a++]=conf->ca_conf; args[a++]=NULL; if(run_script(NULL /* no async yet */, args, NULL, conf, 1 /* wait */, 0, 0 /* do not use logp - stupid openssl prints lots of dots one at a time with no way to turn it off */)) { logp("Error running %s\n", conf->ca_burp_ca); return -1; } logp("Generating server key and cert signing request\n"); logp("Running '%s --key --request --name %s --dir %s --config %s'\n", conf->ca_burp_ca, conf->ca_server_name, ca_dir, conf->ca_conf); a=0; args[a++]=conf->ca_burp_ca; args[a++]="--key"; args[a++]="--request"; args[a++]="--name"; args[a++]=conf->ca_server_name; args[a++]="--dir"; args[a++]=ca_dir; args[a++]="--config"; args[a++]=conf->ca_conf; args[a++]=NULL; if(run_script(NULL /* no async yet */, args, NULL, conf, 1 /* wait */, 0, 0 /* do not use logp - stupid openssl prints lots of dots one at a time with no way to turn it off */)) { logp("Error running %s\n", conf->ca_burp_ca); return -1; } logp("Signing request\n"); logp("Running '%s --sign --ca %s --name %s --batch --dir %s --config %s'\n", conf->ca_burp_ca, conf->ca_name, conf->ca_server_name, ca_dir, conf->ca_conf); a=0; args[a++]=conf->ca_burp_ca; args[a++]="--sign"; args[a++]="--ca"; args[a++]=conf->ca_name; args[a++]="--name"; args[a++]=conf->ca_server_name; args[a++]="--batch"; args[a++]="--dir"; args[a++]=ca_dir; args[a++]="--config"; args[a++]=conf->ca_conf; args[a++]=NULL; if(run_script(NULL /* no async yet */, args, NULL, conf, 1 /* wait */, 0, 0 /* do not use logp - stupid openssl prints lots of dots one at a time with no way to turn it off */)) { logp("Error running %s\n", conf->ca_burp_ca); return -1; } snprintf(linktarget, sizeof(linktarget), "%s/CA_%s.crt", ca_dir, conf->ca_name); if(strcmp(linktarget, conf->ssl_cert_ca)) { remove_file(conf->ssl_cert_ca); if(symlink_file(linktarget, conf->ssl_cert_ca)) return -1; } snprintf(linktarget, sizeof(linktarget), "%s/%s.crt", ca_dir, conf->ca_server_name); if(strcmp(linktarget, conf->ssl_cert)) { remove_file(conf->ssl_cert); if(symlink_file(linktarget, conf->ssl_cert)) return -1; } snprintf(linktarget, sizeof(linktarget), "%s/%s.key", ca_dir, conf->ca_server_name); if(strcmp(linktarget, conf->ssl_key)) { remove_file(conf->ssl_key); if(symlink_file(linktarget, conf->ssl_key)) return -1; } return 0; }
/*! * \brief Recursively symlink and copy a directory. * \param L the lua state to use * \param src the source directory * \param dst the destination directory * * This function recursively creates symlinks to files in src in the dst * directory. It does not symlink directories and instead makes new * directories in dst matching the corisponding dir in src. * * \note On error an error message is pushed onto the given lua stack. * * \retval 0 success * \retval -1 error */ static int symlink_copy_dir(lua_State *L, const char *src, const char *dst) { DIR *src_dir; struct dirent *d; char src_path[PATH_MAX], dst_path[PATH_MAX]; struct stat st; if (!(src_dir = opendir(src))) { lua_pushstring(L, "error opening dir '"); lua_pushstring(L, src); lua_pushstring(L, "': "); lua_pushstring(L, strerror(errno)); lua_concat(L, 4); goto e_return; } while ((d = readdir(src_dir))) { snprintf(src_path, sizeof(src_path), "%s/%s", src, d->d_name); snprintf(dst_path, sizeof(dst_path), "%s/%s", dst, d->d_name); if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) { continue; } if (lstat(src_path, &st)) { lua_pushstring(L, "error with stat for '"); lua_pushstring(L, src_path); lua_pushstring(L, "': "); lua_pushstring(L, strerror(errno)); lua_concat(L, 4); goto e_closedir; } if (S_ISDIR(st.st_mode)) { if (mkdir(dst_path, st.st_mode)) { lua_pushstring(L, "error creating dir '"); lua_pushstring(L, dst_path); lua_pushstring(L, "': "); lua_pushstring(L, strerror(errno)); lua_concat(L, 4); goto e_closedir; } if (symlink_copy_dir(L, src_path, dst_path)) { goto e_closedir; } } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { if (symlink_file(L, src_path, dst_path)) { goto e_closedir; } } else { /* XXX we don't know what kind of file this is so we * will ignore it silently, at some point in the future * we should log this event somewhere */ continue; #if 0 /* unsupported file type */ lua_pushstring(L, "don't know how to symlink '"); lua_pushstring(L, src_path); lua_pushstring(L, "' (unsupported file type)"); lua_concat(L, 3); goto e_closedir; #endif } } closedir(src_dir); return 0; e_closedir: closedir(src_dir); e_return: return -1; }