Added SILC Server library.
[silc.git] / lib / silcclient / client_attrs.c
1 /*
2
3   client_attrs.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 - 2004 Pekka Riikonen
8
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.
12
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.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22 #include "silcclient.h"
23 #include "client_internal.h"
24
25 typedef struct {
26   SilcBuffer buffer;
27 } SilcAttrForeach;
28
29 /* Add one attribute that was found from hash table */
30
31 static void silc_client_attributes_process_foreach(void *key, void *context,
32                                                    void *user_context)
33 {
34   SilcAttribute attribute = (SilcAttribute)SILC_PTR_TO_32(key);
35   SilcAttributePayload attr = context;
36   SilcAttrForeach *f = user_context;
37   const unsigned char *data;
38   SilcUInt32 data_len;
39
40   if (!context) {
41     SILC_LOG_DEBUG(("Attribute %d was not set", attribute));
42
43     /* USER_PUBLIC_KEY we have set earlier */
44     if (attribute == SILC_ATTRIBUTE_USER_PUBLIC_KEY)
45       return;
46
47     /* The requested attribute was not found */
48     f->buffer = silc_attribute_payload_encode(f->buffer, attribute,
49                                               SILC_ATTRIBUTE_FLAG_INVALID,
50                                               NULL, 0);
51     return;
52   }
53
54   SILC_LOG_DEBUG(("Attribute %d found", attribute));
55   data = silc_attribute_get_data(attr, &data_len);
56
57 #if 0
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);
65     return;
66   }
67 #endif
68
69   f->buffer = silc_attribute_payload_encode_data(f->buffer, attribute,
70                                                  SILC_ATTRIBUTE_FLAG_VALID,
71                                                  data, data_len);
72 }
73
74 /* Process list of attributes.  Returns reply to the requested attributes. */
75
76 SilcBuffer silc_client_attributes_process(SilcClient client,
77                                           SilcSocketConnection sock,
78                                           SilcDList attrs)
79 {
80   SilcClientConnection conn = sock->user_data;
81   SilcBuffer buffer = NULL;
82   SilcAttrForeach f;
83   SilcAttribute attribute;
84   SilcAttributePayload attr;
85   SilcAttributeObjPk pk;
86   unsigned char sign[2048 + 1];
87   SilcUInt32 sign_len;
88
89   SILC_LOG_DEBUG(("Process Requested Attributes"));
90
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)
94     return NULL;
95
96   /* Always put our public key. */
97   pk.type = "silc-rsa";
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,
103                                          &pk, sizeof(pk));
104   silc_free(pk.data);
105
106   /* Go through all requested attributes */
107   f.buffer = buffer;
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);
112
113     /* Ignore signature since we will compute it later */
114     if (attribute == SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE)
115       continue;
116
117     silc_hash_table_find_foreach(conn->internal->attrs,
118                                  SILC_32_TO_PTR(attribute),
119                                  silc_client_attributes_process_foreach,
120                                  &f);
121   }
122   buffer = f.buffer;
123
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,
127                                sign, &sign_len)) {
128     pk.type = NULL;
129     pk.data = sign;
130     pk.data_len = sign_len;
131     buffer =
132       silc_attribute_payload_encode(buffer,
133                                     SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE,
134                                     SILC_ATTRIBUTE_FLAG_VALID,
135                                     &pk, sizeof(pk));
136   }
137
138   return buffer;
139 }
140
141 static void silc_client_attribute_destruct(void *key, void *context,
142                                            void *user_context)
143 {
144   silc_attribute_payload_free(context);
145 }
146
147 /* Add new attribute */
148
149 SilcAttributePayload silc_client_attribute_add(SilcClient client,
150                                                SilcClientConnection conn,
151                                                SilcAttribute attribute,
152                                                void *object,
153                                                SilcUInt32 object_size)
154 {
155   SilcAttributePayload attr;
156
157   attr = silc_attribute_payload_alloc(attribute, SILC_ATTRIBUTE_FLAG_VALID,
158                                       object, object_size);
159   if (!attr)
160     return NULL;
161
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,
166                             NULL, TRUE);
167   silc_hash_table_add(conn->internal->attrs,
168                       SILC_32_TO_PTR(attribute), attr);
169   return attr;
170 }
171
172 static void silc_client_attribute_del_foreach(void *key, void *context,
173                                               void *user_context)
174 {
175   SilcClientConnection conn = user_context;
176   SilcAttributePayload attr = context;
177   SilcAttribute attribute;
178   if (!attr)
179     return;
180   attribute = silc_attribute_get_attribute(attr);
181   silc_hash_table_del_by_context(conn->internal->attrs,
182                                  SILC_32_TO_PTR(attribute), attr);
183 }
184
185 /* Delete one attribute */
186
187 SilcBool silc_client_attribute_del(SilcClient client,
188                                SilcClientConnection conn,
189                                SilcAttribute attribute,
190                                SilcAttributePayload attr)
191 {
192   SilcBool ret;
193
194   if (!conn->internal->attrs)
195     return FALSE;
196
197   if (attr) {
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);
205     ret = TRUE;
206   } else{
207     return FALSE;
208   }
209
210   if (ret)
211     if (!silc_hash_table_count(conn->internal->attrs)) {
212       silc_hash_table_free(conn->internal->attrs);
213       conn->internal->attrs = NULL;
214     }
215
216   return ret;
217 }
218
219 /* Return all attributes */
220
221 SilcHashTable silc_client_attributes_get(SilcClient client,
222                                          SilcClientConnection conn)
223 {
224   return conn->internal->attrs;
225 }
226
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. */
232
233 SilcBuffer silc_client_attributes_request(SilcAttribute attribute, ...)
234 {
235   va_list va;
236   SilcBuffer buffer = NULL;
237
238   if (!attribute)
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);
250
251   va_start(va, attribute);
252   while (attribute) {
253     buffer = silc_attribute_payload_encode(buffer, attribute, 0, NULL, 0);
254     attribute = (SilcAttribute)va_arg(va, SilcUInt32);
255   }
256   va_end(va);
257
258   return buffer;
259 }