/* This function returns all the email-password pairs currently in the * database. * This function sets the KMO error string. It returns -1 on failure. */ static int maildb_sqlite_get_all_pwd(maildb *mdb, karray *addr_array, karray *pwd_array) { sqlite3 *db = (sqlite3 *) mdb->db; sqlite3_stmt *stmt = NULL; int error = 0; addr_array->size = 0; pwd_array->size = 0; if (sqlite3_prepare(db, "SELECT email, pwd FROM pwd;", -1, &stmt, NULL)) goto ERR; while (1) { error = sqlite3_step(stmt); if (error == SQLITE_DONE) break; if (error != SQLITE_ROW) goto ERR; kstr *str = kstr_new(); karray_add(addr_array, str); read_string(stmt, str, 0); str = kstr_new(); karray_add(pwd_array, str); read_string(stmt, str, 1); } finalize_stmt(db, &stmt); return 0; ERR: kmo_seterror(sqlite3_errmsg(db)); finalize_stmt(db, &stmt); return -1; }
struct kstr *kstr_fetch(struct ymd_mach *vm, const char *z, int count) { if (count < 0) count = strlen(z); if (count < MAX_KPOOL_LEN) return kpool_index(vm, z, count); return kstr_new(vm, 0, z, count); }
/* This function reads the next element in the buffer. This function sets the * KMOD error string. It returns -1 on failure. If el is null just skip the * element. */ int anp_read_element(kbuffer *buf, struct anp_element *el) { int error = 0; uint8_t type; struct anp_element dummy; struct anp_element *work_el = &dummy; if (el != NULL) work_el = el; if (kbuffer_read8(buf, &type)) return -1; work_el->type = type; switch (work_el->type) { case ANP_UINT32: error = kbuffer_read32(buf, &work_el->uint32); break; case ANP_UINT64: error = kbuffer_read64(buf, &work_el->uint64); break; case ANP_STR: work_el->str = kstr_new(); error = anp_read_element_string(buf, work_el->str); if (error) { kstr_destroy(work_el->str); work_el->str = NULL; } break; case ANP_BIN: work_el->bin = kbuffer_new(); error = anp_read_element_bin(buf, work_el->bin); if (error) { kbuffer_destroy(work_el->bin); work_el->bin = NULL; } break; } if (el == NULL) anp_element_clean(&dummy); return error; }
void anp_msg_write_cstr(struct anp_msg *self, char *str) { struct anp_element *el = anp_element_new(); el->type = ANP_STR; el->str = kstr_new(); kstr_assign_cstr(el->str, str); karray_push(&self->element_array, el); anp_write_kstr(&self->payload, el->str); }
/* This function handles a connection error that occurred while processing a * query. Normally the connection error message should have been set by * kmo_seterror(). The KNP connection is closed if it is open. */ static void knp_query_handle_conn_error(struct knp_query *query, int serv_error_id) { kmod_log_msg(3, "knp_query_handle_conn_error() called.\n"); assert(! query->res_type && query->res_payload == NULL); query->res_type = KNP_RES_SERV_ERROR; query->serv_error_id = serv_error_id; query->serv_error_msg = kstr_new(); kstr_assign_kstr(query->serv_error_msg, kmo_kstrerror()); knp_query_disconnect(query); }
static struct kstr *kpool_insert(struct ymd_mach *vm, const char *z, int count) { struct kpool *kt = &vm->kpool; struct kstr *kz = kstr_new(vm, 1, z, count); struct gc_node **list; if (kt->used >= (1 << kt->shift)) kpool_resize(vm, kt->shift + 1); list = kt->slot + kstr_hash(kz) % (1 << kt->shift); kz->next = *list; *list = gcx(kz); kt->used++; return kz; }
/* This function verifies the attachments specified. */ void kmocrypt_signature_check_attachments2(struct kmocrypt_signature2 *self, karray *attch_array) { int i = 0; size_t spkt_cnt = 0; int * sig_seen, * kmo_seen; struct kmocrypt_attachment_hash * attch_cache; struct kmod_attachment * att; struct kmocrypt_subpacket_list2 * sp; /* If there are no attachments in the signature, then any attachment sent by the plugin needs to be viewed as injected. */ if (self->subpacket_array[KMO_SP_TYPE_ATTACHMENT] == NULL) { for (i = 0; i < attch_array->size; i++) { att = (struct kmod_attachment *) attch_array->data[i]; if (att->status != KMO_EVAL_ATTACHMENT_ERROR) { att->status = KMO_EVAL_ATTACHMENT_INJECTED; } } /* No need to do any further processing at this point. */ return; } /* Count the attachments in the signature. */ sp = self->subpacket_array[KMO_SP_TYPE_ATTACHMENT]; do { spkt_cnt++; sp = sp->next; } while (sp != NULL); /* Allocate the seen array and the hash-cache array. */ sig_seen = kmo_calloc(spkt_cnt * sizeof(uint32_t)); kmo_seen = kmo_calloc(attch_array->size * sizeof(uint32_t)); attch_cache = kmo_calloc(attch_array->size * sizeof(struct kmocrypt_attachment_hash)); /* Mark all faulty attachments sent by KMO as seen so we don't need to care about them later. */ for (i = 0; i < attch_array->size; i++) { att = (struct kmod_attachment *) attch_array->data[i]; if (att->status == KMO_EVAL_ATTACHMENT_ERROR) kmo_seen[i] = 1; } /* Check the name/payload matches. */ sig_check_attachments_name_payload(self, spkt_cnt, attch_array, sig_seen, kmo_seen, attch_cache); /* Check name-only matches(potentially changed payloads). */ sig_check_attachments_name(self, spkt_cnt, attch_array, sig_seen, kmo_seen, attch_cache); /* Check payload matches(potentially changed names). */ sig_check_attachments_payload(self, spkt_cnt, attch_array, sig_seen, kmo_seen, attch_cache); /* Search for injected attachments in what KMO has sent. */ for (i = 0; i < attch_array->size; i++) { att = (struct kmod_attachment *) attch_array->data[i]; if (kmo_seen[i] == 0) att->status = KMO_EVAL_ATTACHMENT_INJECTED; } /* Search for dropped attachments in the signature. */ for (i = 0; i < (int) spkt_cnt; i++) { if (sig_seen[i] == 0) { /* Append an item saying that an attachment was dropped. */ att = (struct kmod_attachment *) kmo_calloc(sizeof(struct kmod_attachment)); att->status = KMO_EVAL_ATTACHMENT_DROPPED; att->name = kstr_new(); karray_add(attch_array, att); } } /* Cleanup. */ free(attch_cache); free(kmo_seen); free(sig_seen); }
/* This function fills an array with the name of the files and directories * contained within the directory specified. The array's content must be freed * by the user. * This function sets the KMO error string. It returns -1 on failure. * Arguments: * Path to the directory to list. * Array that will contain the file names. */ int kfs_ls(char *path, karray *listing) { int error = 0; listing->size = 0; #ifdef __UNIX__ DIR *dir = NULL; struct dirent *dir_entry; /* Try. */ do { dir = opendir(path); if (dir == NULL) { KTOOLS_ERROR_SET("cannot list directory %s: %s", path, kerror_syserror()); error = -1; break; } /* Loop until there are no more files. */ while (1) { dir_entry = readdir(dir); kstr *file_name = NULL; /* OK, error occurred because there is no file (we must assume this * since readdir()'s semantics are not fully specified). */ if (dir_entry == NULL) { break; } /* Skip '.' and '..'. */ if (strcmp(dir_entry->d_name, ".") == 0 || strcmp(dir_entry->d_name, "..") == 0) { continue; } /* Add the file name in the array. */ file_name = kstr_new(); kstr_assign_cstr(file_name, dir_entry->d_name); karray_push(listing, file_name); } } while (0); if (dir) closedir(dir); #endif /* A twisted mind you need indeed to come up with functions such as * FindFirstFile() and FindNextFile(). */ #ifdef __WINDOWS__ HANDLE search_handle = INVALID_HANDLE_VALUE; WIN32_FIND_DATA find_data; int first_flag = 1; kstr *file_name; /* Try. */ do { /* Loop until there are no more files. */ while (1) { /* Get the data about the next file, if any. */ if (first_flag) { first_flag = 0; search_handle = FindFirstFile(path, &find_data); if (search_handle == INVALID_HANDLE_VALUE) { error = GetLastError(); if (error != ERROR_FILE_NOT_FOUND && error != ERROR_NO_MORE_FILES) { KTOOLS_ERROR_SET("cannot list directory %s", path); error = -1; break; } else { error = 0; } /* OK, error occurred because there was no file. */ break; } } else { if (FindNextFile(search_handle, &find_data) == 0) { error = GetLastError(); if (error != ERROR_FILE_NOT_FOUND && error != ERROR_NO_MORE_FILES) { KTOOLS_ERROR_SET("cannot list directory %s", path); error = -1; break; } else { error = 0; } /* OK, error occurred because there was no file. */ break; } } /* Add the file name in the array. */ file_name = kstr_new(); kstr_assign_cstr(file_name, find_data.cFileName); karray_push(listing, file_name); } if (error) break; } while (0); if (search_handle != INVALID_HANDLE_VALUE) FindClose(search_handle); #endif if (error) karray_clear_kstr(listing); return error; }
/* This function decomposes the directory path specified into an array of * subcomponents. If the path is absolute, the absolute part will be something * like 'C:\' on Windows and '/' on UNIX, otherwise it will be empty. The * remaining components will not contain delimiters. * * The path 'C:/toto/bar' will yield ('C:\', 'toto', 'bar') on Windows with alt * support. * The path '/toto/bar/' will yield ('/', 'toto', 'bar') on UNIX. * The path './../foo/' will yield ('.', '..', 'foo'). * The path '' will yield (). */ void kpath_decompose_dir(kstr *path, struct kpath_dir *dir, int format) { int scan_pos = 0; kstr cur_component; /* Get the platform format. */ int platform_format = kpath_get_platform_format(format); /* Determine if the path is absolute. */ int is_absolute = kpath_is_absolute(path, format); kstr_init(&cur_component); kpath_dir_reset(dir); /* If the path is absolute, obtain the absolute part. */ if (is_absolute) { if (platform_format == KPATH_FORMAT_UNIX) { kstr_append_char(&dir->abs_part, '/'); scan_pos = 1; } else { kstr_append_buf(&dir->abs_part, path->data, 2); kstr_append_char(&dir->abs_part, kpath_delim(format)); scan_pos = 3; } } /* Scan the rest of the path. */ while (scan_pos < path->slen) { /* We encountered a delimiter. */ if (kpath_is_delim(path->data[scan_pos], format)) { /* If the current component is empty, ignore it. Otherwise, add the * component in the component array. */ if (cur_component.slen) { kstr *s = kstr_new(); kstr_assign_kstr(s, &cur_component); karray_push(&dir->components, s); } /* Reset the current component. */ kstr_reset(&cur_component); } /* Append the current character. */ else kstr_append_char(&cur_component, path->data[scan_pos]); scan_pos++; } /* Add the last component, if any, in the component array. */ if (cur_component.slen) { kstr *s = kstr_new(); kstr_assign_kstr(s, &cur_component); karray_push(&dir->components, s); } kstr_clean(&cur_component); }