err_t __netconn_recv(struct netconn *netconn, struct netbuf **nb, int bsize) { size_t numRead = 0; char *buf = malloc(bsize + 1); if(!buf) { error("malloc(NULL)"); return -1; } struct netbuf *b = netbuf_new(); if(!b) { error("netbuf_new()"); return -1; } numRead = sk_readline(netconn->sk, buf, bsize); if(numRead == -1) { error("read(%d)", netconn->sk); return -1; } dprintf("line [%d]:", numRead); mem_printf(buf, MIN(numRead, bsize-1)); numRead -= (buf[MIN(numRead, bsize) - 1] == '\0'); if(netbuf_ref(b, buf, numRead) != ERR_OK) { error("netbuf_ref()"); return -1; } *nb = b; return 0; }
int util_tar_extract(const char *tar_filename, const char* index_file, const char* out_dir) { TAR *tar = NULL; int ret = tar_open(&tar, tar_filename, &gztype, O_RDONLY, 0, TAR_GNU); if (ret != 0) { ERROR_ERRNO("Fail to open tarfile: %s\n", tar_filename); return ret; } //ret = tar_extract_all(tar, tar_prefix); while (th_read(tar) == 0) { char *archive_filename = th_get_pathname(tar); char *out_filename = mem_printf("%s/%s", out_dir, archive_filename); char *mode = mem_alloc0(4*sizeof(char)); int_to_oct(th_get_mode(tar), mode, 4*sizeof(char)); uid_t uid = th_get_uid(tar); gid_t gid = th_get_gid(tar); INFO("Writing file %s to %s", archive_filename, out_filename); if (TH_ISCHR(tar)) { // writing file attributes to index file (TODO use hashmap to handle duplicates) file_printf_append(index_file, "%s c %s %d %d %d %d\n", archive_filename, mode, uid, gid, th_get_devmajor(tar), th_get_devminor(tar)); } else if (TH_ISBLK(tar)) { // writing file attributes to index file (TODO use hashmap to handle duplicates) file_printf_append(index_file, "%s b %s %d %d %d %d\n", archive_filename, mode, uid, gid, th_get_devmajor(tar), th_get_devminor(tar)); } else { if (tar_extract_file(tar, out_filename) != 0) { INFO_ERRNO("Skipping file: %s", archive_filename); } else { // writing file attributes to index file (TODO use hashmap to handle duplicates) file_printf_append(index_file, "%s m %s %d %d\n", archive_filename, mode, uid, gid); } } mem_free(mode); mem_free(archive_filename); mem_free(out_filename); } ret |= tar_close(tar); return ret; }
bool file_is_mountpoint(const char *file) { bool ret; struct stat s, s_parent; char *parent = mem_printf("%s/..", file); ret = !lstat(file, &s); ret &= !lstat(parent, &s_parent); ret &= (s.st_dev != s_parent.st_dev); mem_free(parent); return ret; }
//-------------------------------------------------------------------------- void new_file_name_c_str_in_buffer( const std::string& folder, const std::string& prefix, const std::string& suffix, u64 cpu, u64 calendar_us ) { assert (m_buffer.size() >= (folder.size() + prefix.size() + suffix.size() + fixed_chars_in_name + 1 ) ); char* str = &m_buffer[0]; auto sz = folder.size(); std::memcpy (str, &folder[0], sz); str += sz; sz = prefix.size(); std::memcpy (str, &prefix[0], sz); str += sz; sz = cpu_clock_chars_in_name + 1; #if defined (MAL_32) const char* fmt = "[%016llx][%016llx]["; #elif defined (MAL_64) const char* fmt = "[c%016lx_b][%016lx]["; #else #error "fix util/system.hpp for your platform (if possible)" #endif mem_printf (str, sz + 1, fmt, cpu, m_cpu_time_base); str += sz; mal_side_effect_assert( calendar_str::write (str, calendar_str::c_str_size, calendar_us) > 0 ); str += calendar_str::str_size; *str++ = ']'; sz = suffix.size(); std::memcpy (str, &suffix[0], sz); str += sz; *str = 0; }
static int scd_load_token_cb(const char *path, const char *name, UNUSED void *data) { if (scd_token) { DEBUG("Token already loaded, stopping scan."); return -1; } /* Only do the rest of the callback if the file name ends with .p12 */ if (strlen(name) >= strlen(TOKEN_DEFAULT_EXT) && !strcmp(name + strlen(name) - strlen(TOKEN_DEFAULT_EXT), TOKEN_DEFAULT_EXT)) { char *token_file = mem_printf("%s/%s", path, name); scd_token = softtoken_new_from_p12(token_file); mem_free(token_file); return 1; } return 0; }
container_config_t * container_config_new(const char *file, const char *buf, UNUSED size_t len) { ASSERT(file); DEBUG("Loading container config from file \"%s\".", file); ContainerConfig *ccfg = (ContainerConfig *) protobuf_message_new_from_textfile(file, &container_config__descriptor); if (!ccfg) { // TODO properly initialize defaults (buf != name/os) if (!buf) { ERROR("Cannot create default container config instance " "without specifying the name."); return NULL; } WARN("Failed loading container config from file \"%s\"." " Using default values and given name.", file); //static LEDColor led_color = LEDCOLOR__RED; ccfg = mem_new(ContainerConfig, 1); container_config__init(ccfg); ccfg->name = mem_strdup(buf); // FIXME ccfg->guest_os = "axos"; // FIXME ccfg->color = container_config_get_rand_color();//led_color++; // FIXME ccfg->guestos_version = 0; // FIXME //if (led_color > LEDCOLOR__YELLOW) { // WARN("Running out of LED colors. Going back to RED."); // led_color = LEDCOLOR__RED; //} //config->max_ram = 1024; // use default from .proto definition //config->storage_size = 100; // use default from .proto definition } ASSERT(ccfg); /* Upgrade code, to be removed when all devices are running axos containers only */ if (!strcmp(ccfg->guest_os, "a1os")) { mem_free(ccfg->guest_os); ccfg->guest_os = mem_printf("%s","axos"); ccfg->guestos_version = guestos_get_version(guestos_mgr_get_latest_by_name("axos", true)); ccfg->n_feature_enabled = 1; ccfg->feature_enabled = mem_new0(char*, 1); /* enable fhgapps for "a1" */ ccfg->feature_enabled[0] = mem_strdup(container_config_features[C_CONFIG_FEATURES_LEN-1]); INFO("%s: enable feature %s", ccfg->name, ccfg->feature_enabled[0]); INFO("Upgraded a1os container to axos"); }
/** * returns 1 if a given file is a p12 token, otherwise 0 */ static int is_token(const char *path, const char *file, UNUSED void *data) { char *location = mem_printf("%s/%s", path, file); char *ext; // regular file if (!file_is_regular(location)) return 0; // with fixed extesion if (!(ext = file_get_extension(file))) return 0; if (!strncmp(ext, TOKEN_DEFAULT_EXT, strlen(TOKEN_DEFAULT_EXT))) { DEBUG("Found token file: %s", location); mem_free(location); return 1; } mem_free(location); return 0; }
static void umem_log_enter(const char *error_str, int serious) { #ifdef __native_client__ if ( s_zfslog_fd < 0 ){ s_zfslog_fd = open(ZVM_ZFS_LOG, O_WRONLY); } write(s_zfslog_fd, error_str, strlen(error_str)); #else int looped; char c; looped = 0; #ifdef ECELERITY mem_printf(serious ? DCRITICAL : DINFO, "umem: %s", error_str); #endif (void) mutex_lock(&umem_error_lock); while ((c = *error_str++) != '\0') { WRITE_AND_INC(umem_error_end, c); if (umem_error_end == umem_error_begin) looped = 1; } umem_error_buffer[umem_error_end] = 0; if (looped) { uint_t idx; umem_error_begin = P2PHASE(umem_error_end + 1, ERR_SIZE); idx = umem_error_begin; WRITE_AND_INC(idx, '.'); WRITE_AND_INC(idx, '.'); WRITE_AND_INC(idx, '.'); } (void) mutex_unlock(&umem_error_lock); #endif //__native_client__ }
static void provisioning_mode() { INFO("Check for existence of device certificate"); bool need_initialization = (!file_exists(DEVICE_CERT_FILE) || !token_file_exists()); // device needs a device certificate if (!file_exists(DEVICE_CONF)) { INFO("Going back to bootloader mode, device config does not exist (Proper" "userdata image needs to be flashed first)"); reboot_reboot(REBOOT); } if (need_initialization) { sleep(5); ssl_init(); } // if no certificate exists, create a csr if (!file_exists(DEVICE_CERT_FILE)) { INFO("Device certificate not available. Switch to device provisioning mode"); if (!file_exists(DEVICE_CSR_FILE) || !file_exists(DEVICE_KEY_FILE)) { DEBUG("Create CSR (recreate if corresponding private key file misses)"); if (file_exists(SCD_TOKEN_DIR) && file_is_dir(SCD_TOKEN_DIR)) { DEBUG("CSR folder already exists"); } else if (mkdir(SCD_TOKEN_DIR, 00755) != 0) { FATAL("Failed to create CSR directory"); } #ifdef ANDROID char *hw_serial = mem_alloc0(PROPERTY_VALUE_MAX); char *hw_name = mem_alloc0(PROPERTY_VALUE_MAX); bool property_read_failure = false; if (!(property_get(PROP_SERIALNO, hw_serial, NULL) > 0)) { ERROR("Failed to read hardware serialno property"); property_read_failure = true; } if (!(property_get(PROP_HARDWARE, hw_name, NULL) > 0)) { ERROR("Failed to read hardware name property"); property_read_failure = true; } char *common_name; if (!property_read_failure) common_name = mem_printf("%s %s", hw_name, hw_serial); else common_name = mem_printf("%s %s", "x86", "0000"); DEBUG("Using common name %s", common_name); #else char *common_name = mem_strdup("common_name"); char *hw_serial = mem_strdup("hw_serial"); char *hw_name = mem_strdup("hw_unknown"); #endif // create device uuid and write to device.conf uuid_t *dev_uuid = uuid_new(NULL); const char *uid; if (!dev_uuid || (uid = uuid_string(dev_uuid)) == NULL) { FATAL("Could not create device uuid"); } DeviceConfig *dev_cfg = (DeviceConfig *) protobuf_message_new_from_textfile(DEVICE_CONF, &device_config__descriptor); if (!dev_cfg) { FATAL("Failed load device config from file \"%s\"!", DEVICE_CONF); } // set device uuid char *proto_uid = mem_strdup(uid); // free this element, as it is overwritten mem_free(dev_cfg->uuid); dev_cfg->uuid = proto_uid; // write the uuid to device config file if (protobuf_message_write_to_file(DEVICE_CONF, (ProtobufCMessage *) dev_cfg) < 0) { FATAL("Could not write device config to \"%s\"!", DEVICE_CONF); } if (ssl_create_csr(DEVICE_CSR_FILE, DEVICE_KEY_FILE, NULL, common_name, uid) != 0) { FATAL("Unable to create CSR"); } // this call also frees proto_uid protobuf_free_message((ProtobufCMessage *) dev_cfg); mem_free(hw_serial); mem_free(hw_name); mem_free(common_name); uuid_free(dev_uuid); DEBUG("CSR with keyfile created and stored"); } else { DEBUG("CSR with keyfile already exists"); } // self-sign device csr to bring the device up // corresponding cert is overwritten during provisioning DEBUG("Create self-signed certificate from CSR"); if (ssl_self_sign_csr(DEVICE_CSR_FILE, DEVICE_CERT_FILE, DEVICE_KEY_FILE) != 0) { FATAL("Unable to self sign existing device.csr"); } } else { INFO("Device certificate found"); if (file_exists(DEVICE_CSR_FILE)) { // this is the case when a non-provisioned trustme phone // created its own device.cert and user.p12 WARN("Device CSR still exists. Device was not correctly provisioned!"); } } // self-create a user token to bring the device up // is removed during provisioning if (!token_file_exists()) { DEBUG("Create initial soft token"); char *token_file = mem_printf("%s/%s%s", SCD_TOKEN_DIR, TOKEN_DEFAULT_NAME, TOKEN_DEFAULT_EXT); if (ssl_create_pkcs12_token(token_file, NULL, TOKEN_DEFAULT_PASS, TOKEN_DEFAULT_NAME) != 0) { FATAL("Unable to create initial user token"); } mem_free(token_file); } // we now have anything for a clean startup so just die and let us be restarted by init if (need_initialization) { ssl_free(); exit(0); } // remark: no certificate validation checks are carried out if (!file_exists(DEVICE_KEY_FILE) || !file_exists(SSIG_ROOT_CERT) || !file_exists(GEN_ROOT_CERT) || !token_file_exists()) { FATAL("Missing certificate chains, user token, or private key for device certificate"); } }
proc_status_t * proc_status_new(pid_t pid) { proc_status_t *status; char *file, *buf, *tmp; int n; file = mem_printf("/proc/%d/status", pid); buf = file_read_new(file, 4096); mem_free(file); IF_NULL_RETVAL(buf, NULL); status = mem_new0(proc_status_t, 1); n = sscanf(buf, "Name:\t%15c", status->name); IF_FALSE_GOTO(n == 1, error); tmp = strchr(status->name, '\n'); if (tmp) tmp[0] = '\0'; TRACE("Parsed name for %d: %s", pid, status->name); tmp = strstr(buf, "\nState:"); IF_NULL_GOTO(tmp, error); n = sscanf(tmp, "\nState:\t%c\n", &status->state); IF_FALSE_GOTO(n == 1, error); TRACE("Parsed state for %d: %c", pid, status->state); tmp = strstr(buf, "\nPid:"); IF_NULL_GOTO(tmp, error); n = sscanf(tmp, "\nPid:\t%d\n", &status->pid); IF_FALSE_GOTO(n == 1, error); TRACE("Parsed pid for %d: %d", pid, status->pid); tmp = strstr(buf, "\nPPid:"); IF_NULL_GOTO(tmp, error); n = sscanf(tmp, "\nPPid:\t%d\n", &status->ppid); IF_FALSE_GOTO(n == 1, error); TRACE("Parsed ppid for %d: %d", pid, status->ppid); tmp = strstr(buf, "\nUid:"); IF_NULL_GOTO(tmp, error); n = sscanf(tmp, "\nUid:\t%u\t%u\t%u\t%u\n", &status->ruid, &status->euid, &status->suid, &status->fuid); IF_FALSE_GOTO(n == 4, error); TRACE("Parsed uid for %d: %u %u %u %u", pid, status->ruid, status->euid, status->suid, status->fuid); tmp = strstr(buf, "\nGid:"); IF_NULL_GOTO(tmp, error); n = sscanf(tmp, "\nGid:\t%u\t%u\t%u\t%u\n", &status->rgid, &status->egid, &status->sgid, &status->fgid); IF_FALSE_GOTO(n == 4, error); TRACE("Parsed gid for %d: %u %u %u %u", pid, status->rgid, status->egid, status->sgid, status->fgid); mem_free(buf); return status; error: mem_free(status); mem_free(buf); return NULL; }