5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 - 2004 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "silcincludes.h"
22 #include "silcclient.h"
23 #include "client_internal.h"
29 /* Add one attribute that was found from hash table */
31 static void silc_client_attributes_process_foreach(void *key, void *context,
34 SilcAttribute attribute = (SilcAttribute)SILC_PTR_TO_32(key);
35 SilcAttributePayload attr = context;
36 SilcAttrForeach *f = user_context;
37 const unsigned char *data;
41 SILC_LOG_DEBUG(("Attribute %d was not set", attribute));
43 /* USER_PUBLIC_KEY we have set earlier */
44 if (attribute == SILC_ATTRIBUTE_USER_PUBLIC_KEY)
47 /* The requested attribute was not found */
48 f->buffer = silc_attribute_payload_encode(f->buffer, attribute,
49 SILC_ATTRIBUTE_FLAG_INVALID,
54 SILC_LOG_DEBUG(("Attribute %d found", attribute));
55 data = silc_attribute_get_data(attr, &data_len);
58 /* We replace the TIMEZONE with valid value here */
59 if (attribute == SILC_ATTRIBUTE_TIMEZONE) {
60 data = (const unsigned char *)silc_get_time(0);
61 data_len = strlen(data);
62 f->buffer = silc_attribute_payload_encode(f->buffer, attribute,
63 SILC_ATTRIBUTE_FLAG_VALID,
64 (void *)data, data_len);
69 f->buffer = silc_attribute_payload_encode_data(f->buffer, attribute,
70 SILC_ATTRIBUTE_FLAG_VALID,
74 /* Process list of attributes. Returns reply to the requested attributes. */
76 SilcBuffer silc_client_attributes_process(SilcClient client,
77 SilcSocketConnection sock,
80 SilcClientConnection conn = sock->user_data;
81 SilcBuffer buffer = NULL;
83 SilcAttribute attribute;
84 SilcAttributePayload attr;
85 SilcAttributeObjPk pk;
86 unsigned char sign[2048 + 1];
89 SILC_LOG_DEBUG(("Process Requested Attributes"));
91 /* If nothing is set by application assume that we don't want to use
92 attributes, ignore the request. */
93 if (!conn->internal->attrs)
96 /* Always put our public key. */
98 pk.data = silc_pkcs_public_key_encode(client->public_key, &pk.data_len);
99 buffer = silc_attribute_payload_encode(buffer,
100 SILC_ATTRIBUTE_USER_PUBLIC_KEY,
101 pk.data ? SILC_ATTRIBUTE_FLAG_VALID :
102 SILC_ATTRIBUTE_FLAG_INVALID,
106 /* Go through all requested attributes */
108 silc_dlist_start(attrs);
109 while ((attr = silc_dlist_get(attrs)) != SILC_LIST_END) {
110 /* Put all attributes of this type */
111 attribute = silc_attribute_get_attribute(attr);
113 /* Ignore signature since we will compute it later */
114 if (attribute == SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE)
117 silc_hash_table_find_foreach(conn->internal->attrs,
118 SILC_32_TO_PTR(attribute),
119 silc_client_attributes_process_foreach,
124 /* Finally compute the digital signature of all the data we provided. */
125 if (silc_pkcs_sign_with_hash(client->pkcs, client->sha1hash,
126 buffer->data, buffer->len,
130 pk.data_len = sign_len;
132 silc_attribute_payload_encode(buffer,
133 SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE,
134 SILC_ATTRIBUTE_FLAG_VALID,
141 static void silc_client_attribute_destruct(void *key, void *context,
144 silc_attribute_payload_free(context);
147 /* Add new attribute */
149 SilcAttributePayload silc_client_attribute_add(SilcClient client,
150 SilcClientConnection conn,
151 SilcAttribute attribute,
153 SilcUInt32 object_size)
155 SilcAttributePayload attr;
157 attr = silc_attribute_payload_alloc(attribute, SILC_ATTRIBUTE_FLAG_VALID,
158 object, object_size);
162 if (!conn->internal->attrs)
163 conn->internal->attrs =
164 silc_hash_table_alloc(0, silc_hash_ptr, NULL, NULL,
165 NULL, silc_client_attribute_destruct,
167 silc_hash_table_add(conn->internal->attrs,
168 SILC_32_TO_PTR(attribute), attr);
172 static void silc_client_attribute_del_foreach(void *key, void *context,
175 SilcClientConnection conn = user_context;
176 SilcAttributePayload attr = context;
177 SilcAttribute attribute;
180 attribute = silc_attribute_get_attribute(attr);
181 silc_hash_table_del_by_context(conn->internal->attrs,
182 SILC_32_TO_PTR(attribute), attr);
185 /* Delete one attribute */
187 bool silc_client_attribute_del(SilcClient client,
188 SilcClientConnection conn,
189 SilcAttribute attribute,
190 SilcAttributePayload attr)
194 if (!conn->internal->attrs)
198 attribute = silc_attribute_get_attribute(attr);
199 ret = silc_hash_table_del_by_context(conn->internal->attrs,
200 SILC_32_TO_PTR(attribute), attr);
201 } else if (attribute) {
202 silc_hash_table_find_foreach(conn->internal->attrs,
203 SILC_32_TO_PTR(attribute),
204 silc_client_attribute_del_foreach, conn);
211 if (!silc_hash_table_count(conn->internal->attrs)) {
212 silc_hash_table_free(conn->internal->attrs);
213 conn->internal->attrs = NULL;
219 /* Return all attributes */
221 SilcHashTable silc_client_attributes_get(SilcClient client,
222 SilcClientConnection conn)
224 return conn->internal->attrs;
227 /* Construct a Requested Attributes buffer. If the `attribute' is zero (0)
228 then all attributes are requested. Additionally `attribute' and
229 all variable arguments can be one requested attribute. Always set
230 the last requested attribute to zero (0) to complete list of
231 requested attribute. */
233 SilcBuffer silc_client_attributes_request(SilcAttribute attribute, ...)
236 SilcBuffer buffer = NULL;
239 return silc_client_attributes_request(SILC_ATTRIBUTE_USER_INFO,
240 SILC_ATTRIBUTE_SERVICE,
241 SILC_ATTRIBUTE_STATUS_MOOD,
242 SILC_ATTRIBUTE_STATUS_FREETEXT,
243 SILC_ATTRIBUTE_STATUS_MESSAGE,
244 SILC_ATTRIBUTE_PREFERRED_LANGUAGE,
245 SILC_ATTRIBUTE_PREFERRED_CONTACT,
246 SILC_ATTRIBUTE_TIMEZONE,
247 SILC_ATTRIBUTE_GEOLOCATION,
248 SILC_ATTRIBUTE_DEVICE_INFO,
249 SILC_ATTRIBUTE_USER_PUBLIC_KEY, 0);
251 va_start(va, attribute);
253 buffer = silc_attribute_payload_encode(buffer, attribute, 0, NULL, 0);
254 attribute = (SilcAttribute)va_arg(va, SilcUInt32);