X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcclient%2Fclient_attrs.c;h=1947c1fb3e5d7592f892ef40f6953a88f0d64400;hb=1ea936cbf1bb3b19bd55839b904ef59ada84b8b5;hp=5369310631b75789e965a2410ac905ce35fa5db8;hpb=5d90b0684f07a8a51d5dff5cd108a328ec82ffa9;p=silc.git diff --git a/lib/silcclient/client_attrs.c b/lib/silcclient/client_attrs.c index 53693106..1947c1fb 100644 --- a/lib/silcclient/client_attrs.c +++ b/lib/silcclient/client_attrs.c @@ -1,10 +1,10 @@ /* - client_attrs.c + client_attrs.c Author: Pekka Riikonen - Copyright (C) 2002 Pekka Riikonen + Copyright (C) 2002 - 2007 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,19 +18,30 @@ */ /* $Id$ */ -#include "silcincludes.h" +#include "silc.h" #include "silcclient.h" #include "client_internal.h" +typedef struct { + SilcBuffer buffer; + SilcPKCSSignCb sign_cb; + void *context; +} *SilcAttrSign; + +typedef struct { + SilcBuffer buffer; +} SilcAttrForeach; + /* Add one attribute that was found from hash table */ static void silc_client_attributes_process_foreach(void *key, void *context, void *user_context) { - SilcAttribute attribute = (SilcAttribute)(SilcUInt32)key; + SilcAttribute attribute = (SilcAttribute)SILC_PTR_TO_32(key); SilcAttributePayload attr = context; - SilcBuffer buffer = user_context; + SilcAttrForeach *f = user_context; const unsigned char *data; + unsigned char tmp[32]; SilcUInt32 data_len; if (!context) { @@ -41,43 +52,89 @@ static void silc_client_attributes_process_foreach(void *key, void *context, return; /* The requested attribute was not found */ - buffer = silc_attribute_payload_encode(buffer, attribute, - SILC_ATTRIBUTE_FLAG_INVALID, - NULL, 0); + f->buffer = silc_attribute_payload_encode(f->buffer, attribute, + SILC_ATTRIBUTE_FLAG_INVALID, + NULL, 0); return; } SILC_LOG_DEBUG(("Attribute %d found", attribute)); data = silc_attribute_get_data(attr, &data_len); - buffer = silc_attribute_payload_encode_data(buffer, attribute, - SILC_ATTRIBUTE_FLAG_VALID, - data, data_len); + + /* We replace the TIMEZONE with valid value here */ + if (attribute == SILC_ATTRIBUTE_TIMEZONE) { + if (silc_timezone(tmp, sizeof(tmp))) { + data = tmp; + data_len = strlen(tmp); + f->buffer = silc_attribute_payload_encode(f->buffer, attribute, + SILC_ATTRIBUTE_FLAG_VALID, + (void *)data, data_len); + } + return; + } + + f->buffer = silc_attribute_payload_encode_data(f->buffer, attribute, + SILC_ATTRIBUTE_FLAG_VALID, + data, data_len); +} + +/* Attribute signature callback */ + +static void +silc_client_attributes_process_signed(SilcBool success, + const unsigned char *signature, + SilcUInt32 signature_len, + void *context) +{ + SilcAttrSign s = context; + SilcAttributeObjPk pk; + + if (success) { + pk.type = NULL; + pk.data = (unsigned char *)signature; + pk.data_len = signature_len; + s->buffer = + silc_attribute_payload_encode(s->buffer, + SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE, + SILC_ATTRIBUTE_FLAG_VALID, + &pk, sizeof(pk)); + } + + s->sign_cb(TRUE, silc_buffer_data(s->buffer), silc_buffer_len(s->buffer), + s->context); + + silc_buffer_free(s->buffer); + silc_free(s); } /* Process list of attributes. Returns reply to the requested attributes. */ -SilcBuffer silc_client_attributes_process(SilcClient client, - SilcSocketConnection sock, - SilcDList attrs) +void silc_client_attributes_process(SilcClient client, + SilcClientConnection conn, + SilcDList attrs, + SilcPKCSSignCb sign_cb, + void *context) { - SilcClientConnection conn = sock->user_data; + SilcAttrSign s; SilcBuffer buffer = NULL; + SilcAttrForeach f; SilcAttribute attribute; SilcAttributePayload attr; SilcAttributeObjPk pk; - unsigned char sign[2048]; - SilcUInt32 sign_len; SILC_LOG_DEBUG(("Process Requested Attributes")); /* If nothing is set by application assume that we don't want to use attributes, ignore the request. */ - if (!conn->attrs) - return NULL; + if (!conn->internal->attrs) { + SILC_LOG_DEBUG(("User has not set any attributes")); + sign_cb(FALSE, NULL, 0, context); + return; + } /* Always put our public key. */ pk.type = "silc-rsa"; - pk.data = silc_pkcs_public_key_encode(client->public_key, &pk.data_len); + pk.data = silc_pkcs_public_key_encode(NULL, conn->public_key, &pk.data_len); buffer = silc_attribute_payload_encode(buffer, SILC_ATTRIBUTE_USER_PUBLIC_KEY, pk.data ? SILC_ATTRIBUTE_FLAG_VALID : @@ -86,6 +143,7 @@ SilcBuffer silc_client_attributes_process(SilcClient client, silc_free(pk.data); /* Go through all requested attributes */ + f.buffer = buffer; silc_dlist_start(attrs); while ((attr = silc_dlist_get(attrs)) != SILC_LIST_END) { /* Put all attributes of this type */ @@ -95,26 +153,26 @@ SilcBuffer silc_client_attributes_process(SilcClient client, if (attribute == SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE) continue; - silc_hash_table_find_foreach(conn->attrs, (void *)(SilcUInt32)attribute, + silc_hash_table_find_foreach(conn->internal->attrs, + SILC_32_TO_PTR(attribute), silc_client_attributes_process_foreach, - buffer); + &f); } + buffer = f.buffer; - /* Finally compute the digital signature of all the data we provided. */ - if (silc_pkcs_sign_with_hash(client->pkcs, client->internal->sha1hash, - buffer->data, buffer->len, - sign, &sign_len)) { - pk.type = NULL; - pk.data = sign; - pk.data_len = sign_len; - buffer = - silc_attribute_payload_encode(buffer, - SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE, - SILC_ATTRIBUTE_FLAG_VALID, - &pk, sizeof(pk)); + s = silc_calloc(1, sizeof(*s)); + if (!s) { + sign_cb(FALSE, NULL, 0, context); + return; } + s->sign_cb = sign_cb; + s->context = context; + s->buffer = buffer; - return buffer; + /* Finally compute the digital signature of all the data we provided. */ + silc_pkcs_sign_async(conn->private_key, silc_buffer_data(buffer), + silc_buffer_len(buffer), TRUE, NULL, + client->rng, silc_client_attributes_process_signed, s); } static void silc_client_attribute_destruct(void *key, void *context, @@ -138,30 +196,58 @@ SilcAttributePayload silc_client_attribute_add(SilcClient client, if (!attr) return NULL; - if (!conn->attrs) - conn->attrs = silc_hash_table_alloc(0, silc_hash_ptr, NULL, NULL, - NULL, silc_client_attribute_destruct, - NULL, TRUE); - silc_hash_table_add(conn->attrs, (void *)(SilcUInt32)attribute, attr); + if (!conn->internal->attrs) + conn->internal->attrs = + silc_hash_table_alloc(NULL, 0, silc_hash_ptr, NULL, NULL, + NULL, silc_client_attribute_destruct, + NULL, TRUE); + silc_hash_table_add(conn->internal->attrs, + SILC_32_TO_PTR(attribute), attr); return attr; } +static void silc_client_attribute_del_foreach(void *key, void *context, + void *user_context) +{ + SilcClientConnection conn = user_context; + SilcAttributePayload attr = context; + SilcAttribute attribute; + if (!attr) + return; + attribute = silc_attribute_get_attribute(attr); + silc_hash_table_del_by_context(conn->internal->attrs, + SILC_32_TO_PTR(attribute), attr); +} + /* Delete one attribute */ -bool silc_client_attribute_del(SilcClient client, - SilcClientConnection conn, - SilcAttributePayload attr) +SilcBool silc_client_attribute_del(SilcClient client, + SilcClientConnection conn, + SilcAttribute attribute, + SilcAttributePayload attr) { - SilcAttribute attribute = silc_attribute_get_attribute(attr); - bool ret; + SilcBool ret; + + if (!conn->internal->attrs) + return FALSE; - ret = silc_hash_table_del_by_context(conn->attrs, - (void *)(SilcUInt32)attribute, attr); + if (attr) { + attribute = silc_attribute_get_attribute(attr); + ret = silc_hash_table_del_by_context(conn->internal->attrs, + SILC_32_TO_PTR(attribute), attr); + } else if (attribute) { + silc_hash_table_find_foreach(conn->internal->attrs, + SILC_32_TO_PTR(attribute), + silc_client_attribute_del_foreach, conn); + ret = TRUE; + } else{ + return FALSE; + } if (ret) - if (!silc_hash_table_count(conn->attrs)) { - silc_hash_table_free(conn->attrs); - conn->attrs = NULL; + if (!silc_hash_table_count(conn->internal->attrs)) { + silc_hash_table_free(conn->internal->attrs); + conn->internal->attrs = NULL; } return ret; @@ -169,10 +255,10 @@ bool silc_client_attribute_del(SilcClient client, /* Return all attributes */ -const SilcHashTable silc_client_attributes_get(SilcClient client, - SilcClientConnection conn) +SilcHashTable silc_client_attributes_get(SilcClient client, + SilcClientConnection conn) { - return (const SilcHashTable)conn->attrs; + return conn->internal->attrs; } /* Construct a Requested Attributes buffer. If the `attribute' is zero (0) @@ -188,6 +274,7 @@ SilcBuffer silc_client_attributes_request(SilcAttribute attribute, ...) if (!attribute) return silc_client_attributes_request(SILC_ATTRIBUTE_USER_INFO, + SILC_ATTRIBUTE_USER_ICON, SILC_ATTRIBUTE_SERVICE, SILC_ATTRIBUTE_STATUS_MOOD, SILC_ATTRIBUTE_STATUS_FREETEXT,