Fixed command calling crash
[silc.git] / lib / silcclient / client_attrs.c
index b244979fb263da9a450a2a602747455041e5f70a..a7825270e4e450cd242f64b53def19c311a6d008 100644 (file)
@@ -1,10 +1,10 @@
 /*
 
-  client_attrs.c 
+  client_attrs.c
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  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,7 +18,7 @@
 */
 /* $Id$ */
 
-#include "silcincludes.h"
+#include "silc.h"
 #include "silcclient.h"
 #include "client_internal.h"
 
@@ -31,10 +31,11 @@ typedef struct {
 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;
   SilcAttrForeach *f = user_context;
   const unsigned char *data;
+  unsigned char tmp[32];
   SilcUInt32 data_len;
 
   if (!context) {
@@ -56,11 +57,13 @@ static void silc_client_attributes_process_foreach(void *key, void *context,
 
   /* We replace the TIMEZONE with valid value here */
   if (attribute == SILC_ATTRIBUTE_TIMEZONE) {
-    data = (const unsigned char *)silc_get_time(0);
-    data_len = strlen(data);
-    f->buffer = silc_attribute_payload_encode(f->buffer, attribute,
-                                             SILC_ATTRIBUTE_FLAG_VALID,
-                                             (void *)data, data_len);
+    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;
   }
 
@@ -72,28 +75,29 @@ static void silc_client_attributes_process_foreach(void *key, void *context,
 /* Process list of attributes.  Returns reply to the requested attributes. */
 
 SilcBuffer silc_client_attributes_process(SilcClient client,
-                                         SilcSocketConnection sock,
+                                         SilcClientConnection conn,
                                          SilcDList attrs)
 {
-  SilcClientConnection conn = sock->user_data;
   SilcBuffer buffer = NULL;
   SilcAttrForeach f;
   SilcAttribute attribute;
   SilcAttributePayload attr;
   SilcAttributeObjPk pk;
-  unsigned char sign[2048];
+  unsigned char sign[2048 + 1];
   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->internal->attrs)
+  if (!conn->internal->attrs) {
+    SILC_LOG_DEBUG(("User has not set any attributes"));
     return NULL;
+  }
 
   /* 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(conn->public_key, &pk.data_len);
   buffer = silc_attribute_payload_encode(buffer,
                                         SILC_ATTRIBUTE_USER_PUBLIC_KEY,
                                         pk.data ? SILC_ATTRIBUTE_FLAG_VALID :
@@ -113,16 +117,16 @@ SilcBuffer silc_client_attributes_process(SilcClient client,
       continue;
 
     silc_hash_table_find_foreach(conn->internal->attrs,
-                                (void *)(SilcUInt32)attribute,
+                                SILC_32_TO_PTR(attribute),
                                 silc_client_attributes_process_foreach,
                                 &f);
   }
   buffer = f.buffer;
 
   /* Finally compute the digital signature of all the data we provided. */
-  if (silc_pkcs_sign_with_hash(client->pkcs, client->sha1hash,
-                              buffer->data, buffer->len,
-                              sign, &sign_len)) {
+  if (silc_pkcs_sign(conn->private_key, silc_buffer_data(buffer),
+                    silc_buffer_len(buffer), sign, sizeof(sign), &sign_len,
+                    TRUE, conn->internal->sha1hash)) {
     pk.type = NULL;
     pk.data = sign;
     pk.data_len = sign_len;
@@ -163,7 +167,7 @@ SilcAttributePayload silc_client_attribute_add(SilcClient client,
                            NULL, silc_client_attribute_destruct,
                            NULL, TRUE);
   silc_hash_table_add(conn->internal->attrs,
-                     (void *)(SilcUInt32)attribute, attr);
+                     SILC_32_TO_PTR(attribute), attr);
   return attr;
 }
 
@@ -177,17 +181,17 @@ static void silc_client_attribute_del_foreach(void *key, void *context,
     return;
   attribute = silc_attribute_get_attribute(attr);
   silc_hash_table_del_by_context(conn->internal->attrs,
-                                (void *)(SilcUInt32)attribute, attr);
+                                SILC_32_TO_PTR(attribute), attr);
 }
 
 /* Delete one attribute */
 
-bool silc_client_attribute_del(SilcClient client,
-                              SilcClientConnection conn,
-                              SilcAttribute attribute,
-                              SilcAttributePayload attr)
+SilcBool silc_client_attribute_del(SilcClient client,
+                                  SilcClientConnection conn,
+                                  SilcAttribute attribute,
+                                  SilcAttributePayload attr)
 {
-  bool ret;
+  SilcBool ret;
 
   if (!conn->internal->attrs)
     return FALSE;
@@ -195,10 +199,10 @@ bool silc_client_attribute_del(SilcClient client,
   if (attr) {
     attribute = silc_attribute_get_attribute(attr);
     ret = silc_hash_table_del_by_context(conn->internal->attrs,
-                                        (void *)(SilcUInt32)attribute, attr);
+                                        SILC_32_TO_PTR(attribute), attr);
   } else if (attribute) {
     silc_hash_table_find_foreach(conn->internal->attrs,
-                                (void *)(SilcUInt32)attribute,
+                                SILC_32_TO_PTR(attribute),
                                 silc_client_attribute_del_foreach, conn);
     ret = TRUE;
   } else{
@@ -216,10 +220,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->internal->attrs;
+  return conn->internal->attrs;
 }
 
 /* Construct a Requested Attributes buffer. If the `attribute' is zero (0)
@@ -235,6 +239,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,