static void wr_command_icon(wi_array_t *arguments) { wi_p7_message_t *message; wi_string_t *path; wi_data_t *data; path = wi_string_by_normalizing_path(WI_ARRAY(arguments, 0)); data = wi_data_init_with_contents_of_file(wi_data_alloc(), path); if(data) { wi_release(wr_icon_path); wr_icon_path = wi_retain(path); wi_release(wr_icon); wr_icon = wi_retain(data); if(wr_connected) { message = wi_p7_message_with_name(WI_STR("wired.user.set_icon"), wr_p7_spec); wi_p7_message_set_data_for_name(message, wr_icon, WI_STR("wired.user.icon")); wr_commands_send_message(message, WI_STR("icon")); } wi_release(data); } else { wr_printf_prefix(WI_STR("icon: %@: %m"), path); } }
void wr_client_init(void) { wr_server_string_encoding = wi_string_encoding_init_with_charset( wi_string_encoding_alloc(), WI_STR("UTF-8"), WI_STRING_ENCODING_IGNORE | WI_STRING_ENCODING_TRANSLITERATE); wr_client_set_charset(WI_STR("UTF-8")); wr_nick = wi_retain(wi_user_name()); wr_icon = wi_data_init_with_base64(wi_data_alloc(), wi_string_with_cstring(wr_default_icon)); }
void wr_client_reload_icon(void) { wi_data_t *data; wi_string_t *icon_path; if(wr_icon) wi_release(wr_icon), wr_icon = NULL; if(wr_icon_path) wi_release(wr_icon_path), wr_icon_path = NULL; icon_path = wi_config_path_for_name(wd_config, WI_STR("icon path")); if(!wi_string_has_prefix(icon_path, WI_STR("/"))) wr_icon_path = wi_retain(wi_string_by_appending_path_component(wi_string_by_appending_path_component(wi_user_home(), WI_STR(".wirebot")), icon_path)); else { wr_icon_path = wi_retain(icon_path); } if(wi_fs_path_exists(wr_icon_path, false)) { wr_icon = wi_data_init_with_contents_of_file(wi_data_alloc(), wr_icon_path); } else { wr_icon = wi_data_init_with_base64(wi_data_alloc(), wi_string_with_cstring(wr_default_icon)); } }
wi_data_t * wi_p7_message_data_for_name(wi_p7_message_t *p7_message, wi_string_t *field_name) { wi_string_t *string; unsigned char *binary; uint32_t field_size; if(p7_message->serialization == WI_P7_BINARY) { if(!_wi_p7_message_get_binary_buffer_for_reading_for_name(p7_message, field_name, &binary, &field_size)) return NULL; return wi_data_with_bytes_no_copy(binary, field_size, false); } else { string = _wi_p7_message_xml_value_for_name(p7_message, field_name); if(!string) return NULL; return wi_autorelease(wi_data_init_with_base64(wi_data_alloc(), string)); } }
wi_data_t * wi_rsa_private_key(wi_rsa_t *rsa) { unsigned char *buffer; int length; if(!rsa->private_key) { buffer = NULL; length = i2d_RSAPrivateKey(rsa->rsa, &buffer); if(length <= 0) { wi_error_set_openssl_error(); return NULL; } rsa->private_key = wi_data_init_with_bytes(wi_data_alloc(), buffer, length); OPENSSL_free(buffer); } return rsa->private_key; }
wi_data_t * wi_string_encoding_data_from_utf8_bytes(wi_string_encoding_t *encoding, const char *buffer, wi_uinteger_t size) { wi_data_t *data; char *inbuffer, *outbuffer, *outbufferp; wi_uinteger_t inbytes, outbytes; size_t bytes, inbytesleft, outbytesleft; iconv_t iconvd; iconvd = iconv_open(wi_string_utf8_string(encoding->target_encoding), wi_string_utf8_string(encoding->utf8_encoding)); if(iconvd == (iconv_t) -1) { wi_error_set_errno(errno); return NULL; } inbytes = inbytesleft = size; outbytes = outbytesleft = size * 4; inbuffer = (char *) buffer; outbuffer = outbufferp = wi_malloc(outbytes); bytes = iconv(iconvd, &inbuffer, &inbytesleft, &outbuffer, &outbytesleft); if(bytes == (size_t) -1) { wi_free(outbufferp); iconv_close(iconvd); wi_error_set_errno(errno); return NULL; } data = wi_data_init_with_bytes(wi_data_alloc(), outbufferp, outbytes - outbytesleft); wi_free(outbufferp); iconv_close(iconvd); return wi_autorelease(data); }
void wi_p7_message_deserialize(wi_p7_message_t *p7_message, wi_p7_serialization_t serialization) { xmlDocPtr doc; xmlNodePtr root_node, field_node; wi_string_t *field_name, *field_value; wi_p7_spec_message_t *message; wi_p7_spec_field_t *field; wi_uuid_t *uuid; wi_date_t *date; wi_data_t *data; if(serialization == WI_P7_BINARY) { p7_message->binary_id = wi_read_swap_big_to_host_int32(p7_message->binary_buffer, 0); message = wi_p7_spec_message_with_id(p7_message->spec, p7_message->binary_id); if(message) p7_message->name = wi_retain(wi_p7_spec_message_name(message)); } else { p7_message->binary_capacity = _WI_P7_MESSAGE_BINARY_BUFFER_INITIAL_SIZE; p7_message->binary_buffer = wi_malloc(p7_message->binary_capacity); p7_message->binary_size = WI_P7_MESSAGE_BINARY_HEADER_SIZE; doc = xmlParseDoc((xmlChar *) wi_string_cstring(p7_message->xml_string)); root_node = xmlDocGetRootElement(doc); if(root_node) { wi_p7_message_set_name(p7_message, wi_xml_node_attribute_with_name(root_node, WI_STR("name"))); for(field_node = root_node->children; field_node != NULL; field_node = field_node->next) { if(field_node->type == XML_ELEMENT_NODE) { if(wi_is_equal(wi_xml_node_name(field_node), WI_STR("field"))) { field_name = wi_xml_node_attribute_with_name(field_node, WI_STR("name")); field_value = wi_xml_node_content(field_node); field = wi_p7_spec_field_with_name(p7_message->spec, field_name); if(!field_name || !field_value || !field) continue; switch(wi_p7_spec_type_id(wi_p7_spec_field_type(field))) { case WI_P7_BOOL: wi_p7_message_set_bool_for_name(p7_message, wi_string_bool(field_value), field_name); break; case WI_P7_ENUM: wi_p7_message_set_enum_for_name(p7_message, wi_string_uint32(field_value), field_name); break; case WI_P7_INT32: wi_p7_message_set_int32_for_name(p7_message, wi_string_int32(field_value), field_name); break; case WI_P7_UINT32: wi_p7_message_set_uint32_for_name(p7_message, wi_string_uint32(field_value), field_name); break; case WI_P7_INT64: wi_p7_message_set_int64_for_name(p7_message, wi_string_int64(field_value), field_name); break; case WI_P7_UINT64: wi_p7_message_set_uint64_for_name(p7_message, wi_string_uint64(field_value), field_name); break; case WI_P7_DOUBLE: wi_p7_message_set_double_for_name(p7_message, wi_string_double(field_value), field_name); break; case WI_P7_STRING: wi_p7_message_set_string_for_name(p7_message, field_value, field_name); break; case WI_P7_UUID: uuid = wi_uuid_with_string(field_value); if(uuid) wi_p7_message_set_uuid_for_name(p7_message, uuid, field_name); break; case WI_P7_DATE: date = wi_date_with_rfc3339_string(field_value); if(date) wi_p7_message_set_date_for_name(p7_message, date, field_name); break; case WI_P7_DATA: data = wi_autorelease(wi_data_init_with_base64(wi_data_alloc(), field_value)); if(data) wi_p7_message_set_data_for_name(p7_message, data, field_name); break; case WI_P7_OOBDATA: wi_p7_message_set_oobdata_for_name(p7_message, wi_string_uint64(field_value), field_name); break; case WI_P7_LIST: WI_ASSERT(0, "Can't deserialize XML with lists at the moment"); break; } } } } } xmlFreeDoc(doc); } }
wi_cipher_t * wi_cipher_init_with_random_key(wi_cipher_t *cipher, wi_cipher_type_t type) { unsigned char *key_buffer, *iv_buffer; int key_length, iv_length; cipher->type = type; cipher->cipher = _wi_cipher_cipher(cipher); key_length = EVP_MAX_KEY_LENGTH; key_buffer = wi_malloc(key_length); iv_length = EVP_CIPHER_iv_length(cipher->cipher); iv_buffer = (iv_length > 0) ? wi_malloc(iv_length) : NULL; if(RAND_bytes(key_buffer, key_length) <= 0) { wi_error_set_openssl_error(); wi_release(cipher); return NULL; } if(iv_buffer) { if(RAND_bytes(iv_buffer, iv_length) <= 0) { wi_error_set_openssl_error(); wi_release(cipher); return NULL; } } if(EVP_EncryptInit_ex(&cipher->encrypt_ctx, cipher->cipher, NULL, NULL, NULL) != 1) { wi_error_set_openssl_error(); wi_release(cipher); return NULL; } if(EVP_DecryptInit_ex(&cipher->decrypt_ctx, cipher->cipher, NULL, NULL, NULL) != 1) { wi_error_set_openssl_error(); wi_release(cipher); return NULL; } _wi_cipher_configure_cipher(cipher); if(EVP_EncryptInit_ex(&cipher->encrypt_ctx, cipher->cipher, NULL, key_buffer, iv_buffer) != 1) { wi_error_set_openssl_error(); wi_release(cipher); return NULL; } if(EVP_DecryptInit_ex(&cipher->decrypt_ctx, cipher->cipher, NULL, key_buffer, iv_buffer) != 1) { wi_error_set_openssl_error(); wi_release(cipher); return NULL; } cipher->key = wi_data_init_with_bytes_no_copy(wi_data_alloc(), key_buffer, key_length, true); cipher->iv = (iv_length > 0) ? wi_data_init_with_bytes_no_copy(wi_data_alloc(), iv_buffer, iv_length, true) : NULL; return cipher; }
void wd_server_apply_settings(void) { wi_data_t *data; wi_string_t *hostname; /* reload banner */ if(wd_settings.banner) { if(wd_settings.banner_changed) { data = wi_data_init_with_contents_of_file(wi_data_alloc(), wd_settings.banner); if(data) { wi_release(wd_banner); wd_banner = wi_retain(wi_data_base64(data)); } else { wi_log_err(WI_STR("Could not open %@: %m"), wd_settings.banner); } wi_release(data); } } else { wi_release(wd_banner); wd_banner = NULL; } /* reload server name/description */ if(wd_settings.name_changed || wd_settings.description_changed) wd_server_send_server_info(true); /* set SSL cipher list */ if(wd_settings.controlcipher) { if(!wi_socket_tls_set_ciphers(wd_control_socket_tls, wd_settings.controlcipher)) { wi_log_err(WI_STR("Could not set TLS cipher list \"%@\": %m"), wd_settings.controlcipher); } } if(wd_settings.transfercipher) { if(!wi_socket_tls_set_ciphers(wd_transfer_socket_tls, wd_settings.transfercipher)) { wi_log_err(WI_STR("Could not set TLS cipher list \"%@\": %m"), wd_settings.transfercipher); } } /* load SSL certificate */ if(!wd_certificate && !wd_private_key) { if(wd_settings.certificate) { wd_private_key = wi_rsa_init_with_pem_file(wi_rsa_alloc(), wd_settings.certificate); if(!wd_private_key) wi_log_warn(WI_STR("Could not find RSA key in %@, creating one..."), wd_settings.certificate); wd_certificate = wi_x509_init_with_pem_file(wi_x509_alloc(), wd_settings.certificate); if(!wd_certificate) wi_log_warn(WI_STR("Could not find certificate in %@, creating one..."), wd_settings.certificate); } if(!wd_private_key) { wd_private_key = wi_rsa_init_with_bits(wi_rsa_alloc(), 1024); if(wd_private_key) wi_log_info(WI_STR("Created 1024-bit RSA key")); else wi_log_err(WI_STR("Could not create RSA key: %m")); } if(!wd_certificate) { hostname = wi_process_hostname(wi_process()); wd_certificate = wi_x509_init_with_common_name(wi_x509_alloc(), wd_private_key, hostname); if(wd_certificate) wi_log_info(WI_STR("Created self-signed certificate for %@"), hostname); else wi_log_err(WI_STR("Could not create self-signed certificate: %m")); } if(!wi_socket_tls_set_private_key(wd_control_socket_tls, wd_private_key) || !wi_socket_tls_set_private_key(wd_transfer_socket_tls, wd_private_key)) { wi_log_err(WI_STR("Could not set TLS private key: %m")); } if(!wi_socket_tls_set_certificate(wd_control_socket_tls, wd_certificate) || !wi_socket_tls_set_certificate(wd_transfer_socket_tls, wd_certificate)) { wi_log_err(WI_STR("Could not set TLS certificate: %m")); } } }
static wi_runtime_instance_t * _wi_data_copy(wi_runtime_instance_t *instance) { wi_data_t *data = instance; return wi_data_init_with_bytes(wi_data_alloc(), data->bytes, data->length); }
wi_data_t * wi_data_with_base64(wi_string_t *base64) { return wi_autorelease(wi_data_init_with_base64(wi_data_alloc(), base64)); }
wi_data_t * wi_data_with_random_bytes(wi_uinteger_t length) { return wi_autorelease(wi_data_init_with_random_bytes(wi_data_alloc(), length)); }
wi_data_t * wi_data_with_bytes_no_copy(void *bytes, wi_uinteger_t length, wi_boolean_t free) { return wi_autorelease(wi_data_init_with_bytes_no_copy(wi_data_alloc(), bytes, length, free)); }
wi_data_t * wi_data_with_bytes(const void *bytes, wi_uinteger_t length) { return wi_autorelease(wi_data_init_with_bytes(wi_data_alloc(), bytes, length)); }
wi_data_t * wi_data(void) { return wi_autorelease(wi_data_init(wi_data_alloc())); }
void wd_server_apply_settings(void) { wi_string_t *string; wi_data_t *data; /* reload banner */ if(wd_settings.banner) { if(wd_settings.banner_changed) { data = wi_data_init_with_contents_of_file(wi_data_alloc(), wd_settings.banner); if(data) { wi_release(wd_banner); wd_banner = wi_retain(wi_data_base64(data)); } else { wi_log_err(WI_STR("Could not open %@: %m"), wd_settings.banner); } wi_release(data); } } else { wi_release(wd_banner); wd_banner = NULL; } /* reload server name/description */ if(wd_settings.name_changed || wd_settings.description_changed) { string = wi_date_iso8601_string(wd_start_date); wd_broadcast(wd_public_chat, 200, WI_STR("%#@%c%#@%c%#@%c%#@%c%#@%c%u%c%llu"), wd_server_version_string, WD_FIELD_SEPARATOR, wd_protocol_version_string, WD_FIELD_SEPARATOR, wd_settings.name, WD_FIELD_SEPARATOR, wd_settings.description, WD_FIELD_SEPARATOR, string, WD_FIELD_SEPARATOR, wd_files_unique_count, WD_FIELD_SEPARATOR, wd_files_unique_size); } /* set SSL cipher list */ if(wd_settings.controlcipher) { if(!wi_socket_context_set_ssl_ciphers(wd_control_socket_context, wd_settings.controlcipher)) { wi_log_err(WI_STR("Could not set SSL cipher list \"%@\": %m"), wd_settings.controlcipher); } } if(wd_settings.transfercipher) { if(!wi_socket_context_set_ssl_ciphers(wd_transfer_socket_context, wd_settings.transfercipher)) { wi_log_err(WI_STR("Could not set SSL cipher list \"%@\": %m"), wd_settings.transfercipher); } } /* load SSL certificate */ if(wd_settings.certificate) { if(!wi_socket_context_set_ssl_certificate(wd_control_socket_context, wd_settings.certificate) || !wi_socket_context_set_ssl_certificate(wd_transfer_socket_context, wd_settings.certificate)) { wi_log_err(WI_STR("Could not load certificate %@: %m"), wd_settings.certificate); } } }