updates.
[silc.git] / apps / silcd / command.c
1 /*
2
3   command.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /* $Id$ */
21
22 #include "serverincludes.h"
23 #include "server_internal.h"
24
25 static int silc_server_is_registered(SilcServer server,
26                                      SilcSocketConnection sock,
27                                      SilcServerCommandContext cmd,
28                                      SilcCommand command);
29 static void 
30 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
31                                       SilcCommand command,
32                                       SilcCommandStatus status);
33 static void 
34 silc_server_command_send_status_data(SilcServerCommandContext cmd,
35                                      SilcCommand command,
36                                      SilcCommandStatus status,
37                                      uint32 arg_type,
38                                      unsigned char *arg,
39                                      uint32 arg_len);
40 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
41
42 /* Server command list. */
43 SilcServerCommand silc_command_list[] =
44 {
45   SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
46   SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
47   SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
48   SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
49   SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
50   SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
51   SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
52   SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
53   SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
54   SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
55   SILC_SERVER_CMD(connect, CONNECT, 
56                   SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
57   SILC_SERVER_CMD(ping, PING, SILC_CF_LAG | SILC_CF_REG),
58   SILC_SERVER_CMD(oper, OPER, SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
59   SILC_SERVER_CMD(join, JOIN, SILC_CF_LAG_STRICT | SILC_CF_REG),
60   SILC_SERVER_CMD(motd, MOTD, SILC_CF_LAG | SILC_CF_REG),
61   SILC_SERVER_CMD(umode, UMODE, SILC_CF_LAG | SILC_CF_REG),
62   SILC_SERVER_CMD(cmode, CMODE, SILC_CF_LAG_STRICT | SILC_CF_REG),
63   SILC_SERVER_CMD(cumode, CUMODE, SILC_CF_LAG | SILC_CF_REG),
64   SILC_SERVER_CMD(kick, KICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
65   SILC_SERVER_CMD(ban, BAN, SILC_CF_LAG_STRICT | SILC_CF_REG),
66   SILC_SERVER_CMD(close, CLOSE,
67                   SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
68   SILC_SERVER_CMD(shutdown, SHUTDOWN, SILC_CF_LAG | SILC_CF_REG | 
69                   SILC_CF_OPER),
70   SILC_SERVER_CMD(silcoper, SILCOPER,
71                   SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
72   SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
73   SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
74   SILC_SERVER_CMD(getkey, GETKEY, SILC_CF_LAG | SILC_CF_REG),
75
76   { NULL, 0 },
77 };
78
79 #define SILC_SERVER_COMMAND_CHECK_ARGC(command, context, min, max)            \
80 do {                                                                          \
81   uint32 _argc = silc_argument_get_arg_num(cmd->args);                \
82                                                                               \
83   SILC_LOG_DEBUG(("Start"));                                                  \
84                                                                               \
85   if (_argc < min) {                                                          \
86     silc_server_command_send_status_reply(cmd, command,                       \
87                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS); \
88     silc_server_command_free(cmd);                                            \
89     return;                                                                   \
90   }                                                                           \
91   if (_argc > max) {                                                          \
92     silc_server_command_send_status_reply(cmd, command,                       \
93                                           SILC_STATUS_ERR_TOO_MANY_PARAMS);   \
94     silc_server_command_free(cmd);                                            \
95     return;                                                                   \
96   }                                                                           \
97 } while(0)
98
99 /* Returns TRUE if the connection is registered. Unregistered connections
100    usually cannot send commands hence the check. */
101
102 static int silc_server_is_registered(SilcServer server,
103                                      SilcSocketConnection sock,
104                                      SilcServerCommandContext cmd,
105                                      SilcCommand command)
106 {
107   SilcIDListData idata = (SilcIDListData)sock->user_data;
108   if (idata->registered)
109     return TRUE;
110
111   silc_server_command_send_status_reply(cmd, command,
112                                         SILC_STATUS_ERR_NOT_REGISTERED);
113   silc_server_command_free(cmd);
114   return FALSE;
115 }
116
117 /* Internal context to hold data when executed command with timeout. */
118 typedef struct {
119   SilcServerCommandContext ctx;
120   SilcServerCommand *cmd;
121 } *SilcServerCommandTimeout;
122
123 /* Timeout callback to process commands with timeout for client. Client's
124    commands are always executed with timeout. */
125
126 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
127 {
128   SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
129   SilcClientEntry client = (SilcClientEntry)timeout->ctx->sock->user_data;
130
131   /* Update access time */
132   client->last_command = time(NULL);
133
134   if (!(timeout->cmd->flags & SILC_CF_REG))
135     timeout->cmd->cb(timeout->ctx);
136   else if (silc_server_is_registered(timeout->ctx->server, 
137                                      timeout->ctx->sock, 
138                                      timeout->ctx, 
139                                      timeout->cmd->cmd))
140     timeout->cmd->cb(timeout->ctx);
141
142   silc_free(timeout);
143 }
144
145 /* Processes received command packet. */
146
147 void silc_server_command_process(SilcServer server,
148                                  SilcSocketConnection sock,
149                                  SilcPacketContext *packet)
150 {
151   SilcServerCommandContext ctx;
152   SilcServerCommand *cmd;
153   SilcCommand command;
154
155   /* Allocate command context. This must be free'd by the
156      command routine receiving it. */
157   ctx = silc_server_command_alloc();
158   ctx->server = server;
159   ctx->sock = silc_socket_dup(sock);
160   ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
161   
162   /* Parse the command payload in the packet */
163   ctx->payload = silc_command_payload_parse(packet->buffer);
164   if (!ctx->payload) {
165     SILC_LOG_ERROR(("Bad command payload, packet dropped"));
166     silc_buffer_free(packet->buffer);
167     silc_packet_context_free(packet);
168     silc_socket_free(ctx->sock);
169     silc_free(ctx);
170     return;
171   }
172   ctx->args = silc_command_get_args(ctx->payload);
173
174   /* Get the command */
175   command = silc_command_get(ctx->payload);
176   for (cmd = silc_command_list; cmd->cb; cmd++)
177     if (cmd->cmd == command)
178       break;
179
180   if (cmd == NULL) {
181     silc_server_command_send_status_reply(ctx, command,
182                                           SILC_STATUS_ERR_UNKNOWN_COMMAND);
183     silc_server_command_free(ctx);
184     return;
185   }
186
187   /* Execute client's commands always with timeout.  Normally they are
188      executed with zero (0) timeout but if client is sending command more
189      frequently than once in 2 seconds, then the timeout may be 0 to 2
190      seconds. */
191   if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
192     SilcClientEntry client = (SilcClientEntry)sock->user_data;
193     SilcServerCommandTimeout timeout = silc_calloc(1, sizeof(*timeout));
194     int fast;
195
196     timeout->ctx = ctx;
197     timeout->cmd = cmd;
198
199     if (client->last_command && (time(NULL) - client->last_command) < 2) {
200       client->fast_command++;
201       fast = FALSE;
202     } else {
203       client->fast_command = ((client->fast_command - 1) <= 0 ? 0 : 
204                               client->fast_command--);
205       fast = TRUE;
206     }
207
208     if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
209                   (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
210       silc_task_register(server->timeout_queue, sock->sock, 
211                          silc_server_command_process_timeout,
212                          (void *)timeout, 
213                          2 - (time(NULL) - client->last_command), 0,
214                          SILC_TASK_TIMEOUT,
215                          SILC_TASK_PRI_NORMAL);
216     else
217       silc_task_register(server->timeout_queue, sock->sock, 
218                          silc_server_command_process_timeout,
219                          (void *)timeout, 
220                          0, 1,
221                          SILC_TASK_TIMEOUT,
222                          SILC_TASK_PRI_NORMAL);
223     return;
224   }
225
226   /* Execute for server */
227
228   if (!(cmd->flags & SILC_CF_REG))
229     cmd->cb(ctx);
230   else if (silc_server_is_registered(server, sock, ctx, cmd->cmd))
231     cmd->cb(ctx);
232 }
233
234 /* Allocate Command Context */
235
236 SilcServerCommandContext silc_server_command_alloc()
237 {
238   SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
239   ctx->users++;
240   return ctx;
241 }
242
243 /* Free's the command context allocated before executing the command */
244
245 void silc_server_command_free(SilcServerCommandContext ctx)
246 {
247   ctx->users--;
248   SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
249                   ctx->users));
250   if (ctx->users < 1) {
251     if (ctx->payload)
252       silc_command_payload_free(ctx->payload);
253     if (ctx->packet)
254       silc_packet_context_free(ctx->packet);
255     if (ctx->sock)
256       silc_socket_free(ctx->sock); /* Decrease reference counter */
257     silc_free(ctx);
258   }
259 }
260
261 /* Duplicate Command Context by adding reference counter. The context won't
262    be free'd untill it hits zero. */
263
264 SilcServerCommandContext 
265 silc_server_command_dup(SilcServerCommandContext ctx)
266 {
267   ctx->users++;
268   SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
269                   ctx->users));
270   return ctx;
271 }
272
273 /* Add new pending command to be executed when reply to a command has been
274    received. The `reply_cmd' is the command that will call the `callback'
275    with `context' when reply has been received.  If `ident' is non-zero
276    the `callback' will be executed when received reply with command
277    identifier `ident'. */
278
279 void silc_server_command_pending(SilcServer server,
280                                  SilcCommand reply_cmd,
281                                  uint16 ident,
282                                  SilcServerPendingDestructor destructor,
283                                  SilcCommandCb callback,
284                                  void *context)
285 {
286   SilcServerCommandPending *reply;
287
288   reply = silc_calloc(1, sizeof(*reply));
289   reply->reply_cmd = reply_cmd;
290   reply->ident = ident;
291   reply->context = context;
292   reply->callback = callback;
293   reply->destructor = destructor;
294   silc_dlist_add(server->pending_commands, reply);
295 }
296
297 /* Deletes pending command by reply command type. */
298
299 void silc_server_command_pending_del(SilcServer server,
300                                      SilcCommand reply_cmd,
301                                      uint16 ident)
302 {
303   SilcServerCommandPending *r;
304
305   silc_dlist_start(server->pending_commands);
306   while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
307     if (r->reply_cmd == reply_cmd && r->ident == ident) {
308       silc_dlist_del(server->pending_commands, r);
309       break;
310     }
311   }
312 }
313
314 /* Checks for pending commands and marks callbacks to be called from
315    the command reply function. Returns TRUE if there were pending command. */
316
317 int silc_server_command_pending_check(SilcServer server,
318                                       SilcServerCommandReplyContext ctx,
319                                       SilcCommand command, 
320                                       uint16 ident)
321 {
322   SilcServerCommandPending *r;
323
324   silc_dlist_start(server->pending_commands);
325   while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
326     if (r->reply_cmd == command && r->ident == ident) {
327       ctx->context = r->context;
328       ctx->callback = r->callback;
329       ctx->destructor = r->destructor;
330       ctx->ident = ident;
331       return TRUE;
332     }
333   }
334
335   return FALSE;
336 }
337
338 /* Destructor function for pending callbacks. This is called when using
339    pending commands to free the context given for the pending command. */
340
341 static void silc_server_command_destructor(void *context)
342 {
343   silc_server_command_free((SilcServerCommandContext)context);
344 }
345
346 /* Sends simple status message as command reply packet */
347
348 static void 
349 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
350                                       SilcCommand command,
351                                       SilcCommandStatus status)
352 {
353   SilcBuffer buffer;
354
355   SILC_LOG_DEBUG(("Sending command status %d", status));
356
357   buffer = 
358     silc_command_reply_payload_encode_va(command, status, 
359                                          silc_command_get_ident(cmd->payload),
360                                          0);
361   silc_server_packet_send(cmd->server, cmd->sock,
362                           SILC_PACKET_COMMAND_REPLY, 0, 
363                           buffer->data, buffer->len, FALSE);
364   silc_buffer_free(buffer);
365 }
366
367 /* Sends command status reply with one extra argument. The argument
368    type must be sent as argument. */
369
370 static void 
371 silc_server_command_send_status_data(SilcServerCommandContext cmd,
372                                      SilcCommand command,
373                                      SilcCommandStatus status,
374                                      uint32 arg_type,
375                                      unsigned char *arg,
376                                      uint32 arg_len)
377 {
378   SilcBuffer buffer;
379
380   SILC_LOG_DEBUG(("Sending command status %d", status));
381
382   buffer = 
383     silc_command_reply_payload_encode_va(command, status, 
384                                          silc_command_get_ident(cmd->payload),
385                                          1, arg_type, arg, arg_len);
386   silc_server_packet_send(cmd->server, cmd->sock,
387                           SILC_PACKET_COMMAND_REPLY, 0, 
388                           buffer->data, buffer->len, FALSE);
389   silc_buffer_free(buffer);
390 }
391
392 /******************************************************************************
393
394                               WHOIS Functions
395
396 ******************************************************************************/
397
398 static int
399 silc_server_command_whois_parse(SilcServerCommandContext cmd,
400                                 SilcClientID ***client_id,
401                                 uint32 *client_id_count,
402                                 char **nickname,
403                                 char **server_name,
404                                 int *count,
405                                 SilcCommand command)
406 {
407   unsigned char *tmp;
408   uint32 len;
409   uint32 argc = silc_argument_get_arg_num(cmd->args);
410   int i, k;
411
412   /* If client ID is in the command it must be used instead of nickname */
413   tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
414   if (!tmp) {
415     /* No ID, get the nickname@server string and parse it. */
416     tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
417     if (tmp) {
418       if (strchr(tmp, '@')) {
419         len = strcspn(tmp, "@");
420         *nickname = silc_calloc(len + 1, sizeof(char));
421         memcpy(*nickname, tmp, len);
422         *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
423         memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
424       } else {
425         *nickname = strdup(tmp);
426       }
427     } else {
428       silc_server_command_send_status_reply(cmd, command,
429                                             SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
430       return FALSE;
431     }
432   } else {
433     /* Command includes ID, we must use that.  Also check whether the command
434        has more than one ID set - take them all. */
435
436     *client_id = silc_calloc(1, sizeof(**client_id));
437     (*client_id)[0] = silc_id_payload_parse_id(tmp, len);
438     if ((*client_id)[0] == NULL) {
439       silc_free(*client_id);
440       return FALSE;
441     }
442     *client_id_count = 1;
443
444     /* Take all ID's from the command packet */
445     if (argc > 1) {
446       for (k = 1, i = 1; i < argc; i++) {
447         tmp = silc_argument_get_arg_type(cmd->args, i + 3, &len);
448         if (tmp) {
449           *client_id = silc_realloc(*client_id, sizeof(**client_id) *
450                                     (*client_id_count + 1));
451           (*client_id)[k] = silc_id_payload_parse_id(tmp, len);
452           if ((*client_id)[k] == NULL) {
453             /* Cleanup all and fail */
454             for (i = 0; i < *client_id_count; i++)
455               silc_free((*client_id)[i]);
456             silc_free(*client_id);
457             return FALSE;
458           }
459           (*client_id_count)++;
460           k++;
461         }
462       }
463     }
464
465     /* Command includes ID, use that */
466   }
467
468   /* Get the max count of reply messages allowed */
469   tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
470   if (tmp)
471     *count = atoi(tmp);
472   else
473     *count = 0;
474
475   return TRUE;
476 }
477
478 static char
479 silc_server_command_whois_check(SilcServerCommandContext cmd,
480                                 SilcClientEntry *clients,
481                                 uint32 clients_count)
482 {
483   SilcServer server = cmd->server;
484   int i;
485   SilcClientEntry entry;
486
487   for (i = 0; i < clients_count; i++) {
488     entry = clients[i];
489
490     if (!entry || entry->data.registered == FALSE)
491       continue;
492
493     if (!entry->nickname || !entry->username || !entry->userinfo) {
494       SilcBuffer tmpbuf;
495       uint16 old_ident;
496
497       if (!entry->router)
498         continue;
499       
500       old_ident = silc_command_get_ident(cmd->payload);
501       silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
502       tmpbuf = silc_command_payload_encode_payload(cmd->payload);
503
504       /* Send WHOIS command */
505       silc_server_packet_send(server, entry->router->connection,
506                               SILC_PACKET_COMMAND, cmd->packet->flags,
507                               tmpbuf->data, tmpbuf->len, TRUE);
508       
509       /* Reprocess this packet after received reply */
510       silc_server_command_pending(server, SILC_COMMAND_WHOIS, 
511                                   silc_command_get_ident(cmd->payload),
512                                   silc_server_command_destructor,
513                                   silc_server_command_whois, 
514                                   silc_server_command_dup(cmd));
515       cmd->pending = TRUE;
516       
517       silc_command_set_ident(cmd->payload, old_ident);
518
519       silc_buffer_free(tmpbuf);
520       return FALSE;
521     }
522   }
523
524   return TRUE;
525 }
526
527 static void
528 silc_server_command_whois_send_reply(SilcServerCommandContext cmd,
529                                      SilcClientEntry *clients,
530                                      uint32 clients_count,
531                                      int count)
532 {
533   SilcServer server = cmd->server;
534   char *tmp;
535   int i, k, len;
536   SilcBuffer packet, idp, channels;
537   SilcClientEntry entry;
538   SilcCommandStatus status;
539   uint16 ident = silc_command_get_ident(cmd->payload);
540   char nh[256], uh[256];
541   unsigned char idle[4], mode[4];
542   SilcSocketConnection hsock;
543
544   len = 0;
545   for (i = 0; i < clients_count; i++)
546     if (clients[i]->data.registered)
547       len++;
548
549   status = SILC_STATUS_OK;
550   if (len > 1)
551     status = SILC_STATUS_LIST_START;
552
553   for (i = 0, k = 0; i < clients_count; i++) {
554     entry = clients[i];
555
556     if (entry->data.registered == FALSE) {
557       if (clients_count == 1) {
558         if (entry->nickname) {
559           silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
560                                                SILC_STATUS_ERR_NO_SUCH_NICK,
561                                                3, entry->nickname, 
562                                                strlen(entry->nickname));
563         } else {
564           SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
565           silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
566                                      SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
567                                                2, idp->data, idp->len);
568           silc_buffer_free(idp);
569         }
570       }
571       continue;
572     }
573
574     if (k >= 1)
575       status = SILC_STATUS_LIST_ITEM;
576
577     if (clients_count > 1 && k == clients_count - 1)
578       status = SILC_STATUS_LIST_END;
579
580     if (count && k - 1 == count)
581       status = SILC_STATUS_LIST_END;
582
583     if (count && k - 1 > count)
584       break;
585
586     /* Sanity check, however these should never fail. However, as
587        this sanity check has been added here they have failed. */
588     if (!entry->nickname || !entry->username || !entry->userinfo)
589       continue;
590       
591     /* Send WHOIS reply */
592     idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
593     tmp = silc_argument_get_first_arg(cmd->args, NULL);
594     
595     memset(uh, 0, sizeof(uh));
596     memset(nh, 0, sizeof(nh));
597     memset(idle, 0, sizeof(idle));
598     
599     strncat(nh, entry->nickname, strlen(entry->nickname));
600     if (!strchr(entry->nickname, '@')) {
601       strncat(nh, "@", 1);
602       if (entry->servername) {
603         strncat(nh, entry->servername, strlen(entry->servername));
604       } else {
605         len = entry->router ? strlen(entry->router->server_name) :
606           strlen(server->server_name);
607         strncat(nh, entry->router ? entry->router->server_name :
608                 server->server_name, len);
609       }
610     }
611       
612     strncat(uh, entry->username, strlen(entry->username));
613     if (!strchr(entry->username, '@')) {
614       strncat(uh, "@", 1);
615       hsock = (SilcSocketConnection)entry->connection;
616       len = strlen(hsock->hostname);
617       strncat(uh, hsock->hostname, len);
618     }
619
620     channels = silc_server_get_client_channel_list(server, entry);
621       
622     SILC_PUT32_MSB(entry->mode, mode);
623
624     if (entry->connection) {
625       SILC_PUT32_MSB((time(NULL) - entry->data.last_receive), idle);
626     }
627
628     if (channels)
629       packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
630                                                     status, ident, 7, 
631                                                     2, idp->data, idp->len,
632                                                     3, nh, strlen(nh),
633                                                     4, uh, strlen(uh),
634                                                     5, entry->userinfo, 
635                                                     strlen(entry->userinfo),
636                                                     6, channels->data,
637                                                     channels->len,
638                                                     7, mode, 4,
639                                                     8, idle, 4);
640     else
641       packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
642                                                     status, ident, 6, 
643                                                     2, idp->data, idp->len,
644                                                     3, nh, strlen(nh),
645                                                     4, uh, strlen(uh),
646                                                     5, entry->userinfo, 
647                                                     strlen(entry->userinfo),
648                                                     7, mode, 4,
649                                                     8, idle, 4);
650     
651     silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
652                             0, packet->data, packet->len, FALSE);
653     
654     silc_buffer_free(packet);
655     silc_buffer_free(idp);
656     if (channels)
657       silc_buffer_free(channels);
658
659     k++;
660   }
661 }
662
663 static int
664 silc_server_command_whois_from_client(SilcServerCommandContext cmd)
665 {
666   SilcServer server = cmd->server;
667   char *nick = NULL, *server_name = NULL;
668   int count = 0;
669   SilcClientEntry *clients = NULL, entry;
670   SilcClientID **client_id = NULL;
671   uint32 client_id_count = 0, clients_count = 0;
672   int i, ret = 0;
673
674   /* Protocol dictates that we must always send the received WHOIS request
675      to our router if we are normal server, so let's do it now unless we
676      are standalone. We will not send any replies to the client until we
677      have received reply from the router. */
678   if (server->server_type == SILC_SERVER && !cmd->pending && 
679       !server->standalone) {
680     SilcBuffer tmpbuf;
681     uint16 old_ident;
682
683     old_ident = silc_command_get_ident(cmd->payload);
684     silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
685     tmpbuf = silc_command_payload_encode_payload(cmd->payload);
686
687     /* Send WHOIS command to our router */
688     silc_server_packet_send(server, (SilcSocketConnection)
689                             server->router->connection,
690                             SILC_PACKET_COMMAND, cmd->packet->flags,
691                             tmpbuf->data, tmpbuf->len, TRUE);
692
693     /* Reprocess this packet after received reply from router */
694     silc_server_command_pending(server, SILC_COMMAND_WHOIS, 
695                                 silc_command_get_ident(cmd->payload),
696                                 silc_server_command_destructor,
697                                 silc_server_command_whois,
698                                 silc_server_command_dup(cmd));
699     cmd->pending = TRUE;
700
701     silc_command_set_ident(cmd->payload, old_ident);
702
703     silc_buffer_free(tmpbuf);
704     ret = -1;
705     goto out;
706   }
707
708   /* We are ready to process the command request. Let's search for the
709      requested client and send reply to the requesting client. */
710
711   /* Parse the whois request */
712   if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count, 
713                                        &nick, &server_name, &count,
714                                        SILC_COMMAND_WHOIS))
715     return 0;
716
717   /* Get all clients matching that ID or nickname from local list */
718   if (client_id_count) {
719     /* Check all Client ID's received in the command packet */
720     for (i = 0; i < client_id_count; i++) {
721       entry = silc_idlist_find_client_by_id(server->local_list, 
722                                             client_id[i], NULL);
723       if (entry) {
724         clients = silc_realloc(clients, sizeof(*clients) * 
725                                (clients_count + 1));
726         clients[clients_count++] = entry;
727       }
728     }
729   } else {
730     if (!silc_idlist_get_clients_by_hash(server->local_list, 
731                                          nick, server->md5hash,
732                                          &clients, &clients_count))
733       silc_idlist_get_clients_by_nickname(server->local_list, 
734                                           nick, server_name,
735                                           &clients, &clients_count);
736   }
737   
738   /* Check global list as well */
739   if (client_id_count) {
740     /* Check all Client ID's received in the command packet */
741     for (i = 0; i < client_id_count; i++) {
742       entry = silc_idlist_find_client_by_id(server->global_list, 
743                                             client_id[i], NULL);
744       if (entry) {
745         clients = silc_realloc(clients, sizeof(*clients) * 
746                                (clients_count + 1));
747         clients[clients_count++] = entry;
748       }
749     }
750   } else {
751     if (!silc_idlist_get_clients_by_hash(server->global_list, 
752                                          nick, server->md5hash,
753                                          &clients, &clients_count))
754       silc_idlist_get_clients_by_nickname(server->global_list, 
755                                           nick, server_name,
756                                           &clients, &clients_count);
757   }
758   
759   if (!clients) {
760     /* Such client(s) really does not exist in the SILC network. */
761     if (!client_id_count) {
762       silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
763                                            SILC_STATUS_ERR_NO_SUCH_NICK,
764                                            3, nick, strlen(nick));
765     } else {
766       SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
767       silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
768                                            SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
769                                            2, idp->data, idp->len);
770       silc_buffer_free(idp);
771     }
772     goto out;
773   }
774
775   /* Router always finds the client entry if it exists in the SILC network.
776      However, it might be incomplete entry and does not include all the
777      mandatory fields that WHOIS command reply requires. Check for these and
778      make query from the server who owns the client if some fields are 
779      missing. */
780   if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
781     ret = -1;
782     goto out;
783   }
784
785   /* Send the command reply to the client */
786   silc_server_command_whois_send_reply(cmd, clients, clients_count,
787                                        count);
788
789  out:
790   if (client_id_count) {
791     for (i = 0; i < client_id_count; i++)
792       silc_free(client_id[i]);
793     silc_free(client_id);
794   }
795   if (clients)
796     silc_free(clients);
797   if (nick)
798     silc_free(nick);
799   if (server_name)
800     silc_free(server_name);
801
802   return ret;
803 }
804
805 static int
806 silc_server_command_whois_from_server(SilcServerCommandContext cmd)
807 {
808   SilcServer server = cmd->server;
809   char *nick = NULL, *server_name = NULL;
810   int count = 0;
811   SilcClientEntry *clients = NULL, entry;
812   SilcClientID **client_id = NULL;
813   uint32 client_id_count = 0, clients_count = 0;
814   int i, ret = 0;
815
816   /* Parse the whois request */
817   if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count, 
818                                        &nick, &server_name, &count,
819                                        SILC_COMMAND_WHOIS))
820     return 0;
821
822   /* Process the command request. Let's search for the requested client and
823      send reply to the requesting server. */
824
825   if (client_id_count) {
826     /* Check all Client ID's received in the command packet */
827     for (i = 0; i < client_id_count; i++) {
828       entry = silc_idlist_find_client_by_id(server->local_list, 
829                                             client_id[i], NULL);
830       if (entry) {
831         clients = silc_realloc(clients, sizeof(*clients) * 
832                                (clients_count + 1));
833         clients[clients_count++] = entry;
834       }
835     }
836   } else {
837     if (!silc_idlist_get_clients_by_hash(server->local_list, 
838                                          nick, server->md5hash,
839                                          &clients, &clients_count))
840       silc_idlist_get_clients_by_nickname(server->local_list, 
841                                           nick, server_name,
842                                           &clients, &clients_count);
843   }
844   
845   /* If we are router we will check our global list as well. */
846   if (server->server_type == SILC_ROUTER) {
847     if (client_id_count) {
848       /* Check all Client ID's received in the command packet */
849       for (i = 0; i < client_id_count; i++) {
850         entry = silc_idlist_find_client_by_id(server->global_list, 
851                                               client_id[i], NULL);
852         if (entry) {
853           clients = silc_realloc(clients, sizeof(*clients) * 
854                                  (clients_count + 1));
855           clients[clients_count++] = entry;
856         }
857       }
858     } else {
859       if (!silc_idlist_get_clients_by_hash(server->global_list, 
860                                            nick, server->md5hash,
861                                            &clients, &clients_count))
862         silc_idlist_get_clients_by_nickname(server->global_list, 
863                                             nick, server_name,
864                                             &clients, &clients_count);
865     }
866   }
867
868   if (!clients) {
869     /* Such a client really does not exist in the SILC network. */
870     if (!client_id_count) {
871       silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
872                                            SILC_STATUS_ERR_NO_SUCH_NICK,
873                                            3, nick, strlen(nick));
874     } else {
875       SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
876       silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
877                                            SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
878                                            2, idp->data, idp->len);
879       silc_buffer_free(idp);
880     }
881     goto out;
882   }
883
884   /* Router always finds the client entry if it exists in the SILC network.
885      However, it might be incomplete entry and does not include all the
886      mandatory fields that WHOIS command reply requires. Check for these and
887      make query from the server who owns the client if some fields are 
888      missing. */
889   if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
890     ret = -1;
891     goto out;
892   }
893
894   /* Send the command reply to the client */
895   silc_server_command_whois_send_reply(cmd, clients, clients_count,
896                                        count);
897
898  out:
899   if (client_id_count) {
900     for (i = 0; i < client_id_count; i++)
901       silc_free(client_id[i]);
902     silc_free(client_id);
903   }
904   if (clients)
905     silc_free(clients);
906   if (nick)
907     silc_free(nick);
908   if (server_name)
909     silc_free(server_name);
910
911   return ret;
912 }
913
914 /* Server side of command WHOIS. Processes user's query and sends found 
915    results as command replies back to the client. */
916
917 SILC_SERVER_CMD_FUNC(whois)
918 {
919   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
920   int ret = 0;
921
922   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_WHOIS, cmd, 1, 3328);
923
924   if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
925     ret = silc_server_command_whois_from_client(cmd);
926   else if ((cmd->sock->type == SILC_SOCKET_TYPE_SERVER) ||
927            (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER))
928     ret = silc_server_command_whois_from_server(cmd);
929
930   if (!ret)
931     silc_server_command_free(cmd);
932 }
933
934 /******************************************************************************
935
936                               WHOWAS Functions
937
938 ******************************************************************************/
939
940 static int
941 silc_server_command_whowas_parse(SilcServerCommandContext cmd,
942                                  char **nickname,
943                                  char **server_name,
944                                  int *count)
945 {
946   unsigned char *tmp;
947   uint32 len;
948
949   tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
950   if (!tmp) {
951     silc_server_command_send_status_reply(cmd, SILC_COMMAND_WHOWAS,
952                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
953     return FALSE;
954   }
955
956   /* Get the nickname@server string and parse it. */
957   if (strchr(tmp, '@')) {
958     len = strcspn(tmp, "@");
959     *nickname = silc_calloc(len + 1, sizeof(char));
960     memcpy(*nickname, tmp, len);
961     *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
962     memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
963   } else {
964     *nickname = strdup(tmp);
965   }
966   /* Get the max count of reply messages allowed */
967   tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
968   if (tmp)
969     *count = atoi(tmp);
970   else
971     *count = 0;
972
973   return TRUE;
974 }
975
976 static char
977 silc_server_command_whowas_check(SilcServerCommandContext cmd,
978                                  SilcClientEntry *clients,
979                                  uint32 clients_count)
980 {
981   SilcServer server = cmd->server;
982   int i;
983   SilcClientEntry entry;
984
985   for (i = 0; i < clients_count; i++) {
986     entry = clients[i];
987
988     if (!entry->nickname || !entry->username) {
989       SilcBuffer tmpbuf;
990       uint16 old_ident;
991
992       if (!entry->router)
993         continue;
994       
995       old_ident = silc_command_get_ident(cmd->payload);
996       silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
997       tmpbuf = silc_command_payload_encode_payload(cmd->payload);
998
999       /* Send WHOWAS command */
1000       silc_server_packet_send(server, entry->router->connection,
1001                               SILC_PACKET_COMMAND, cmd->packet->flags,
1002                               tmpbuf->data, tmpbuf->len, TRUE);
1003       
1004       /* Reprocess this packet after received reply */
1005       silc_server_command_pending(server, SILC_COMMAND_WHOWAS, 
1006                                   silc_command_get_ident(cmd->payload),
1007                                   silc_server_command_destructor,
1008                                   silc_server_command_whowas, 
1009                                   silc_server_command_dup(cmd));
1010       cmd->pending = TRUE;
1011       
1012       silc_command_set_ident(cmd->payload, old_ident);
1013
1014       silc_buffer_free(tmpbuf);
1015       return FALSE;
1016     }
1017   }
1018
1019   return TRUE;
1020 }
1021
1022 static void
1023 silc_server_command_whowas_send_reply(SilcServerCommandContext cmd,
1024                                       SilcClientEntry *clients,
1025                                       uint32 clients_count)
1026 {
1027   SilcServer server = cmd->server;
1028   char *tmp;
1029   int i, count = 0, len;
1030   SilcBuffer packet, idp;
1031   SilcClientEntry entry = NULL;
1032   SilcCommandStatus status;
1033   uint16 ident = silc_command_get_ident(cmd->payload);
1034   char found = FALSE;
1035   char nh[256], uh[256];
1036
1037   status = SILC_STATUS_OK;
1038   if (clients_count > 1)
1039     status = SILC_STATUS_LIST_START;
1040
1041   for (i = 0; i < clients_count; i++) {
1042     entry = clients[i];
1043
1044     /* We will take only clients that are not valid anymore. They are the
1045        ones that are not registered anymore but still have a ID. They
1046        have disconnected us, and thus valid for WHOWAS. */
1047     if (entry->data.registered == TRUE)
1048       continue;
1049     if (entry->id == NULL)
1050       continue;
1051
1052     if (count && i - 1 == count)
1053       break;
1054
1055     found = TRUE;
1056
1057     if (clients_count > 2)
1058       status = SILC_STATUS_LIST_ITEM;
1059
1060     if (clients_count > 1 && i == clients_count - 1)
1061       status = SILC_STATUS_LIST_END;
1062
1063     /* Sanity check, however these should never fail. However, as
1064        this sanity check has been added here they have failed. */
1065     if (!entry->nickname || !entry->username)
1066       continue;
1067       
1068     /* Send WHOWAS reply */
1069     idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1070     tmp = silc_argument_get_first_arg(cmd->args, NULL);
1071     
1072     memset(uh, 0, sizeof(uh));
1073     memset(nh, 0, sizeof(nh));
1074
1075     strncat(nh, entry->nickname, strlen(entry->nickname));
1076     if (!strchr(entry->nickname, '@')) {
1077       strncat(nh, "@", 1);
1078       if (entry->servername) {
1079         strncat(nh, entry->servername, strlen(entry->servername));
1080       } else {
1081         len = entry->router ? strlen(entry->router->server_name) :
1082           strlen(server->server_name);
1083         strncat(nh, entry->router ? entry->router->server_name :
1084                 server->server_name, len);
1085       }
1086     }
1087       
1088     strncat(uh, entry->username, strlen(entry->username));
1089     if (!strchr(entry->username, '@')) {
1090       strncat(uh, "@", 1);
1091       strcat(uh, "*private*");
1092     }
1093       
1094     if (entry->userinfo)
1095       packet = 
1096         silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1097                                              status, ident, 4, 
1098                                              2, idp->data, idp->len,
1099                                              3, nh, strlen(nh),
1100                                              4, uh, strlen(uh),
1101                                              5, entry->userinfo, 
1102                                              strlen(entry->userinfo));
1103     else
1104       packet = 
1105         silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1106                                              status, ident, 3, 
1107                                              2, idp->data, idp->len,
1108                                              3, nh, strlen(nh),
1109                                              4, uh, strlen(uh));
1110
1111     silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1112                             0, packet->data, packet->len, FALSE);
1113     
1114     silc_buffer_free(packet);
1115     silc_buffer_free(idp);
1116   }
1117
1118   if (found == FALSE && entry)
1119     silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1120                                          SILC_STATUS_ERR_NO_SUCH_NICK,
1121                                          3, entry->nickname, 
1122                                          strlen(entry->nickname));
1123 }
1124
1125 static int
1126 silc_server_command_whowas_from_client(SilcServerCommandContext cmd)
1127 {
1128   SilcServer server = cmd->server;
1129   char *nick = NULL, *server_name = NULL;
1130   int count = 0;
1131   SilcClientEntry *clients = NULL;
1132   uint32 clients_count = 0;
1133   int ret = 0;
1134
1135   /* Protocol dictates that we must always send the received WHOWAS request
1136      to our router if we are normal server, so let's do it now unless we
1137      are standalone. We will not send any replies to the client until we
1138      have received reply from the router. */
1139   if (server->server_type == SILC_SERVER && 
1140       !cmd->pending && !server->standalone) {
1141     SilcBuffer tmpbuf;
1142     uint16 old_ident;
1143
1144     old_ident = silc_command_get_ident(cmd->payload);
1145     silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1146     tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1147
1148     /* Send WHOWAS command to our router */
1149     silc_server_packet_send(server, (SilcSocketConnection)
1150                             server->router->connection,
1151                             SILC_PACKET_COMMAND, cmd->packet->flags,
1152                             tmpbuf->data, tmpbuf->len, TRUE);
1153
1154     /* Reprocess this packet after received reply from router */
1155     silc_server_command_pending(server, SILC_COMMAND_WHOWAS, 
1156                                 silc_command_get_ident(cmd->payload),
1157                                 silc_server_command_destructor,
1158                                 silc_server_command_whowas,
1159                                 silc_server_command_dup(cmd));
1160     cmd->pending = TRUE;
1161
1162     silc_command_set_ident(cmd->payload, old_ident);
1163
1164     silc_buffer_free(tmpbuf);
1165     ret = -1;
1166     goto out;
1167   }
1168
1169   /* We are ready to process the command request. Let's search for the
1170      requested client and send reply to the requesting client. */
1171
1172   /* Parse the whowas request */
1173   if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1174     return 0;
1175
1176   /* Get all clients matching that nickname from local list */
1177   if (!silc_idlist_get_clients_by_nickname(server->local_list, 
1178                                            nick, server_name,
1179                                            &clients, &clients_count))
1180     silc_idlist_get_clients_by_hash(server->local_list, 
1181                                     nick, server->md5hash,
1182                                     &clients, &clients_count);
1183   
1184   /* Check global list as well */
1185   if (!silc_idlist_get_clients_by_nickname(server->global_list, 
1186                                            nick, server_name,
1187                                            &clients, &clients_count))
1188     silc_idlist_get_clients_by_hash(server->global_list, 
1189                                     nick, server->md5hash,
1190                                     &clients, &clients_count);
1191   
1192   if (!silc_server_command_whowas_check(cmd, clients, clients_count)) {
1193     ret = -1;
1194     goto out;
1195   }
1196
1197   /* Send the command reply to the client */
1198   silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1199
1200  out:
1201   if (clients)
1202     silc_free(clients);
1203   if (nick)
1204     silc_free(nick);
1205   if (server_name)
1206     silc_free(server_name);
1207
1208   return ret;
1209 }
1210
1211 static int
1212 silc_server_command_whowas_from_server(SilcServerCommandContext cmd)
1213 {
1214   SilcServer server = cmd->server;
1215   char *nick = NULL, *server_name = NULL;
1216   int count = 0;
1217   SilcClientEntry *clients = NULL;
1218   uint32 clients_count = 0;
1219   int ret = 0;
1220
1221   /* Parse the whowas request */
1222   if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1223     return 0;
1224
1225   /* Process the command request. Let's search for the requested client and
1226      send reply to the requesting server. */
1227
1228   if (!silc_idlist_get_clients_by_nickname(server->local_list, 
1229                                            nick, server_name,
1230                                            &clients, &clients_count))
1231     silc_idlist_get_clients_by_hash(server->local_list, 
1232                                     nick, server->md5hash,
1233                                     &clients, &clients_count);
1234   
1235   /* If we are router we will check our global list as well. */
1236   if (server->server_type == SILC_ROUTER) {
1237     if (!silc_idlist_get_clients_by_nickname(server->global_list, 
1238                                              nick, server_name,
1239                                              &clients, &clients_count))
1240       silc_idlist_get_clients_by_hash(server->global_list, 
1241                                       nick, server->md5hash,
1242                                       &clients, &clients_count);
1243   }
1244
1245   if (!clients) {
1246     /* Such a client really does not exist in the SILC network. */
1247     silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1248                                          SILC_STATUS_ERR_NO_SUCH_NICK,
1249                                          3, nick, strlen(nick));
1250     goto out;
1251   }
1252
1253   /* Send the command reply to the client */
1254   silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1255
1256  out:
1257   if (clients)
1258     silc_free(clients);
1259   if (nick)
1260     silc_free(nick);
1261   if (server_name)
1262     silc_free(server_name);
1263
1264   return ret;
1265 }
1266
1267 /* Server side of command WHOWAS. */
1268
1269 SILC_SERVER_CMD_FUNC(whowas)
1270 {
1271   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1272   int ret = 0;
1273
1274   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_WHOWAS, cmd, 1, 2);
1275
1276   if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1277     ret = silc_server_command_whowas_from_client(cmd);
1278   else if ((cmd->sock->type == SILC_SOCKET_TYPE_SERVER) ||
1279            (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER))
1280     ret = silc_server_command_whowas_from_server(cmd);
1281
1282   if (!ret)
1283     silc_server_command_free(cmd);
1284 }
1285
1286 /******************************************************************************
1287
1288                               IDENTIFY Functions
1289
1290 ******************************************************************************/
1291
1292 /* Checks that all mandatory fields are present. If not then send WHOIS 
1293    request to the server who owns the client. We use WHOIS because we want
1294    to get as much information as possible at once. */
1295
1296 static char
1297 silc_server_command_identify_check(SilcServerCommandContext cmd,
1298                                    SilcClientEntry *clients,
1299                                    uint32 clients_count)
1300 {
1301   SilcServer server = cmd->server;
1302   int i;
1303   SilcClientEntry entry;
1304
1305   for (i = 0; i < clients_count; i++) {
1306     entry = clients[i];
1307
1308     if (!entry || entry->data.registered == FALSE)
1309       continue;
1310
1311     if (!entry->nickname) {
1312       SilcBuffer tmpbuf;
1313       uint16 old_ident;
1314       
1315       if (!entry->router)
1316         continue;
1317       
1318       old_ident = silc_command_get_ident(cmd->payload);
1319       silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1320       silc_command_set_command(cmd->payload, SILC_COMMAND_WHOIS);
1321       tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1322       
1323       /* Send WHOIS request. We send WHOIS since we're doing the requesting
1324          now anyway so make it a good one. */
1325       silc_server_packet_send(server, entry->router->connection,
1326                               SILC_PACKET_COMMAND, cmd->packet->flags,
1327                               tmpbuf->data, tmpbuf->len, TRUE);
1328       
1329       /* Reprocess this packet after received reply */
1330       silc_server_command_pending(server, SILC_COMMAND_WHOIS, 
1331                                   silc_command_get_ident(cmd->payload),
1332                                   silc_server_command_destructor,
1333                                   silc_server_command_identify,
1334                                   silc_server_command_dup(cmd));
1335
1336       cmd->pending = TRUE;
1337       
1338       /* Put old data back to the Command Payload we just changed */
1339       silc_command_set_ident(cmd->payload, old_ident);
1340       silc_command_set_command(cmd->payload, SILC_COMMAND_IDENTIFY);
1341
1342       silc_buffer_free(tmpbuf);
1343       return FALSE;
1344     }
1345   }
1346
1347   return TRUE;
1348 }
1349
1350 static void
1351 silc_server_command_identify_send_reply(SilcServerCommandContext cmd,
1352                                         SilcClientEntry *clients,
1353                                         uint32 clients_count,
1354                                         int count)
1355 {
1356   SilcServer server = cmd->server;
1357   char *tmp;
1358   int i, k, len;
1359   SilcBuffer packet, idp;
1360   SilcClientEntry entry;
1361   SilcCommandStatus status;
1362   uint16 ident = silc_command_get_ident(cmd->payload);
1363   char nh[256], uh[256];
1364   SilcSocketConnection hsock;
1365
1366   len = 0;
1367   for (i = 0; i < clients_count; i++)
1368     if (clients[i]->data.registered)
1369       len++;
1370
1371   status = SILC_STATUS_OK;
1372   if (len > 1)
1373     status = SILC_STATUS_LIST_START;
1374
1375   for (i = 0, k = 0; i < clients_count; i++) {
1376     entry = clients[i];
1377
1378     if (entry->data.registered == FALSE) {
1379       if (clients_count == 1) {
1380         SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1381         silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1382                                              SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1383                                              2, idp->data, idp->len);
1384         silc_buffer_free(idp);
1385       }
1386       continue;
1387     }
1388
1389     if (k >= 1)
1390       status = SILC_STATUS_LIST_ITEM;
1391
1392     if (clients_count > 1 && k == clients_count - 1)
1393       status = SILC_STATUS_LIST_END;
1394
1395     if (count && k - 1 == count)
1396       status = SILC_STATUS_LIST_END;
1397
1398     if (count && k - 1 > count)
1399       break;
1400
1401     /* Send IDENTIFY reply */
1402     idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1403     tmp = silc_argument_get_first_arg(cmd->args, NULL);
1404     
1405     memset(uh, 0, sizeof(uh));
1406     memset(nh, 0, sizeof(nh));
1407       
1408     strncat(nh, entry->nickname, strlen(entry->nickname));
1409     if (!strchr(entry->nickname, '@')) {
1410       strncat(nh, "@", 1);
1411       if (entry->servername) {
1412         strncat(nh, entry->servername, strlen(entry->servername));
1413       } else {
1414         len = entry->router ? strlen(entry->router->server_name) :
1415           strlen(server->server_name);
1416         strncat(nh, entry->router ? entry->router->server_name :
1417                 server->server_name, len);
1418       }
1419     }
1420       
1421     if (!entry->username) {
1422       packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1423                                                     status, ident, 2,
1424                                                     2, idp->data, idp->len, 
1425                                                     3, nh, strlen(nh));
1426     } else {
1427       strncat(uh, entry->username, strlen(entry->username));
1428       if (!strchr(entry->username, '@')) {
1429         strncat(uh, "@", 1);
1430         hsock = (SilcSocketConnection)entry->connection;
1431         len = strlen(hsock->hostname);
1432         strncat(uh, hsock->hostname, len);
1433       }
1434       
1435       packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1436                                                     status, ident, 3,
1437                                                     2, idp->data, idp->len, 
1438                                                     3, nh, strlen(nh),
1439                                                     4, uh, strlen(uh));
1440     }
1441       
1442     silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1443                             0, packet->data, packet->len, FALSE);
1444     
1445     silc_buffer_free(packet);
1446     silc_buffer_free(idp);
1447
1448     k++;
1449   }
1450 }
1451
1452 static int
1453 silc_server_command_identify_from_client(SilcServerCommandContext cmd)
1454 {
1455   SilcServer server = cmd->server;
1456   char *nick = NULL, *server_name = NULL;
1457   int count = 0;
1458   SilcClientEntry *clients = NULL, entry;
1459   SilcClientID **client_id = NULL;
1460   uint32 client_id_count = 0, clients_count = 0;
1461   int i, ret = 0;
1462
1463   /* Protocol dictates that we must always send the received IDENTIFY request
1464      to our router if we are normal server, so let's do it now unless we
1465      are standalone. We will not send any replies to the client until we
1466      have received reply from the router. */
1467   if (server->server_type == SILC_SERVER && 
1468       !cmd->pending && !server->standalone) {
1469     SilcBuffer tmpbuf;
1470     uint16 old_ident;
1471
1472     old_ident = silc_command_get_ident(cmd->payload);
1473     silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1474     tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1475
1476     /* Send IDENTIFY command to our router */
1477     silc_server_packet_send(server, (SilcSocketConnection)
1478                             server->router->connection,
1479                             SILC_PACKET_COMMAND, cmd->packet->flags,
1480                             tmpbuf->data, tmpbuf->len, TRUE);
1481
1482     /* Reprocess this packet after received reply from router */
1483     silc_server_command_pending(server, SILC_COMMAND_IDENTIFY, 
1484                                 silc_command_get_ident(cmd->payload),
1485                                 silc_server_command_destructor,
1486                                 silc_server_command_identify,
1487                                 silc_server_command_dup(cmd));
1488     cmd->pending = TRUE;
1489
1490     silc_command_set_ident(cmd->payload, old_ident);
1491
1492     silc_buffer_free(tmpbuf);
1493     ret = -1;
1494     goto out;
1495   }
1496
1497   /* We are ready to process the command request. Let's search for the
1498      requested client and send reply to the requesting client. */
1499
1500   /* Parse the IDENTIFY request */
1501   if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
1502                                        &nick, &server_name, &count,
1503                                        SILC_COMMAND_IDENTIFY))
1504     return 0;
1505
1506   /* Get all clients matching that ID or nickname from local list */
1507   if (client_id_count) { 
1508     /* Check all Client ID's received in the command packet */
1509     for (i = 0; i < client_id_count; i++) {
1510       entry = silc_idlist_find_client_by_id(server->local_list, 
1511                                             client_id[i], NULL);
1512       if (entry) {
1513         clients = silc_realloc(clients, sizeof(*clients) * 
1514                                (clients_count + 1));
1515         clients[clients_count++] = entry;
1516       }
1517     }
1518   } else {
1519     if (!silc_idlist_get_clients_by_hash(server->local_list, 
1520                                          nick, server->md5hash,
1521                                          &clients, &clients_count))
1522       silc_idlist_get_clients_by_nickname(server->local_list, 
1523                                           nick, server_name,
1524                                           &clients, &clients_count);
1525   }
1526   
1527   /* Check global list as well */
1528   if (client_id_count) {
1529     /* Check all Client ID's received in the command packet */
1530     for (i = 0; i < client_id_count; i++) {
1531       entry = silc_idlist_find_client_by_id(server->global_list, 
1532                                             client_id[i], NULL);
1533       if (entry) {
1534         clients = silc_realloc(clients, sizeof(*clients) * 
1535                                (clients_count + 1));
1536         clients[clients_count++] = entry;
1537       }
1538     }
1539   } else {
1540     if (!silc_idlist_get_clients_by_hash(server->global_list, 
1541                                          nick, server->md5hash,
1542                                          &clients, &clients_count))
1543       silc_idlist_get_clients_by_nickname(server->global_list, 
1544                                           nick, server_name,
1545                                           &clients, &clients_count);
1546   }
1547   
1548   if (!clients) {
1549     /* Such a client really does not exist in the SILC network. */
1550     if (!client_id_count) {
1551       silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1552                                            SILC_STATUS_ERR_NO_SUCH_NICK,
1553                                            3, nick, strlen(nick));
1554     } else {
1555       SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
1556       silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1557                                            SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1558                                            2, idp->data, idp->len);
1559       silc_buffer_free(idp);
1560     }
1561     goto out;
1562   }
1563
1564   /* Check that all mandatory fields are present and request those data
1565      from the server who owns the client if necessary. */
1566   if (!silc_server_command_identify_check(cmd, clients, clients_count)) {
1567     ret = -1;
1568     goto out;
1569   }
1570
1571   /* Send the command reply to the client */
1572   silc_server_command_identify_send_reply(cmd, clients, clients_count,
1573                                           count);
1574
1575  out:
1576   if (client_id_count) {
1577     for (i = 0; i < client_id_count; i++)
1578       silc_free(client_id[i]);
1579     silc_free(client_id);
1580   }
1581   if (clients)
1582     silc_free(clients);
1583   if (nick)
1584     silc_free(nick);
1585   if (server_name)
1586     silc_free(server_name);
1587
1588   return ret;
1589 }
1590
1591 static int
1592 silc_server_command_identify_from_server(SilcServerCommandContext cmd)
1593 {
1594   SilcServer server = cmd->server;
1595   char *nick = NULL, *server_name = NULL;
1596   int count = 0;
1597   SilcClientEntry *clients = NULL, entry;
1598   SilcClientID **client_id = NULL;
1599   uint32 client_id_count = 0, clients_count = 0;
1600   int i, ret = 0;
1601
1602   /* Parse the IDENTIFY request */
1603   if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
1604                                        &nick, &server_name, &count,
1605                                        SILC_COMMAND_IDENTIFY))
1606     return 0;
1607
1608   /* Process the command request. Let's search for the requested client and
1609      send reply to the requesting server. */
1610
1611   if (client_id_count) {
1612     /* Check all Client ID's received in the command packet */
1613     for (i = 0; i < client_id_count; i++) {
1614       entry = silc_idlist_find_client_by_id(server->local_list, 
1615                                             client_id[i], NULL);
1616       if (entry) {
1617         clients = silc_realloc(clients, sizeof(*clients) * 
1618                                (clients_count + 1));
1619         clients[clients_count++] = entry;
1620       }
1621     }
1622   } else {
1623     if (!silc_idlist_get_clients_by_hash(server->local_list, 
1624                                          nick, server->md5hash,
1625                                          &clients, &clients_count))
1626       silc_idlist_get_clients_by_nickname(server->local_list, 
1627                                           nick, server_name,
1628                                           &clients, &clients_count);
1629   }
1630   
1631   /* If we are router we will check our global list as well. */
1632   if (server->server_type == SILC_ROUTER) {
1633     if (client_id_count) {
1634       /* Check all Client ID's received in the command packet */
1635       for (i = 0; i < client_id_count; i++) {
1636         entry = silc_idlist_find_client_by_id(server->global_list, 
1637                                               client_id[i], NULL);
1638         if (entry) {
1639           clients = silc_realloc(clients, sizeof(*clients) * 
1640                                  (clients_count + 1));
1641           clients[clients_count++] = entry;
1642         }
1643       }
1644     } else {
1645       if (!silc_idlist_get_clients_by_hash(server->global_list, 
1646                                            nick, server->md5hash,
1647                                            &clients, &clients_count))
1648         silc_idlist_get_clients_by_nickname(server->global_list, 
1649                                             nick, server_name,
1650                                             &clients, &clients_count);
1651     }
1652   }
1653
1654   if (!clients) {
1655     /* Such a client really does not exist in the SILC network. */
1656     if (!client_id_count) {
1657       silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1658                                            SILC_STATUS_ERR_NO_SUCH_NICK,
1659                                            3, nick, strlen(nick));
1660     } else {
1661       SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
1662       silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1663                                            SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1664                                            2, idp->data, idp->len);
1665       silc_buffer_free(idp);
1666     }
1667     goto out;
1668   }
1669
1670   /* Check that all mandatory fields are present and request those data
1671      from the server who owns the client if necessary. */
1672   if (!silc_server_command_identify_check(cmd, clients, clients_count)) {
1673     ret = -1;
1674     goto out;
1675   }
1676
1677   /* Send the command reply */
1678   silc_server_command_identify_send_reply(cmd, clients, clients_count, count);
1679
1680  out:
1681   if (client_id_count) {
1682     for (i = 0; i < client_id_count; i++)
1683       silc_free(client_id[i]);
1684     silc_free(client_id);
1685   }
1686   if (clients)
1687     silc_free(clients);
1688   if (nick)
1689     silc_free(nick);
1690   if (server_name)
1691     silc_free(server_name);
1692
1693   return ret;
1694 }
1695
1696 SILC_SERVER_CMD_FUNC(identify)
1697 {
1698   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1699   int ret = 0;
1700
1701   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_IDENTIFY, cmd, 1, 3328);
1702
1703   if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1704     ret = silc_server_command_identify_from_client(cmd);
1705   else if ((cmd->sock->type == SILC_SOCKET_TYPE_SERVER) |
1706            (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER))
1707     ret = silc_server_command_identify_from_server(cmd);
1708
1709   if (!ret)
1710     silc_server_command_free(cmd);
1711 }
1712
1713 /* Checks string for bad characters and returns TRUE if they are found. */
1714
1715 static int silc_server_command_bad_chars(char *nick)
1716 {
1717   if (strchr(nick, '\\')) return TRUE;
1718   if (strchr(nick, '\"')) return TRUE;
1719   if (strchr(nick, '´')) return TRUE;
1720   if (strchr(nick, '`')) return TRUE;
1721   if (strchr(nick, '\'')) return TRUE;
1722   if (strchr(nick, '*')) return TRUE;
1723   if (strchr(nick, '/')) return TRUE;
1724   if (strchr(nick, '@')) return TRUE;
1725
1726   return FALSE;
1727 }
1728
1729 /* Server side of command NICK. Sets nickname for user. Setting
1730    nickname causes generation of a new client ID for the client. The
1731    new client ID is sent to the client after changing the nickname. */
1732
1733 SILC_SERVER_CMD_FUNC(nick)
1734 {
1735   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1736   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1737   SilcServer server = cmd->server;
1738   SilcBuffer packet, nidp, oidp;
1739   SilcClientID *new_id;
1740   char *nick;
1741   uint16 ident = silc_command_get_ident(cmd->payload);
1742
1743   if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1744     goto out;
1745
1746   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_NICK, cmd, 1, 1);
1747
1748   /* Check nickname */
1749   nick = silc_argument_get_arg_type(cmd->args, 1, NULL);
1750   if (silc_server_command_bad_chars(nick) == TRUE) {
1751     silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
1752                                           SILC_STATUS_ERR_BAD_NICKNAME);
1753     goto out;
1754   }
1755
1756   if (strlen(nick) > 128)
1757     nick[127] = '\0';
1758
1759   /* Create new Client ID */
1760   silc_id_create_client_id(cmd->server->id, cmd->server->rng, 
1761                            cmd->server->md5hash, nick,
1762                            &new_id);
1763
1764   /* Send notify about nickname change to our router. We send the new
1765      ID and ask to replace it with the old one. If we are router the
1766      packet is broadcasted. Send NICK_CHANGE notify. */
1767   if (!server->standalone)
1768     silc_server_send_notify_nick_change(server, server->router->connection, 
1769                                         server->server_type == SILC_SERVER ? 
1770                                         FALSE : TRUE, client->id,
1771                                         new_id);
1772
1773   /* Remove old cache entry */
1774   silc_idcache_del_by_context(server->local_list->clients, client);
1775
1776   oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1777
1778   /* Free old ID */
1779   if (client->id)
1780     silc_free(client->id);
1781
1782   /* Save the nickname as this client is our local client */
1783   if (client->nickname)
1784     silc_free(client->nickname);
1785
1786   client->nickname = strdup(nick);
1787   client->id = new_id;
1788
1789   /* Update client cache */
1790   silc_idcache_add(server->local_list->clients, client->nickname, 
1791                    client->id, (void *)client, FALSE);
1792
1793   nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1794
1795   /* Send NICK_CHANGE notify to the client's channels */
1796   silc_server_send_notify_on_channels(server, NULL, client, 
1797                                       SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
1798                                       oidp->data, oidp->len, 
1799                                       nidp->data, nidp->len);
1800
1801   /* Send the new Client ID as reply command back to client */
1802   packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK, 
1803                                                 SILC_STATUS_OK, ident, 1, 
1804                                                 2, nidp->data, nidp->len);
1805   silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1806                           0, packet->data, packet->len, FALSE);
1807
1808   silc_buffer_free(packet);
1809   silc_buffer_free(nidp);
1810   silc_buffer_free(oidp);
1811   
1812  out:
1813   silc_server_command_free(cmd);
1814 }
1815
1816 /* Sends the LIST command reply */
1817
1818 static void
1819 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
1820                                     SilcChannelEntry *lch, 
1821                                     uint32 lch_count,
1822                                     SilcChannelEntry *gch,
1823                                     uint32 gch_count)
1824 {
1825   int i;
1826   SilcBuffer packet, idp;
1827   SilcChannelEntry entry;
1828   SilcCommandStatus status;
1829   uint16 ident = silc_command_get_ident(cmd->payload);
1830   char *topic;
1831   unsigned char usercount[4];
1832   uint32 users;
1833
1834   for (i = 0; i < lch_count; i++)
1835     if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1836       lch[i] = NULL;
1837   for (i = 0; i < gch_count; i++)
1838     if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1839       gch[i] = NULL;
1840
1841   status = SILC_STATUS_OK;
1842   if ((lch_count + gch_count) > 1)
1843     status = SILC_STATUS_LIST_START;
1844
1845   /* Local list */
1846   for (i = 0; i < lch_count; i++) {
1847     entry = lch[i];
1848
1849     if (!entry)
1850       continue;
1851
1852     if (i >= 1)
1853       status = SILC_STATUS_LIST_ITEM;
1854
1855     if (i == lch_count - 1 && gch_count)
1856       break;
1857     if (lch_count > 1 && i == lch_count - 1)
1858       status = SILC_STATUS_LIST_END;
1859
1860     idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1861
1862     if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1863       topic = "*private*";
1864       memset(usercount, 0, sizeof(usercount));
1865     } else {
1866       topic = entry->topic;
1867       users = silc_hash_table_count(entry->user_list);
1868       SILC_PUT32_MSB(users, usercount);
1869     }
1870
1871     /* Send the reply */
1872     if (topic)
1873       packet = 
1874         silc_command_reply_payload_encode_va(SILC_COMMAND_LIST, 
1875                                              status, ident, 4, 
1876                                              2, idp->data, idp->len,
1877                                              3, entry->channel_name, 
1878                                              strlen(entry->channel_name),
1879                                              4, topic, strlen(topic),
1880                                              5, usercount, 4);
1881     else
1882       packet = 
1883         silc_command_reply_payload_encode_va(SILC_COMMAND_LIST, 
1884                                              status, ident, 3, 
1885                                              2, idp->data, idp->len,
1886                                              3, entry->channel_name, 
1887                                              strlen(entry->channel_name),
1888                                              5, usercount, 4);
1889     silc_server_packet_send(cmd->server, cmd->sock, 
1890                             SILC_PACKET_COMMAND_REPLY, 0, packet->data, 
1891                             packet->len, FALSE);
1892     silc_buffer_free(packet);
1893     silc_buffer_free(idp);
1894   }
1895
1896   status = i ? SILC_STATUS_LIST_ITEM : SILC_STATUS_OK;
1897
1898   /* Global list */
1899   for (i = 0; i < gch_count; i++) {
1900     entry = gch[i];
1901
1902     if (!entry)
1903       continue;
1904
1905     if (i >= 1)
1906       status = SILC_STATUS_LIST_ITEM;
1907
1908     if (gch_count > 1 && i == lch_count - 1)
1909       status = SILC_STATUS_LIST_END;
1910
1911     idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1912
1913     if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1914       topic = "*private*";
1915       memset(usercount, 0, sizeof(usercount));
1916     } else {
1917       topic = entry->topic;
1918       users = silc_hash_table_count(entry->user_list);
1919       SILC_PUT32_MSB(users, usercount);
1920     }
1921
1922     /* Send the reply */
1923     if (topic)
1924       packet = 
1925         silc_command_reply_payload_encode_va(SILC_COMMAND_LIST, 
1926                                              status, ident, 4, 
1927                                              2, idp->data, idp->len,
1928                                              3, entry->channel_name, 
1929                                              strlen(entry->channel_name),
1930                                              4, topic, strlen(topic),
1931                                              5, usercount, 4);
1932     else
1933       packet = 
1934         silc_command_reply_payload_encode_va(SILC_COMMAND_LIST, 
1935                                              status, ident, 3, 
1936                                              2, idp->data, idp->len,
1937                                              3, entry->channel_name, 
1938                                              strlen(entry->channel_name),
1939                                              5, usercount, 4);
1940     silc_server_packet_send(cmd->server, cmd->sock, 
1941                             SILC_PACKET_COMMAND_REPLY, 0, packet->data, 
1942                             packet->len, FALSE);
1943     silc_buffer_free(packet);
1944     silc_buffer_free(idp);
1945   }
1946 }
1947
1948 /* Server side of LIST command. This lists the channel of the requested
1949    server. Secret channels are not listed. */
1950
1951 SILC_SERVER_CMD_FUNC(list)
1952 {
1953   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1954   SilcServer server = cmd->server;
1955   SilcChannelID *channel_id = NULL;
1956   unsigned char *tmp;
1957   uint32 tmp_len;
1958   SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
1959   uint32 lch_count = 0, gch_count = 0;
1960
1961   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_LIST, cmd, 0, 2);
1962
1963   /* Get Channel ID */
1964   tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1965   if (tmp) {
1966     channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1967     if (!channel_id) {
1968       silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
1969                                             SILC_STATUS_ERR_NO_CHANNEL_ID);
1970       goto out;
1971     }
1972   }
1973
1974   /* Get the channels from local list */
1975   lchannels = silc_idlist_get_channels(server->local_list, channel_id,
1976                                        &lch_count);
1977   
1978   /* Get the channels from global list if we are router */
1979   if (server->server_type == SILC_ROUTER) 
1980     gchannels = silc_idlist_get_channels(server->global_list, channel_id,
1981                                          &gch_count);
1982
1983   /* Send the reply */
1984   silc_server_command_list_send_reply(cmd, lchannels, lch_count, 
1985                                       gchannels, gch_count);
1986
1987  out:
1988   silc_server_command_free(cmd);
1989 }
1990
1991 /* Server side of TOPIC command. Sets topic for channel and/or returns
1992    current topic to client. */
1993
1994 SILC_SERVER_CMD_FUNC(topic)
1995 {
1996   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1997   SilcServer server = cmd->server;
1998   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1999   SilcChannelID *channel_id;
2000   SilcChannelEntry channel;
2001   SilcChannelClientEntry chl;
2002   SilcBuffer packet, idp;
2003   unsigned char *tmp;
2004   uint32 argc, tmp_len;
2005   uint16 ident = silc_command_get_ident(cmd->payload);
2006
2007   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_TOPIC, cmd, 1, 2);
2008
2009   argc = silc_argument_get_arg_num(cmd->args);
2010
2011   /* Get Channel ID */
2012   tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2013   if (!tmp) {
2014     silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2015                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
2016     goto out;
2017   }
2018   channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2019   if (!channel_id) {
2020     silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2021                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
2022     goto out;
2023   }
2024
2025   /* Check whether the channel exists */
2026   channel = silc_idlist_find_channel_by_id(server->local_list, 
2027                                            channel_id, NULL);
2028   if (!channel) {
2029     channel = silc_idlist_find_channel_by_id(server->global_list, 
2030                                              channel_id, NULL);
2031     if (!channel) {
2032       silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2033                                             SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2034       goto out;
2035     }
2036   }
2037
2038   if (argc > 1) {
2039     /* Get the topic */
2040     tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
2041     if (!tmp) {
2042       silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2043                                             SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2044       goto out;
2045     }
2046
2047     if (strlen(tmp) > 256) {
2048       silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2049                                             SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2050       goto out;
2051     }
2052
2053     /* See whether the client is on channel and has rights to change topic */
2054     if (!silc_hash_table_find(channel->user_list, client, NULL, 
2055                               (void *)&chl)) {
2056       silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2057                                             SILC_STATUS_ERR_NOT_ON_CHANNEL);
2058       goto out;
2059     }
2060
2061     if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2062       if (channel->mode & SILC_CHANNEL_MODE_TOPIC) {
2063         silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2064                                               SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2065         goto out;
2066       }
2067     }
2068
2069     /* Set the topic for channel */
2070     if (channel->topic)
2071       silc_free(channel->topic);
2072     channel->topic = strdup(tmp);
2073
2074     /* Send TOPIC_SET notify type to the network */
2075     if (!server->standalone)
2076       silc_server_send_notify_topic_set(server, server->router->connection,
2077                                         server->server_type == SILC_ROUTER ?
2078                                         TRUE : FALSE, channel, client->id,
2079                                         channel->topic);
2080
2081     idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2082
2083     /* Send notify about topic change to all clients on the channel */
2084     silc_server_send_notify_to_channel(server, NULL, channel, TRUE,
2085                                        SILC_NOTIFY_TYPE_TOPIC_SET, 2,
2086                                        idp->data, idp->len,
2087                                        channel->topic, strlen(channel->topic));
2088     silc_buffer_free(idp);
2089   }
2090
2091   /* Send the topic to client as reply packet */
2092   idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2093   if (channel->topic)
2094     packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC, 
2095                                                   SILC_STATUS_OK, ident, 2, 
2096                                                   2, idp->data, idp->len,
2097                                                   3, channel->topic, 
2098                                                   strlen(channel->topic));
2099   else
2100     packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC, 
2101                                                   SILC_STATUS_OK, ident, 1, 
2102                                                   2, idp->data, idp->len);
2103   silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
2104                           0, packet->data, packet->len, FALSE);
2105
2106   silc_buffer_free(packet);
2107   silc_buffer_free(idp);
2108   silc_free(channel_id);
2109
2110  out:
2111   silc_server_command_free(cmd);
2112 }
2113
2114 /* Server side of INVITE command. Invites some client to join some channel. 
2115    This command is also used to manage the invite list of the channel. */
2116
2117 SILC_SERVER_CMD_FUNC(invite)
2118 {
2119   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2120   SilcServer server = cmd->server;
2121   SilcSocketConnection sock = cmd->sock, dest_sock;
2122   SilcChannelClientEntry chl;
2123   SilcClientEntry sender, dest;
2124   SilcClientID *dest_id = NULL;
2125   SilcChannelEntry channel;
2126   SilcChannelID *channel_id = NULL;
2127   SilcIDListData idata;
2128   SilcBuffer idp, idp2, packet;
2129   unsigned char *tmp, *add, *del;
2130   uint32 len;
2131   uint16 ident = silc_command_get_ident(cmd->payload);
2132
2133   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_INVITE, cmd, 1, 4);
2134
2135   /* Get Channel ID */
2136   tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2137   if (!tmp) {
2138     silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2139                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
2140     goto out;
2141   }
2142   channel_id = silc_id_payload_parse_id(tmp, len);
2143   if (!channel_id) {
2144     silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2145                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
2146     goto out;
2147   }
2148
2149   /* Get the channel entry */
2150   channel = silc_idlist_find_channel_by_id(server->local_list, 
2151                                            channel_id, NULL);
2152   if (!channel) {
2153     channel = silc_idlist_find_channel_by_id(server->global_list, 
2154                                              channel_id, NULL);
2155     if (!channel) {
2156       silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2157                                             SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2158       goto out;
2159     }
2160   }
2161
2162   /* Check whether the sender of this command is on the channel. */
2163   sender = (SilcClientEntry)sock->user_data;
2164   if (!silc_server_client_on_channel(sender, channel)) {
2165     silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2166                                           SILC_STATUS_ERR_NOT_ON_CHANNEL);
2167     goto out;
2168   }
2169
2170   /* Check whether the channel is invite-only channel. If yes then the
2171      sender of this command must be at least channel operator. */
2172   if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2173     silc_hash_table_find(channel->user_list, sender, NULL, (void *)&chl);
2174     if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2175       silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2176                                             SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2177       goto out;
2178     }
2179   }
2180
2181   /* Get destination client ID */
2182   tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
2183   if (tmp) {
2184     char invite[512];
2185
2186     dest_id = silc_id_payload_parse_id(tmp, len);
2187     if (!dest_id) {
2188       silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2189                                             SILC_STATUS_ERR_NO_CLIENT_ID);
2190       goto out;
2191     }
2192
2193     /* Get the client entry */
2194     dest = silc_server_get_client_resolve(server, dest_id);
2195     if (!dest) {
2196       if (server->server_type == SILC_ROUTER) {
2197         silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2198                                      SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2199         goto out;
2200       }
2201       
2202       /* The client info is being resolved. Reprocess this packet after
2203          receiving the reply to the query. */
2204       silc_server_command_pending(server, SILC_COMMAND_WHOIS, 
2205                                   server->cmd_ident,
2206                                   silc_server_command_destructor,
2207                                   silc_server_command_invite, 
2208                                   silc_server_command_dup(cmd));
2209       cmd->pending = TRUE;
2210       silc_free(channel_id);
2211       silc_free(dest_id);
2212       return;
2213     }
2214
2215     /* Check whether the requested client is already on the channel. */
2216     if (silc_server_client_on_channel(dest, channel)) {
2217       silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2218                                             SILC_STATUS_ERR_USER_ON_CHANNEL);
2219       goto out;
2220     }
2221     
2222     /* Get route to the client */
2223     dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id, &idata);
2224     if (!dest_sock) {
2225       silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2226                                             SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2227       goto out;
2228     }
2229
2230     memset(invite, 0, sizeof(invite));
2231     strncat(invite, dest->nickname, strlen(dest->nickname));
2232     strncat(invite, "!", 1);
2233     strncat(invite, dest->username, strlen(dest->username));
2234     if (!strchr(dest->username, '@')) {
2235       strncat(invite, "@", 1);
2236       strncat(invite, cmd->sock->hostname, strlen(cmd->sock->hostname));
2237     }
2238
2239     len = strlen(invite);
2240     if (!channel->invite_list)
2241       channel->invite_list = silc_calloc(len + 2, 
2242                                          sizeof(*channel->invite_list));
2243     else
2244       channel->invite_list = silc_realloc(channel->invite_list, 
2245                                           sizeof(*channel->invite_list) * 
2246                                           (len + 
2247                                            strlen(channel->invite_list) + 2));
2248     strncat(channel->invite_list, invite, len);
2249     strncat(channel->invite_list, ",", 1);
2250
2251     /* Send notify to the client that is invited to the channel */
2252     idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2253     idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
2254     silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id, 
2255                                  SILC_ID_CLIENT,
2256                                  SILC_NOTIFY_TYPE_INVITE, 3, 
2257                                  idp->data, idp->len, 
2258                                  channel->channel_name, 
2259                                  strlen(channel->channel_name),
2260                                  idp2->data, idp2->len);
2261     silc_buffer_free(idp);
2262     silc_buffer_free(idp2);
2263   }
2264
2265   /* Add the client to the invite list of the channel */
2266   add = silc_argument_get_arg_type(cmd->args, 3, &len);
2267   if (add) {
2268     if (!channel->invite_list)
2269       channel->invite_list = silc_calloc(len + 2, 
2270                                          sizeof(*channel->invite_list));
2271     else
2272       channel->invite_list = silc_realloc(channel->invite_list, 
2273                                           sizeof(*channel->invite_list) * 
2274                                           (len + 
2275                                            strlen(channel->invite_list) + 2));
2276     if (add[len - 1] == ',')
2277       add[len - 1] = '\0';
2278     
2279     strncat(channel->invite_list, add, len);
2280     strncat(channel->invite_list, ",", 1);
2281   }
2282
2283   /* Get the invite to be removed and remove it from the list */
2284   del = silc_argument_get_arg_type(cmd->args, 4, &len);
2285   if (del && channel->invite_list) {
2286     char *start, *end, *n;
2287
2288     if (!strncmp(channel->invite_list, del, 
2289                  strlen(channel->invite_list) - 1)) {
2290       silc_free(channel->invite_list);
2291       channel->invite_list = NULL;
2292     } else {
2293       start = strstr(channel->invite_list, del);
2294       if (start && strlen(start) >= len) {
2295         end = start + len;
2296         n = silc_calloc(strlen(channel->invite_list) - len, sizeof(*n));
2297         strncat(n, channel->invite_list, start - channel->invite_list);
2298         strncat(n, end + 1, ((channel->invite_list + 
2299                               strlen(channel->invite_list)) - end) - 1);
2300         silc_free(channel->invite_list);
2301         channel->invite_list = n;
2302       }
2303     }
2304   }
2305
2306   /* Send notify to the primary router */
2307   if (!server->standalone)
2308     silc_server_send_notify_invite(server, server->router->connection,
2309                                    server->server_type == SILC_ROUTER ?
2310                                    TRUE : FALSE, channel,
2311                                    sender->id, add, del);
2312
2313   /* Send command reply */
2314   tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2315
2316   if (add || del)
2317     packet = 
2318       silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2319                                            SILC_STATUS_OK, ident, 2,
2320                                            2, tmp, len,
2321                                            3, channel->invite_list,
2322                                            channel->invite_list ?
2323                                            strlen(channel->invite_list) : 0);
2324   else
2325     packet = 
2326       silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2327                                            SILC_STATUS_OK, ident, 1,
2328                                            2, tmp, len);
2329   silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0, 
2330                           packet->data, packet->len, FALSE);
2331   silc_buffer_free(packet);
2332
2333  out:
2334   if (dest_id)
2335     silc_free(dest_id);
2336   if (channel_id)
2337     silc_free(channel_id);
2338   silc_server_command_free(cmd);
2339 }
2340
2341 typedef struct {
2342   SilcServer server;
2343   SilcSocketConnection sock;
2344   char *signoff;
2345 } *QuitInternal;
2346
2347 /* Quits connection to client. This gets called if client won't
2348    close the connection even when it has issued QUIT command. */
2349
2350 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
2351 {
2352   QuitInternal q = (QuitInternal)context;
2353
2354   /* Free all client specific data, such as client entry and entires
2355      on channels this client may be on. */
2356   silc_server_free_client_data(q->server, q->sock, q->sock->user_data,
2357                                TRUE, q->signoff);
2358   q->sock->user_data = NULL;
2359
2360   /* Close the connection on our side */
2361   silc_server_close_connection(q->server, q->sock);
2362
2363   silc_free(q->signoff);
2364   silc_free(q);
2365 }
2366
2367 /* Quits SILC session. This is the normal way to disconnect client. */
2368  
2369 SILC_SERVER_CMD_FUNC(quit)
2370 {
2371   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2372   SilcServer server = cmd->server;
2373   SilcSocketConnection sock = cmd->sock;
2374   QuitInternal q;
2375   unsigned char *tmp = NULL;
2376   uint32 len = 0;
2377
2378   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_QUIT, cmd, 0, 1);
2379
2380   if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2381     goto out;
2382
2383   /* Get destination ID */
2384   tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2385   if (len > 128)
2386     tmp = NULL;
2387
2388   q = silc_calloc(1, sizeof(*q));
2389   q->server = server;
2390   q->sock = sock;
2391   q->signoff = tmp ? strdup(tmp) : NULL;
2392
2393   /* We quit the connection with little timeout */
2394   silc_task_register(server->timeout_queue, sock->sock,
2395                      silc_server_command_quit_cb, (void *)q,
2396                      0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2397
2398  out:
2399   silc_server_command_free(cmd);
2400 }
2401
2402 /* Server side of command KILL. This command is used by router operator
2403    to remove an client from the SILC Network temporarily. */
2404
2405 SILC_SERVER_CMD_FUNC(kill)
2406 {
2407   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2408   SilcServer server = cmd->server;
2409   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2410   SilcClientEntry remote_client;
2411   SilcClientID *client_id;
2412   unsigned char *tmp, *comment;
2413   uint32 tmp_len, tmp_len2;
2414
2415   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_KILL, cmd, 1, 2);
2416
2417   if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2418     goto out;
2419
2420   /* KILL command works only on router */
2421   if (server->server_type != SILC_ROUTER) {
2422     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2423                                           SILC_STATUS_ERR_NO_ROUTER_PRIV);
2424     goto out;
2425   }
2426
2427   /* Check whether client has the permissions. */
2428   if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
2429     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2430                                           SILC_STATUS_ERR_NO_ROUTER_PRIV);
2431     goto out;
2432   }
2433
2434   /* Get the client ID */
2435   tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2436   if (!tmp) {
2437     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2438                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2439     goto out;
2440   }
2441   client_id = silc_id_payload_parse_id(tmp, tmp_len);
2442   if (!client_id) {
2443     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2444                                           SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2445     goto out;
2446   }
2447
2448   /* Get the client entry */
2449   remote_client = silc_idlist_find_client_by_id(server->local_list, 
2450                                                 client_id, NULL);
2451   if (!remote_client) {
2452     remote_client = silc_idlist_find_client_by_id(server->global_list, 
2453                                                   client_id, NULL);
2454     if (!remote_client) {
2455       silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2456                                             SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2457       goto out;
2458     }
2459   }
2460
2461   if (remote_client->data.registered == FALSE) {
2462     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2463                                           SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2464     goto out;
2465   }
2466
2467   /* Get comment */
2468   comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
2469   if (tmp_len2 > 128)
2470     comment = NULL;
2471
2472   /* Send reply to the sender */
2473   silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2474                                         SILC_STATUS_OK);
2475
2476   /* Send the KILL notify packets. First send it to the channel, then
2477      to our primary router and then directly to the client who is being
2478      killed right now. */
2479
2480   /* Send KILLED notify to the channels. It is not sent to the client
2481      as it will be sent differently destined directly to the client and not
2482      to the channel. */
2483   silc_server_send_notify_on_channels(server, remote_client, 
2484                                       remote_client, SILC_NOTIFY_TYPE_KILLED,
2485                                       comment ? 2 : 1,
2486                                       tmp, tmp_len,
2487                                       comment, comment ? tmp_len2 : 0);
2488
2489   /* Send KILLED notify to primary route */
2490   if (!server->standalone)
2491     silc_server_send_notify_killed(server, server->router->connection, TRUE,
2492                                    remote_client->id, comment);
2493
2494   /* Send KILLED notify to the client directly */
2495   silc_server_send_notify_killed(server, remote_client->connection ? 
2496                                  remote_client->connection : 
2497                                  remote_client->router->connection, FALSE,
2498                                  remote_client->id, comment);
2499
2500   /* Remove the client from all channels. This generates new keys to the
2501      channels as well. */
2502   silc_server_remove_from_channels(server, NULL, remote_client, FALSE, 
2503                                    NULL, TRUE);
2504
2505   /* Remove the client entry, If it is locally connected then we will also
2506      disconnect the client here */
2507   if (remote_client->data.registered && remote_client->connection) {
2508     /* Remove locally conneted client */
2509     SilcSocketConnection sock = remote_client->connection;
2510     silc_server_free_client_data(server, sock, remote_client, FALSE, NULL);
2511     silc_server_close_connection(server, sock);
2512   } else {
2513     /* Remove remote client */
2514     if (!silc_idlist_del_client(server->global_list, remote_client))
2515       silc_idlist_del_client(server->local_list, remote_client);
2516   }
2517
2518  out:
2519   silc_server_command_free(cmd);
2520 }
2521
2522 /* Server side of command INFO. This sends information about us to 
2523    the client. If client requested specific server we will send the 
2524    command to that server. */
2525
2526 SILC_SERVER_CMD_FUNC(info)
2527 {
2528   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2529   SilcServer server = cmd->server;
2530   SilcBuffer packet, idp;
2531   unsigned char *tmp;
2532   uint32 tmp_len;
2533   char *dest_server, *server_info = NULL, *server_name;
2534   uint16 ident = silc_command_get_ident(cmd->payload);
2535   SilcServerEntry entry = NULL;
2536   SilcServerID *server_id = NULL;
2537
2538   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_INFO, cmd, 0, 2);
2539
2540   /* Get server name */
2541   dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2542
2543   /* Get Server ID */
2544   tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2545   if (tmp) {
2546     server_id = silc_id_payload_parse_id(tmp, tmp_len);
2547     if (!server_id) {
2548       silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2549                                             SILC_STATUS_ERR_NO_SERVER_ID);
2550       goto out;
2551     }
2552   }
2553
2554   if (server_id) {
2555     /* Check whether we have this server cached */
2556     entry = silc_idlist_find_server_by_id(server->local_list,
2557                                           server_id, NULL);
2558     if (!entry) {
2559       entry = silc_idlist_find_server_by_id(server->global_list,
2560                                             server_id, NULL);
2561       if (!entry && server->server_type == SILC_ROUTER) {
2562         silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2563                                               SILC_STATUS_ERR_NO_SUCH_SERVER);
2564         goto out;
2565       }
2566     }
2567   }
2568
2569   if ((!dest_server && !server_id) || 
2570       (dest_server && !cmd->pending && 
2571        !strncasecmp(dest_server, server->server_name, strlen(dest_server)))) {
2572     /* Send our reply */
2573     char info_string[256];
2574
2575     memset(info_string, 0, sizeof(info_string));
2576     snprintf(info_string, sizeof(info_string), 
2577              "location: %s server: %s admin: %s <%s>",
2578              server->config->admin_info->location,
2579              server->config->admin_info->server_type,
2580              server->config->admin_info->admin_name,
2581              server->config->admin_info->admin_email);
2582
2583     server_info = info_string;
2584     entry = server->id_entry;
2585   } else {
2586     /* Check whether we have this server cached */
2587     if (!entry && dest_server) {
2588       entry = silc_idlist_find_server_by_name(server->global_list,
2589                                               dest_server, NULL);
2590       if (!entry) {
2591         entry = silc_idlist_find_server_by_name(server->local_list,
2592                                                 dest_server, NULL);
2593       }
2594     }
2595
2596     if (!cmd->pending &&
2597         server->server_type == SILC_ROUTER && entry && !entry->server_info) {
2598       /* Send to the server */
2599       SilcBuffer tmpbuf;
2600       uint16 old_ident;
2601
2602       old_ident = silc_command_get_ident(cmd->payload);
2603       silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2604       tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2605
2606       silc_server_packet_send(server, entry->connection,
2607                               SILC_PACKET_COMMAND, cmd->packet->flags,
2608                               tmpbuf->data, tmpbuf->len, TRUE);
2609
2610       /* Reprocess this packet after received reply from router */
2611       silc_server_command_pending(server, SILC_COMMAND_INFO, 
2612                                   silc_command_get_ident(cmd->payload),
2613                                   silc_server_command_destructor,
2614                                   silc_server_command_info,
2615                                   silc_server_command_dup(cmd));
2616       cmd->pending = TRUE;
2617       silc_command_set_ident(cmd->payload, old_ident);
2618       silc_buffer_free(tmpbuf);
2619       return;
2620     }
2621
2622     if (!entry && !cmd->pending && !server->standalone) {
2623       /* Send to the primary router */
2624       SilcBuffer tmpbuf;
2625       uint16 old_ident;
2626
2627       old_ident = silc_command_get_ident(cmd->payload);
2628       silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2629       tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2630
2631       silc_server_packet_send(server, server->router->connection,
2632                               SILC_PACKET_COMMAND, cmd->packet->flags,
2633                               tmpbuf->data, tmpbuf->len, TRUE);
2634
2635       /* Reprocess this packet after received reply from router */
2636       silc_server_command_pending(server, SILC_COMMAND_INFO, 
2637                                   silc_command_get_ident(cmd->payload),
2638                                   silc_server_command_destructor,
2639                                   silc_server_command_info,
2640                                   silc_server_command_dup(cmd));
2641       cmd->pending = TRUE;
2642       silc_command_set_ident(cmd->payload, old_ident);
2643       silc_buffer_free(tmpbuf);
2644       return;
2645     }
2646   }
2647
2648   if (server_id)
2649     silc_free(server_id);
2650
2651   if (!entry) {
2652     silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2653                                           SILC_STATUS_ERR_NO_SUCH_SERVER);
2654     goto out;
2655   }
2656
2657   idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2658   if (!server_info)
2659     server_info = entry->server_info;
2660   server_name = entry->server_name;
2661
2662   /* Send the reply */
2663   if (server_info)
2664     packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2665                                                   SILC_STATUS_OK, ident, 3,
2666                                                   2, idp->data, idp->len,
2667                                                   3, server_name, 
2668                                                   strlen(server_name),
2669                                                   4, server_info, 
2670                                                   strlen(server_info));
2671   else
2672     packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2673                                                   SILC_STATUS_OK, ident, 2,
2674                                                   2, idp->data, idp->len,
2675                                                   3, server_name, 
2676                                                   strlen(server_name));
2677   silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0, 
2678                           packet->data, packet->len, FALSE);
2679     
2680   silc_buffer_free(packet);
2681   silc_buffer_free(idp);
2682
2683  out:
2684   silc_server_command_free(cmd);
2685 }
2686
2687 /* Server side of command PING. This just replies to the ping. */
2688
2689 SILC_SERVER_CMD_FUNC(ping)
2690 {
2691   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2692   SilcServer server = cmd->server;
2693   SilcServerID *id;
2694   uint32 len;
2695   unsigned char *tmp;
2696
2697   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_INFO, cmd, 1, 2);
2698
2699   /* Get Server ID */
2700   tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2701   if (!tmp) {
2702     silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2703                                           SILC_STATUS_ERR_NO_SERVER_ID);
2704     goto out;
2705   }
2706   id = silc_id_str2id(tmp, len, SILC_ID_SERVER);
2707   if (!id)
2708     goto out;
2709
2710   if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2711     /* Send our reply */
2712     silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2713                                           SILC_STATUS_OK);
2714   } else {
2715     silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2716                                           SILC_STATUS_ERR_NO_SUCH_SERVER);
2717     goto out;
2718   }
2719
2720   silc_free(id);
2721
2722  out:
2723   silc_server_command_free(cmd);
2724 }
2725
2726 /* Internal routine to join channel. The channel sent to this function
2727    has been either created or resolved from ID lists. This joins the sent
2728    client to the channel. */
2729
2730 static void silc_server_command_join_channel(SilcServer server, 
2731                                              SilcServerCommandContext cmd,
2732                                              SilcChannelEntry channel,
2733                                              SilcClientID *client_id,
2734                                              int created,
2735                                              uint32 umode)
2736 {
2737   SilcSocketConnection sock = cmd->sock;
2738   unsigned char *tmp;
2739   uint32 tmp_len, user_count;
2740   unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
2741   SilcClientEntry client;
2742   SilcChannelClientEntry chl;
2743   SilcBuffer reply, chidp, clidp, keyp = NULL, user_list, mode_list;
2744   uint16 ident = silc_command_get_ident(cmd->payload);
2745   char check[512];
2746
2747   SILC_LOG_DEBUG(("Start"));
2748
2749   if (!channel)
2750     return;
2751
2752   /* Get the client entry */
2753   if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2754     client = (SilcClientEntry)sock->user_data;
2755   } else {
2756     client = silc_idlist_find_client_by_id(server->local_list, client_id, 
2757                                            NULL);
2758     if (!client)
2759       goto out;
2760   }
2761
2762   /*
2763    * Check channel modes
2764    */
2765
2766   if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2767     strncat(check, client->nickname, strlen(client->nickname));
2768     if (!strchr(client->nickname, '@')) {
2769       strncat(check, "@", 1);
2770       strncat(check, server->server_name, strlen(server->server_name));
2771     }
2772     strncat(check, "!", 1);
2773     strncat(check, client->username, strlen(client->username));
2774     if (!strchr(client->username, '@')) {
2775       strncat(check, "@", 1);
2776       strncat(check, cmd->sock->hostname, strlen(cmd->sock->hostname));
2777     }
2778   }
2779
2780   /* Check invite list if channel is invite-only channel */
2781   if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT && 
2782       channel->mode & SILC_CHANNEL_MODE_INVITE) {
2783     if (!channel->invite_list) {
2784       silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2785                                             SILC_STATUS_ERR_NOT_INVITED);
2786       goto out;
2787     }
2788
2789     if (!silc_string_match(channel->invite_list, check)) {
2790       silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2791                                             SILC_STATUS_ERR_NOT_INVITED);
2792       goto out;
2793     }
2794   }
2795
2796   /* Check ban list if it exists. If the client's nickname, server,
2797      username and/or hostname is in the ban list the access to the
2798      channel is denied. */
2799   if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT && channel->ban_list) {
2800     if (silc_string_match(channel->ban_list, check)) {
2801       silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2802                               SILC_STATUS_ERR_BANNED_FROM_CHANNEL);
2803       goto out;
2804     }
2805   }
2806
2807   /* Get passphrase */
2808   tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
2809   if (tmp) {
2810     passphrase = silc_calloc(tmp_len, sizeof(*passphrase));
2811     memcpy(passphrase, tmp, tmp_len);
2812   }
2813   
2814   /* Check the channel passphrase if set. */
2815   if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2816     if (!passphrase || memcmp(channel->passphrase, passphrase,
2817                               strlen(channel->passphrase))) {
2818       silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2819                                             SILC_STATUS_ERR_BAD_PASSWORD);
2820       goto out;
2821     }
2822   }
2823
2824   /* Check user count limit if set. */
2825   if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
2826     if (silc_hash_table_count(channel->user_list) + 1 > 
2827         channel->user_limit) {
2828       silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2829                                             SILC_STATUS_ERR_CHANNEL_IS_FULL);
2830       goto out;
2831     }
2832   }
2833
2834   /*
2835    * Client is allowed to join to the channel. Make it happen.
2836    */
2837
2838   /* Check whether the client already is on the channel */
2839   if (silc_server_client_on_channel(client, channel)) {
2840     silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2841                                           SILC_STATUS_ERR_USER_ON_CHANNEL);
2842     goto out;
2843   }
2844
2845   /* Generate new channel key as protocol dictates */
2846   if ((!created && silc_hash_table_count(channel->user_list) > 0) || 
2847       !channel->channel_key)
2848     silc_server_create_channel_key(server, channel, 0);
2849
2850   /* Send the channel key. This is broadcasted to the channel but is not
2851      sent to the client who is joining to the channel. */
2852   if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
2853     silc_server_send_channel_key(server, NULL, channel, 
2854                                  server->server_type == SILC_ROUTER ? 
2855                                  FALSE : !server->standalone);
2856
2857   /* Join the client to the channel by adding it to channel's user list.
2858      Add also the channel to client entry's channels list for fast cross-
2859      referencing. */
2860   chl = silc_calloc(1, sizeof(*chl));
2861   chl->mode = umode;
2862   chl->client = client;
2863   chl->channel = channel;
2864   silc_hash_table_add(channel->user_list, client, chl);
2865   silc_hash_table_add(client->channels, channel, chl);
2866
2867   /* Get users on the channel */
2868   silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2869                                    &user_count);
2870
2871   /* Encode Client ID Payload of the original client who wants to join */
2872   clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2873
2874   /* Encode command reply packet */
2875   chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2876   SILC_PUT32_MSB(channel->mode, mode);
2877   SILC_PUT32_MSB(created, tmp2);
2878   SILC_PUT32_MSB(user_count, tmp3);
2879
2880   if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2881     tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2882     keyp = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, tmp, 
2883                                            strlen(channel->channel_key->
2884                                                   cipher->name),
2885                                            channel->channel_key->cipher->name,
2886                                            channel->key_len / 8, channel->key);
2887     silc_free(tmp);
2888   }
2889
2890   reply = 
2891     silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2892                                          SILC_STATUS_OK, ident, 13,
2893                                          2, channel->channel_name,
2894                                          strlen(channel->channel_name),
2895                                          3, chidp->data, chidp->len,
2896                                          4, clidp->data, clidp->len,
2897                                          5, mode, 4,
2898                                          6, tmp2, 4,
2899                                          7, keyp ? keyp->data : NULL, 
2900                                          keyp ? keyp->len : 0,
2901                                          8, channel->ban_list, 
2902                                          channel->ban_list ?
2903                                          strlen(channel->ban_list) : 0,
2904                                          9, channel->invite_list,
2905                                          channel->invite_list ?
2906                                          strlen(channel->invite_list) : 0,
2907                                          10, channel->topic,
2908                                          channel->topic ?
2909                                          strlen(channel->topic) : 0,
2910                                          11, channel->hmac->hmac->name,
2911                                          strlen(channel->hmac->hmac->name),
2912                                          12, tmp3, 4,
2913                                          13, user_list->data, user_list->len,
2914                                          14, mode_list->data, 
2915                                          mode_list->len);
2916
2917   /* Send command reply */
2918   silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0, 
2919                           reply->data, reply->len, FALSE);
2920
2921   if (!cmd->pending) {
2922     /* Send JOIN notify to locally connected clients on the channel */
2923     silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2924                                        SILC_NOTIFY_TYPE_JOIN, 2,
2925                                        clidp->data, clidp->len,
2926                                        chidp->data, chidp->len);
2927
2928     /* Send JOIN notify packet to our primary router */
2929     if (!server->standalone)
2930       silc_server_send_notify_join(server, server->router->connection,
2931                                    server->server_type == SILC_ROUTER ?
2932                                    TRUE : FALSE, channel, client->id);
2933   }
2934
2935   silc_buffer_free(reply);
2936   silc_buffer_free(clidp);
2937   silc_buffer_free(chidp);
2938   silc_buffer_free(keyp);
2939   silc_buffer_free(user_list);
2940   silc_buffer_free(mode_list);
2941
2942  out:
2943   if (passphrase)
2944     silc_free(passphrase);
2945 }
2946
2947 /* Server side of command JOIN. Joins client into requested channel. If 
2948    the channel does not exist it will be created. */
2949
2950 SILC_SERVER_CMD_FUNC(join)
2951 {
2952   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2953   SilcServer server = cmd->server;
2954   uint32 tmp_len;
2955   char *tmp, *channel_name = NULL, *cipher, *hmac;
2956   SilcChannelEntry channel;
2957   uint32 umode = 0;
2958   int created = FALSE;
2959   SilcClientID *client_id;
2960
2961   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_JOIN, cmd, 1, 4);
2962
2963   /* Get channel name */
2964   tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2965   if (!tmp) {
2966     silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2967                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2968     goto out;
2969   }
2970   channel_name = tmp;
2971
2972   if (strlen(channel_name) > 256)
2973     channel_name[255] = '\0';
2974
2975   if (silc_server_command_bad_chars(channel_name) == TRUE) {
2976     silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2977                                           SILC_STATUS_ERR_BAD_CHANNEL);
2978     silc_free(channel_name);
2979     goto out;
2980   }
2981
2982   /* Get Client ID of the client who is joining to the channel */
2983   tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2984   if (!tmp) {
2985     silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2986                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2987     goto out;
2988   }
2989   client_id = silc_id_payload_parse_id(tmp, tmp_len);
2990   if (!client_id) {
2991     silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2992                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2993     goto out;
2994   }
2995
2996   /* Get cipher and hmac name */
2997   cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
2998   hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
2999
3000   /* See if the channel exists */
3001   channel = silc_idlist_find_channel_by_name(server->local_list, 
3002                                              channel_name, NULL);
3003
3004   if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3005     /* If this is coming from client the Client ID in the command packet must
3006        be same as the client's ID. */
3007     if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3008       SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
3009       if (!SILC_ID_CLIENT_COMPARE(entry->id, client_id)) {
3010         silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3011                                         SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3012         goto out;
3013       }
3014     }
3015
3016     if (!channel || !channel->id) {
3017       /* Channel not found */
3018
3019       /* If we are standalone server we don't have a router, we just create 
3020          the channel by ourselves. */
3021       if (server->standalone) {
3022         channel = silc_server_create_new_channel(server, server->id, cipher, 
3023                                                  hmac, channel_name, TRUE);
3024         if (!channel) {
3025           silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3026                                      SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3027           goto out;
3028         }
3029
3030         umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3031         created = TRUE;
3032
3033       } else {
3034
3035         /* The channel does not exist on our server. If we are normal server 
3036            we will send JOIN command to our router which will handle the
3037            joining procedure (either creates the channel if it doesn't exist 
3038            or joins the client to it). */
3039         if (server->server_type == SILC_SERVER) {
3040           SilcBuffer tmpbuf;
3041           uint16 old_ident;
3042           
3043           old_ident = silc_command_get_ident(cmd->payload);
3044           silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3045           tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3046           
3047           /* Send JOIN command to our router */
3048           silc_server_packet_send(server, (SilcSocketConnection)
3049                                   server->router->connection,
3050                                   SILC_PACKET_COMMAND, cmd->packet->flags,
3051                                   tmpbuf->data, tmpbuf->len, TRUE);
3052           
3053           /* Reprocess this packet after received reply from router */
3054           silc_server_command_pending(server, SILC_COMMAND_JOIN, 
3055                                       silc_command_get_ident(cmd->payload),
3056                                       silc_server_command_destructor,
3057                                       silc_server_command_join,
3058                                       silc_server_command_dup(cmd));
3059           cmd->pending = TRUE;
3060           return;
3061         }
3062         
3063         /* We are router and the channel does not seem exist so we will check
3064            our global list as well for the channel. */
3065         channel = silc_idlist_find_channel_by_name(server->global_list, 
3066                                                    channel_name, NULL);
3067         if (!channel) {
3068           /* Channel really does not exist, create it */
3069           channel = silc_server_create_new_channel(server, server->id, cipher, 
3070                                                    hmac, channel_name, TRUE);
3071           if (!channel) {
3072             silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3073                                        SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3074             goto out;
3075           }
3076
3077           umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3078           created = TRUE;
3079         }
3080       }
3081     }
3082   } else {
3083     if (!channel) {
3084       /* Channel not found */
3085
3086       /* If the command came from router and/or we are normal server then
3087          something went wrong with the joining as the channel was not found.
3088          We can't do anything else but ignore this. */
3089       if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
3090           server->server_type == SILC_SERVER)
3091         goto out;
3092       
3093       /* We are router and the channel does not seem exist so we will check
3094          our global list as well for the channel. */
3095       channel = silc_idlist_find_channel_by_name(server->global_list, 
3096                                                  channel_name, NULL);
3097       if (!channel) {
3098         /* Channel really does not exist, create it */
3099         channel = silc_server_create_new_channel(server, server->id, cipher, 
3100                                                  hmac, channel_name, TRUE);
3101         if (!channel) {
3102           silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3103                                        SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3104           goto out;
3105         }
3106
3107         umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3108         created = TRUE;
3109       }
3110     }
3111   }
3112
3113   /* If the channel does not have global users and is also empty it means the
3114      channel was created globally (by our router) and the client will be the
3115      channel founder and operator. */
3116   if (!channel->global_users && !silc_hash_table_count(channel->user_list)) {
3117     umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3118     created = TRUE;             /* Created globally by our router */
3119   }
3120
3121   /* Join to the channel */
3122   silc_server_command_join_channel(server, cmd, channel, client_id,
3123                                    created, umode);
3124
3125   silc_free(client_id);
3126
3127  out:
3128   silc_server_command_free(cmd);
3129 }
3130
3131 /* Server side of command MOTD. Sends server's current "message of the
3132    day" to the client. */
3133
3134 SILC_SERVER_CMD_FUNC(motd)
3135 {
3136   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3137   SilcServer server = cmd->server;
3138   SilcBuffer packet, idp;
3139   char *motd, *dest_server;
3140   uint32 motd_len;
3141   uint16 ident = silc_command_get_ident(cmd->payload);
3142   
3143   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_MOTD, cmd, 1, 1);
3144
3145   /* Get server name */
3146   dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
3147   if (!dest_server) {
3148     silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
3149                                           SILC_STATUS_ERR_NO_SUCH_SERVER);
3150     goto out;
3151   }
3152
3153   if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
3154     /* Send our MOTD */
3155
3156     idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3157
3158     if (server->config && server->config->motd && 
3159         server->config->motd->motd_file) {
3160       /* Send motd */
3161       motd = silc_file_read(server->config->motd->motd_file, &motd_len);
3162       if (!motd)
3163         goto out;
3164       
3165       motd[motd_len] = 0;
3166       packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3167                                                     SILC_STATUS_OK, ident, 2,
3168                                                     2, idp, idp->len,
3169                                                     3, motd, motd_len);
3170       goto out;
3171     } else {
3172       /* No motd */
3173       packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3174                                                     SILC_STATUS_OK, ident, 1,
3175                                                     2, idp, idp->len);
3176     }
3177
3178     silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0, 
3179                             packet->data, packet->len, FALSE);
3180     silc_buffer_free(packet);
3181     silc_buffer_free(idp);
3182   } else {
3183     SilcServerEntry entry;
3184
3185     /* Check whether we have this server cached */
3186     entry = silc_idlist_find_server_by_name(server->global_list,
3187                                             dest_server, NULL);
3188     if (!entry) {
3189       entry = silc_idlist_find_server_by_name(server->local_list,
3190                                               dest_server, NULL);
3191     }
3192
3193     if (server->server_type == SILC_ROUTER && !cmd->pending && 
3194         entry && !entry->motd) {
3195       /* Send to the server */
3196       SilcBuffer tmpbuf;
3197       uint16 old_ident;
3198
3199       old_ident = silc_command_get_ident(cmd->payload);
3200       silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3201       tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3202
3203       silc_server_packet_send(server, entry->connection,
3204                               SILC_PACKET_COMMAND, cmd->packet->flags,
3205                               tmpbuf->data, tmpbuf->len, TRUE);
3206
3207       /* Reprocess this packet after received reply from router */
3208       silc_server_command_pending(server, SILC_COMMAND_MOTD, 
3209                                   silc_command_get_ident(cmd->payload),
3210                                   silc_server_command_destructor,
3211                                   silc_server_command_motd,
3212                                   silc_server_command_dup(cmd));
3213       cmd->pending = TRUE;
3214       silc_command_set_ident(cmd->payload, old_ident);
3215       silc_buffer_free(tmpbuf);
3216       return;
3217     }
3218
3219     if (!entry && !cmd->pending && !server->standalone) {
3220       /* Send to the primary router */
3221       SilcBuffer tmpbuf;
3222       uint16 old_ident;
3223
3224       old_ident = silc_command_get_ident(cmd->payload);
3225       silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3226       tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3227
3228       silc_server_packet_send(server, server->router->connection,
3229                               SILC_PACKET_COMMAND, cmd->packet->flags,
3230                               tmpbuf->data, tmpbuf->len, TRUE);
3231
3232       /* Reprocess this packet after received reply from router */
3233       silc_server_command_pending(server, SILC_COMMAND_MOTD, 
3234                                   silc_command_get_ident(cmd->payload),
3235                                   silc_server_command_destructor,
3236                                   silc_server_command_motd,
3237                                   silc_server_command_dup(cmd));
3238       cmd->pending = TRUE;
3239       silc_command_set_ident(cmd->payload, old_ident);
3240       silc_buffer_free(tmpbuf);
3241       return;
3242     }
3243
3244     if (!entry) {
3245       silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
3246                                             SILC_STATUS_ERR_NO_SUCH_SERVER);
3247       goto out;
3248     }
3249
3250     idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3251
3252     if (entry->motd)
3253       packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3254                                                     SILC_STATUS_OK, ident, 2,
3255                                                     2, idp, idp->len,
3256                                                     3, entry->motd,
3257                                                     strlen(entry->motd));
3258     else
3259       packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3260                                                     SILC_STATUS_OK, ident, 1,
3261                                                     2, idp, idp->len);
3262
3263     silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0, 
3264                             packet->data, packet->len, FALSE);
3265     silc_buffer_free(packet);
3266     silc_buffer_free(idp);
3267   }
3268
3269  out:
3270   silc_server_command_free(cmd);
3271 }
3272
3273 /* Server side of command UMODE. Client can use this command to set/unset
3274    user mode. Client actually cannot set itself to be as server/router
3275    operator so this can be used only to unset the modes. */
3276
3277 SILC_SERVER_CMD_FUNC(umode)
3278 {
3279   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3280   SilcServer server = cmd->server;
3281   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3282   SilcBuffer packet;
3283   unsigned char *tmp_mask;
3284   uint32 mask;
3285   uint16 ident = silc_command_get_ident(cmd->payload);
3286
3287   if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
3288     goto out;
3289
3290   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_UMODE, cmd, 2, 2);
3291
3292   /* Get the client's mode mask */
3293   tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3294   if (!tmp_mask) {
3295     silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3296                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3297     goto out;
3298   }
3299   SILC_GET32_MSB(mask, tmp_mask);
3300
3301   /* 
3302    * Change the mode 
3303    */
3304
3305   if (mask & SILC_UMODE_SERVER_OPERATOR) {
3306     if (!(client->mode & SILC_UMODE_SERVER_OPERATOR)) {
3307       /* Cannot operator mode */
3308       silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3309                                             SILC_STATUS_ERR_PERM_DENIED);
3310       goto out;
3311     }
3312   } else {
3313     if (client->mode & SILC_UMODE_SERVER_OPERATOR)
3314       /* Remove the server operator rights */
3315       client->mode &= ~SILC_UMODE_SERVER_OPERATOR;
3316   }
3317
3318   if (mask & SILC_UMODE_ROUTER_OPERATOR) {
3319     if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
3320       /* Cannot operator mode */
3321       silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3322                                             SILC_STATUS_ERR_PERM_DENIED);
3323       goto out;
3324     }
3325   } else {
3326     if (client->mode & SILC_UMODE_ROUTER_OPERATOR)
3327       /* Remove the router operator rights */
3328       client->mode &= ~SILC_UMODE_ROUTER_OPERATOR;
3329   }
3330
3331   if (mask & SILC_UMODE_GONE) {
3332     client->mode |= SILC_UMODE_GONE;
3333   } else {
3334     if (client->mode & SILC_UMODE_GONE)
3335       /* Remove the gone status */
3336       client->mode &= ~SILC_UMODE_GONE;
3337   }
3338
3339   /* Send UMODE change to primary router */
3340   if (!server->standalone)
3341     silc_server_send_notify_umode(server, server->router->connection, TRUE,
3342                                   client->id, client->mode);
3343
3344   /* Send command reply to sender */
3345   packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
3346                                                 SILC_STATUS_OK, ident, 1,
3347                                                 2, tmp_mask, 4);
3348   silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0, 
3349                           packet->data, packet->len, FALSE);
3350   silc_buffer_free(packet);
3351
3352  out:
3353   silc_server_command_free(cmd);
3354 }
3355
3356 /* Checks that client has rights to add or remove channel modes. If any
3357    of the checks fails FALSE is returned. */
3358
3359 int silc_server_check_cmode_rights(SilcChannelEntry channel,
3360                                    SilcChannelClientEntry client,
3361                                    uint32 mode)
3362 {
3363   int is_op = client->mode & SILC_CHANNEL_UMODE_CHANOP;
3364   int is_fo = client->mode & SILC_CHANNEL_UMODE_CHANFO;
3365
3366   /* Check whether has rights to change anything */
3367   if (!is_op && !is_fo)
3368     return FALSE;
3369
3370   /* Check whether has rights to change everything */
3371   if (is_op && is_fo)
3372     return TRUE;
3373
3374   /* We know that client is channel operator, check that they are not
3375      changing anything that requires channel founder rights. Rest of the
3376      modes are available automatically for channel operator. */
3377
3378   if (mode & SILC_CHANNEL_MODE_PRIVKEY) {
3379     if (is_op && !is_fo)
3380       return FALSE;
3381   } else {
3382     if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3383       if (is_op && !is_fo)
3384         return FALSE;
3385     }
3386   }
3387   
3388   if (mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3389     if (is_op && !is_fo)
3390       return FALSE;
3391   } else {
3392     if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3393       if (is_op && !is_fo)
3394         return FALSE;
3395     }
3396   }
3397
3398   if (mode & SILC_CHANNEL_MODE_CIPHER) {
3399     if (is_op && !is_fo)
3400       return FALSE;
3401   } else {
3402     if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3403       if (is_op && !is_fo)
3404         return FALSE;
3405     }
3406   }
3407   
3408   if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3409     if (is_op && !is_fo)
3410       return FALSE;
3411   } else {
3412     if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3413       if (is_op && !is_fo)
3414         return FALSE;
3415     }
3416   }
3417   
3418   return TRUE;
3419 }
3420
3421 /* Server side command of CMODE. Changes channel mode */
3422
3423 SILC_SERVER_CMD_FUNC(cmode)
3424 {
3425   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3426   SilcServer server = cmd->server;
3427   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3428   SilcIDListData idata = (SilcIDListData)client;
3429   SilcChannelID *channel_id;
3430   SilcChannelEntry channel;
3431   SilcChannelClientEntry chl;
3432   SilcBuffer packet, cidp;
3433   unsigned char *tmp, *tmp_id, *tmp_mask;
3434   char *cipher = NULL, *hmac = NULL;
3435   uint32 mode_mask, tmp_len, tmp_len2;
3436   uint16 ident = silc_command_get_ident(cmd->payload);
3437
3438   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CMODE, cmd, 2, 7);
3439
3440   /* Get Channel ID */
3441   tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
3442   if (!tmp_id) {
3443     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3444                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
3445     goto out;
3446   }
3447   channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2);
3448   if (!channel_id) {
3449     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3450                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
3451     goto out;
3452   }
3453
3454   /* Get the channel mode mask */
3455   tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3456   if (!tmp_mask) {
3457     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3458                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3459     goto out;
3460   }
3461   SILC_GET32_MSB(mode_mask, tmp_mask);
3462
3463   /* Get channel entry */
3464   channel = silc_idlist_find_channel_by_id(server->local_list, 
3465                                            channel_id, NULL);
3466   if (!channel) {
3467     channel = silc_idlist_find_channel_by_id(server->global_list, 
3468                                              channel_id, NULL);
3469     if (!channel) {
3470       silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3471                                             SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3472       goto out;
3473     }
3474   }
3475
3476   /* Check whether this client is on the channel */
3477   if (!silc_server_client_on_channel(client, channel)) {
3478     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3479                                           SILC_STATUS_ERR_NOT_ON_CHANNEL);
3480     goto out;
3481   }
3482
3483   /* Get entry to the channel user list */
3484   silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
3485
3486   /* Check that client has rights to change any requested channel modes */
3487   if (!silc_server_check_cmode_rights(channel, chl, mode_mask)) {
3488     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3489                                           SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3490     goto out;
3491   }
3492
3493   /*
3494    * Check the modes. Modes that requires nothing special operation are
3495    * not checked here.
3496    */
3497
3498   if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
3499     /* Channel uses private keys to protect traffic. Client(s) has set the
3500        key locally they want to use, server does not know that key. */
3501     /* Nothing interesting to do here */
3502   } else {
3503     if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3504       /* The mode is removed and we need to generate and distribute
3505          new channel key. Clients are not using private channel keys
3506          anymore after this. */
3507
3508       /* Re-generate channel key */
3509       silc_server_create_channel_key(server, channel, 0);
3510       
3511       /* Send the channel key. This sends it to our local clients and if
3512          we are normal server to our router as well. */
3513       silc_server_send_channel_key(server, NULL, channel, 
3514                                    server->server_type == SILC_ROUTER ? 
3515                                    FALSE : !server->standalone);
3516
3517       cipher = channel->channel_key->cipher->name;
3518       hmac = channel->hmac->hmac->name;
3519     }
3520   }
3521   
3522   if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
3523     /* User limit is set on channel */
3524     uint32 user_limit;
3525       
3526     /* Get user limit */
3527     tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
3528     if (!tmp) {
3529       if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
3530         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3531                                    SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3532         goto out;
3533       }
3534     } else {
3535       SILC_GET32_MSB(user_limit, tmp);
3536       channel->user_limit = user_limit;
3537     }
3538   } else {
3539     if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
3540       /* User limit mode is unset. Remove user limit */
3541       channel->user_limit = 0;
3542   }
3543
3544   if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
3545     if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
3546       /* Passphrase has been set to channel */
3547       
3548       /* Get the passphrase */
3549       tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
3550       if (!tmp) {
3551         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3552                                    SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3553         goto out;
3554       }
3555
3556       /* Save the passphrase */
3557       channel->passphrase = strdup(tmp);
3558     }
3559   } else {
3560     if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3561       /* Passphrase mode is unset. remove the passphrase */
3562       if (channel->passphrase) {
3563         silc_free(channel->passphrase);
3564         channel->passphrase = NULL;
3565       }
3566     }
3567   }
3568
3569   if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
3570     if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
3571       /* Cipher to use protect the traffic */
3572
3573       /* Get cipher */
3574       cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
3575       if (!cipher) {
3576         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3577                                    SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3578         goto out;
3579       }
3580
3581       /* Delete old cipher and allocate the new one */
3582       silc_cipher_free(channel->channel_key);
3583       if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3584         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3585                                        SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3586         goto out;
3587       }
3588
3589       /* Re-generate channel key */
3590       silc_server_create_channel_key(server, channel, 0);
3591     
3592       /* Send the channel key. This sends it to our local clients and if
3593          we are normal server to our router as well. */
3594       silc_server_send_channel_key(server, NULL, channel, 
3595                                    server->server_type == SILC_ROUTER ? 
3596                                    FALSE : !server->standalone);
3597     }
3598   } else {
3599     if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3600       /* Cipher mode is unset. Remove the cipher and revert back to 
3601          default cipher */
3602       cipher = channel->cipher;
3603
3604       /* Delete old cipher and allocate default one */
3605       silc_cipher_free(channel->channel_key);
3606       if (!silc_cipher_alloc(cipher ? cipher : "aes-256-cbc", 
3607                              &channel->channel_key)) {
3608         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3609                                    SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3610         goto out;
3611       }
3612
3613       /* Re-generate channel key */
3614       silc_server_create_channel_key(server, channel, 0);
3615       
3616       /* Send the channel key. This sends it to our local clients and if
3617          we are normal server to our router as well. */
3618       silc_server_send_channel_key(server, NULL, channel, 
3619                                    server->server_type == SILC_ROUTER ? 
3620                                    FALSE : !server->standalone);
3621     }
3622   }
3623
3624   if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
3625     if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
3626       /* HMAC to use protect the traffic */
3627       unsigned char hash[32];
3628
3629       /* Get hmac */
3630       hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
3631       if (!hmac) {
3632         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3633                                    SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3634         goto out;
3635       }
3636
3637       /* Delete old hmac and allocate the new one */
3638       silc_hmac_free(channel->hmac);
3639       if (!silc_hmac_alloc(hmac, NULL, &channel->hmac)) {
3640         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3641                                        SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3642         goto out;
3643       }
3644
3645       /* Set the HMAC key out of current channel key. The client must do
3646          this locally. */
3647       silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8, 
3648                      hash);
3649       silc_hmac_set_key(channel->hmac, hash, 
3650                         silc_hash_len(channel->hmac->hash));
3651       memset(hash, 0, sizeof(hash));
3652     }
3653   } else {
3654     if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
3655       /* Hmac mode is unset. Remove the hmac and revert back to 
3656          default hmac */
3657       unsigned char hash[32];
3658       hmac = channel->hmac_name;
3659
3660       /* Delete old hmac and allocate default one */
3661       silc_hmac_free(channel->hmac);
3662       if (!silc_hmac_alloc(hmac ? hmac : "hmac-sha1-96", NULL, 
3663                            &channel->hmac)) {
3664         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3665                                        SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3666         goto out;
3667       }
3668
3669       /* Set the HMAC key out of current channel key. The client must do
3670          this locally. */
3671       silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8, 
3672                      hash);
3673       silc_hmac_set_key(channel->hmac, hash, 
3674                         silc_hash_len(channel->hmac->hash));
3675       memset(hash, 0, sizeof(hash));
3676     }
3677   }
3678
3679   if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3680     if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3681       if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
3682         /* Set the founder authentication */
3683         SilcAuthPayload auth;
3684         
3685         tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
3686         if (!tmp) {
3687           silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3688                                      SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3689           goto out;
3690         }
3691
3692         auth = silc_auth_payload_parse(tmp, tmp_len);
3693         if (!auth) {
3694           silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3695                                      SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3696           goto out;
3697         }
3698
3699         /* Save the public key */
3700         tmp = silc_pkcs_public_key_encode(idata->public_key, &tmp_len);
3701         silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
3702         silc_free(tmp);
3703         
3704         channel->founder_method = silc_auth_get_method(auth);
3705
3706         if (channel->founder_method == SILC_AUTH_PASSWORD) {
3707           tmp = silc_auth_get_data(auth, &tmp_len);
3708           channel->founder_passwd = 
3709             silc_calloc(tmp_len + 1, sizeof(*channel->founder_passwd));
3710           memcpy(channel->founder_passwd, tmp, tmp_len);
3711           channel->founder_passwd_len = tmp_len;
3712         }
3713
3714         silc_auth_payload_free(auth);
3715       }
3716     }
3717   } else {
3718     if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3719       if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3720         if (channel->founder_key)
3721           silc_pkcs_public_key_free(channel->founder_key);
3722         if (channel->founder_passwd) {
3723           silc_free(channel->founder_passwd);
3724           channel->founder_passwd = NULL;
3725         }
3726       }
3727     }
3728   }
3729
3730   /* Finally, set the mode */
3731   channel->mode = mode_mask;
3732
3733   /* Send CMODE_CHANGE notify */
3734   cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3735   silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3736                                      SILC_NOTIFY_TYPE_CMODE_CHANGE, 4,
3737                                      cidp->data, cidp->len, 
3738                                      tmp_mask, 4,
3739                                      cipher, cipher ? strlen(cipher) : 0,
3740                                      hmac, hmac ? strlen(hmac) : 0);
3741
3742   /* Set CMODE notify type to network */
3743   if (!server->standalone)
3744     silc_server_send_notify_cmode(server, server->router->connection,
3745                                   server->server_type == SILC_ROUTER ? 
3746                                   TRUE : FALSE, channel,
3747                                   mode_mask, client->id, SILC_ID_CLIENT,
3748                                   cipher, hmac);
3749
3750   /* Send command reply to sender */
3751   packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3752                                                 SILC_STATUS_OK, ident, 1,
3753                                                 2, tmp_mask, 4);
3754   silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0, 
3755                           packet->data, packet->len, FALSE);
3756     
3757   silc_buffer_free(packet);
3758   silc_free(channel_id);
3759   silc_free(cidp);
3760
3761  out:
3762   silc_server_command_free(cmd);
3763 }
3764
3765 /* Server side of CUMODE command. Changes client's mode on a channel. */
3766
3767 SILC_SERVER_CMD_FUNC(cumode)
3768 {
3769   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3770   SilcServer server = cmd->server;
3771   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3772   SilcIDListData idata = (SilcIDListData)client;
3773   SilcChannelID *channel_id;
3774   SilcClientID *client_id;
3775   SilcChannelEntry channel;
3776   SilcClientEntry target_client;
3777   SilcChannelClientEntry chl;
3778   SilcBuffer packet, idp;
3779   unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3780   uint32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
3781   int notify = FALSE;
3782   uint16 ident = silc_command_get_ident(cmd->payload);
3783
3784   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CUMODE, cmd, 3, 4);
3785
3786   /* Get Channel ID */
3787   tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3788   if (!tmp_ch_id) {
3789     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3790                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
3791     goto out;
3792   }
3793   channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len);
3794   if (!channel_id) {
3795     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3796                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
3797     goto out;
3798   }
3799
3800   /* Get channel entry */
3801   channel = silc_idlist_find_channel_by_id(server->local_list, 
3802                                            channel_id, NULL);
3803   if (!channel) {
3804     channel = silc_idlist_find_channel_by_id(server->global_list, 
3805                                              channel_id, NULL);
3806     if (!channel) {
3807       silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3808                                             SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3809       goto out;
3810     }
3811   }
3812
3813   /* Check whether sender is on the channel */
3814   if (!silc_server_client_on_channel(client, channel)) {
3815     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3816                                           SILC_STATUS_ERR_NOT_ON_CHANNEL);
3817     goto out;
3818   }
3819
3820   /* Check that client has rights to change other's rights */
3821   silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
3822   sender_mask = chl->mode;
3823   
3824   /* Get the target client's channel mode mask */
3825   tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3826   if (!tmp_mask) {
3827     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3828                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3829     goto out;
3830   }
3831   SILC_GET32_MSB(target_mask, tmp_mask);
3832
3833   /* Get target Client ID */
3834   tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3835   if (!tmp_id) {
3836     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3837                                           SILC_STATUS_ERR_NO_CLIENT_ID);
3838     goto out;
3839   }
3840   client_id = silc_id_payload_parse_id(tmp_id, tmp_len);
3841   if (!client_id) {
3842     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3843                                           SILC_STATUS_ERR_NO_CLIENT_ID);
3844     goto out;
3845   }
3846
3847   /* Get target client's entry */
3848   target_client = silc_idlist_find_client_by_id(server->local_list, 
3849                                                 client_id, NULL);
3850   if (!target_client) {
3851     target_client = silc_idlist_find_client_by_id(server->global_list, 
3852                                                   client_id, NULL);
3853   }
3854
3855   if (target_client != client &&
3856       !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
3857       !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
3858     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3859                                           SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3860     goto out;
3861   }
3862
3863   /* Check whether target client is on the channel */
3864   if (target_client != client) {
3865     if (!silc_server_client_on_channel(target_client, channel)) {
3866       silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3867                                  SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
3868       goto out;
3869     }
3870
3871     /* Get entry to the channel user list */
3872     silc_hash_table_find(channel->user_list, target_client, NULL, 
3873                          (void *)&chl);
3874   }
3875
3876   /* 
3877    * Change the mode 
3878    */
3879
3880   /* If the target client is founder, no one else can change their mode
3881      but themselves. */
3882   if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && chl->client != target_client) {
3883     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3884                                           SILC_STATUS_ERR_NOT_YOU);
3885     goto out;
3886   }
3887
3888   if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3889     /* The client tries to claim the founder rights. */
3890     unsigned char *tmp_auth;
3891     uint32 tmp_auth_len, auth_len;
3892     void *auth;
3893     
3894     if (target_client != client) {
3895       silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3896                                             SILC_STATUS_ERR_NOT_YOU);
3897       goto out;
3898     }
3899
3900     if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
3901         !channel->founder_key) {
3902       silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3903                                             SILC_STATUS_ERR_NOT_YOU);
3904       goto out;
3905     }
3906
3907     tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
3908     if (!tmp_auth) {
3909       silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3910                                             SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3911       goto out;
3912     }
3913
3914     auth = (channel->founder_method == SILC_AUTH_PASSWORD ?
3915             (void *)channel->founder_passwd : (void *)channel->founder_key);
3916     auth_len = (channel->founder_method == SILC_AUTH_PASSWORD ?
3917                 channel->founder_passwd_len : 0);
3918     
3919     if (!silc_auth_verify_data(tmp_auth, tmp_auth_len,
3920                                channel->founder_method, auth, auth_len,
3921                                idata->hash, client->id, SILC_ID_CLIENT)) {
3922       silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3923                                             SILC_STATUS_ERR_AUTH_FAILED);
3924       goto out;
3925     }
3926
3927     sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
3928     notify = TRUE;
3929   } else {
3930     if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3931       if (target_client == client) {
3932         /* Remove channel founder rights from itself */
3933         chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3934         notify = TRUE;
3935       } else {
3936         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3937                                               SILC_STATUS_ERR_NOT_YOU);
3938         goto out;
3939       }
3940     }
3941   }
3942
3943   if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
3944     /* Promote to operator */
3945     if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
3946       if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3947           !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3948         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3949                                               SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3950         goto out;
3951       }
3952
3953       chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
3954       notify = TRUE;
3955     }
3956   } else {
3957     if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
3958       if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3959           !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3960         silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3961                                               SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3962         goto out;
3963       }
3964
3965       /* Demote to normal user */
3966       chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
3967       notify = TRUE;
3968     }
3969   }
3970
3971   idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3972   tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3973
3974   /* Send notify to channel, notify only if mode was actually changed. */
3975   if (notify) {
3976     silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3977                                        SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
3978                                        idp->data, idp->len,
3979                                        tmp_mask, 4, 
3980                                        tmp_id, tmp_len);
3981
3982     /* Set CUMODE notify type to network */
3983     if (!server->standalone)
3984       silc_server_send_notify_cumode(server, server->router->connection,
3985                                      server->server_type == SILC_ROUTER ? 
3986                                      TRUE : FALSE, channel,
3987                                      target_mask, client->id, 
3988                                      SILC_ID_CLIENT,
3989                                      target_client->id);
3990   }
3991
3992   /* Send command reply to sender */
3993   packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
3994                                                 SILC_STATUS_OK, ident, 2,
3995                                                 2, tmp_mask, 4,
3996                                                 3, tmp_id, tmp_len);
3997   silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0, 
3998                           packet->data, packet->len, FALSE);
3999     
4000   silc_buffer_free(packet);
4001   silc_free(channel_id);
4002   silc_free(client_id);
4003   silc_buffer_free(idp);
4004
4005  out:
4006   silc_server_command_free(cmd);
4007 }
4008
4009 /* Server side of KICK command. Kicks client out of channel. */
4010
4011 SILC_SERVER_CMD_FUNC(kick)
4012 {
4013   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4014   SilcServer server = cmd->server;
4015   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4016   SilcClientEntry target_client;
4017   SilcChannelID *channel_id;
4018   SilcClientID *client_id;
4019   SilcChannelEntry channel;
4020   SilcChannelClientEntry chl;
4021   SilcBuffer idp;
4022   uint32 tmp_len;
4023   unsigned char *tmp, *comment;
4024
4025   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_LEAVE, cmd, 1, 3);
4026
4027   /* Get Channel ID */
4028   tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4029   if (!tmp) {
4030     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4031                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
4032     goto out;
4033   }
4034   channel_id = silc_id_payload_parse_id(tmp, tmp_len);
4035   if (!channel_id) {
4036     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4037                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
4038     goto out;
4039   }
4040
4041   /* Get channel entry */
4042   channel = silc_idlist_find_channel_by_id(server->local_list, 
4043                                            channel_id, NULL);
4044   if (!channel) {
4045     channel = silc_idlist_find_channel_by_id(server->local_list, 
4046                                              channel_id, NULL);
4047     if (!channel) {
4048       silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4049                                             SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4050       goto out;
4051     }
4052   }
4053
4054   /* Check whether sender is on the channel */
4055   if (!silc_server_client_on_channel(client, channel)) {
4056     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4057                                           SILC_STATUS_ERR_NOT_ON_CHANNEL);
4058     goto out;
4059   }
4060
4061   /* Check that the kicker is channel operator or channel founder */
4062   silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4063   if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
4064     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4065                                           SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4066     goto out;
4067   }
4068   
4069   /* Get target Client ID */
4070   tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4071   if (!tmp) {
4072     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4073                                           SILC_STATUS_ERR_NO_CLIENT_ID);
4074     goto out;
4075   }
4076   client_id = silc_id_payload_parse_id(tmp, tmp_len);
4077   if (!client_id) {
4078     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4079                                           SILC_STATUS_ERR_NO_CLIENT_ID);
4080     goto out;
4081   }
4082
4083   /* Get target client's entry */
4084   target_client = silc_idlist_find_client_by_id(server->local_list, 
4085                                                 client_id, NULL);
4086   if (!target_client) {
4087     target_client = silc_idlist_find_client_by_id(server->global_list, 
4088                                                   client_id, NULL);
4089   }
4090
4091   /* Check that the target client is not channel founder. Channel founder
4092      cannot be kicked from the channel. */
4093   silc_hash_table_find(channel->user_list, target_client, NULL, (void *)&chl);
4094   if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4095     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4096                                           SILC_STATUS_ERR_NO_CHANNEL_FOPRIV);
4097     goto out;
4098   }
4099   
4100   /* Check whether target client is on the channel */
4101   if (!silc_server_client_on_channel(target_client, channel)) {
4102     silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4103                                           SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
4104     goto out;
4105   }
4106
4107   /* Get comment */
4108   tmp_len = 0;
4109   comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4110   if (tmp_len > 128)
4111     comment = NULL;
4112
4113   /* Send command reply to sender */
4114   silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK, 
4115                                         SILC_STATUS_OK);
4116
4117   /* Send KICKED notify to local clients on the channel */
4118   idp = silc_id_payload_encode(target_client->id, SILC_ID_CLIENT);
4119   silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4120                                      SILC_NOTIFY_TYPE_KICKED, 
4121                                      comment ? 2 : 1,
4122                                      idp->data, idp->len,
4123                                      comment, comment ? strlen(comment) : 0);
4124   silc_buffer_free(idp);
4125
4126   /* Remove the client from the channel. If the channel does not exist
4127      after removing the client then the client kicked itself off the channel
4128      and we don't have to send anything after that. */
4129   if (!silc_server_remove_from_one_channel(server, NULL, channel, 
4130                                            target_client, FALSE))
4131     goto out;
4132
4133   /* Send KICKED notify to primary route */
4134   if (!server->standalone)
4135     silc_server_send_notify_kicked(server, server->router->connection,
4136                                    server->server_type == SILC_ROUTER ?
4137                                    TRUE : FALSE, channel,
4138                                    target_client->id, comment);
4139
4140   if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4141     /* Re-generate channel key */
4142     silc_server_create_channel_key(server, channel, 0);
4143     
4144     /* Send the channel key to the channel. The key of course is not sent
4145        to the client who was kicked off the channel. */
4146     silc_server_send_channel_key(server, target_client->connection, channel, 
4147                                  server->server_type == SILC_ROUTER ? 
4148                                  FALSE : !server->standalone);
4149   }
4150
4151  out:
4152   silc_server_command_free(cmd);
4153 }
4154
4155 /* Server side of OPER command. Client uses this comand to obtain server
4156    operator privileges to this server/router. */
4157
4158 SILC_SERVER_CMD_FUNC(oper)
4159 {
4160   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4161   SilcServer server = cmd->server;
4162   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4163   unsigned char *username, *auth;
4164   uint32 tmp_len;
4165   SilcServerConfigSectionAdminConnection *admin;
4166   SilcIDListData idata = (SilcIDListData)client;
4167
4168   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_OPER, cmd, 1, 2);
4169
4170   if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4171     goto out;
4172
4173   /* Get the username */
4174   username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4175   if (!username) {
4176     silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4177                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4178     goto out;
4179   }
4180
4181   /* Get the admin configuration */
4182   admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4183                                         username, client->nickname);
4184   if (!admin) {
4185     admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4186                                           username, client->nickname);
4187     if (!admin) {
4188       silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4189                                             SILC_STATUS_ERR_AUTH_FAILED);
4190       goto out;
4191     }
4192   }
4193
4194   /* Get the authentication payload */
4195   auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4196   if (!auth) {
4197     silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4198                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4199     goto out;
4200   }
4201
4202   /* Verify the authentication data */
4203   if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth, 
4204                              admin->auth_data, admin->auth_data_len,
4205                              idata->hash, client->id, SILC_ID_CLIENT)) {
4206     silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4207                                           SILC_STATUS_ERR_AUTH_FAILED);
4208     goto out;
4209   }
4210
4211   /* Client is now server operator */
4212   client->mode |= SILC_UMODE_SERVER_OPERATOR;
4213
4214   /* Send UMODE change to primary router */
4215   if (!server->standalone)
4216     silc_server_send_notify_umode(server, server->router->connection, TRUE,
4217                                   client->id, client->mode);
4218
4219   /* Send reply to the sender */
4220   silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4221                                         SILC_STATUS_OK);
4222
4223  out:
4224   silc_server_command_free(cmd);
4225 }
4226
4227 /* Server side of SILCOPER command. Client uses this comand to obtain router
4228    operator privileges to this router. */
4229
4230 SILC_SERVER_CMD_FUNC(silcoper)
4231 {
4232   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4233   SilcServer server = cmd->server;
4234   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4235   unsigned char *username, *auth;
4236   uint32 tmp_len;
4237   SilcServerConfigSectionAdminConnection *admin;
4238   SilcIDListData idata = (SilcIDListData)client;
4239
4240   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_SILCOPER, cmd, 1, 2);
4241
4242   if (server->server_type == SILC_SERVER)
4243     goto out;
4244
4245   if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4246     goto out;
4247
4248   /* Get the username */
4249   username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4250   if (!username) {
4251     silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4252                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4253     goto out;
4254   }
4255
4256   /* Get the admin configuration */
4257   admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4258                                         username, client->nickname);
4259   if (!admin) {
4260     admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4261                                           username, client->nickname);
4262     if (!admin) {
4263       silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4264                                             SILC_STATUS_ERR_AUTH_FAILED);
4265       goto out;
4266     }
4267   }
4268
4269   /* Get the authentication payload */
4270   auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4271   if (!auth) {
4272     silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4273                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4274     goto out;
4275   }
4276
4277   /* Verify the authentication data */
4278   if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth, 
4279                              admin->auth_data, admin->auth_data_len,
4280                              idata->hash, client->id, SILC_ID_CLIENT)) {
4281     silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4282                                           SILC_STATUS_ERR_AUTH_FAILED);
4283     goto out;
4284   }
4285
4286   /* Client is now router operator */
4287   client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4288
4289   /* Send UMODE change to primary router */
4290   if (!server->standalone)
4291     silc_server_send_notify_umode(server, server->router->connection, TRUE,
4292                                   client->id, client->mode);
4293
4294   /* Send reply to the sender */
4295   silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4296                                         SILC_STATUS_OK);
4297
4298  out:
4299   silc_server_command_free(cmd);
4300 }
4301
4302 /* Server side command of CONNECT. Connects us to the specified remote
4303    server or router. */
4304
4305 SILC_SERVER_CMD_FUNC(connect)
4306 {
4307   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4308   SilcServer server = cmd->server;
4309   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4310   unsigned char *tmp, *host;
4311   uint32 tmp_len;
4312   uint32 port = SILC_PORT;
4313
4314   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CONNECT, cmd, 1, 2);
4315
4316   if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4317     goto out;
4318
4319   /* Check whether client has the permissions. */
4320   if (client->mode == SILC_UMODE_NONE) {
4321     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4322                                           SILC_STATUS_ERR_NO_SERVER_PRIV);
4323     goto out;
4324   }
4325
4326   if (server->server_type == SILC_ROUTER && 
4327       client->mode & SILC_UMODE_SERVER_OPERATOR) {
4328     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4329                                           SILC_STATUS_ERR_NO_ROUTER_PRIV);
4330     goto out;
4331   }
4332
4333   /* Get the remote server */
4334   host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4335   if (!host) {
4336     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4337                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4338     goto out;
4339   }
4340
4341   /* Get port */
4342   tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4343   if (tmp)
4344     SILC_GET32_MSB(port, tmp);
4345
4346   /* Create the connection. It is done with timeout and is async. */
4347   silc_server_create_connection(server, host, port);
4348
4349   /* Send reply to the sender */
4350   silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4351                                         SILC_STATUS_OK);
4352
4353  out:
4354   silc_server_command_free(cmd);
4355 }
4356
4357 /* Server side of command BAN. This is used to manage the ban list of the
4358    channel. To add clients and remove clients from the ban list. */
4359
4360 SILC_SERVER_CMD_FUNC(ban)
4361 {
4362   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4363   SilcServer server = cmd->server;
4364   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4365   SilcBuffer packet;
4366   SilcChannelEntry channel;
4367   SilcChannelClientEntry chl;
4368   SilcChannelID *channel_id = NULL;
4369   unsigned char *id, *add, *del;
4370   uint32 id_len, tmp_len;
4371   uint16 ident = silc_command_get_ident(cmd->payload);
4372
4373   if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4374     goto out;
4375
4376   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_BAN, cmd, 0, 3);
4377
4378   /* Get Channel ID */
4379   id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4380   if (id) {
4381     channel_id = silc_id_payload_parse_id(id, id_len);
4382     if (!channel_id) {
4383       silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4384                                             SILC_STATUS_ERR_NO_CHANNEL_ID);
4385       goto out;
4386     }
4387   }
4388
4389   /* Get channel entry. The server must know about the channel since the
4390      client is expected to be on the channel. */
4391   channel = silc_idlist_find_channel_by_id(server->local_list, 
4392                                            channel_id, NULL);
4393   if (!channel) {
4394     channel = silc_idlist_find_channel_by_id(server->global_list, 
4395                                              channel_id, NULL);
4396     if (!channel) {
4397       silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4398                                             SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4399       goto out;
4400     }
4401   }
4402
4403   /* Check whether this client is on the channel */
4404   if (!silc_server_client_on_channel(client, channel)) {
4405     silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4406                                           SILC_STATUS_ERR_NOT_ON_CHANNEL);
4407     goto out;
4408   }
4409
4410   /* Get entry to the channel user list */
4411   silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4412
4413   /* The client must be at least channel operator. */
4414   if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4415     silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4416                                           SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4417     goto out;
4418   }
4419
4420   /* Get the new ban and add it to the ban list */
4421   add = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4422   if (add) {
4423     if (!channel->ban_list)
4424       channel->ban_list = silc_calloc(tmp_len + 2, sizeof(*channel->ban_list));
4425     else
4426       channel->ban_list = silc_realloc(channel->ban_list, 
4427                                        sizeof(*channel->ban_list) * 
4428                                        (tmp_len + 
4429                                         strlen(channel->ban_list) + 2));
4430     if (add[tmp_len - 1] == ',')
4431       add[tmp_len - 1] = '\0';
4432
4433     strncat(channel->ban_list, add, tmp_len);
4434     strncat(channel->ban_list, ",", 1);
4435   }
4436
4437   /* Get the ban to be removed and remove it from the list */
4438   del = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4439   if (del && channel->ban_list) {
4440     char *start, *end, *n;
4441
4442     if (!strncmp(channel->ban_list, del, strlen(channel->ban_list) - 1)) {
4443       silc_free(channel->ban_list);
4444       channel->ban_list = NULL;
4445     } else {
4446       start = strstr(channel->ban_list, del);
4447       if (start && strlen(start) >= tmp_len) {
4448         end = start + tmp_len;
4449         n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
4450         strncat(n, channel->ban_list, start - channel->ban_list);
4451         strncat(n, end + 1, ((channel->ban_list + strlen(channel->ban_list)) - 
4452                              end) - 1);
4453         silc_free(channel->ban_list);
4454         channel->ban_list = n;
4455       }
4456     }
4457   }
4458
4459   /* Send the BAN notify type to our primary router. */
4460   if (!server->standalone && (add || del))
4461     silc_server_send_notify_ban(server, server->router->connection,
4462                                 server->server_type == SILC_ROUTER ?
4463                                 TRUE : FALSE, channel, add, del);
4464
4465   /* Send the reply back to the client */
4466   if (channel->ban_list)
4467     packet = 
4468       silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4469                                            SILC_STATUS_OK, ident, 2,
4470                                            2, id, id_len,
4471                                            3, channel->ban_list, 
4472                                            strlen(channel->ban_list) - 1);
4473   else
4474     packet = 
4475       silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4476                                            SILC_STATUS_OK, ident, 1,
4477                                            2, id, id_len);
4478
4479   silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0, 
4480                           packet->data, packet->len, FALSE);
4481     
4482   silc_buffer_free(packet);
4483
4484  out:
4485   if (channel_id)
4486     silc_free(channel_id);
4487   silc_server_command_free(cmd);
4488 }
4489
4490 /* Server side command of CLOSE. Closes connection to a specified server. */
4491  
4492 SILC_SERVER_CMD_FUNC(close)
4493 {
4494   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4495   SilcServer server = cmd->server;
4496   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4497   SilcServerEntry server_entry;
4498   SilcSocketConnection sock;
4499   unsigned char *tmp;
4500   uint32 tmp_len;
4501   unsigned char *name;
4502   uint32 port = SILC_PORT;
4503
4504   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CLOSE, cmd, 1, 2);
4505
4506   if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4507     goto out;
4508
4509   /* Check whether client has the permissions. */
4510   if (client->mode == SILC_UMODE_NONE) {
4511     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4512                                           SILC_STATUS_ERR_NO_SERVER_PRIV);
4513     goto out;
4514   }
4515
4516   /* Get the remote server */
4517   name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4518   if (!name) {
4519     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4520                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4521     goto out;
4522   }
4523
4524   /* Get port */
4525   tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4526   if (tmp)
4527     SILC_GET32_MSB(port, tmp);
4528
4529   server_entry = silc_idlist_find_server_by_conn(server->local_list,
4530                                                  name, port, NULL);
4531   if (!server_entry) {
4532     silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4533                                           SILC_STATUS_ERR_NO_SERVER_ID);
4534     goto out;
4535   }
4536
4537   /* Send reply to the sender */
4538   silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4539                                         SILC_STATUS_OK);
4540
4541   /* Close the connection to the server */
4542   sock = (SilcSocketConnection)server_entry->connection;
4543   silc_server_free_sock_user_data(server, sock);
4544   silc_server_close_connection(server, sock);
4545   
4546  out:
4547   silc_server_command_free(cmd);
4548 }
4549
4550 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
4551    active connections. */
4552  
4553 SILC_SERVER_CMD_FUNC(shutdown)
4554 {
4555   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4556   SilcServer server = cmd->server;
4557   SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4558
4559   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_SHUTDOWN, cmd, 0, 0);
4560
4561   if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4562     goto out;
4563
4564   /* Check whether client has the permission. */
4565   if (client->mode == SILC_UMODE_NONE) {
4566     silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4567                                           SILC_STATUS_ERR_NO_SERVER_PRIV);
4568     goto out;
4569   }
4570
4571   /* Send reply to the sender */
4572   silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4573                                         SILC_STATUS_OK);
4574
4575   /* Then, gracefully, or not, bring the server down. */
4576   silc_server_stop(server);
4577   exit(0);
4578
4579  out:
4580   silc_server_command_free(cmd);
4581 }
4582  
4583 /* Server side command of LEAVE. Removes client from a channel. */
4584
4585 SILC_SERVER_CMD_FUNC(leave)
4586 {
4587   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4588   SilcServer server = cmd->server;
4589   SilcSocketConnection sock = cmd->sock;
4590   SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4591   SilcChannelID *id = NULL;
4592   SilcChannelEntry channel;
4593   uint32 len;
4594   unsigned char *tmp;
4595
4596   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_LEAVE, cmd, 1, 2);
4597
4598   /* Get Channel ID */
4599   tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4600   if (!tmp) {
4601     silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4602                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
4603     goto out;
4604   }
4605   id = silc_id_payload_parse_id(tmp, len);
4606   if (!id) {
4607     silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4608                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
4609     goto out;
4610   }
4611
4612   /* Get channel entry */
4613   channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4614   if (!channel) {
4615     channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4616     if (!channel) {
4617       silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4618                                             SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4619       goto out;
4620     }
4621   }
4622
4623   /* Check whether this client is on the channel */
4624   if (!silc_server_client_on_channel(id_entry, channel)) {
4625     silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4626                                           SILC_STATUS_ERR_NOT_ON_CHANNEL);
4627     goto out;
4628   }
4629
4630   /* Notify routers that they should remove this client from their list
4631      of clients on the channel. Send LEAVE notify type. */
4632   if (!server->standalone)
4633     silc_server_send_notify_leave(server, server->router->connection,
4634                                   server->server_type == SILC_ROUTER ?
4635                                   TRUE : FALSE, channel, id_entry->id);
4636
4637   silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4638                                         SILC_STATUS_OK);
4639
4640   /* Remove client from channel */
4641   if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4642                                            TRUE))
4643     /* If the channel does not exist anymore we won't send anything */
4644     goto out;
4645
4646   if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4647     /* Re-generate channel key */
4648     silc_server_create_channel_key(server, channel, 0);
4649
4650     /* Send the channel key */
4651     silc_server_send_channel_key(server, NULL, channel, 
4652                                  server->server_type == SILC_ROUTER ? 
4653                                  FALSE : !server->standalone);
4654   }
4655
4656  out:
4657   if (id)
4658     silc_free(id);
4659   silc_server_command_free(cmd);
4660 }
4661
4662 /* Server side of command USERS. Resolves clients and their USERS currently
4663    joined on the requested channel. The list of Client ID's and their modes
4664    on the channel is sent back. */
4665
4666 SILC_SERVER_CMD_FUNC(users)
4667 {
4668   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4669   SilcServer server = cmd->server;
4670   SilcChannelEntry channel;
4671   SilcChannelID *id;
4672   SilcBuffer packet;
4673   unsigned char *channel_id;
4674   uint32 channel_id_len;
4675   SilcBuffer client_id_list;
4676   SilcBuffer client_mode_list;
4677   unsigned char lc[4];
4678   uint32 list_count = 0;
4679   uint16 ident = silc_command_get_ident(cmd->payload);
4680
4681   SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_USERS, cmd, 1, 1);
4682
4683   /* Get Channel ID */
4684   channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4685   if (!channel_id) {
4686     silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4687                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
4688     goto out;
4689   }
4690   id = silc_id_payload_parse_id(channel_id, channel_id_len);
4691   if (!id) {
4692     silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4693                                           SILC_STATUS_ERR_NO_CHANNEL_ID);
4694     goto out;
4695   }
4696
4697   /* If we are server and we don't know about this channel we will send
4698      the command to our router. If we know about the channel then we also
4699      have the list of users already. */
4700   channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4701   if (!channel) {
4702     if (server->server_type == SILC_SERVER && !server->standalone &&
4703         !cmd->pending) {
4704       SilcBuffer tmpbuf;
4705       
4706       silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4707       tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4708       
4709       /* Send USERS command */
4710       silc_server_packet_send(server, server->router->connection,
4711                               SILC_PACKET_COMMAND, cmd->packet->flags,
4712                               tmpbuf->data, tmpbuf->len, TRUE);
4713       
4714       /* Reprocess this packet after received reply */
4715       silc_server_command_pending(server, SILC_COMMAND_USERS, 
4716                                   silc_command_get_ident(cmd->payload),
4717                                   silc_server_command_destructor,
4718                                   silc_server_command_users,
4719                                   silc_server_command_dup(cmd));
4720       cmd->pending = TRUE;
4721       silc_command_set_ident(cmd->payload, ident);
4722       
4723       silc_buffer_free(tmpbuf);
4724       silc_free(id);
4725       return;
4726     }
4727
4728     /* We are router and we will check the global list as well. */
4729     channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4730     if (!channel) {
4731       /* Channel really does not exist */
4732       silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4733                                             SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4734       goto out;
4735     }
4736   }
4737
4738   /* Get the users list */
4739   silc_server_get_users_on_channel(server, channel, &client_id_list,
4740                                    &client_mode_list, &list_count);
4741
4742   /* List count */
4743   SILC_PUT32_MSB(list_count, lc);
4744
4745   /* Send reply */
4746   packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
4747                                                 SILC_STATUS_OK, ident, 4,
4748                                                 2, channel_id, channel_id_len,
4749                                                 3, lc, 4,
4750                                                 4, client_id_list->data,
4751                                                 client_id_list->len,
4752                                                 5, client_mode_list->data,
4753                                                 client_mode_list->len);
4754   silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0, 
4755                           packet->data, packet->len, FALSE);
4756     
4757   silc_buffer_free(packet);
4758   silc_buffer_free(client_id_list);
4759   silc_buffer_free(client_mode_list);
4760   silc_free(id);
4761
4762  out:
4763   silc_server_command_free(cmd);
4764 }
4765
4766 /* Server side of command GETKEY. This fetches the client's public key
4767    from the server where to the client is connected. */
4768
4769 SILC_SERVER_CMD_FUNC(getkey)
4770 {
4771   SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4772   SilcServer server = cmd->server;
4773   SilcBuffer packet;
4774   SilcClientEntry client;
4775   SilcServerEntry server_entry;
4776   SilcClientID *client_id = NULL;
4777   SilcServerID *server_id = NULL;
4778   SilcIDPayload idp = NULL;
4779   uint16 ident = silc_command_get_ident(cmd->payload);
4780   unsigned char *tmp;
4781   uint32 tmp_len;
4782   SilcBuffer pk;
4783   SilcIdType id_type;
4784
4785   SILC_LOG_DEBUG(("Start"));
4786
4787   tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4788   if (!tmp) {
4789     silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4790                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4791     goto out;
4792   }
4793   idp = silc_id_payload_parse_data(tmp, tmp_len);
4794   if (!idp) {
4795     silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4796                                           SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4797     goto out;
4798   }
4799
4800   id_type = silc_id_payload_get_type(idp);
4801   if (id_type == SILC_ID_CLIENT) {
4802     client_id = silc_id_payload_get_id(idp);
4803
4804     /* If the client is not found from local list there is no chance it
4805        would be locally connected client so send the command further. */
4806     client = silc_idlist_find_client_by_id(server->local_list, 
4807                                            client_id, NULL);
4808     if (!client)
4809       client = silc_idlist_find_client_by_id(server->global_list, 
4810                                              client_id, NULL);
4811     
4812     if ((!client && !cmd->pending && !server->standalone) ||
4813         (client && !client->connection && !cmd->pending && 
4814          !server->standalone) ||
4815         (client && !client->data.public_key && !cmd->pending &&
4816          !server->standalone)) {
4817       SilcBuffer tmpbuf;
4818       uint16 old_ident;
4819       SilcSocketConnection dest_sock;
4820       
4821       dest_sock = silc_server_get_client_route(server, NULL, 0, 
4822                                                client_id, NULL);
4823       if (!dest_sock)
4824         goto out;
4825       
4826       old_ident = silc_command_get_ident(cmd->payload);
4827       silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4828       tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4829       
4830       silc_server_packet_send(server, dest_sock,
4831                               SILC_PACKET_COMMAND, cmd->packet->flags,
4832                               tmpbuf->data, tmpbuf->len, TRUE);
4833       
4834       /* Reprocess this packet after received reply from router */
4835       silc_server_command_pending(server, SILC_COMMAND_GETKEY, 
4836                                   silc_command_get_ident(cmd->payload),
4837                                   silc_server_command_destructor,
4838                                   silc_server_command_getkey,
4839                                   silc_server_command_dup(cmd));
4840       cmd->pending = TRUE;
4841       
4842       silc_command_set_ident(cmd->payload, old_ident);
4843       silc_buffer_free(tmpbuf);
4844       return;
4845     }
4846
4847     if (!client && cmd->pending) {
4848       silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4849                                             SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
4850       goto out;
4851     }
4852
4853     /* The client is locally connected, just get the public key and
4854        send it back. */
4855     tmp = silc_pkcs_public_key_encode(client->data.public_key, &tmp_len);
4856     pk = silc_buffer_alloc(4 + tmp_len);
4857     silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
4858     silc_buffer_format(pk,
4859                        SILC_STR_UI_SHORT(tmp_len),
4860                        SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
4861                        SILC_STR_UI_XNSTRING(tmp, tmp_len),
4862                        SILC_STR_END);
4863     silc_free(tmp);
4864
4865   } else if (id_type == SILC_ID_SERVER) {
4866     server_id = silc_id_payload_get_id(idp);
4867
4868     /* If the server is not found from local list there is no chance it
4869        would be locally connected server so send the command further. */
4870     server_entry = silc_idlist_find_server_by_id(server->local_list, 
4871                                                  server_id, NULL);
4872     if (!server_entry)
4873       server_entry = silc_idlist_find_server_by_id(server->global_list, 
4874                                                    server_id, NULL);
4875     
4876     if ((!server_entry && !cmd->pending && !server->standalone) ||
4877         (server_entry && !server_entry->connection && !cmd->pending &&
4878          !server->standalone) ||
4879         (server_entry && !server_entry->data.public_key && !cmd->pending &&
4880          !server->standalone)) {
4881       SilcBuffer tmpbuf;
4882       uint16 old_ident;
4883       
4884       old_ident = silc_command_get_ident(cmd->payload);
4885       silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4886       tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4887       
4888       silc_server_packet_send(server, server->router->connection,
4889                               SILC_PACKET_COMMAND, cmd->packet->flags,
4890                               tmpbuf->data, tmpbuf->len, TRUE);
4891       
4892       /* Reprocess this packet after received reply from router */
4893       silc_server_command_pending(server, SILC_COMMAND_GETKEY, 
4894                                   silc_command_get_ident(cmd->payload),
4895                                   silc_server_command_destructor,
4896                                   silc_server_command_getkey,
4897                                   silc_server_command_dup(cmd));
4898       cmd->pending = TRUE;
4899       
4900       silc_command_set_ident(cmd->payload, old_ident);
4901       silc_buffer_free(tmpbuf);
4902       return;
4903     }
4904
4905     if (!server_entry && cmd->pending) {
4906       silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4907                                             SILC_STATUS_ERR_NO_SUCH_SERVER_ID);
4908       goto out;
4909     }
4910
4911     /* The client is locally connected, just get the public key and
4912        send it back. */
4913     tmp = silc_pkcs_public_key_encode(server_entry->data.public_key, &tmp_len);
4914     pk = silc_buffer_alloc(4 + tmp_len);
4915     silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
4916     silc_buffer_format(pk,
4917                        SILC_STR_UI_SHORT(tmp_len),
4918                        SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
4919                        SILC_STR_UI_XNSTRING(tmp, tmp_len),
4920                        SILC_STR_END);
4921     silc_free(tmp);
4922   } else {
4923     goto out;
4924   }
4925
4926   tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4927   packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
4928                                                 SILC_STATUS_OK, ident, 2,
4929                                                 2, tmp, tmp_len,
4930                                                 3, pk->data, pk->len);
4931   silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0, 
4932                           packet->data, packet->len, FALSE);
4933   silc_buffer_free(packet);
4934   silc_buffer_free(pk);
4935
4936  out:
4937   if (idp)
4938     silc_id_payload_free(idp);
4939   silc_free(client_id);
4940   silc_free(server_id);
4941   silc_server_command_free(cmd);
4942 }