5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 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 "serverincludes.h"
22 #include "server_internal.h"
24 static int silc_server_is_registered(SilcServer server,
25 SilcSocketConnection sock,
26 SilcServerCommandContext cmd,
29 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
34 silc_server_command_send_status_data(SilcServerCommandContext cmd,
39 const unsigned char *arg,
42 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
43 SilcServerCommandReplyContext cmdr,
45 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
47 /* Server command list. */
48 SilcServerCommand silc_command_list[] =
50 SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
51 SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
52 SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
53 SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
54 SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
55 SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
56 SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
57 SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
58 SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
59 SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
60 SILC_SERVER_CMD(stats, STATS, SILC_CF_LAG | SILC_CF_REG),
61 SILC_SERVER_CMD(ping, PING, SILC_CF_LAG | SILC_CF_REG),
62 SILC_SERVER_CMD(oper, OPER, SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
63 SILC_SERVER_CMD(join, JOIN, SILC_CF_LAG_STRICT | SILC_CF_REG),
64 SILC_SERVER_CMD(motd, MOTD, SILC_CF_LAG | SILC_CF_REG),
65 SILC_SERVER_CMD(umode, UMODE, SILC_CF_LAG | SILC_CF_REG),
66 SILC_SERVER_CMD(cmode, CMODE, SILC_CF_LAG_STRICT | SILC_CF_REG),
67 SILC_SERVER_CMD(cumode, CUMODE, SILC_CF_LAG | SILC_CF_REG),
68 SILC_SERVER_CMD(kick, KICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
69 SILC_SERVER_CMD(ban, BAN, SILC_CF_LAG_STRICT | SILC_CF_REG),
70 SILC_SERVER_CMD(detach, DETACH, SILC_CF_LAG_STRICT | SILC_CF_REG),
71 SILC_SERVER_CMD(watch, WATCH, SILC_CF_LAG | SILC_CF_REG),
72 SILC_SERVER_CMD(silcoper, SILCOPER,
73 SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
74 SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
75 SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
76 SILC_SERVER_CMD(getkey, GETKEY, SILC_CF_LAG | SILC_CF_REG),
78 SILC_SERVER_CMD(connect, PRIV_CONNECT,
79 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
80 SILC_SERVER_CMD(close, PRIV_CLOSE,
81 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
82 SILC_SERVER_CMD(shutdown, PRIV_SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
88 /* Performs several checks to the command. It first checks whether this
89 command was called as pending command callback. If it was then it checks
90 whether error occurred in the command reply where the pending command
93 It also checks that the requested command includes correct amount
95 #define SILC_SERVER_COMMAND_CHECK(command, context, min, max) \
99 if (silc_server_command_pending_error_check(cmd, context2, command)) { \
100 SILC_LOG_DEBUG(("Error occurred in command reply, command not called")); \
101 silc_server_command_free(cmd); \
105 _argc = silc_argument_get_arg_num(cmd->args); \
107 SILC_LOG_DEBUG(("Not enough parameters in command")); \
108 silc_server_command_send_status_reply(cmd, command, \
109 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, \
111 silc_server_command_free(cmd); \
115 SILC_LOG_DEBUG(("Too many parameters in command")); \
116 silc_server_command_send_status_reply(cmd, command, \
117 SILC_STATUS_ERR_TOO_MANY_PARAMS, \
119 silc_server_command_free(cmd); \
124 /* Returns TRUE if the connection is registered. Unregistered connections
125 usually cannot send commands hence the check. */
127 static int silc_server_is_registered(SilcServer server,
128 SilcSocketConnection sock,
129 SilcServerCommandContext cmd,
132 SilcIDListData idata = (SilcIDListData)sock->user_data;
137 if (idata->status & SILC_IDLIST_STATUS_REGISTERED)
140 silc_server_command_send_status_reply(cmd, command,
141 SILC_STATUS_ERR_NOT_REGISTERED, 0);
145 /* Internal context to hold data when executed command with timeout. */
147 SilcServerCommandContext ctx;
148 SilcServerCommand *cmd;
149 } *SilcServerCommandTimeout;
151 /* Timeout callback to process commands with timeout for client. Client's
152 commands are always executed with timeout. */
154 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
156 SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
157 SilcClientEntry client = (SilcClientEntry)timeout->ctx->sock->user_data;
160 SILC_LOG_DEBUG(("Client entry is invalid"));
161 silc_server_command_free(timeout->ctx);
165 /* Update access time */
166 client->last_command = time(NULL);
168 if (!(timeout->cmd->flags & SILC_CF_REG)) {
169 SILC_LOG_DEBUG(("Calling %s command",
170 silc_get_command_name(timeout->cmd->cmd)));
171 timeout->cmd->cb(timeout->ctx, NULL);
172 } else if (silc_server_is_registered(timeout->ctx->server,
175 timeout->cmd->cmd)) {
176 SILC_LOG_DEBUG(("Calling %s command",
177 silc_get_command_name(timeout->cmd->cmd)));
178 timeout->cmd->cb(timeout->ctx, NULL);
180 SILC_LOG_DEBUG(("Client is not registered"));
181 silc_server_command_free(timeout->ctx);
187 /* Processes received command packet. */
189 void silc_server_command_process(SilcServer server,
190 SilcSocketConnection sock,
191 SilcPacketContext *packet)
193 SilcServerCommandContext ctx;
194 SilcServerCommand *cmd;
197 /* Allocate command context. This must be free'd by the
198 command routine receiving it. */
199 ctx = silc_server_command_alloc();
200 ctx->server = server;
201 ctx->sock = silc_socket_dup(sock);
202 ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
204 /* Parse the command payload in the packet */
205 ctx->payload = silc_command_payload_parse(packet->buffer->data,
206 packet->buffer->len);
208 SILC_LOG_ERROR(("Bad command payload, packet dropped"));
209 silc_packet_context_free(packet);
210 silc_socket_free(ctx->sock);
214 ctx->args = silc_command_get_args(ctx->payload);
216 /* Get the command */
217 command = silc_command_get(ctx->payload);
218 for (cmd = silc_command_list; cmd->cb; cmd++)
219 if (cmd->cmd == command)
222 if (!cmd || !cmd->cb) {
223 SILC_LOG_DEBUG(("Unknown command %d", command));
224 silc_server_command_send_status_reply(ctx, command,
225 SILC_STATUS_ERR_UNKNOWN_COMMAND, 0);
226 silc_server_command_free(ctx);
230 /* Execute client's commands always with timeout. Normally they are
231 executed with zero (0) timeout but if client is sending command more
232 frequently than once in 2 seconds, then the timeout may be 0 to 2
234 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
235 SilcClientEntry client = (SilcClientEntry)sock->user_data;
236 SilcServerCommandTimeout timeout;
240 SILC_LOG_DEBUG(("Client entry is invalid"));
241 silc_server_command_free(ctx);
244 timeout = silc_calloc(1, sizeof(*timeout));
248 if (client->last_command && (time(NULL) - client->last_command) < 2) {
249 client->fast_command++;
252 if (client->fast_command - 2 <= 0)
253 client->fast_command = 0;
255 client->fast_command -= 2;
259 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
260 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
261 silc_schedule_task_add(server->schedule, sock->sock,
262 silc_server_command_process_timeout, timeout,
263 (client->fast_command < 3 ? 0 :
264 2 - (time(NULL) - client->last_command)),
265 (client->fast_command < 3 ? 200000 : 0),
266 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
268 silc_schedule_task_add(server->schedule, sock->sock,
269 silc_server_command_process_timeout, timeout,
270 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
274 /* Execute for server */
276 if (!(cmd->flags & SILC_CF_REG)) {
277 SILC_LOG_DEBUG(("Calling %s command", silc_get_command_name(cmd->cmd)));
279 } else if (silc_server_is_registered(server, sock, ctx, cmd->cmd)) {
280 SILC_LOG_DEBUG(("Calling %s command", silc_get_command_name(cmd->cmd)));
283 SILC_LOG_DEBUG(("Server is not registered"));
284 silc_server_command_free(ctx);
288 /* Allocate Command Context */
290 SilcServerCommandContext silc_server_command_alloc()
292 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
297 /* Free's the command context allocated before executing the command */
299 void silc_server_command_free(SilcServerCommandContext ctx)
302 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
304 if (ctx->users < 1) {
306 silc_command_payload_free(ctx->payload);
308 silc_packet_context_free(ctx->packet);
310 silc_socket_free(ctx->sock); /* Decrease reference counter */
315 /* Duplicate Command Context by adding reference counter. The context won't
316 be free'd untill it hits zero. */
318 SilcServerCommandContext
319 silc_server_command_dup(SilcServerCommandContext ctx)
322 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
327 /* Timeout for pending command. If reply to pending command never arrives
328 this is called to free resources. */
330 SILC_TASK_CALLBACK(silc_server_command_pending_timeout)
332 SilcServer server = app_context;
333 SilcServerCommandPending *reply = context;
334 SilcServerCommandReplyContext cmdr;
338 SILC_LOG_DEBUG(("Timeout pending command"));
340 /* Allocate temporary and bogus command reply context */
341 cmdr = silc_calloc(1, sizeof(*cmdr));
342 cmdr->server = server;
343 cmdr->ident = reply->ident;
345 /* Check for pending commands and mark to be exeucted */
347 silc_server_command_pending_check(server, reply->reply_cmd,
348 reply->ident, &cmdr->callbacks_count);
350 /* Create bogus command reply with an error inside */
352 silc_command_reply_payload_encode_va(reply->reply_cmd ? reply->reply_cmd :
353 SILC_COMMAND_RESERVED,
354 SILC_STATUS_ERR_TIMEDOUT, 0,
356 cmdr->payload = silc_command_payload_parse(tmpreply->data, tmpreply->len);
357 silc_buffer_free(tmpreply);
359 /* Call all callbacks. Same as SILC_SERVER_PENDING_EXEC macro. */
360 for (i = 0; i < cmdr->callbacks_count; i++)
361 if (cmdr->callbacks[i].callback)
362 (*cmdr->callbacks[i].callback)(cmdr->callbacks[i].context, cmdr);
364 silc_server_command_pending_del(server, reply->reply_cmd, reply->ident);
365 silc_server_command_reply_free(cmdr);
368 /* Add new pending command to be executed when reply to a command has been
369 received. The `reply_cmd' is the command that will call the `callback'
370 with `context' when reply has been received. It can be SILC_COMMAND_NONE
371 to match any command with the `ident'. If `ident' is non-zero
372 the `callback' will be executed when received reply with command
373 identifier `ident'. If there already exists pending command for the
374 specified command, ident, callback and context this function has no
377 bool silc_server_command_pending(SilcServer server,
378 SilcCommand reply_cmd,
380 SilcCommandCb callback,
383 return silc_server_command_pending_timed(server, reply_cmd, ident, callback,
387 /* Same as silc_server_command_pending with specific timeout for pending
388 commands. If the `timeout' is zero default timeout is used. */
390 bool silc_server_command_pending_timed(SilcServer server,
391 SilcCommand reply_cmd,
393 SilcCommandCb callback,
397 SilcServerCommandPending *reply;
399 /* Check whether identical pending already exists for same command,
400 ident, callback and callback context. If it does then it would be
401 error to register it again. */
402 silc_dlist_start(server->pending_commands);
403 while ((reply = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
404 if (reply->reply_cmd == reply_cmd && reply->ident == ident &&
405 reply->callback == callback && reply->context == context)
409 reply = silc_calloc(1, sizeof(*reply));
410 reply->reply_cmd = reply_cmd;
411 reply->ident = ident;
412 reply->context = context;
413 reply->callback = callback;
415 silc_schedule_task_add(server->schedule, 0,
416 silc_server_command_pending_timeout, reply,
417 timeout ? timeout : 10, 0,
418 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
419 silc_dlist_add(server->pending_commands, reply);
424 /* Deletes pending command by reply command type. */
426 void silc_server_command_pending_del(SilcServer server,
427 SilcCommand reply_cmd,
430 SilcServerCommandPending *r;
432 silc_dlist_start(server->pending_commands);
433 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
434 if ((r->reply_cmd == reply_cmd || (r->reply_cmd == SILC_COMMAND_NONE &&
436 && r->ident == ident) {
437 silc_dlist_del(server->pending_commands, r);
439 silc_schedule_task_del(server->schedule, r->timeout);
445 /* Checks for pending commands and marks callbacks to be called from
446 the command reply function. Returns TRUE if there were pending command. */
448 SilcServerCommandPendingCallbacks
449 silc_server_command_pending_check(SilcServer server,
452 SilcUInt32 *callbacks_count)
454 SilcServerCommandPending *r;
455 SilcServerCommandPendingCallbacks callbacks = NULL;
458 silc_dlist_start(server->pending_commands);
459 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
460 if ((r->reply_cmd == command || r->reply_cmd == SILC_COMMAND_NONE)
461 && r->ident == ident) {
462 callbacks = silc_realloc(callbacks, sizeof(*callbacks) * (i + 1));
463 callbacks[i].context = r->context;
464 callbacks[i].callback = r->callback;
465 r->reply_check = TRUE;
470 *callbacks_count = i;
474 /* Sends simple status message as command reply packet */
477 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
484 SILC_LOG_DEBUG(("Sending command status %d", status));
487 silc_command_reply_payload_encode_va(command, status, error,
488 silc_command_get_ident(cmd->payload),
490 silc_server_packet_send(cmd->server, cmd->sock,
491 SILC_PACKET_COMMAND_REPLY, 0,
492 buffer->data, buffer->len, FALSE);
493 silc_buffer_free(buffer);
496 /* Sends command status reply with one extra argument. The argument
497 type must be sent as argument. */
500 silc_server_command_send_status_data(SilcServerCommandContext cmd,
505 const unsigned char *arg,
510 SILC_LOG_DEBUG(("Sending command status %d", status));
513 silc_command_reply_payload_encode_va(command, status, 0,
514 silc_command_get_ident(cmd->payload),
515 1, arg_type, arg, arg_len);
516 silc_server_packet_send(cmd->server, cmd->sock,
517 SILC_PACKET_COMMAND_REPLY, 0,
518 buffer->data, buffer->len, FALSE);
519 silc_buffer_free(buffer);
522 /* This function can be called to check whether in the command reply
523 an error occurred. This function has no effect if this is called
524 when the command function was not called as pending command callback.
525 This returns TRUE if error had occurred. */
528 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
529 SilcServerCommandReplyContext cmdr,
532 if (!cmd->pending || !cmdr)
535 if (!silc_command_get_status(cmdr->payload, NULL, NULL)) {
538 /* Send the same command reply payload */
539 silc_command_set_command(cmdr->payload, silc_command_get(cmd->payload));
540 silc_command_set_ident(cmdr->payload,
541 silc_command_get_ident(cmd->payload));
542 buffer = silc_command_payload_encode_payload(cmdr->payload);
543 silc_server_packet_send(cmd->server, cmd->sock,
544 SILC_PACKET_COMMAND_REPLY, 0,
545 buffer->data, buffer->len, FALSE);
546 silc_buffer_free(buffer);
553 /* Server side of command WHOIS. */
555 SILC_SERVER_CMD_FUNC(whois)
557 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
558 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOIS, cmd, 1, 256);
559 silc_server_query_command(cmd->server, SILC_COMMAND_WHOIS, cmd);
560 silc_server_command_free(cmd);
563 /* Server side of command WHOWAS. */
565 SILC_SERVER_CMD_FUNC(whowas)
567 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
568 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOWAS, cmd, 1, 2);
569 silc_server_query_command(cmd->server, SILC_COMMAND_WHOWAS, cmd);
570 silc_server_command_free(cmd);
573 /* Server side of command IDENTIFY. */
575 SILC_SERVER_CMD_FUNC(identify)
577 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
578 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_IDENTIFY, cmd, 1, 256);
579 silc_server_query_command(cmd->server, SILC_COMMAND_IDENTIFY, cmd);
580 silc_server_command_free(cmd);
583 /* Server side of command NICK. Sets nickname for user. Setting
584 nickname causes generation of a new client ID for the client. The
585 new client ID is sent to the client after changing the nickname. */
587 SILC_SERVER_CMD_FUNC(nick)
589 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
590 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
591 SilcServer server = cmd->server;
592 SilcBuffer packet, nidp, oidp = NULL;
593 SilcClientID *new_id;
596 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
599 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
602 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_NICK, cmd, 1, 1);
605 nick = silc_argument_get_arg_type(cmd->args, 1, &nick_len);
607 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
608 SILC_STATUS_ERR_BAD_NICKNAME, 0);
613 if (silc_server_name_bad_chars(nick, nick_len) == TRUE) {
614 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
615 SILC_STATUS_ERR_BAD_NICKNAME, 0);
619 /* Check for same nickname */
620 if (!strcmp(client->nickname, nick)) {
621 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
625 /* Create new Client ID */
626 while (!silc_id_create_client_id(cmd->server, cmd->server->id,
628 cmd->server->md5hash, nick,
632 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
633 SILC_STATUS_ERR_BAD_NICKNAME, 0);
636 snprintf(&nick[strlen(nick) - 1], 1, "%d", nickfail);
639 /* Send notify about nickname change to our router. We send the new
640 ID and ask to replace it with the old one. If we are router the
641 packet is broadcasted. Send NICK_CHANGE notify. */
642 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
643 SILC_BROADCAST(server), client->id,
646 /* Check if anyone is watching the old nickname */
647 if (server->server_type == SILC_ROUTER)
648 silc_server_check_watcher_list(server, client, nick,
649 SILC_NOTIFY_TYPE_NICK_CHANGE);
651 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
653 /* Remove old cache entry */
654 silc_idcache_del_by_context(server->local_list->clients, client);
656 silc_free(client->id);
659 silc_free(client->nickname);
660 client->nickname = strdup(nick);
662 /* Update client cache */
663 silc_idcache_add(server->local_list->clients, client->nickname,
664 client->id, (void *)client, 0, NULL);
666 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
668 /* Send NICK_CHANGE notify to the client's channels */
669 silc_server_send_notify_on_channels(server, NULL, client,
670 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
671 oidp->data, oidp->len,
672 nidp->data, nidp->len,
674 strlen(client->nickname));
676 /* Check if anyone is watching the new nickname */
677 if (server->server_type == SILC_ROUTER)
678 silc_server_check_watcher_list(server, client, NULL,
679 SILC_NOTIFY_TYPE_NICK_CHANGE);
682 /* Send the new Client ID as reply command back to client */
683 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
684 SILC_STATUS_OK, 0, ident, 2,
685 2, nidp->data, nidp->len,
686 3, nick, strlen(nick));
687 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
688 0, packet->data, packet->len, FALSE);
690 silc_buffer_free(packet);
691 silc_buffer_free(nidp);
693 silc_buffer_free(oidp);
696 silc_server_command_free(cmd);
699 /* Sends the LIST command reply */
702 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
703 SilcChannelEntry *lch,
704 SilcUInt32 lch_count,
705 SilcChannelEntry *gch,
706 SilcUInt32 gch_count)
709 SilcBuffer packet, idp;
710 SilcChannelEntry entry;
712 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
714 unsigned char usercount[4];
716 int valid_lcount = 0, valid_rcount = 0;
718 for (i = 0; i < lch_count; i++) {
719 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
724 for (i = 0; i < gch_count; i++) {
725 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
731 status = SILC_STATUS_OK;
732 if ((lch_count + gch_count) > 1)
733 status = SILC_STATUS_LIST_START;
736 for (i = 0, k = 0; i < lch_count; i++) {
742 status = SILC_STATUS_LIST_ITEM;
743 if (valid_lcount > 1 && k == valid_lcount - 1 && !valid_rcount)
744 status = SILC_STATUS_LIST_END;
746 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
748 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
750 memset(usercount, 0, sizeof(usercount));
752 topic = entry->topic;
753 users = silc_hash_table_count(entry->user_list);
754 SILC_PUT32_MSB(users, usercount);
759 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
761 2, idp->data, idp->len,
762 3, entry->channel_name,
763 strlen(entry->channel_name),
764 4, topic, topic ? strlen(topic) : 0,
766 silc_server_packet_send(cmd->server, cmd->sock,
767 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
769 silc_buffer_free(packet);
770 silc_buffer_free(idp);
775 for (i = 0, k = 0; i < gch_count; i++) {
781 status = SILC_STATUS_LIST_ITEM;
782 if (valid_rcount > 1 && k == valid_rcount - 1)
783 status = SILC_STATUS_LIST_END;
785 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
787 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
789 memset(usercount, 0, sizeof(usercount));
791 topic = entry->topic;
792 users = entry->user_count;
793 SILC_PUT32_MSB(users, usercount);
798 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
800 2, idp->data, idp->len,
801 3, entry->channel_name,
802 strlen(entry->channel_name),
803 4, topic, topic ? strlen(topic) : 0,
805 silc_server_packet_send(cmd->server, cmd->sock,
806 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
808 silc_buffer_free(packet);
809 silc_buffer_free(idp);
814 /* Server side of LIST command. This lists the channel of the requested
815 server. Secret channels are not listed. */
817 SILC_SERVER_CMD_FUNC(list)
819 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
820 SilcServer server = cmd->server;
821 SilcChannelID *channel_id = NULL;
824 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
825 SilcUInt32 lch_count = 0, gch_count = 0;
827 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LIST, cmd, 0, 1);
829 /* If we are normal server, send the command to router, since we
830 want to know all channels in the network. */
831 if (!cmd->pending && server->server_type != SILC_ROUTER &&
832 !server->standalone) {
834 SilcUInt16 old_ident;
836 old_ident = silc_command_get_ident(cmd->payload);
837 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
838 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
839 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
840 SILC_PACKET_COMMAND, cmd->packet->flags,
841 tmpbuf->data, tmpbuf->len, TRUE);
843 /* Reprocess this packet after received reply from router */
844 silc_server_command_pending(server, SILC_COMMAND_LIST,
845 silc_command_get_ident(cmd->payload),
846 silc_server_command_list,
847 silc_server_command_dup(cmd));
849 silc_command_set_ident(cmd->payload, old_ident);
850 silc_buffer_free(tmpbuf);
855 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
857 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
859 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
860 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
865 /* Get the channels from local list */
866 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
869 /* Get the channels from global list */
870 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
874 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
875 gchannels, gch_count);
877 silc_free(lchannels);
878 silc_free(gchannels);
881 silc_server_command_free(cmd);
884 /* Server side of TOPIC command. Sets topic for channel and/or returns
885 current topic to client. */
887 SILC_SERVER_CMD_FUNC(topic)
889 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
890 SilcServer server = cmd->server;
891 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
892 SilcChannelID *channel_id;
893 SilcChannelEntry channel;
894 SilcChannelClientEntry chl;
895 SilcBuffer packet, idp;
897 SilcUInt32 argc, tmp_len;
898 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
900 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
903 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_TOPIC, cmd, 1, 2);
905 argc = silc_argument_get_arg_num(cmd->args);
908 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
910 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
911 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
914 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
916 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
917 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
921 /* Check whether the channel exists */
922 channel = silc_idlist_find_channel_by_id(server->local_list,
925 channel = silc_idlist_find_channel_by_id(server->global_list,
928 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
929 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
937 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
939 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
940 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
945 if (strlen(tmp) > 256) {
946 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
947 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
952 /* See whether the client is on channel and has rights to change topic */
953 if (!silc_server_client_on_channel(client, channel, &chl)) {
954 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
955 SILC_STATUS_ERR_NOT_ON_CHANNEL,
960 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
961 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
962 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
963 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
964 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
969 if (!channel->topic || strcmp(channel->topic, tmp)) {
970 /* Set the topic for channel */
971 silc_free(channel->topic);
972 channel->topic = strdup(tmp);
974 /* Send TOPIC_SET notify type to the network */
975 silc_server_send_notify_topic_set(server, SILC_PRIMARY_ROUTE(server),
976 SILC_BROADCAST(server), channel,
977 client->id, SILC_ID_CLIENT,
980 /* Send notify about topic change to all clients on the channel */
981 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
982 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
983 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
986 strlen(channel->topic));
987 silc_buffer_free(idp);
991 /* Send the topic to client as reply packet */
992 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
993 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
994 SILC_STATUS_OK, 0, ident, 2,
995 2, idp->data, idp->len,
998 strlen(channel->topic) : 0);
999 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1000 0, packet->data, packet->len, FALSE);
1002 silc_buffer_free(packet);
1003 silc_buffer_free(idp);
1004 silc_free(channel_id);
1007 silc_server_command_free(cmd);
1010 /* Server side of INVITE command. Invites some client to join some channel.
1011 This command is also used to manage the invite list of the channel. */
1013 SILC_SERVER_CMD_FUNC(invite)
1015 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1016 SilcServer server = cmd->server;
1017 SilcSocketConnection sock = cmd->sock, dest_sock;
1018 SilcChannelClientEntry chl;
1019 SilcClientEntry sender, dest;
1020 SilcClientID *dest_id = NULL;
1021 SilcChannelEntry channel;
1022 SilcChannelID *channel_id = NULL;
1023 SilcIDListData idata;
1024 SilcArgumentPayload args;
1025 SilcHashTableList htl;
1026 SilcBuffer packet, list, tmp2;
1028 SilcUInt32 len, type;
1029 SilcUInt16 argc = 0, ident = silc_command_get_ident(cmd->payload);
1031 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INVITE, cmd, 1, 4);
1033 /* Get Channel ID */
1034 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1036 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1037 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
1040 channel_id = silc_id_payload_parse_id(tmp, len, NULL);
1042 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1043 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
1047 /* Get the channel entry */
1048 channel = silc_idlist_find_channel_by_id(server->local_list,
1051 channel = silc_idlist_find_channel_by_id(server->global_list,
1054 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1055 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
1061 /* Check whether the sender of this command is on the channel. */
1062 sender = (SilcClientEntry)sock->user_data;
1063 if (!sender || !silc_server_client_on_channel(sender, channel, &chl)) {
1064 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1065 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
1069 /* Check whether the channel is invite-only channel. If yes then the
1070 sender of this command must be at least channel operator. */
1071 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
1072 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1073 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1074 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1075 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
1080 /* Get destination client ID */
1081 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
1085 dest_id = silc_id_payload_parse_id(tmp, len, NULL);
1087 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1088 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
1092 /* Get the client entry */
1093 dest = silc_server_query_client(server, dest_id, FALSE, &resolve);
1095 if (server->server_type != SILC_SERVER || !resolve || cmd->pending) {
1096 silc_server_command_send_status_reply(
1097 cmd, SILC_COMMAND_INVITE,
1098 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID, 0);
1102 /* The client info is being resolved. Reprocess this packet after
1103 receiving the reply to the query. */
1104 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1106 silc_server_command_invite,
1107 silc_server_command_dup(cmd));
1108 cmd->pending = TRUE;
1109 silc_free(channel_id);
1114 /* Check whether the requested client is already on the channel. */
1115 if (silc_server_client_on_channel(dest, channel, NULL)) {
1116 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1117 SILC_STATUS_ERR_USER_ON_CHANNEL,
1122 /* Get route to the client */
1123 dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id,
1126 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1127 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1132 /* Add the client to the invite list */
1134 /* Allocate hash table for invite list if it doesn't exist yet */
1135 if (!channel->invite_list)
1136 channel->invite_list = silc_hash_table_alloc(0, silc_hash_ptr,
1140 /* Check if the ID is in the list already */
1141 silc_hash_table_list(channel->invite_list, &htl);
1142 while (silc_hash_table_get(&htl, (void **)&type, (void **)&tmp2)) {
1143 if (type == 3 && !memcmp(tmp2->data, tmp, len)) {
1148 silc_hash_table_list_reset(&htl);
1150 /* Add new Client ID to invite list */
1152 list = silc_buffer_alloc_size(len);
1153 silc_buffer_put(list, tmp, len);
1154 silc_hash_table_add(channel->invite_list, (void *)3, list);
1157 if (!(dest->mode & SILC_UMODE_BLOCK_INVITE)) {
1158 /* Send notify to the client that is invited to the channel */
1159 SilcBuffer idp, idp2;
1160 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
1161 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
1162 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
1164 SILC_NOTIFY_TYPE_INVITE, 3,
1165 idp->data, idp->len,
1166 channel->channel_name,
1167 strlen(channel->channel_name),
1168 idp2->data, idp2->len);
1169 silc_buffer_free(idp);
1170 silc_buffer_free(idp2);
1174 /* Get the invite information */
1175 tmp = silc_argument_get_arg_type(cmd->args, 4, &len);
1176 if (tmp && len > 2) {
1177 /* Parse the arguments to see they are constructed correctly */
1178 SILC_GET16_MSB(argc, tmp);
1179 args = silc_argument_payload_parse(tmp + 2, len - 2, argc);
1181 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1182 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1187 /* Get the type of action */
1188 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
1189 if (tmp && len == 1) {
1190 if (tmp[0] == 0x00) {
1191 /* Allocate hash table for invite list if it doesn't exist yet */
1192 if (!channel->invite_list)
1193 channel->invite_list = silc_hash_table_alloc(0, silc_hash_ptr,
1197 /* Check for resource limit */
1198 if (silc_hash_table_count(channel->invite_list) > 64) {
1199 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1200 SILC_STATUS_ERR_RESOURCE_LIMIT,
1206 /* Now add or delete the information. */
1207 silc_server_inviteban_process(server, channel->invite_list,
1208 (SilcUInt8)tmp[0], args);
1210 silc_argument_payload_free(args);
1213 /* Encode invite list */
1215 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
1216 list = silc_buffer_alloc_size(2);
1217 silc_buffer_format(list,
1218 SILC_STR_UI_SHORT(silc_hash_table_count(
1219 channel->invite_list)),
1221 silc_hash_table_list(channel->invite_list, &htl);
1222 while (silc_hash_table_get(&htl, (void **)&type, (void **)&tmp2)) {
1224 list = silc_argument_payload_encode_one(list, (char *)tmp2,
1225 strlen((char *)tmp2), type);
1227 list = silc_argument_payload_encode_one(list, tmp2->data, tmp2->len,
1230 silc_hash_table_list_reset(&htl);
1233 /* Send notify to the primary router */
1234 silc_server_send_notify_invite(
1235 server, SILC_PRIMARY_ROUTE(server),
1236 SILC_BROADCAST(server), channel, sender->id,
1237 silc_argument_get_arg_type(cmd->args, 3, NULL),
1240 /* Send command reply */
1241 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1242 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
1243 SILC_STATUS_OK, 0, ident, 2,
1245 3, list ? list->data : NULL,
1246 list ? list->len : 0);
1247 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
1248 packet->data, packet->len, FALSE);
1249 silc_buffer_free(packet);
1250 silc_buffer_free(list);
1254 silc_free(channel_id);
1255 silc_server_command_free(cmd);
1259 SilcSocketConnection sock;
1263 /* Quits connection to client. This gets called if client won't
1264 close the connection even when it has issued QUIT command. */
1266 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
1268 SilcServer server = app_context;
1269 QuitInternal q = (QuitInternal)context;
1271 /* Free all client specific data, such as client entry and entires
1272 on channels this client may be on. */
1273 silc_server_free_client_data(server, q->sock, q->sock->user_data,
1275 q->sock->user_data = NULL;
1277 /* Close the connection on our side */
1278 silc_server_close_connection(server, q->sock);
1280 silc_socket_free(q->sock);
1281 silc_free(q->signoff);
1285 /* Quits SILC session. This is the normal way to disconnect client. */
1287 SILC_SERVER_CMD_FUNC(quit)
1289 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1290 SilcServer server = cmd->server;
1291 SilcSocketConnection sock = cmd->sock;
1293 unsigned char *tmp = NULL;
1296 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_QUIT, cmd, 0, 1);
1298 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1302 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1306 q = silc_calloc(1, sizeof(*q));
1307 q->sock = silc_socket_dup(sock);
1308 q->signoff = tmp ? strdup(tmp) : NULL;
1310 /* We quit the connection with little timeout */
1311 silc_schedule_task_add(server->schedule, sock->sock,
1312 silc_server_command_quit_cb, (void *)q,
1313 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
1316 silc_server_command_free(cmd);
1319 /* Server side of command KILL. This command is used by router operator
1320 to remove an client from the SILC Network temporarily. */
1322 SILC_SERVER_CMD_FUNC(kill)
1324 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1325 SilcServer server = cmd->server;
1326 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1327 SilcClientEntry remote_client;
1328 SilcClientID *client_id = NULL;
1329 unsigned char *tmp, *comment, *auth;
1330 SilcUInt32 tmp_len, tmp_len2, auth_len;
1332 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_KILL, cmd, 1, 3);
1334 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
1337 /* Get authentication payload if present */
1338 auth = silc_argument_get_arg_type(cmd->args, 3, &auth_len);
1341 /* Router operator killing */
1343 /* KILL command works only on router */
1344 if (server->server_type != SILC_ROUTER) {
1345 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1346 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
1350 /* Check whether client has the permissions. */
1351 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1352 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1353 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
1358 /* Get the client ID */
1359 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1361 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1362 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1366 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1368 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1369 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1374 /* Get the client entry */
1375 remote_client = silc_idlist_find_client_by_id(server->local_list,
1376 client_id, TRUE, NULL);
1377 if (!remote_client) {
1378 remote_client = silc_idlist_find_client_by_id(server->global_list,
1379 client_id, TRUE, NULL);
1380 if (!remote_client) {
1381 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1382 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1389 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
1390 if (comment && tmp_len2 > 128) {
1392 comment[127] = '\0';
1395 /* If authentication data is provided then verify that killing is
1397 if (auth && auth_len) {
1398 SilcSocketConnection sock;
1400 if (!SILC_IS_LOCAL(remote_client) || !remote_client->data.public_key) {
1401 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1402 SILC_STATUS_ERR_OPERATION_ALLOWED,
1407 /* Verify the signature */
1408 if (!silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
1409 remote_client->data.public_key, 0,
1410 server->sha1hash, remote_client->id,
1412 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1413 SILC_STATUS_ERR_AUTH_FAILED,
1418 /* Send reply to the sender */
1419 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1422 /* Do normal signoff for the destination client */
1423 sock = remote_client->connection;
1424 silc_server_free_client_data(server, NULL, remote_client, TRUE,
1426 (unsigned char *)"Killed");
1428 silc_server_close_connection(server, sock);
1430 /* Router operator killing */
1432 /* Send reply to the sender */
1433 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1436 /* Check if anyone is watching this nickname */
1437 if (server->server_type == SILC_ROUTER)
1438 silc_server_check_watcher_list(server, client, NULL,
1439 SILC_NOTIFY_TYPE_KILLED);
1441 /* Now do the killing */
1442 silc_server_kill_client(server, remote_client, comment, client->id,
1447 silc_free(client_id);
1448 silc_server_command_free(cmd);
1451 /* Server side of command INFO. This sends information about us to
1452 the client. If client requested specific server we will send the
1453 command to that server. */
1455 SILC_SERVER_CMD_FUNC(info)
1457 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1458 SilcServer server = cmd->server;
1459 SilcBuffer packet, idp;
1462 char *dest_server, *server_info = NULL, *server_name;
1463 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1464 SilcServerEntry entry = NULL;
1465 SilcServerID *server_id = NULL;
1467 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 0, 2);
1469 /* Get server name */
1470 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
1473 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
1475 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1477 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1478 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1484 /* Check whether we have this server cached */
1485 entry = silc_idlist_find_server_by_id(server->local_list,
1486 server_id, TRUE, NULL);
1488 entry = silc_idlist_find_server_by_id(server->global_list,
1489 server_id, TRUE, NULL);
1490 if (!entry && server->server_type != SILC_SERVER) {
1491 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1492 SILC_STATUS_ERR_NO_SUCH_SERVER,
1499 /* Some buggy servers has sent request to router about themselves. */
1500 if (server->server_type != SILC_SERVER && cmd->sock->user_data == entry)
1503 if ((!dest_server && !server_id && !entry) || (entry &&
1504 entry == server->id_entry) ||
1505 (dest_server && !cmd->pending &&
1506 !strncasecmp(dest_server, server->server_name, strlen(dest_server)))) {
1507 /* Send our reply */
1508 char info_string[256];
1510 memset(info_string, 0, sizeof(info_string));
1511 snprintf(info_string, sizeof(info_string),
1512 "location: %s server: %s admin: %s <%s>",
1513 server->config->server_info->location,
1514 server->config->server_info->server_type,
1515 server->config->server_info->admin,
1516 server->config->server_info->email);
1518 server_info = info_string;
1519 entry = server->id_entry;
1521 /* Check whether we have this server cached */
1522 if (!entry && dest_server) {
1523 entry = silc_idlist_find_server_by_name(server->global_list,
1524 dest_server, TRUE, NULL);
1526 entry = silc_idlist_find_server_by_name(server->local_list,
1527 dest_server, TRUE, NULL);
1531 if (!cmd->pending &&
1532 server->server_type != SILC_SERVER && entry && !entry->server_info) {
1533 /* Send to the server */
1535 SilcUInt16 old_ident;
1537 old_ident = silc_command_get_ident(cmd->payload);
1538 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1539 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1541 silc_server_packet_send(server, entry->connection,
1542 SILC_PACKET_COMMAND, cmd->packet->flags,
1543 tmpbuf->data, tmpbuf->len, TRUE);
1545 /* Reprocess this packet after received reply from router */
1546 silc_server_command_pending(server, SILC_COMMAND_INFO,
1547 silc_command_get_ident(cmd->payload),
1548 silc_server_command_info,
1549 silc_server_command_dup(cmd));
1550 cmd->pending = TRUE;
1551 silc_command_set_ident(cmd->payload, old_ident);
1552 silc_buffer_free(tmpbuf);
1556 if (!entry && !cmd->pending && !server->standalone) {
1557 /* Send to the primary router */
1559 SilcUInt16 old_ident;
1561 old_ident = silc_command_get_ident(cmd->payload);
1562 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1563 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1565 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
1566 SILC_PACKET_COMMAND, cmd->packet->flags,
1567 tmpbuf->data, tmpbuf->len, TRUE);
1569 /* Reprocess this packet after received reply from router */
1570 silc_server_command_pending(server, SILC_COMMAND_INFO,
1571 silc_command_get_ident(cmd->payload),
1572 silc_server_command_info,
1573 silc_server_command_dup(cmd));
1574 cmd->pending = TRUE;
1575 silc_command_set_ident(cmd->payload, old_ident);
1576 silc_buffer_free(tmpbuf);
1581 silc_free(server_id);
1584 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1585 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
1589 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1591 server_info = entry->server_info;
1592 server_name = entry->server_name;
1594 /* Send the reply */
1595 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
1596 SILC_STATUS_OK, 0, ident, 3,
1597 2, idp->data, idp->len,
1599 strlen(server_name),
1602 strlen(server_info) : 0);
1603 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
1604 packet->data, packet->len, FALSE);
1606 silc_buffer_free(packet);
1607 silc_buffer_free(idp);
1610 silc_server_command_free(cmd);
1613 /* Server side of command PING. This just replies to the ping. */
1615 SILC_SERVER_CMD_FUNC(ping)
1617 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1618 SilcServer server = cmd->server;
1621 SilcServerID *server_id = NULL;
1623 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PING, cmd, 1, 1);
1626 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1628 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1629 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1632 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1636 if (SILC_ID_SERVER_COMPARE(server_id, server->id)) {
1637 /* Send our reply */
1638 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1641 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1642 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
1647 silc_free(server_id);
1648 silc_server_command_free(cmd);
1651 /* Server side of command STATS. */
1653 SILC_SERVER_CMD_FUNC(stats)
1655 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1656 SilcServer server = cmd->server;
1657 SilcServerID *server_id;
1660 SilcBuffer packet, stats;
1661 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1664 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_STATS, cmd, 1, 1);
1667 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1669 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1670 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1673 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1677 /* The ID must be ours */
1678 if (!SILC_ID_SERVER_COMPARE(server->id, server_id)) {
1679 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1680 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
1681 silc_free(server_id);
1684 silc_free(server_id);
1686 /* If we are router then just send everything we got. If we are normal
1687 server then we'll send this to our router to get all the latest
1688 statistical information. */
1689 if (!cmd->pending && server->server_type != SILC_ROUTER &&
1690 !server->standalone) {
1691 /* Send request to our router */
1692 SilcBuffer idp = silc_id_payload_encode(server->router->id,
1694 packet = silc_command_payload_encode_va(SILC_COMMAND_STATS,
1695 ++server->cmd_ident, 1,
1696 1, idp->data, idp->len);
1697 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
1698 SILC_PACKET_COMMAND, 0, packet->data,
1699 packet->len, FALSE);
1701 /* Reprocess this packet after received reply from router */
1702 silc_server_command_pending(server, SILC_COMMAND_STATS,
1704 silc_server_command_stats,
1705 silc_server_command_dup(cmd));
1706 cmd->pending = TRUE;
1707 silc_buffer_free(packet);
1708 silc_buffer_free(idp);
1712 /* Send our reply to sender */
1713 uptime = time(NULL) - server->starttime;
1715 stats = silc_buffer_alloc_size(60);
1716 silc_buffer_format(stats,
1717 SILC_STR_UI_INT(server->starttime),
1718 SILC_STR_UI_INT(uptime),
1719 SILC_STR_UI_INT(server->stat.my_clients),
1720 SILC_STR_UI_INT(server->stat.my_channels),
1721 SILC_STR_UI_INT(server->stat.my_server_ops),
1722 SILC_STR_UI_INT(server->stat.my_router_ops),
1723 SILC_STR_UI_INT(server->stat.cell_clients),
1724 SILC_STR_UI_INT(server->stat.cell_channels),
1725 SILC_STR_UI_INT(server->stat.cell_servers),
1726 SILC_STR_UI_INT(server->stat.clients),
1727 SILC_STR_UI_INT(server->stat.channels),
1728 SILC_STR_UI_INT(server->stat.servers),
1729 SILC_STR_UI_INT(server->stat.routers),
1730 SILC_STR_UI_INT(server->stat.server_ops),
1731 SILC_STR_UI_INT(server->stat.router_ops),
1734 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_STATS,
1735 SILC_STATUS_OK, 0, ident, 2,
1737 3, stats->data, stats->len);
1738 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1739 0, packet->data, packet->len, FALSE);
1740 silc_buffer_free(packet);
1741 silc_buffer_free(stats);
1744 silc_server_command_free(cmd);
1747 /* Internal routine to join channel. The channel sent to this function
1748 has been either created or resolved from ID lists. This joins the sent
1749 client to the channel. */
1751 static void silc_server_command_join_channel(SilcServer server,
1752 SilcServerCommandContext cmd,
1753 SilcChannelEntry channel,
1754 SilcClientID *client_id,
1758 const unsigned char *auth,
1759 SilcUInt32 auth_len)
1761 SilcSocketConnection sock = cmd->sock;
1763 SilcUInt32 tmp_len, user_count;
1764 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
1765 SilcClientEntry client;
1766 SilcChannelClientEntry chl;
1767 SilcBuffer reply, chidp, clidp, keyp = NULL;
1768 SilcBuffer user_list, mode_list, invite_list, ban_list;
1769 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1770 char check[512], check2[512];
1771 bool founder = FALSE;
1773 SilcBuffer fkey = NULL;
1776 SILC_LOG_DEBUG(("Joining client to channel"));
1781 /* Get the client entry */
1782 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
1783 client = (SilcClientEntry)sock->user_data;
1787 client = silc_server_query_client(server, client_id, FALSE,
1790 if (!resolve || cmd->pending) {
1791 silc_server_command_send_status_reply(
1792 cmd, SILC_COMMAND_JOIN,
1793 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
1797 /* The client info is being resolved. Reprocess this packet after
1798 receiving the reply to the query. */
1799 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1801 silc_server_command_join,
1802 silc_server_command_dup(cmd));
1803 cmd->pending = TRUE;
1807 if (auth && auth_len && !client->data.public_key) {
1808 if (cmd->pending == 2)
1811 /* We must retrieve the detached client's public key by sending
1812 GETKEY command. Reprocess this packet after receiving the key */
1813 clidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1814 silc_server_send_command(server, cmd->sock,
1815 SILC_COMMAND_GETKEY, ++server->cmd_ident,
1816 1, 1, clidp->data, clidp->len);
1817 silc_buffer_free(clidp);
1818 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
1820 silc_server_command_join,
1821 silc_server_command_dup(cmd));
1826 cmd->pending = FALSE;
1830 * Check founder auth payload if provided. If client can gain founder
1831 * privileges it can override various conditions on joining the channel,
1832 * and can have directly the founder mode set on the channel.
1834 if (auth && auth_len && channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
1835 SilcIDListData idata = (SilcIDListData)client;
1836 SilcChannelClientEntry chl2;
1837 SilcHashTableList htl;
1839 if (channel->founder_key && idata->public_key &&
1840 silc_pkcs_public_key_compare(channel->founder_key,
1841 idata->public_key)) {
1842 /* Check whether the client is to become founder */
1843 if (silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
1844 channel->founder_key, 0, server->sha1hash,
1845 client->id, SILC_ID_CLIENT)) {
1847 /* There cannot be anyone else as founder on the channel now. This
1848 client is definitely the founder due to this authentication */
1849 silc_hash_table_list(channel->user_list, &htl);
1850 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
1851 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
1852 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
1853 silc_server_force_cumode_change(server, NULL, channel, chl2,
1857 silc_hash_table_list_reset(&htl);
1859 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
1866 * Check channel modes
1870 memset(check, 0, sizeof(check));
1871 memset(check2, 0, sizeof(check2));
1872 silc_strncat(check, sizeof(check),
1873 client->nickname, strlen(client->nickname));
1874 silc_strncat(check, sizeof(check), "!", 1);
1875 silc_strncat(check, sizeof(check),
1876 client->username, strlen(client->username));
1877 if (!strchr(client->username, '@')) {
1878 silc_strncat(check, sizeof(check), "@", 1);
1879 silc_strncat(check, sizeof(check),
1880 cmd->sock->hostname, strlen(cmd->sock->hostname));
1883 silc_strncat(check2, sizeof(check2),
1884 client->nickname, strlen(client->nickname));
1885 if (!strchr(client->nickname, '@')) {
1886 silc_strncat(check2, sizeof(check2), "@", 1);
1887 silc_strncat(check2, sizeof(check2),
1888 server->server_name, strlen(server->server_name));
1890 silc_strncat(check2, sizeof(check2), "!", 1);
1891 silc_strncat(check2, sizeof(check2),
1892 client->username, strlen(client->username));
1893 if (!strchr(client->username, '@')) {
1894 silc_strncat(check2, sizeof(check2), "@", 1);
1895 silc_strncat(check2, sizeof(check2),
1896 cmd->sock->hostname, strlen(cmd->sock->hostname));
1899 /* Check invite list if channel is invite-only channel */
1900 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
1901 if (!channel->invite_list ||
1902 !silc_hash_table_count(channel->invite_list) ||
1903 (!silc_server_inviteban_match(server, channel->invite_list,
1905 !silc_server_inviteban_match(server, channel->invite_list,
1906 2, client->data.public_key) &&
1907 !silc_server_inviteban_match(server, channel->invite_list,
1909 !silc_server_inviteban_match(server, channel->invite_list,
1911 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1912 SILC_STATUS_ERR_NOT_INVITED, 0);
1917 /* Check ban list if it exists. If the client's nickname, server,
1918 username and/or hostname is in the ban list the access to the
1919 channel is denied. */
1920 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
1921 if (silc_server_inviteban_match(server, channel->ban_list,
1923 silc_server_inviteban_match(server, channel->ban_list,
1924 2, client->data.public_key) ||
1925 !silc_server_inviteban_match(server, channel->ban_list,
1927 !silc_server_inviteban_match(server, channel->ban_list,
1929 silc_server_command_send_status_reply(
1930 cmd, SILC_COMMAND_JOIN,
1931 SILC_STATUS_ERR_BANNED_FROM_CHANNEL, 0);
1936 /* Check user count limit if set. */
1937 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
1938 if (silc_hash_table_count(channel->user_list) + 1 >
1939 channel->user_limit) {
1940 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1941 SILC_STATUS_ERR_CHANNEL_IS_FULL,
1948 /* Check the channel passphrase if set. */
1949 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
1950 /* Get passphrase */
1951 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
1953 passphrase = silc_memdup(tmp, tmp_len);
1955 if (!passphrase || !channel->passphrase ||
1956 memcmp(passphrase, channel->passphrase, strlen(channel->passphrase))) {
1957 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1958 SILC_STATUS_ERR_BAD_PASSWORD, 0);
1964 * Client is allowed to join to the channel. Make it happen.
1967 /* Check whether the client already is on the channel */
1968 if (silc_server_client_on_channel(client, channel, NULL)) {
1969 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1970 SILC_STATUS_ERR_USER_ON_CHANNEL, 0);
1974 /* Generate new channel key as protocol dictates */
1976 if (!silc_server_create_channel_key(server, channel, 0))
1979 /* Send the channel key. This is broadcasted to the channel but is not
1980 sent to the client who is joining to the channel. */
1981 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
1982 silc_server_send_channel_key(server, NULL, channel,
1983 server->server_type == SILC_ROUTER ?
1984 FALSE : !server->standalone);
1987 /* Join the client to the channel by adding it to channel's user list.
1988 Add also the channel to client entry's channels list for fast cross-
1990 chl = silc_calloc(1, sizeof(*chl));
1992 chl->client = client;
1993 chl->channel = channel;
1994 silc_hash_table_add(channel->user_list, client, chl);
1995 silc_hash_table_add(client->channels, channel, chl);
1996 channel->user_count++;
1997 channel->disabled = FALSE;
1999 /* Get users on the channel */
2000 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2003 /* Encode Client ID Payload of the original client who wants to join */
2004 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2006 /* Encode command reply packet */
2007 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2008 SILC_PUT32_MSB(channel->mode, mode);
2009 SILC_PUT32_MSB(created, tmp2);
2010 SILC_PUT32_MSB(user_count, tmp3);
2012 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2013 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2014 cipher = silc_cipher_get_name(channel->channel_key);
2015 keyp = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
2018 strlen(cipher), cipher,
2019 channel->key_len / 8, channel->key);
2023 if (channel->founder_key)
2024 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
2026 /* Encode invite list */
2028 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
2029 SilcHashTableList htl;
2031 invite_list = silc_buffer_alloc_size(2);
2032 silc_buffer_format(invite_list,
2033 SILC_STR_UI_SHORT(silc_hash_table_count(
2034 channel->invite_list)),
2037 silc_hash_table_list(channel->invite_list, &htl);
2038 while (silc_hash_table_get(&htl, (void **)&tmp_len, (void **)&reply)) {
2040 invite_list = silc_argument_payload_encode_one(invite_list,
2042 strlen((char *)reply),
2045 invite_list = silc_argument_payload_encode_one(invite_list,
2047 reply->len, tmp_len);
2049 silc_hash_table_list_reset(&htl);
2052 /* Encode ban list */
2054 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
2055 SilcHashTableList htl;
2057 ban_list = silc_buffer_alloc_size(2);
2058 silc_buffer_format(ban_list,
2059 SILC_STR_UI_SHORT(silc_hash_table_count(
2060 channel->ban_list)),
2063 silc_hash_table_list(channel->ban_list, &htl);
2064 while (silc_hash_table_get(&htl, (void **)&tmp_len, (void **)&reply)) {
2066 ban_list = silc_argument_payload_encode_one(ban_list,
2068 strlen((char *)reply),
2071 ban_list = silc_argument_payload_encode_one(ban_list,
2073 reply->len, tmp_len);
2075 silc_hash_table_list_reset(&htl);
2079 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2080 SILC_STATUS_OK, 0, ident, 14,
2081 2, channel->channel_name,
2082 strlen(channel->channel_name),
2083 3, chidp->data, chidp->len,
2084 4, clidp->data, clidp->len,
2087 7, keyp ? keyp->data : NULL,
2088 keyp ? keyp->len : 0,
2089 8, ban_list ? ban_list->data : NULL,
2090 ban_list ? ban_list->len : 0,
2091 9, invite_list ? invite_list->data :
2093 invite_list ? invite_list->len : 0,
2096 strlen(channel->topic) : 0,
2097 11, silc_hmac_get_name(channel->hmac),
2098 strlen(silc_hmac_get_name(channel->
2101 13, user_list->data, user_list->len,
2102 14, mode_list->data,
2104 15, fkey ? fkey->data : NULL,
2105 fkey ? fkey->len : 0);
2107 /* Send command reply */
2108 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
2109 reply->data, reply->len, FALSE);
2111 /* Send JOIN notify to locally connected clients on the channel. If
2112 we are normal server then router will send or have sent JOIN notify
2113 already. However since we've added the client already to our channel
2114 we'll ignore it (in packet_receive.c) so we must send it here. If
2115 we are router then this will send it to local clients and local
2117 SILC_LOG_DEBUG(("Send JOIN notify to channel"));
2118 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2119 SILC_NOTIFY_TYPE_JOIN, 2,
2120 clidp->data, clidp->len,
2121 chidp->data, chidp->len);
2123 /* Update statistics */
2124 server->stat.my_chanclients++;
2125 if (server->server_type == SILC_ROUTER) {
2126 server->stat.cell_chanclients++;
2127 server->stat.chanclients++;
2130 if (!cmd->pending) {
2131 /* Send JOIN notify packet to our primary router */
2132 silc_server_send_notify_join(server, SILC_PRIMARY_ROUTE(server),
2133 SILC_BROADCAST(server), channel, client->id);
2136 /* Distribute the channel key to all backup routers. */
2137 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
2138 keyp->data, keyp->len, FALSE, TRUE);
2140 /* If client became founder by providing correct founder auth data
2141 notify the mode change to the channel. */
2143 SILC_PUT32_MSB(chl->mode, mode);
2144 SILC_LOG_DEBUG(("Send CUMODE_CHANGE notify to channel"));
2145 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2146 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
2147 clidp->data, clidp->len,
2148 mode, 4, clidp->data, clidp->len,
2149 fkey ? fkey->data : NULL,
2150 fkey ? fkey->len : 0);
2154 /* Set CUMODE notify type to network */
2156 silc_server_send_notify_cumode(server, SILC_PRIMARY_ROUTE(server),
2157 SILC_BROADCAST(server), channel,
2158 chl->mode, client->id, SILC_ID_CLIENT,
2159 client->id, channel->founder_key);
2161 silc_buffer_free(reply);
2162 silc_buffer_free(clidp);
2163 silc_buffer_free(chidp);
2164 silc_buffer_free(keyp);
2165 silc_buffer_free(user_list);
2166 silc_buffer_free(mode_list);
2167 silc_buffer_free(fkey);
2168 silc_buffer_free(invite_list);
2169 silc_buffer_free(ban_list);
2172 silc_free(passphrase);
2175 /* Server side of command JOIN. Joins client into requested channel. If
2176 the channel does not exist it will be created. */
2178 SILC_SERVER_CMD_FUNC(join)
2180 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2181 SilcServer server = cmd->server;
2182 unsigned char *auth;
2183 SilcUInt32 tmp_len, auth_len;
2184 char *tmp, *channel_name = NULL, *cipher, *hmac;
2185 SilcChannelEntry channel;
2186 SilcUInt32 umode = 0;
2187 bool created = FALSE, create_key = TRUE;
2188 SilcClientID *client_id;
2190 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_JOIN, cmd, 2, 6);
2192 /* Get channel name */
2193 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2195 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2196 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2203 channel_name[255] = '\0';
2205 if (silc_server_name_bad_chars(channel_name, tmp_len) == TRUE) {
2206 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2207 SILC_STATUS_ERR_BAD_CHANNEL, 0);
2211 /* Get Client ID of the client who is joining to the channel */
2212 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2214 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2215 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2219 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
2221 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2222 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2227 /* Get cipher, hmac name and auth payload */
2228 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
2229 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
2230 auth = silc_argument_get_arg_type(cmd->args, 6, &auth_len);
2232 /* See if the channel exists */
2233 channel = silc_idlist_find_channel_by_name(server->local_list,
2234 channel_name, NULL);
2236 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2237 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
2239 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2240 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2245 silc_free(client_id);
2246 client_id = silc_id_dup(entry->id, SILC_ID_CLIENT);
2249 (channel->disabled && server->server_type != SILC_ROUTER)) {
2250 /* Channel not found or not valid */
2252 /* If we are standalone server we don't have a router, we just create
2253 the channel by ourselves (unless it existed). */
2254 if (server->standalone) {
2256 channel = silc_server_create_new_channel(server, server->id, cipher,
2257 hmac, channel_name, TRUE);
2259 silc_server_command_send_status_reply(
2260 cmd, SILC_COMMAND_JOIN,
2261 SILC_STATUS_ERR_UNKNOWN_ALGORITHM,
2263 silc_free(client_id);
2267 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2273 /* The channel does not exist on our server. If we are normal server
2274 we will send JOIN command to our router which will handle the
2275 joining procedure (either creates the channel if it doesn't exist
2276 or joins the client to it). */
2277 if (server->server_type != SILC_ROUTER) {
2279 SilcUInt16 old_ident;
2281 /* If this is pending command callback then we've resolved
2282 it and it didn't work, return since we've notified the
2283 client already in the command reply callback. */
2285 silc_free(client_id);
2289 old_ident = silc_command_get_ident(cmd->payload);
2290 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2291 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2293 /* Send JOIN command to our router */
2294 silc_server_packet_send(server, (SilcSocketConnection)
2295 SILC_PRIMARY_ROUTE(server),
2296 SILC_PACKET_COMMAND, cmd->packet->flags,
2297 tmpbuf->data, tmpbuf->len, TRUE);
2299 /* Reprocess this packet after received reply from router */
2300 silc_server_command_pending(server, SILC_COMMAND_JOIN,
2301 silc_command_get_ident(cmd->payload),
2302 silc_server_command_join,
2303 silc_server_command_dup(cmd));
2304 cmd->pending = TRUE;
2305 silc_command_set_ident(cmd->payload, old_ident);
2306 silc_buffer_free(tmpbuf);
2307 silc_free(client_id);
2311 /* We are router and the channel does not seem exist so we will check
2312 our global list as well for the channel. */
2313 channel = silc_idlist_find_channel_by_name(server->global_list,
2314 channel_name, NULL);
2316 /* Channel really does not exist, create it */
2317 channel = silc_server_create_new_channel(server, server->id, cipher,
2318 hmac, channel_name, TRUE);
2320 silc_server_command_send_status_reply(
2321 cmd, SILC_COMMAND_JOIN,
2322 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2323 silc_free(client_id);
2327 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2335 /* Channel not found */
2337 /* If the command came from router and we are normal server then
2338 something went wrong with the joining as the channel was not found.
2339 We can't do anything else but ignore this. */
2340 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
2341 server->server_type != SILC_ROUTER) {
2342 silc_free(client_id);
2346 /* We are router and the channel does not seem exist so we will check
2347 our global list as well for the channel. */
2348 channel = silc_idlist_find_channel_by_name(server->global_list,
2349 channel_name, NULL);
2351 /* Channel really does not exist, create it */
2352 channel = silc_server_create_new_channel(server, server->id, cipher,
2353 hmac, channel_name, TRUE);
2355 silc_server_command_send_status_reply(
2356 cmd, SILC_COMMAND_JOIN,
2357 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2358 silc_free(client_id);
2362 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2369 /* Check whether the channel was created by our router */
2370 if (cmd->pending && context2) {
2371 SilcServerCommandReplyContext reply = context2;
2373 if (silc_command_get(reply->payload) == SILC_COMMAND_JOIN) {
2374 tmp = silc_argument_get_arg_type(reply->args, 6, NULL);
2375 SILC_GET32_MSB(created, tmp);
2376 if (silc_argument_get_arg_type(reply->args, 7, NULL))
2377 create_key = FALSE; /* Router returned the key already */
2379 if (silc_command_get_status(reply->payload, NULL, NULL) &&
2380 channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2381 /* Save channel passphrase, if user provided it successfully */
2384 pa = silc_argument_get_arg_type(cmd->args, 3, &pa_len);
2386 silc_free(channel->passphrase);
2387 channel->passphrase = silc_memdup(pa, pa_len);
2392 if (silc_command_get(reply->payload) == SILC_COMMAND_WHOIS &&
2393 !channel->disabled && !silc_hash_table_count(channel->user_list))
2397 /* If the channel does not have global users and is also empty the client
2398 will be the channel founder and operator. */
2399 if (!channel->disabled &&
2400 !channel->global_users && !silc_hash_table_count(channel->user_list))
2401 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2403 /* Join to the channel */
2404 silc_server_command_join_channel(server, cmd, channel, client_id,
2405 created, create_key, umode,
2408 silc_free(client_id);
2411 silc_server_command_free(cmd);
2414 /* Server side of command MOTD. Sends server's current "message of the
2415 day" to the client. */
2417 SILC_SERVER_CMD_FUNC(motd)
2419 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2420 SilcServer server = cmd->server;
2421 SilcBuffer packet, idp;
2422 char *motd, *dest_server;
2423 SilcUInt32 motd_len;
2424 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2426 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_MOTD, cmd, 1, 1);
2428 /* Get server name */
2429 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2431 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2432 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
2436 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
2439 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
2441 if (server->config && server->config->server_info &&
2442 server->config->server_info->motd_file) {
2444 motd = silc_file_readfile(server->config->server_info->motd_file,
2450 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2457 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2463 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2464 packet->data, packet->len, FALSE);
2465 silc_buffer_free(packet);
2466 silc_buffer_free(idp);
2468 SilcServerEntry entry;
2470 /* Check whether we have this server cached */
2471 entry = silc_idlist_find_server_by_name(server->global_list,
2472 dest_server, TRUE, NULL);
2474 entry = silc_idlist_find_server_by_name(server->local_list,
2475 dest_server, TRUE, NULL);
2478 if (server->server_type != SILC_SERVER && !cmd->pending &&
2479 entry && !entry->motd) {
2480 /* Send to the server */
2482 SilcUInt16 old_ident;
2484 old_ident = silc_command_get_ident(cmd->payload);
2485 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2486 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2488 silc_server_packet_send(server, entry->connection,
2489 SILC_PACKET_COMMAND, cmd->packet->flags,
2490 tmpbuf->data, tmpbuf->len, TRUE);
2492 /* Reprocess this packet after received reply from router */
2493 silc_server_command_pending(server, SILC_COMMAND_MOTD,
2494 silc_command_get_ident(cmd->payload),
2495 silc_server_command_motd,
2496 silc_server_command_dup(cmd));
2497 cmd->pending = TRUE;
2498 silc_command_set_ident(cmd->payload, old_ident);
2499 silc_buffer_free(tmpbuf);
2503 if (!entry && !cmd->pending && !server->standalone) {
2504 /* Send to the primary router */
2506 SilcUInt16 old_ident;
2508 old_ident = silc_command_get_ident(cmd->payload);
2509 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2510 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2512 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2513 SILC_PACKET_COMMAND, cmd->packet->flags,
2514 tmpbuf->data, tmpbuf->len, TRUE);
2516 /* Reprocess this packet after received reply from router */
2517 silc_server_command_pending(server, SILC_COMMAND_MOTD,
2518 silc_command_get_ident(cmd->payload),
2519 silc_server_command_motd,
2520 silc_server_command_dup(cmd));
2521 cmd->pending = TRUE;
2522 silc_command_set_ident(cmd->payload, old_ident);
2523 silc_buffer_free(tmpbuf);
2528 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2529 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
2533 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
2534 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2535 SILC_STATUS_OK, 0, ident, 2,
2539 strlen(entry->motd) : 0);
2540 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2541 packet->data, packet->len, FALSE);
2542 silc_buffer_free(packet);
2543 silc_buffer_free(idp);
2547 silc_server_command_free(cmd);
2550 /* Server side of command UMODE. Client can use this command to set/unset
2551 user mode. Client actually cannot set itself to be as server/router
2552 operator so this can be used only to unset the modes. */
2554 SILC_SERVER_CMD_FUNC(umode)
2556 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2557 SilcServer server = cmd->server;
2558 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2560 unsigned char *tmp_mask, m[4];
2561 SilcUInt32 mask = 0;
2562 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2563 bool set_mask = FALSE;
2565 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
2568 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_UMODE, cmd, 1, 2);
2570 /* Get the client's mode mask */
2571 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
2573 SILC_GET32_MSB(mask, tmp_mask);
2578 /* Check that mode changing is allowed. */
2579 if (!silc_server_check_umode_rights(server, client, mask)) {
2580 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2581 SILC_STATUS_ERR_PERM_DENIED, 0);
2585 /* Anonymous mode cannot be set by client */
2586 if (mask & SILC_UMODE_ANONYMOUS) {
2587 if (!(client->mode & SILC_UMODE_ANONYMOUS)) {
2588 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2589 SILC_STATUS_ERR_PERM_DENIED, 0);
2593 if (client->mode & SILC_UMODE_ANONYMOUS) {
2594 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2595 SILC_STATUS_ERR_PERM_DENIED, 0);
2600 /* Update statistics */
2601 if (mask & SILC_UMODE_GONE) {
2602 if (!(client->mode & SILC_UMODE_GONE))
2603 server->stat.my_aways++;
2605 if (client->mode & SILC_UMODE_GONE)
2606 server->stat.my_aways--;
2609 /* Change the mode */
2610 client->mode = mask;
2612 /* Send UMODE change to primary router */
2613 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
2614 SILC_BROADCAST(server), client->id,
2617 /* Check if anyone is watching this nickname */
2618 if (server->server_type == SILC_ROUTER)
2619 silc_server_check_watcher_list(server, client, NULL,
2620 SILC_NOTIFY_TYPE_UMODE_CHANGE);
2623 /* Send command reply to sender */
2624 SILC_PUT32_MSB(client->mode, m);
2625 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
2626 SILC_STATUS_OK, 0, ident, 1,
2628 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2629 packet->data, packet->len, FALSE);
2630 silc_buffer_free(packet);
2633 silc_server_command_free(cmd);
2636 /* Server side command of CMODE. Changes channel mode */
2638 SILC_SERVER_CMD_FUNC(cmode)
2640 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2641 SilcServer server = cmd->server;
2642 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2643 SilcIDListData idata = (SilcIDListData)client;
2644 SilcChannelID *channel_id = NULL;
2645 SilcChannelEntry channel;
2646 SilcChannelClientEntry chl;
2647 SilcBuffer packet, cidp;
2648 unsigned char *tmp, *tmp_id, *tmp_mask;
2649 char *cipher = NULL, *hmac = NULL, *passphrase = NULL;
2650 SilcUInt32 mode_mask = 0, old_mask = 0, tmp_len, tmp_len2;
2651 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2652 bool set_mask = FALSE;
2653 SilcPublicKey founder_key = NULL;
2654 SilcBuffer fkey = NULL;
2657 silc_server_command_free(cmd);
2661 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CMODE, cmd, 1, 8);
2663 /* Get Channel ID */
2664 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
2666 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2667 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2668 silc_server_command_free(cmd);
2671 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2, NULL);
2673 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2674 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2675 silc_server_command_free(cmd);
2679 /* Get channel entry */
2680 channel = silc_idlist_find_channel_by_id(server->local_list,
2683 channel = silc_idlist_find_channel_by_id(server->global_list,
2686 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2687 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
2689 silc_free(channel_id);
2690 silc_server_command_free(cmd);
2694 old_mask = channel->mode;
2696 /* Get the channel mode mask */
2697 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2699 SILC_GET32_MSB(mode_mask, tmp_mask);
2703 /* Check whether this client is on the channel */
2704 if (!silc_server_client_on_channel(client, channel, &chl)) {
2705 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2706 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
2710 /* Check that client has rights to change any requested channel modes */
2711 if (set_mask && !silc_server_check_cmode_rights(server, channel, chl,
2713 SILC_LOG_DEBUG(("Client does not have rights to change mode"));
2714 silc_server_command_send_status_reply(
2715 cmd, SILC_COMMAND_CMODE,
2716 (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) ?
2717 SILC_STATUS_ERR_NO_CHANNEL_PRIV :
2718 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV), 0);
2722 /* If mode mask was not sent as argument then merely return the current
2723 mode mask to the sender. */
2726 SILC_PUT32_MSB(channel->mode, m);
2727 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
2728 SILC_STATUS_OK, 0, ident, 2,
2729 2, tmp_id, tmp_len2,
2731 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2732 packet->data, packet->len, FALSE);
2733 silc_buffer_free(packet);
2738 * Check the modes. Modes that requires nothing special operation are
2742 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
2743 /* Channel uses private keys to protect traffic. Client(s) has set the
2744 key locally they want to use, server does not know that key. */
2745 /* Nothing interesting to do here */
2747 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
2748 /* The mode is removed and we need to generate and distribute
2749 new channel key. Clients are not using private channel keys
2750 anymore after this. */
2752 /* Re-generate channel key */
2753 if (!silc_server_create_channel_key(server, channel, 0))
2756 /* Send the channel key. This sends it to our local clients and if
2757 we are normal server to our router as well. */
2758 silc_server_send_channel_key(server, NULL, channel,
2759 server->server_type == SILC_ROUTER ?
2760 FALSE : !server->standalone);
2762 cipher = (char *)silc_cipher_get_name(channel->channel_key);
2763 hmac = (char *)silc_hmac_get_name(channel->hmac);
2767 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
2768 /* User limit is set on channel */
2769 SilcUInt32 user_limit;
2771 /* Get user limit */
2772 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
2774 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
2775 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2776 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2780 SILC_GET32_MSB(user_limit, tmp);
2781 channel->user_limit = user_limit;
2784 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
2785 /* User limit mode is unset. Remove user limit */
2786 channel->user_limit = 0;
2789 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
2790 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
2791 /* Passphrase has been set to channel */
2793 /* Get the passphrase */
2794 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
2796 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2797 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2801 /* Save the passphrase */
2802 passphrase = channel->passphrase = silc_memdup(tmp, strlen(tmp));
2805 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2806 /* Passphrase mode is unset. remove the passphrase */
2807 silc_free(channel->passphrase);
2808 channel->passphrase = NULL;
2812 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
2813 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
2814 /* Cipher to use protect the traffic */
2815 SilcCipher newkey, oldkey;
2818 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
2820 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2821 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2825 /* Delete old cipher and allocate the new one */
2826 if (!silc_cipher_alloc(cipher, &newkey)) {
2827 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2828 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2832 oldkey = channel->channel_key;
2833 channel->channel_key = newkey;
2835 /* Re-generate channel key */
2836 if (!silc_server_create_channel_key(server, channel, 0)) {
2837 /* We don't have new key, revert to old one */
2838 channel->channel_key = oldkey;
2842 /* Remove old channel key for good */
2843 silc_cipher_free(oldkey);
2845 /* Send the channel key. This sends it to our local clients and if
2846 we are normal server to our router as well. */
2847 silc_server_send_channel_key(server, NULL, channel,
2848 server->server_type == SILC_ROUTER ?
2849 FALSE : !server->standalone);
2852 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
2853 /* Cipher mode is unset. Remove the cipher and revert back to
2855 SilcCipher newkey, oldkey;
2856 cipher = channel->cipher;
2858 /* Delete old cipher and allocate default one */
2859 if (!silc_cipher_alloc(cipher ? cipher : SILC_DEFAULT_CIPHER, &newkey)) {
2860 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2861 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2865 oldkey = channel->channel_key;
2866 channel->channel_key = newkey;
2868 /* Re-generate channel key */
2869 if (!silc_server_create_channel_key(server, channel, 0)) {
2870 /* We don't have new key, revert to old one */
2871 channel->channel_key = oldkey;
2875 /* Remove old channel key for good */
2876 silc_cipher_free(oldkey);
2878 /* Send the channel key. This sends it to our local clients and if
2879 we are normal server to our router as well. */
2880 silc_server_send_channel_key(server, NULL, channel,
2881 server->server_type == SILC_ROUTER ?
2882 FALSE : !server->standalone);
2886 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
2887 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
2888 /* HMAC to use protect the traffic */
2889 unsigned char hash[32];
2893 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
2895 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2896 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2900 /* Delete old hmac and allocate the new one */
2901 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2902 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2903 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2907 silc_hmac_free(channel->hmac);
2908 channel->hmac = newhmac;
2910 /* Set the HMAC key out of current channel key. The client must do
2912 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
2913 channel->key_len / 8, hash);
2914 silc_hmac_set_key(channel->hmac, hash,
2915 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
2916 memset(hash, 0, sizeof(hash));
2919 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
2920 /* Hmac mode is unset. Remove the hmac and revert back to
2923 unsigned char hash[32];
2924 hmac = channel->hmac_name;
2926 /* Delete old hmac and allocate default one */
2927 silc_hmac_free(channel->hmac);
2928 if (!silc_hmac_alloc(hmac ? hmac : SILC_DEFAULT_HMAC, NULL, &newhmac)) {
2929 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2930 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2934 silc_hmac_free(channel->hmac);
2935 channel->hmac = newhmac;
2937 /* Set the HMAC key out of current channel key. The client must do
2939 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
2940 channel->key_len / 8,
2942 silc_hmac_set_key(channel->hmac, hash,
2943 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
2944 memset(hash, 0, sizeof(hash));
2948 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
2949 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
2950 /* Check if the founder public key was received */
2951 founder_key = idata->public_key;
2952 tmp = silc_argument_get_arg_type(cmd->args, 8, &tmp_len);
2954 if (!silc_pkcs_public_key_payload_decode(tmp, tmp_len, &founder_key)) {
2955 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2956 SILC_STATUS_ERR_AUTH_FAILED,
2962 /* Set the founder authentication */
2963 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
2965 silc_server_command_send_status_reply(
2966 cmd, SILC_COMMAND_CMODE,
2967 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2971 /* Verify the payload before setting the mode */
2972 if (!silc_auth_verify_data(tmp, tmp_len, SILC_AUTH_PUBLIC_KEY,
2973 founder_key, 0, server->sha1hash,
2974 client->id, SILC_ID_CLIENT)) {
2975 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2976 SILC_STATUS_ERR_AUTH_FAILED,
2981 /* Save the public key */
2982 if (channel->founder_key)
2983 silc_pkcs_public_key_free(channel->founder_key);
2984 if (silc_argument_get_arg_type(cmd->args, 8, NULL))
2985 channel->founder_key = founder_key;
2987 channel->founder_key = silc_pkcs_public_key_copy(founder_key);
2988 if (!channel->founder_key) {
2989 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2990 SILC_STATUS_ERR_AUTH_FAILED,
2995 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
2997 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2998 SILC_STATUS_ERR_AUTH_FAILED,
3000 silc_pkcs_public_key_free(channel->founder_key);
3001 channel->founder_key = NULL;
3006 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3007 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3008 if (channel->founder_key)
3009 silc_pkcs_public_key_free(channel->founder_key);
3010 channel->founder_key = NULL;
3015 /* Finally, set the mode */
3016 old_mask = channel->mode = mode_mask;
3018 /* Send CMODE_CHANGE notify. */
3019 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3020 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3021 SILC_NOTIFY_TYPE_CMODE_CHANGE, 6,
3022 cidp->data, cidp->len,
3024 cipher, cipher ? strlen(cipher) : 0,
3025 hmac, hmac ? strlen(hmac) : 0,
3026 passphrase, passphrase ?
3027 strlen(passphrase) : 0,
3028 fkey ? fkey->data : NULL,
3029 fkey ? fkey->len : 0);
3031 /* Set CMODE notify type to network */
3032 silc_server_send_notify_cmode(server, SILC_PRIMARY_ROUTE(server),
3033 SILC_BROADCAST(server), channel,
3034 mode_mask, client->id, SILC_ID_CLIENT,
3035 cipher, hmac, passphrase, founder_key);
3037 /* Send command reply to sender */
3038 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3039 SILC_STATUS_OK, 0, ident, 3,
3040 2, tmp_id, tmp_len2,
3042 4, fkey ? fkey->data : NULL,
3043 fkey ? fkey->len : 0);
3044 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3045 packet->data, packet->len, FALSE);
3047 silc_buffer_free(packet);
3048 silc_buffer_free(cidp);
3051 channel->mode = old_mask;
3052 silc_buffer_free(fkey);
3053 silc_free(channel_id);
3054 silc_server_command_free(cmd);
3057 /* Server side of CUMODE command. Changes client's mode on a channel. */
3059 SILC_SERVER_CMD_FUNC(cumode)
3061 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3062 SilcServer server = cmd->server;
3063 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3064 SilcIDListData idata = (SilcIDListData)client;
3065 SilcChannelID *channel_id = NULL;
3066 SilcClientID *client_id = NULL;
3067 SilcChannelEntry channel;
3068 SilcClientEntry target_client;
3069 SilcChannelClientEntry chl;
3070 SilcBuffer packet, idp;
3071 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3072 SilcUInt32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
3074 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3075 SilcPublicKey founder_key = NULL;
3076 SilcBuffer fkey = NULL;
3081 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CUMODE, cmd, 3, 4);
3083 /* Get Channel ID */
3084 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3086 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3087 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3090 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len, NULL);
3092 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3093 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3097 /* Get channel entry */
3098 channel = silc_idlist_find_channel_by_id(server->local_list,
3101 channel = silc_idlist_find_channel_by_id(server->global_list,
3104 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3105 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
3111 /* Check whether sender is on the channel */
3112 if (!silc_server_client_on_channel(client, channel, &chl)) {
3113 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3114 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
3117 sender_mask = chl->mode;
3119 /* Get the target client's channel mode mask */
3120 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3122 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3123 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3127 SILC_GET32_MSB(target_mask, tmp_mask);
3129 /* Get target Client ID */
3130 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3132 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3133 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3136 client_id = silc_id_payload_parse_id(tmp_id, tmp_len, NULL);
3138 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3139 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3143 /* Get target client's entry */
3144 target_client = silc_idlist_find_client_by_id(server->local_list,
3145 client_id, TRUE, NULL);
3146 if (!target_client) {
3147 target_client = silc_idlist_find_client_by_id(server->global_list,
3148 client_id, TRUE, NULL);
3151 if (target_client != client &&
3152 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
3153 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
3154 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3155 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
3159 /* Check whether target client is on the channel */
3160 if (target_client != client) {
3161 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
3162 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3163 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL, 0);
3172 /* If the target client is founder, no one else can change their mode
3174 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && client != target_client) {
3175 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3176 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3181 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3182 if (target_client != client) {
3183 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3184 SILC_STATUS_ERR_NOT_YOU, 0);
3188 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
3189 /* The client tries to claim the founder rights. */
3190 unsigned char *tmp_auth;
3191 SilcUInt32 tmp_auth_len;
3192 SilcChannelClientEntry chl2;
3193 SilcHashTableList htl;
3195 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
3196 !channel->founder_key || !idata->public_key ||
3197 !silc_pkcs_public_key_compare(channel->founder_key,
3198 idata->public_key)) {
3199 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3200 SILC_STATUS_ERR_AUTH_FAILED, 0);
3204 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
3206 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3207 SILC_STATUS_ERR_AUTH_FAILED, 0);
3211 /* Verify the authentication payload */
3212 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len, SILC_AUTH_PUBLIC_KEY,
3213 channel->founder_key, 0, server->sha1hash,
3214 client->id, SILC_ID_CLIENT)) {
3215 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3216 SILC_STATUS_ERR_AUTH_FAILED, 0);
3221 founder_key = channel->founder_key;
3222 fkey = silc_pkcs_public_key_payload_encode(founder_key);
3224 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3225 SILC_STATUS_ERR_AUTH_FAILED, 0);
3229 /* There cannot be anyone else as founder on the channel now. This
3230 client is definitely the founder due to this authentication */
3231 silc_hash_table_list(channel->user_list, &htl);
3232 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
3233 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
3234 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3235 silc_server_force_cumode_change(server, NULL, channel, chl2,
3239 silc_hash_table_list_reset(&htl);
3241 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
3244 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3245 if (target_client == client) {
3246 /* Remove channel founder rights from itself */
3247 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3250 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3251 SILC_STATUS_ERR_NOT_YOU, 0);
3257 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
3258 /* Promote to operator */
3259 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
3260 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3261 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3262 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3263 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3268 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
3272 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
3273 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3274 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3275 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3276 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3281 /* Demote to normal user */
3282 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
3287 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
3288 if (target_client != client) {
3289 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3290 SILC_STATUS_ERR_NOT_YOU, 0);
3294 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)) {
3295 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
3299 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
3300 if (target_client != client) {
3301 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3302 SILC_STATUS_ERR_NOT_YOU, 0);
3306 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
3311 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
3312 if (target_client != client) {
3313 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3314 SILC_STATUS_ERR_NOT_YOU, 0);
3318 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS)) {
3319 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
3323 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
3324 if (target_client != client) {
3325 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3326 SILC_STATUS_ERR_NOT_YOU, 0);
3330 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
3335 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
3336 if (target_client != client) {
3337 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3338 SILC_STATUS_ERR_NOT_YOU, 0);
3342 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS)) {
3343 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
3347 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
3348 if (target_client != client) {
3349 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3350 SILC_STATUS_ERR_NOT_YOU, 0);
3354 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
3359 if (target_mask & SILC_CHANNEL_UMODE_QUIET) {
3360 if (!(chl->mode & SILC_CHANNEL_UMODE_QUIET)) {
3361 if (client == target_client) {
3362 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3363 SILC_STATUS_ERR_PERM_DENIED, 0);
3366 chl->mode |= SILC_CHANNEL_UMODE_QUIET;
3370 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
3371 if (client == target_client) {
3372 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3373 SILC_STATUS_ERR_PERM_DENIED, 0);
3376 chl->mode &= ~SILC_CHANNEL_UMODE_QUIET;
3381 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3382 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3384 /* Send notify to channel, notify only if mode was actually changed. */
3386 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3387 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
3388 idp->data, idp->len,
3391 fkey ? fkey->data : NULL,
3392 fkey ? fkey->len : 0);
3394 /* Set CUMODE notify type to network */
3395 silc_server_send_notify_cumode(server, SILC_PRIMARY_ROUTE(server),
3396 SILC_BROADCAST(server), channel,
3397 target_mask, client->id, SILC_ID_CLIENT,
3398 target_client->id, founder_key);
3401 /* Send command reply to sender */
3402 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
3403 SILC_STATUS_OK, 0, ident, 3,
3405 3, tmp_ch_id, tmp_ch_len,
3406 4, tmp_id, tmp_len);
3407 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3408 packet->data, packet->len, FALSE);
3410 silc_buffer_free(packet);
3411 silc_buffer_free(idp);
3414 silc_free(channel_id);
3415 silc_free(client_id);
3416 silc_buffer_free(fkey);
3417 silc_server_command_free(cmd);
3420 /* Server side of KICK command. Kicks client out of channel. */
3422 SILC_SERVER_CMD_FUNC(kick)
3424 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3425 SilcServer server = cmd->server;
3426 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3427 SilcClientEntry target_client;
3428 SilcChannelID *channel_id;
3429 SilcClientID *client_id;
3430 SilcChannelEntry channel;
3431 SilcChannelClientEntry chl;
3433 SilcUInt32 tmp_len, target_idp_len;
3434 unsigned char *tmp, *comment, *target_idp;
3439 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 3);
3441 /* Get Channel ID */
3442 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3444 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3445 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3448 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
3450 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3451 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3455 /* Get channel entry */
3456 channel = silc_idlist_find_channel_by_id(server->local_list,
3459 channel = silc_idlist_find_channel_by_id(server->local_list,
3462 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3463 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
3469 /* Check whether sender is on the channel */
3470 if (!silc_server_client_on_channel(client, channel, &chl)) {
3471 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3472 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
3476 /* Check that the kicker is channel operator or channel founder */
3477 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
3478 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
3479 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3480 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
3484 /* Get target Client ID */
3485 target_idp = silc_argument_get_arg_type(cmd->args, 2, &target_idp_len);
3487 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3488 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3491 client_id = silc_id_payload_parse_id(target_idp, target_idp_len, NULL);
3493 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3494 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3498 /* Get target client's entry */
3499 target_client = silc_idlist_find_client_by_id(server->local_list,
3500 client_id, TRUE, NULL);
3501 if (!target_client) {
3502 target_client = silc_idlist_find_client_by_id(server->global_list,
3503 client_id, TRUE, NULL);
3506 /* Check whether target client is on the channel */
3507 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
3508 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3509 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL,
3514 /* Check that the target client is not channel founder. Channel founder
3515 cannot be kicked from the channel. */
3516 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3517 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3518 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3525 comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3529 /* Send command reply to sender */
3530 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3533 /* Send KICKED notify to local clients on the channel */
3534 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3535 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3536 SILC_NOTIFY_TYPE_KICKED, 3,
3537 target_idp, target_idp_len,
3538 comment, comment ? strlen(comment) : 0,
3539 idp->data, idp->len);
3540 silc_buffer_free(idp);
3542 /* Send KICKED notify to primary route */
3543 silc_server_send_notify_kicked(server, SILC_PRIMARY_ROUTE(server),
3544 SILC_BROADCAST(server), channel,
3545 target_client->id, client->id, comment);
3547 /* Remove the client from channel's invite list */
3548 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
3550 silc_argument_payload_encode_one(NULL, target_idp, target_idp_len, 3);
3551 SilcArgumentPayload args =
3552 silc_argument_payload_parse(ab->data, ab->len, 1);
3553 silc_server_inviteban_process(server, channel->invite_list, 1, args);
3554 silc_buffer_free(ab);
3555 silc_argument_payload_free(args);
3558 /* Remove the client from the channel. If the channel does not exist
3559 after removing the client then the client kicked itself off the channel
3560 and we don't have to send anything after that. */
3561 if (!silc_server_remove_from_one_channel(server, NULL, channel,
3562 target_client, FALSE))
3565 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3566 /* Re-generate channel key */
3567 if (!silc_server_create_channel_key(server, channel, 0))
3570 /* Send the channel key to the channel. The key of course is not sent
3571 to the client who was kicked off the channel. */
3572 silc_server_send_channel_key(server, target_client->connection, channel,
3573 server->server_type == SILC_ROUTER ?
3574 FALSE : !server->standalone);
3578 silc_server_command_free(cmd);
3581 /* Server side of OPER command. Client uses this comand to obtain server
3582 operator privileges to this server/router. */
3584 SILC_SERVER_CMD_FUNC(oper)
3586 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3587 SilcServer server = cmd->server;
3588 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3589 unsigned char *username, *auth;
3591 SilcServerConfigAdmin *admin;
3592 SilcIDListData idata = (SilcIDListData)client;
3593 bool result = FALSE;
3594 SilcPublicKey cached_key;
3596 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
3599 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_OPER, cmd, 1, 2);
3601 /* Get the username */
3602 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3604 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3605 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3610 /* Get the admin configuration */
3611 admin = silc_server_config_find_admin(server, cmd->sock->ip,
3612 username, client->nickname);
3614 admin = silc_server_config_find_admin(server, cmd->sock->hostname,
3615 username, client->nickname);
3617 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3618 SILC_STATUS_ERR_AUTH_FAILED,
3620 SILC_LOG_INFO(("OPER authentication failed for username '%s' by "
3621 "nickname '%s' from %s", username,
3622 client->nickname, cmd->sock->hostname));
3627 /* Get the authentication payload */
3628 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3630 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3631 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3636 /* Verify the authentication data. If both passphrase and public key
3637 is set then try both of them. */
3638 if (admin->passphrase)
3639 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
3640 admin->passphrase, admin->passphrase_len,
3641 idata->hash, client->id, SILC_ID_CLIENT);
3642 if (!result && admin->publickeys) {
3643 cached_key = silc_server_get_public_key(server, admin->publickeys);
3646 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
3647 cached_key, 0, idata->hash,
3648 client->id, SILC_ID_CLIENT);
3651 /* Authentication failed */
3652 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3653 SILC_STATUS_ERR_AUTH_FAILED,
3658 /* Client is now server operator */
3659 client->mode |= SILC_UMODE_SERVER_OPERATOR;
3661 /* Update statistics */
3662 if (SILC_IS_LOCAL(client))
3663 server->stat.my_server_ops++;
3664 if (server->server_type == SILC_ROUTER)
3665 server->stat.server_ops++;
3667 /* Send UMODE change to primary router */
3668 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
3669 SILC_BROADCAST(server), client->id,
3672 /* Check if anyone is watching this nickname */
3673 if (server->server_type == SILC_ROUTER)
3674 silc_server_check_watcher_list(server, client, NULL,
3675 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3677 /* Send reply to the sender */
3678 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3682 silc_server_command_free(cmd);
3685 SILC_TASK_CALLBACK(silc_server_command_detach_cb)
3687 SilcServer server = app_context;
3688 QuitInternal q = (QuitInternal)context;
3689 SilcClientID *client_id = (SilcClientID *)q->sock;
3690 SilcClientEntry client;
3691 SilcSocketConnection sock;
3693 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3695 if (client && client->connection) {
3696 sock = client->connection;
3698 /* If there is pending outgoing data for the client then purge it
3699 to the network before closing connection. */
3700 silc_server_packet_queue_purge(server, sock);
3702 /* Close the connection on our side */
3703 client->router = NULL;
3704 client->connection = NULL;
3705 sock->user_data = NULL;
3706 silc_server_close_connection(server, sock);
3709 silc_free(client_id);
3713 SILC_TASK_CALLBACK(silc_server_command_detach_timeout)
3715 SilcServer server = app_context;
3716 QuitInternal q = (QuitInternal)context;
3717 SilcClientID *client_id = (SilcClientID *)q->sock;
3718 SilcClientEntry client;
3720 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3722 if (client && client->mode & SILC_UMODE_DETACHED) {
3723 SILC_LOG_DEBUG(("Detach timeout"));
3724 silc_server_free_client_data(server, NULL, client, TRUE,
3728 silc_free(client_id);
3732 /* Server side of DETACH command. Detached the client from the network
3733 by closing the connection but preserving the session. */
3735 SILC_SERVER_CMD_FUNC(detach)
3737 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3738 SilcServer server = cmd->server;
3739 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3742 if (server->config->detach_disabled) {
3743 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
3744 SILC_STATUS_ERR_OPERATION_ALLOWED,
3749 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
3752 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_DETACH, cmd, 0, 0);
3754 /* Remove operator privileges, since the client may resume in some
3755 other server which to it does not have operator privileges. */
3756 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
3757 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
3759 /* Send the user mode notify to notify that client is detached */
3760 client->mode |= SILC_UMODE_DETACHED;
3761 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
3762 client->data.status &= ~SILC_IDLIST_STATUS_NOATTR;
3763 client->last_command = 0;
3764 client->fast_command = 0;
3765 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
3766 SILC_BROADCAST(server), client->id,
3768 server->stat.my_detached++;
3770 /* Check if anyone is watching this nickname */
3771 if (server->server_type == SILC_ROUTER)
3772 silc_server_check_watcher_list(server, client, NULL,
3773 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3775 q = silc_calloc(1, sizeof(*q));
3776 q->sock = silc_id_dup(client->id, SILC_ID_CLIENT);
3777 silc_schedule_task_add(server->schedule, 0, silc_server_command_detach_cb,
3778 q, 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
3780 if (server->config->detach_timeout) {
3781 q = silc_calloc(1, sizeof(*q));
3782 q->sock = silc_id_dup(client->id, SILC_ID_CLIENT);
3783 silc_schedule_task_add(server->schedule, 0,
3784 silc_server_command_detach_timeout,
3785 q, server->config->detach_timeout * 60,
3786 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
3789 /* Send reply to the sender */
3790 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
3794 silc_server_command_free(cmd);
3797 /* Server side of WATCH command. */
3799 SILC_SERVER_CMD_FUNC(watch)
3801 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3802 SilcServer server = cmd->server;
3803 char *add_nick, *del_nick;
3804 SilcUInt32 add_nick_len, del_nick_len, tmp_len;
3806 unsigned char hash[16], *tmp;
3807 SilcClientEntry client;
3808 SilcClientID *client_id = NULL;
3810 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WATCH, cmd, 1, 3);
3812 if (server->server_type != SILC_ROUTER && !server->standalone) {
3813 if (!cmd->pending) {
3814 /* Send the command to router */
3816 SilcUInt16 old_ident;
3818 SILC_LOG_DEBUG(("Forwarding WATCH to router"));
3820 old_ident = silc_command_get_ident(cmd->payload);
3821 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
3822 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3824 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3825 SILC_PACKET_COMMAND, cmd->packet->flags,
3826 tmpbuf->data, tmpbuf->len, TRUE);
3828 /* Reprocess this packet after received reply from router */
3829 silc_server_command_pending(server, SILC_COMMAND_WATCH,
3830 silc_command_get_ident(cmd->payload),
3831 silc_server_command_watch,
3832 silc_server_command_dup(cmd));
3833 cmd->pending = TRUE;
3834 silc_command_set_ident(cmd->payload, old_ident);
3835 silc_buffer_free(tmpbuf);
3836 } else if (context2) {
3837 /* Received reply from router, just send same data to the client. */
3838 SilcServerCommandReplyContext reply = context2;
3841 SILC_LOG_DEBUG(("Received reply to WATCH from router"));
3842 silc_command_get_status(reply->payload, &status, NULL);
3843 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH, status,
3850 /* We are router and keep the watch list for local cell */
3852 /* Get the client ID */
3853 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3855 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3856 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3860 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
3862 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3863 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
3868 /* Get the client entry which must be in local list */
3869 client = silc_idlist_find_client_by_id(server->local_list,
3870 client_id, TRUE, NULL);
3872 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3873 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
3879 add_nick = silc_argument_get_arg_type(cmd->args, 2, &add_nick_len);
3880 del_nick = silc_argument_get_arg_type(cmd->args, 3, &del_nick_len);
3881 if (!add_nick && !del_nick) {
3882 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3883 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3888 if (add_nick && add_nick_len > 128)
3889 add_nick[128] = '\0';
3890 if (del_nick && del_nick_len > 128)
3891 del_nick[128] = '\0';
3893 memset(nick, 0, sizeof(nick));
3895 /* Add new nickname to be watched in our cell */
3897 if (silc_server_name_bad_chars(add_nick, strlen(add_nick)) == TRUE) {
3898 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3899 SILC_STATUS_ERR_BAD_NICKNAME, 0);
3903 /* Hash the nick, we have the hash saved, not nicks because we can
3904 do one to one mapping to the nick from Client ID hash this way. */
3905 silc_to_lower(add_nick, nick, sizeof(nick) - 1);
3906 silc_hash_make(server->md5hash, nick, strlen(nick), hash);
3908 /* Check whether this client is already watching this nickname */
3909 if (silc_hash_table_find_by_context(server->watcher_list, hash,
3911 /* Nickname is alredy being watched for this client */
3912 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3913 SILC_STATUS_ERR_NICKNAME_IN_USE,
3918 /* Get the nickname from the watcher list and use the same key in
3919 new entries as well. If key doesn't exist then create it. */
3920 if (!silc_hash_table_find(server->watcher_list, hash, (void **)&tmp, NULL))
3921 tmp = silc_memdup(hash, CLIENTID_HASH_LEN);
3923 /* Add the client to the watcher list with the specified nickname hash. */
3924 silc_hash_table_add(server->watcher_list, tmp, client);
3927 /* Delete nickname from watch list */
3929 if (silc_server_name_bad_chars(del_nick, strlen(del_nick)) == TRUE) {
3930 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3931 SILC_STATUS_ERR_BAD_NICKNAME, 0);
3935 /* Hash the nick, we have the hash saved, not nicks because we can
3936 do one to one mapping to the nick from Client ID hash this way. */
3937 silc_to_lower(del_nick, nick, sizeof(nick) - 1);
3938 silc_hash_make(server->md5hash, nick, strlen(nick), hash);
3940 /* Check that this client is watching for this nickname */
3941 if (!silc_hash_table_find_by_context(server->watcher_list, hash,
3942 client, (void **)&tmp)) {
3943 /* Nickname is alredy being watched for this client */
3944 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3945 SILC_STATUS_ERR_NO_SUCH_NICK, 0);
3949 /* Delete the nickname from the watcher list. */
3950 silc_hash_table_del_by_context(server->watcher_list, hash, client);
3952 /* Now check whether there still exists entries with this key, if not
3953 then free the key to not leak memory. */
3954 if (!silc_hash_table_find(server->watcher_list, hash, NULL, NULL))
3958 /* Distribute the watch list to backup routers too */
3959 if (server->backup) {
3961 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
3962 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3963 silc_server_backup_send(server, NULL, SILC_PACKET_COMMAND,
3964 cmd->packet->flags, tmpbuf->data, tmpbuf->len,
3966 silc_buffer_free(tmpbuf);
3969 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3973 silc_free(client_id);
3974 silc_server_command_free(cmd);
3977 /* Server side of SILCOPER command. Client uses this comand to obtain router
3978 operator privileges to this router. */
3980 SILC_SERVER_CMD_FUNC(silcoper)
3982 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3983 SilcServer server = cmd->server;
3984 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3985 unsigned char *username, *auth;
3987 SilcServerConfigAdmin *admin;
3988 SilcIDListData idata = (SilcIDListData)client;
3989 bool result = FALSE;
3990 SilcPublicKey cached_key;
3992 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
3995 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SILCOPER, cmd, 1, 2);
3997 if (server->server_type != SILC_ROUTER) {
3998 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
3999 SILC_STATUS_ERR_AUTH_FAILED, 0);
4003 /* Get the username */
4004 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4006 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4007 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4012 /* Get the admin configuration */
4013 admin = silc_server_config_find_admin(server, cmd->sock->ip,
4014 username, client->nickname);
4016 admin = silc_server_config_find_admin(server, cmd->sock->hostname,
4017 username, client->nickname);
4019 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4020 SILC_STATUS_ERR_AUTH_FAILED, 0);
4021 SILC_LOG_INFO(("SILCOPER authentication failed for username '%s' by "
4022 "nickname '%s' from %s", username,
4023 client->nickname, cmd->sock->hostname));
4028 /* Get the authentication payload */
4029 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4031 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4032 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4037 /* Verify the authentication data. If both passphrase and public key
4038 is set then try both of them. */
4039 if (admin->passphrase)
4040 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
4041 admin->passphrase, admin->passphrase_len,
4042 idata->hash, client->id, SILC_ID_CLIENT);
4043 if (!result && admin->publickeys) {
4044 cached_key = silc_server_get_public_key(server, admin->publickeys);
4047 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
4048 cached_key, 0, idata->hash,
4049 client->id, SILC_ID_CLIENT);
4052 /* Authentication failed */
4053 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4054 SILC_STATUS_ERR_AUTH_FAILED, 0);
4058 /* Client is now router operator */
4059 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4061 /* Update statistics */
4062 if (SILC_IS_LOCAL(client))
4063 server->stat.my_router_ops++;
4064 if (server->server_type == SILC_ROUTER)
4065 server->stat.router_ops++;
4067 /* Send UMODE change to primary router */
4068 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
4069 SILC_BROADCAST(server), client->id,
4072 /* Check if anyone is watching this nickname */
4073 if (server->server_type == SILC_ROUTER)
4074 silc_server_check_watcher_list(server, client, NULL,
4075 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4077 /* Send reply to the sender */
4078 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4082 silc_server_command_free(cmd);
4085 /* Server side of command BAN. This is used to manage the ban list of the
4086 channel. To add clients and remove clients from the ban list. */
4088 SILC_SERVER_CMD_FUNC(ban)
4090 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4091 SilcServer server = cmd->server;
4092 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4093 SilcBuffer packet, list, tmp2;
4094 SilcChannelEntry channel;
4095 SilcChannelClientEntry chl;
4096 SilcChannelID *channel_id = NULL;
4097 unsigned char *id, *tmp;
4098 SilcUInt32 id_len, len;
4099 SilcArgumentPayload args;
4100 SilcHashTableList htl;
4102 SilcUInt16 argc = 0, ident = silc_command_get_ident(cmd->payload);
4104 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4107 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_BAN, cmd, 0, 3);
4109 /* Get Channel ID */
4110 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4112 channel_id = silc_id_payload_parse_id(id, id_len, NULL);
4114 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4115 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4120 /* Get channel entry. The server must know about the channel since the
4121 client is expected to be on the channel. */
4122 channel = silc_idlist_find_channel_by_id(server->local_list,
4125 channel = silc_idlist_find_channel_by_id(server->global_list,
4128 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4129 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4135 /* Check whether this client is on the channel */
4136 if (!silc_server_client_on_channel(client, channel, &chl)) {
4137 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4138 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
4142 /* The client must be at least channel operator. */
4143 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4144 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4145 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
4149 /* Get the ban information */
4150 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
4151 if (tmp && len > 2) {
4152 /* Parse the arguments to see they are constructed correctly */
4153 SILC_GET16_MSB(argc, tmp);
4154 args = silc_argument_payload_parse(tmp + 2, len - 2, argc);
4156 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4157 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4162 /* Get the type of action */
4163 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
4164 if (tmp && len == 1) {
4165 if (tmp[0] == 0x00) {
4166 /* Allocate hash table for ban list if it doesn't exist yet */
4167 if (!channel->ban_list)
4168 channel->ban_list = silc_hash_table_alloc(0, silc_hash_ptr,
4172 /* Check for resource limit */
4173 if (silc_hash_table_count(channel->ban_list) > 64) {
4174 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4175 SILC_STATUS_ERR_RESOURCE_LIMIT,
4181 /* Now add or delete the information. */
4182 silc_server_inviteban_process(server, channel->ban_list,
4183 (SilcUInt8)tmp[0], args);
4185 silc_argument_payload_free(args);
4188 /* Encode ban list */
4190 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
4191 list = silc_buffer_alloc_size(2);
4192 silc_buffer_format(list,
4193 SILC_STR_UI_SHORT(silc_hash_table_count(
4194 channel->ban_list)),
4196 silc_hash_table_list(channel->ban_list, &htl);
4197 while (silc_hash_table_get(&htl, (void **)&type, (void **)&tmp2)) {
4199 list = silc_argument_payload_encode_one(list, (char *)tmp2,
4200 strlen((char *)tmp2), type);
4202 list = silc_argument_payload_encode_one(list, tmp2->data, tmp2->len,
4205 silc_hash_table_list_reset(&htl);
4208 /* Send the BAN notify type to our primary router. */
4210 silc_server_send_notify_ban(server, SILC_PRIMARY_ROUTE(server),
4211 SILC_BROADCAST(server), channel,
4212 silc_argument_get_arg_type(cmd->args, 2, NULL),
4215 /* Send the reply back to the client */
4217 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4218 SILC_STATUS_OK, 0, ident, 2,
4220 3, list ? list->data : NULL,
4221 list ? list->len : 0);
4222 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4223 packet->data, packet->len, FALSE);
4225 silc_buffer_free(packet);
4226 silc_buffer_free(list);
4229 silc_free(channel_id);
4230 silc_server_command_free(cmd);
4233 /* Server side command of LEAVE. Removes client from a channel. */
4235 SILC_SERVER_CMD_FUNC(leave)
4237 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4238 SilcServer server = cmd->server;
4239 SilcSocketConnection sock = cmd->sock;
4240 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4241 SilcChannelID *id = NULL;
4242 SilcChannelEntry channel;
4246 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !id_entry)
4249 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 2);
4251 /* Get Channel ID */
4252 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4254 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4255 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4258 id = silc_id_payload_parse_id(tmp, len, NULL);
4260 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4261 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4265 /* Get channel entry */
4266 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4268 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4270 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4271 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4277 /* Check whether this client is on the channel */
4278 if (!silc_server_client_on_channel(id_entry, channel, NULL)) {
4279 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4280 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
4284 /* Notify routers that they should remove this client from their list
4285 of clients on the channel. Send LEAVE notify type. */
4286 silc_server_send_notify_leave(server, SILC_PRIMARY_ROUTE(server),
4287 SILC_BROADCAST(server), channel, id_entry->id);
4289 silc_server_command_send_status_data(cmd, SILC_COMMAND_LEAVE,
4290 SILC_STATUS_OK, 0, 2, tmp, len);
4292 /* Remove client from channel */
4293 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4295 /* If the channel does not exist anymore we won't send anything */
4298 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4299 /* Re-generate channel key */
4300 if (!silc_server_create_channel_key(server, channel, 0))
4303 /* Send the channel key */
4304 silc_server_send_channel_key(server, NULL, channel,
4305 server->server_type == SILC_ROUTER ?
4306 FALSE : !server->standalone);
4311 silc_server_command_free(cmd);
4314 /* Server side of command USERS. Resolves clients and their USERS currently
4315 joined on the requested channel. The list of Client ID's and their modes
4316 on the channel is sent back. */
4318 SILC_SERVER_CMD_FUNC(users)
4320 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4321 SilcServer server = cmd->server;
4322 SilcChannelEntry channel;
4323 SilcChannelID *id = NULL;
4324 SilcBuffer packet, idp;
4325 unsigned char *channel_id;
4326 SilcUInt32 channel_id_len;
4327 SilcBuffer client_id_list;
4328 SilcBuffer client_mode_list;
4329 unsigned char lc[4];
4330 SilcUInt32 list_count = 0;
4331 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4334 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_USERS, cmd, 1, 2);
4336 /* Get Channel ID */
4337 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4339 /* Get channel name */
4340 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
4342 if (!channel_id && !channel_name) {
4343 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4344 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4349 id = silc_id_payload_parse_id(channel_id, channel_id_len, NULL);
4351 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4352 SILC_STATUS_ERR_BAD_CHANNEL_ID, 0);
4357 /* If we are server and we don't know about this channel we will send
4358 the command to our router. If we know about the channel then we also
4359 have the list of users already. */
4361 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4363 channel = silc_idlist_find_channel_by_name(server->local_list,
4364 channel_name, NULL);
4366 if (!channel || (!server->standalone && (channel->disabled ||
4367 !channel->users_resolved))) {
4368 if (server->server_type != SILC_ROUTER && !server->standalone &&
4372 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4373 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4375 /* Send USERS command */
4376 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4377 SILC_PACKET_COMMAND, cmd->packet->flags,
4378 tmpbuf->data, tmpbuf->len, TRUE);
4380 /* Reprocess this packet after received reply */
4381 silc_server_command_pending(server, SILC_COMMAND_USERS,
4382 silc_command_get_ident(cmd->payload),
4383 silc_server_command_users,
4384 silc_server_command_dup(cmd));
4385 cmd->pending = TRUE;
4386 silc_command_set_ident(cmd->payload, ident);
4387 silc_buffer_free(tmpbuf);
4392 /* Check the global list as well. */
4394 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4396 channel = silc_idlist_find_channel_by_name(server->global_list,
4397 channel_name, NULL);
4399 /* Channel really does not exist */
4400 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4401 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4407 /* If the channel is private or secret do not send anything, unless the
4408 user requesting this command is on the channel or is server */
4409 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
4410 if (channel->mode & (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)
4411 && !silc_server_client_on_channel(cmd->sock->user_data, channel,
4413 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4414 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4420 /* Get the users list */
4421 if (!silc_server_get_users_on_channel(server, channel, &client_id_list,
4422 &client_mode_list, &list_count)) {
4424 client_id_list = NULL;
4425 client_mode_list = NULL;
4429 SILC_PUT32_MSB(list_count, lc);
4432 idp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
4433 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
4434 SILC_STATUS_OK, 0, ident, 4,
4435 2, idp->data, idp->len,
4438 client_id_list->data : NULL,
4440 client_id_list->len : 0,
4441 5, client_mode_list ?
4442 client_mode_list->data : NULL,
4444 client_mode_list->len : 0);
4445 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4446 packet->data, packet->len, FALSE);
4448 silc_buffer_free(idp);
4449 silc_buffer_free(packet);
4451 silc_buffer_free(client_id_list);
4452 if (client_mode_list)
4453 silc_buffer_free(client_mode_list);
4457 silc_server_command_free(cmd);
4460 /* Server side of command GETKEY. This fetches the client's public key
4461 from the server where to the client is connected. */
4463 SILC_SERVER_CMD_FUNC(getkey)
4465 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4466 SilcServer server = cmd->server;
4468 SilcClientEntry client;
4469 SilcServerEntry server_entry;
4470 SilcClientID *client_id = NULL;
4471 SilcServerID *server_id = NULL;
4472 SilcIDPayload idp = NULL;
4473 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4476 SilcBuffer pk = NULL;
4478 SilcPublicKey public_key;
4480 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4482 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4483 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4487 idp = silc_id_payload_parse(tmp, tmp_len);
4489 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4490 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4495 id_type = silc_id_payload_get_type(idp);
4496 if (id_type == SILC_ID_CLIENT) {
4497 client_id = silc_id_payload_get_id(idp);
4499 /* If the client is not found from local list there is no chance it
4500 would be locally connected client so send the command further. */
4501 client = silc_idlist_find_client_by_id(server->local_list,
4502 client_id, TRUE, NULL);
4504 client = silc_idlist_find_client_by_id(server->global_list,
4505 client_id, TRUE, NULL);
4507 if ((!client && !cmd->pending && !server->standalone) ||
4508 (client && !client->connection && !cmd->pending &&
4509 !(client->mode & SILC_UMODE_DETACHED)) ||
4510 (client && !client->data.public_key && !cmd->pending)) {
4512 SilcUInt16 old_ident;
4513 SilcSocketConnection dest_sock;
4515 dest_sock = silc_server_get_client_route(server, NULL, 0,
4516 client_id, NULL, NULL);
4520 old_ident = silc_command_get_ident(cmd->payload);
4521 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4522 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4524 silc_server_packet_send(server, dest_sock,
4525 SILC_PACKET_COMMAND, cmd->packet->flags,
4526 tmpbuf->data, tmpbuf->len, TRUE);
4528 /* Reprocess this packet after received reply from router */
4529 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4530 silc_command_get_ident(cmd->payload),
4531 silc_server_command_getkey,
4532 silc_server_command_dup(cmd));
4533 cmd->pending = TRUE;
4534 silc_command_set_ident(cmd->payload, old_ident);
4535 silc_buffer_free(tmpbuf);
4540 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4541 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
4546 /* The client is locally connected, just get the public key and
4547 send it back. If they key does not exist then do not send it,
4548 send just OK reply */
4549 public_key = client->data.public_key;
4551 pk = silc_pkcs_public_key_payload_encode(public_key);
4552 } else if (id_type == SILC_ID_SERVER) {
4553 server_id = silc_id_payload_get_id(idp);
4555 /* If the server is not found from local list there is no chance it
4556 would be locally connected server so send the command further. */
4557 server_entry = silc_idlist_find_server_by_id(server->local_list,
4558 server_id, TRUE, NULL);
4560 server_entry = silc_idlist_find_server_by_id(server->global_list,
4561 server_id, TRUE, NULL);
4563 if (server_entry != server->id_entry &&
4564 ((!server_entry && !cmd->pending && !server->standalone) ||
4565 (server_entry && !server_entry->connection && !cmd->pending &&
4566 !server->standalone) ||
4567 (server_entry && !server_entry->data.public_key && !cmd->pending &&
4568 !server->standalone))) {
4570 SilcUInt16 old_ident;
4572 old_ident = silc_command_get_ident(cmd->payload);
4573 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4574 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4576 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4577 SILC_PACKET_COMMAND, cmd->packet->flags,
4578 tmpbuf->data, tmpbuf->len, TRUE);
4580 /* Reprocess this packet after received reply from router */
4581 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4582 silc_command_get_ident(cmd->payload),
4583 silc_server_command_getkey,
4584 silc_server_command_dup(cmd));
4585 cmd->pending = TRUE;
4586 silc_command_set_ident(cmd->payload, old_ident);
4587 silc_buffer_free(tmpbuf);
4591 if (!server_entry) {
4592 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4593 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
4598 /* If they key does not exist then do not send it, send just OK reply */
4599 public_key = (!server_entry->data.public_key ?
4600 (server_entry == server->id_entry ? server->public_key :
4601 NULL) : server_entry->data.public_key);
4603 pk = silc_pkcs_public_key_payload_encode(public_key);
4608 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4609 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
4610 SILC_STATUS_OK, 0, ident, 2,
4612 3, pk ? pk->data : NULL,
4614 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4615 packet->data, packet->len, FALSE);
4616 silc_buffer_free(packet);
4620 silc_id_payload_free(idp);
4621 silc_buffer_free(pk);
4622 silc_free(client_id);
4623 silc_free(server_id);
4624 silc_server_command_free(cmd);
4628 /* Private range commands, specific to this implementation */
4630 /* Server side command of CONNECT. Connects us to the specified remote
4631 server or router. */
4633 SILC_SERVER_CMD_FUNC(connect)
4635 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4636 SilcServer server = cmd->server;
4637 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4638 unsigned char *tmp, *host;
4640 SilcUInt32 port = SILC_PORT;
4642 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4645 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CONNECT, cmd, 1, 2);
4647 /* Check whether client has the permissions. */
4648 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
4649 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
4650 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4651 SILC_STATUS_ERR_NO_SERVER_PRIV, 0);
4655 if (server->server_type == SILC_ROUTER && !server->backup_router &&
4656 client->mode & SILC_UMODE_SERVER_OPERATOR) {
4657 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4658 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
4662 /* Get the remote server */
4663 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4665 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4666 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4672 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4674 SILC_GET32_MSB(port, tmp);
4676 /* Create the connection. It is done with timeout and is async. */
4677 silc_server_create_connection(server, host, port);
4679 /* Send reply to the sender */
4680 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4684 silc_server_command_free(cmd);
4687 /* Server side command of CLOSE. Closes connection to a specified server. */
4689 SILC_SERVER_CMD_FUNC(close)
4691 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4692 SilcServer server = cmd->server;
4693 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4694 SilcServerEntry server_entry;
4695 SilcSocketConnection sock;
4698 unsigned char *name;
4699 SilcUInt32 port = SILC_PORT;
4701 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4704 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CLOSE, cmd, 1, 2);
4706 /* Check whether client has the permissions. */
4707 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
4708 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
4709 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4710 SILC_STATUS_ERR_NO_SERVER_PRIV,
4715 /* Get the remote server */
4716 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4718 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4719 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4725 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4727 SILC_GET32_MSB(port, tmp);
4729 server_entry = silc_idlist_find_server_by_conn(server->local_list,
4730 name, port, FALSE, NULL);
4732 server_entry = silc_idlist_find_server_by_conn(server->global_list,
4733 name, port, FALSE, NULL);
4734 if (!server_entry) {
4735 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4736 SILC_STATUS_ERR_NO_SERVER_ID, 0);
4740 if (server_entry == server->id_entry) {
4741 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4742 SILC_STATUS_ERR_NO_SERVER_ID, 0);
4746 /* Send reply to the sender */
4747 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4750 /* Close the connection to the server */
4751 sock = (SilcSocketConnection)server_entry->connection;
4753 /* If we shutdown primary router connection manually then don't trigger
4754 any reconnect or backup router connections, by setting the router
4756 if (server->router == server_entry) {
4757 server->id_entry->router = NULL;
4758 server->router = NULL;
4759 server->standalone = TRUE;
4761 silc_server_free_sock_user_data(server, sock, NULL);
4762 silc_server_close_connection(server, sock);
4765 silc_server_command_free(cmd);
4768 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
4769 active connections. */
4771 SILC_SERVER_CMD_FUNC(shutdown)
4773 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4774 SilcServer server = cmd->server;
4775 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4777 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4780 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_SHUTDOWN, cmd, 0, 0);
4782 /* Check whether client has the permission. */
4783 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
4784 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
4785 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
4786 SILC_STATUS_ERR_NO_SERVER_PRIV,
4791 /* Send reply to the sender */
4792 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
4795 /* Then, gracefully, or not, bring the server down. */
4796 silc_server_stop(server);
4800 silc_server_command_free(cmd);