+ /* Add new channel */
+
+ SILC_LOG_DEBUG(("Adding new [%s] channel %s id(%s)",
+ (created == 0 ? "existing" : "created"), channel_name,
+ silc_id_render(id, SILC_ID_CHANNEL)));
+
+ /* If the channel is found from global list we must move it to the
+ local list. */
+ entry = silc_idlist_find_channel_by_name(server->global_list,
+ channel_name, &cache);
+ if (entry)
+ silc_idlist_del_channel(server->global_list, entry);
+
+ /* Add the channel to our local list. */
+ entry = silc_idlist_add_channel(server->local_list, strdup(channel_name),
+ SILC_CHANNEL_MODE_NONE, id,
+ server->router, NULL, hmac, 0);
+ if (!entry) {
+ silc_free(id);
+ goto out;
+ }
+ hmac = NULL;
+ server->stat.my_channels++;
+ server->stat.channels++;
+ } else {
+ /* The entry exists. */
+
+ /* If ID has changed, then update it to the cache too. */
+ if (!SILC_ID_CHANNEL_COMPARE(entry->id, id))
+ silc_idlist_replace_channel_id(server->local_list, entry->id, id);
+
+ entry->disabled = FALSE;
+
+ /* Remove the founder auth data if the mode is not set but we have
+ them in the entry */
+ if (!(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) && entry->founder_key) {
+ silc_pkcs_public_key_free(entry->founder_key);
+ entry->founder_key = NULL;
+ }
+ }
+
+ if (founder_key) {
+ if (entry->founder_key)
+ silc_pkcs_public_key_free(entry->founder_key);
+ entry->founder_key = founder_key;
+ founder_key = NULL;
+ }
+
+ if (entry->hmac_name && (hmac || (!hmac && entry->hmac))) {
+ silc_free(entry->hmac_name);
+ entry->hmac_name = strdup(silc_hmac_get_name(hmac ? hmac : entry->hmac));
+ }
+
+ /* Get the ban list */
+ tmp = silc_argument_get_arg_type(cmd->args, 8, &len);
+ if (tmp && len > 2) {
+ SilcArgumentPayload iargs;
+ SilcUInt16 iargc;
+ SILC_GET16_MSB(iargc, tmp);
+ iargs = silc_argument_payload_parse(tmp + 2, len - 2, iargc);
+ if (iargs) {
+ /* Delete old ban list */
+ if (entry->ban_list)
+ silc_hash_table_free(entry->ban_list);
+ entry->ban_list =
+ silc_hash_table_alloc(0, silc_hash_ptr,
+ NULL, NULL, NULL,
+ silc_server_inviteban_destruct, entry, TRUE);
+
+ /* Add new ban list */
+ silc_server_inviteban_process(server, entry->ban_list, 0, iargs);
+ silc_argument_payload_free(iargs);
+ }
+ }
+
+ /* Get the invite list */
+ tmp = silc_argument_get_arg_type(cmd->args, 9, &len);
+ if (tmp && len > 2) {
+ SilcArgumentPayload iargs;
+ SilcUInt16 iargc;
+ SILC_GET16_MSB(iargc, tmp);
+ iargs = silc_argument_payload_parse(tmp + 2, len - 2, iargc);
+ if (iargs) {
+ /* Delete old invite list */
+ if (entry->invite_list)
+ silc_hash_table_free(entry->invite_list);
+ entry->invite_list =
+ silc_hash_table_alloc(0, silc_hash_ptr,
+ NULL, NULL, NULL,
+ silc_server_inviteban_destruct, entry, TRUE);
+
+ /* Add new invite list */
+ silc_server_inviteban_process(server, entry->invite_list, 0, iargs);
+ silc_argument_payload_free(iargs);
+ }
+ }
+
+ /* Get the topic */
+ tmp = silc_argument_get_arg_type(cmd->args, 10, &len);
+ if (tmp) {
+ silc_free(entry->topic);
+ entry->topic = strdup(tmp);
+ }
+
+ /* If channel was not created we know there is global users on the
+ channel. */
+ entry->global_users = (created == 0 ? TRUE : FALSE);
+
+ /* If channel was just created the mask must be zero */
+ if (!entry->global_users && mode) {
+ SILC_LOG_DEBUG(("Buggy router `%s' sent non-zero mode mask for "
+ "new channel, forcing it to zero", cmd->sock->hostname));
+ mode = 0;
+ }
+
+ /* Save channel mode */
+ entry->mode = mode;
+
+ /* Save channel key */
+ if (keyp) {
+ if (!(entry->mode & SILC_CHANNEL_MODE_PRIVKEY))
+ silc_server_save_channel_key(server, keyp, entry);
+ silc_buffer_free(keyp);
+ }
+
+ /* Save the users to the channel */
+ silc_server_save_users_on_channel(server, cmd->sock, entry,
+ client_id, client_id_list,
+ client_mode_list, list_count);
+ entry->users_resolved = TRUE;
+
+ out:
+ SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_JOIN);
+ err:
+ if (hmac)
+ silc_hmac_free(hmac);
+ silc_free(client_id);
+ silc_server_command_reply_free(cmd);
+
+ silc_pkcs_public_key_free(founder_key);
+ if (client_id_list)
+ silc_buffer_free(client_id_list);
+ if (client_mode_list)
+ silc_buffer_free(client_mode_list);
+}
+
+/* Received reply to STATS command. */
+
+SILC_SERVER_CMD_REPLY_FUNC(stats)
+{
+ SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
+ SilcServer server = cmd->server;
+ SilcStatus status, error;
+ unsigned char *tmp;
+ SilcUInt32 tmp_len;
+ SilcBufferStruct buf;
+
+ COMMAND_CHECK_STATUS;
+
+ /* Get statistics structure */
+ tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
+ if (server->server_type != SILC_ROUTER && tmp) {
+ silc_buffer_set(&buf, tmp, tmp_len);
+ silc_buffer_unformat(&buf,
+ SILC_STR_UI_INT(NULL),
+ SILC_STR_UI_INT(NULL),
+ SILC_STR_UI_INT(NULL),
+ SILC_STR_UI_INT(NULL),
+ SILC_STR_UI_INT(NULL),
+ SILC_STR_UI_INT(NULL),
+ SILC_STR_UI_INT(&server->stat.cell_clients),
+ SILC_STR_UI_INT(&server->stat.cell_channels),
+ SILC_STR_UI_INT(&server->stat.cell_servers),
+ SILC_STR_UI_INT(&server->stat.clients),
+ SILC_STR_UI_INT(&server->stat.channels),
+ SILC_STR_UI_INT(&server->stat.servers),
+ SILC_STR_UI_INT(&server->stat.routers),
+ SILC_STR_UI_INT(&server->stat.server_ops),
+ SILC_STR_UI_INT(&server->stat.router_ops),
+ SILC_STR_END);
+ }
+
+ out:
+ SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_STATS);
+ err:
+ silc_server_command_reply_free(cmd);
+}
+
+SILC_SERVER_CMD_REPLY_FUNC(users)
+{
+ SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
+ SilcServer server = cmd->server;
+ SilcStatus status, error;
+ SilcChannelEntry channel;
+ SilcChannelID *channel_id = NULL;
+ SilcBuffer client_id_list;
+ SilcBuffer client_mode_list;
+ unsigned char *tmp;
+ SilcUInt32 tmp_len;
+ SilcUInt32 list_count;
+
+ COMMAND_CHECK_STATUS;
+
+ /* Get channel ID */
+ tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
+ if (!tmp)
+ goto out;
+ channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
+ if (!channel_id)
+ goto out;
+
+ /* Get channel entry */
+ channel = silc_idlist_find_channel_by_id(server->local_list,
+ channel_id, NULL);
+ if (!channel) {
+ channel = silc_idlist_find_channel_by_id(server->global_list,
+ channel_id, NULL);
+ if (!channel) {
+ SilcBuffer idp;
+
+ if (server->server_type != SILC_SERVER)
+ goto out;
+
+ idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
+ silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
+ SILC_COMMAND_IDENTIFY, ++server->cmd_ident,
+ 1, 5, idp->data, idp->len);
+ silc_buffer_free(idp);
+
+ /* Register pending command callback. After we've received the channel
+ information we will reprocess this command reply by re-calling this
+ USERS command reply callback. */
+ silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
+ server->cmd_ident,
+ silc_server_command_reply_users, cmd);
+ return;
+ }
+ }
+
+ /* Get the list count */
+ tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
+ if (!tmp)
+ goto out;
+ SILC_GET32_MSB(list_count, tmp);
+
+ /* Get Client ID list */
+ tmp = silc_argument_get_arg_type(cmd->args, 4, &tmp_len);
+ if (!tmp)
+ goto out;
+
+ client_id_list = silc_buffer_alloc(tmp_len);
+ silc_buffer_pull_tail(client_id_list, tmp_len);
+ silc_buffer_put(client_id_list, tmp, tmp_len);
+
+ /* Get client mode list */
+ tmp = silc_argument_get_arg_type(cmd->args, 5, &tmp_len);
+ if (!tmp)
+ goto out;
+
+ client_mode_list = silc_buffer_alloc(tmp_len);
+ silc_buffer_pull_tail(client_mode_list, tmp_len);
+ silc_buffer_put(client_mode_list, tmp, tmp_len);
+
+ /* Save the users to the channel */
+ silc_server_save_users_on_channel(server, cmd->sock, channel, NULL,
+ client_id_list, client_mode_list,
+ list_count);
+
+ channel->global_users = silc_server_channel_has_global(channel);
+ channel->users_resolved = TRUE;
+
+ silc_buffer_free(client_id_list);
+ silc_buffer_free(client_mode_list);
+
+ out:
+ SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
+ silc_free(channel_id);
+ err:
+ silc_server_command_reply_free(cmd);
+}
+
+SILC_SERVER_CMD_REPLY_FUNC(getkey)
+{
+ SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
+ SilcServer server = cmd->server;
+ SilcStatus status, error;
+ SilcClientEntry client = NULL;
+ SilcServerEntry server_entry = NULL;
+ SilcClientID *client_id = NULL;
+ SilcServerID *server_id = NULL;
+ unsigned char *tmp;
+ SilcUInt32 len;
+ SilcIDPayload idp = NULL;
+ SilcIdType id_type;
+ SilcPublicKey public_key = NULL;
+
+ COMMAND_CHECK_STATUS;
+
+ tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
+ if (!tmp)
+ goto out;
+ idp = silc_id_payload_parse(tmp, len);
+ if (!idp)
+ goto out;
+
+ /* Get the public key payload */
+ tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
+ if (!tmp)
+ goto out;
+
+ /* Decode the public key payload */
+ if (!silc_pkcs_public_key_payload_decode(tmp, len, &public_key))
+ goto out;
+
+ id_type = silc_id_payload_get_type(idp);
+ if (id_type == SILC_ID_CLIENT) {
+ client_id = silc_id_payload_get_id(idp);
+
+ client = silc_idlist_find_client_by_id(server->local_list, client_id,
+ TRUE, NULL);
+ if (!client) {
+ client = silc_idlist_find_client_by_id(server->global_list,
+ client_id, TRUE, NULL);
+ if (!client)
+ goto out;
+ }
+
+ client->data.public_key = public_key;
+ public_key = NULL;
+ } else if (id_type == SILC_ID_SERVER) {
+ server_id = silc_id_payload_get_id(idp);
+
+ server_entry = silc_idlist_find_server_by_id(server->local_list, server_id,
+ TRUE, NULL);
+ if (!server_entry) {
+ server_entry = silc_idlist_find_server_by_id(server->global_list,
+ server_id, TRUE, NULL);
+ if (!server_entry)
+ goto out;
+ }
+
+ server_entry->data.public_key = public_key;
+ public_key = NULL;
+ } else {