+ /* 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) {
+ if (entry->rekey) {
+ silc_schedule_task_del_by_context(server->schedule, entry->rekey);
+ SILC_LOG_ERROR(("global_list->channels: entry->rekey != NULL, inform Pekka now!!!"));
+ }
+ 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;
+ }
+ server->stat.my_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);
+ silc_free(entry->founder_passwd);
+ entry->founder_passwd = NULL;
+ }
+ }
+
+ if (entry->hmac_name && hmac) {
+ silc_free(entry->hmac_name);
+ entry->hmac_name = strdup(silc_hmac_get_name(hmac));
+ }
+
+ /* Get the ban list */
+ tmp = silc_argument_get_arg_type(cmd->args, 8, &len);
+ if (tmp) {
+ silc_free(entry->ban_list);
+ entry->ban_list = silc_memdup(tmp, len);
+ }
+
+ /* Get the invite list */
+ tmp = silc_argument_get_arg_type(cmd->args, 9, &len);
+ if (tmp) {
+ silc_free(entry->invite_list);
+ entry->invite_list = silc_memdup(tmp, len);
+ }
+
+ /* 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);
+
+ out:
+ SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_JOIN);
+ silc_free(client_id);
+ silc_server_command_reply_free(cmd);
+
+ if (client_id_list)
+ silc_buffer_free(client_id_list);
+ if (client_mode_list)
+ silc_buffer_free(client_mode_list);
+}
+
+SILC_SERVER_CMD_REPLY_FUNC(users)
+{
+ SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
+ SilcServer server = cmd->server;
+ SilcCommandStatus status;
+ 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);
+ 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, server->router->connection,
+ 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);
+
+ 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);
+ silc_server_command_reply_free(cmd);
+}
+
+SILC_SERVER_CMD_REPLY_FUNC(getkey)
+{
+ SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
+ SilcServer server = cmd->server;
+ SilcCommandStatus status;
+ SilcClientEntry client = NULL;
+ SilcServerEntry server_entry = NULL;
+ SilcClientID *client_id = NULL;
+ SilcServerID *server_id = NULL;
+ SilcSKEPKType type;
+ unsigned char *tmp, *pk;
+ SilcUInt32 len;
+ SilcUInt16 pk_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 */
+
+ SILC_GET16_MSB(pk_len, tmp);
+ SILC_GET16_MSB(type, tmp + 2);
+ pk = tmp + 4;
+
+ if (type != SILC_SKE_PK_TYPE_SILC)
+ goto out;
+
+ if (!silc_pkcs_public_key_decode(pk, pk_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;
+ } 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;
+ } else {