updates.
[silc.git] / apps / silcd / idlist.h
1 /*
2
3   idlist.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2002 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
21 #ifndef IDLIST_H
22 #define IDLIST_H
23
24 /* Forward declarations */
25 typedef struct SilcServerEntryStruct *SilcServerEntry;
26 typedef struct SilcClientEntryStruct *SilcClientEntry;
27 typedef struct SilcChannelEntryStruct *SilcChannelEntry;
28
29 /* Context for holding cache information to periodically purge
30    the cache. */
31 typedef struct {
32   SilcIDCache cache;
33   SilcSchedule schedule;
34   SilcUInt32 timeout;
35 } *SilcIDListPurge;
36
37 /* Channel key re-key context. */
38 typedef struct {
39   void *context;
40   SilcChannelEntry channel;
41   SilcUInt32 key_len;
42   SilcTask task;
43 } *SilcServerChannelRekey;
44
45 /* Generic rekey context for connections */
46 typedef struct {
47   /* Current sending encryption key, provided for re-key. The `pfs'
48      is TRUE if the Perfect Forward Secrecy is performed in re-key. */
49   unsigned char *send_enc_key;
50   SilcUInt32 enc_key_len;
51   int ske_group;
52   bool pfs;
53   SilcUInt32 timeout;
54   void *context;
55 } *SilcServerRekey;
56
57 /* ID List Entry status type and all the types. */
58 typedef SilcUInt8 SilcIDListStatus;
59 #define SILC_IDLIST_STATUS_NONE         0x00    /* No status */
60 #define SILC_IDLIST_STATUS_REGISTERED   0x01    /* Entry is registered */
61 #define SILC_IDLIST_STATUS_RESOLVED     0x02    /* Entry info is resolved */
62 #define SILC_IDLIST_STATUS_RESOLVING    0x04    /* Entry is being resolved
63                                                    with WHOIS or IDENTIFY */
64 #define SILC_IDLIST_STATUS_DISABLED     0x08    /* Entry is disabled */
65 #define SILC_IDLIST_STATUS_RESUMED      0x10    /* Entry is resumed */
66
67 /*
68    Generic ID list data structure.
69
70    This structure is included in all ID list entries and it includes data
71    pointers that are common to all ID entries.  This structure is always
72    defined to the first field in the ID entries and is used to explicitly
73    type cast to this type without first explicitly casting to correct ID
74    entry type.  Hence, the ID list entry is type casted to this type to
75    get this data from the ID entry (which is usually opaque pointer).
76
77    Note that some of the fields may be NULL.
78
79 */
80 typedef struct {
81   /* Send and receive symmetric keys */
82   SilcCipher send_key;
83   SilcCipher receive_key;
84
85   /* HMAC */
86   SilcHmac hmac_send;
87   SilcHmac hmac_receive;
88
89   /* Packet sequence numbers */
90   SilcUInt32 psn_send;
91   SilcUInt32 psn_receive;
92
93   /* Hash selected in the SKE protocol, NULL if not needed at all */
94   SilcHash hash;
95
96   /* Public key */
97   SilcPublicKey public_key;
98   unsigned char fingerprint[20];
99
100   /* Re-key context */
101   SilcServerRekey rekey;
102
103   long last_receive;            /* Time last received data */
104   long last_sent;               /* Time last sent data */
105
106   unsigned long created;        /* Time when entry was created */
107
108   SilcIDListStatus status;      /* Status mask of the entry */
109 } *SilcIDListData, SilcIDListDataStruct;
110
111 /* 
112    SILC Server entry object.
113
114    This entry holds information about servers in SILC network. However, 
115    contents of this entry is highly dependent of what kind of server we are 
116    (normal server or router server) and whether the entry is used as a local 
117    list or a global list. These factors dictates the contents of this entry.
118
119    This entry is defined as follows:
120
121    Server type   List type      Contents
122    =======================================================================
123    server        local list     Server itself
124    server        global list    NULL
125    router        local list     All servers is the cell
126    router        global list    All servers in the SILC network
127
128    Following short description of the fields:
129
130    SilcIDListDataStruct data
131
132        Generic data structure to hold data common to all ID entries.
133
134    char *server_name
135
136        Logical name of the server. There is no limit of the length of the
137        server name. This is usually the same name as defined in DNS.
138
139    SilcUInt8 server_type
140
141        Type of the server. SILC_SERVER or SILC_ROUTER are the possible
142        choices for this.
143
144    SilcServerID *id
145
146        ID of the server. This includes all the relevant information about
147        the server SILC will ever need. These are also the informations
148        that is broadcasted between servers and routers in the SILC network.
149
150    char *server_info
151    char *motd
152
153        Server info (from INFO command) saved temporarily and motd (from
154        MOTD command) saved temporarily.
155
156    SilcServerEntry router
157
158        This is a pointer back to the server list. This is the router server 
159        where this server is connected to. If this is the router itself and 
160        it doesn't have a route this is NULL.
161
162    SilcCipher send_key
163    SilcCipher receive_key
164
165        Data sending and receiving keys.
166
167    void *connection
168
169        A pointer, usually, to the socket list for fast referencing to
170        the data used in connection with this server.  This may be anything
171        but as just said, this is usually pointer to the socket connection
172        list.
173    
174 */
175 struct SilcServerEntryStruct {
176   /* Generic data structure. DO NOT add anything before this! */
177   SilcIDListDataStruct data;
178
179   char *server_name;
180   SilcUInt8 server_type;
181   SilcServerID *id;
182   char *server_info;
183   char *motd;
184
185   /* Pointer to the router */
186   SilcServerEntry router;
187
188   /* Connection data */
189   void *connection;
190 };
191
192 /* 
193    SILC Channel Client entry structure.
194
195    This entry used only by the SilcChannelEntry object and it holds
196    information about current clients (ie. users) on channel. Following
197    short description of the fields:
198
199    SilcClientEntry client
200
201        Pointer to the client list. This is the client currently on channel.
202
203    SilcUInt32 mode
204
205        Client's current mode on the channel.
206
207    SilcChannelEntry channel
208
209        Back pointer back to channel. As this structure is also used by
210        SilcClientEntry we have this here for fast access to the channel when
211        used by SilcClientEntry.
212
213 */
214 typedef struct SilcChannelClientEntryStruct {
215   SilcClientEntry client;
216   SilcUInt32 mode;
217   SilcChannelEntry channel;
218 } *SilcChannelClientEntry;
219
220 /* 
221    SILC Client entry object.
222
223    This entry holds information about connected clients ie. users in the SILC
224    network. The contents of this entrt is depended on whether we are normal 
225    server or router server and whether the list is a local or global list.
226
227    This entry is defined as follows:
228
229    Server type   List type      Contents
230    =======================================================================
231    server        local list     All clients in server
232    server        global list    NULL
233    router        local list     All clients in cell
234    router        global list    All clients in SILC
235
236    Following short description of the fields:
237
238    SilcIDListDataStruct data
239
240        Generic data structure to hold data common to all ID entries.
241
242    unsigned char *nickname
243
244        The nickname of the client.
245
246    char *servername
247
248        The name of the server where the client is from. MAy be NULL.
249
250    char username
251
252        Client's usename. This is defined in the following manner:
253
254        Server type   List type      Contents
255        ====================================================
256        server        local list     User's name
257        router        local list     NULL
258        router        global list    NULL
259
260        Router doesn't hold this information since it is not vital data 
261        for the router. If this information is needed by the client it is
262        fetched when it is needed.
263
264    char userinfo
265
266        Information about user. This is free information and can be virtually
267        anything. This is defined in following manner:
268        
269        Server type   List type      Contents
270        ====================================================
271        server        local list     User's information
272        router        local list     NULL
273        router        global list    NULL
274
275        Router doesn't hold this information since it is not vital data 
276        for the router. If this information is needed by the client it is
277        fetched when it is needed.
278
279    SilcClientID *id
280
281        ID of the client. This includes all the information SILC will ever
282        need. Notice that no nickname of the user is saved anywhere. This is
283        beacuse of SilcClientID includes 88 bit hash value of the user's 
284        nickname which can be used to track down specific user by their 
285        nickname. Nickname is not relevant information that would need to be 
286        saved as plain.
287
288    SilcUInt32 mode
289
290        Client's mode.  Client maybe for example server operator or
291        router operator (SILC operator).
292
293    long last_command
294
295        Time of last time client executed command. We are strict and will
296        not allow any command to be exeucted more than once in about
297        2 seconds. This is result of normal time().
298
299    SilcUInt8 fast_command
300
301        Counter to check command bursts.  By default, up to 5 commands
302        are allowed before limiting the execution.  See command flags
303        for more detail.
304
305    SilcServerEntry router
306
307        This is a pointer to the server list. This is the router server whose 
308        cell this client is coming from. This is used to route messages to 
309        this client.
310
311    SilcHashTable channels;
312
313        All the channels this client has joined.  The context saved in the
314        hash table shares memory with the channel entrys `user_list' hash
315        table.
316
317    void *connection
318
319        A pointer, usually, to the socket list for fast referencing to
320        the data used in connection with this client.  This may be anything
321        but as just said, this is usually pointer to the socket connection
322        list.
323
324    SilcUInt16 resolve_cmd_ident
325
326        Command identifier for the entry when the entry's data.status
327        is SILC_IDLIST_STATUS_RESOLVING.  If this entry is asked to be
328        resolved when the status is set then the resolver may attach to
329        this command identifier and handle the process after the resolving
330        is over.
331
332 */
333 struct SilcClientEntryStruct {
334   /* Generic data structure. DO NOT add anything before this! */
335   SilcIDListDataStruct data;
336
337   unsigned char *nickname;
338   char *servername;
339   char *username;
340   char *userinfo;
341   SilcClientID *id;
342   SilcUInt32 mode;
343
344   long last_command;
345   SilcUInt8 fast_command;
346
347   /* Pointer to the router */
348   SilcServerEntry router;
349
350   /* All channels this client has joined */
351   SilcHashTable channels;
352
353   /* Connection data */
354   void *connection;
355
356   /* data.status is RESOLVING and this includes the resolving command 
357      reply identifier. */
358   SilcUInt16 resolve_cmd_ident;
359 };
360
361 /* 
362    SILC Channel entry object.
363
364    This entry holds information about channels in SILC network. The contents 
365    of this entry is depended on whether we are normal server or router server 
366    and whether the list is a local or global list.
367
368    This entry is defined as follows:
369
370    Server type   List type      Contents
371    =======================================================================
372    server        local list     All channels in server
373    server        global list    NULL
374    router        local list     All channels in cell
375    router        global list    All channels in SILC
376
377    Following short description of the fields:
378
379    char *channel_name
380
381        Logical name of the channel.
382
383    SilcUInt32 mode
384
385        Current mode of the channel.  See lib/silccore/silcchannel.h for
386        all modes.
387
388    SilcChannelID *id
389
390        ID of the channel. This includes all the information SILC will ever
391        need.
392
393    bool global_users
394  
395        Boolean value to tell whether there are users outside this server
396        on this channel. This is set to TRUE if router sends message to
397        the server that there are users outside your server on your
398        channel as well. This way server knows that messages needs to be
399        sent to the router for further routing. If this is a normal 
400        server and this channel is not created on this server this field
401        is always TRUE. If this server is a router this field is ignored.
402
403    char *topic
404
405        Current topic of the channel.
406
407    char *cipher
408
409        Default cipher of the channel. If this is NULL then server picks
410        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
411
412    char *hmac_name
413
414        Default hmac of the channel. If this is NULL then server picks
415        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
416
417    SilcPublicKey founder_key
418    SilcAuthMethod founder_method
419    unsigned char *founder_passwd
420    SilcUInt32 founder_passwd_len
421
422        If the SILC_CMODE_FOUNDER_AUTH has been set then these will include
423        the founder's public key, authentication method and the password
424        if the method is SILC_AUTH_PASSWORD.  If it is SILC_AUTH_PUBLIC_KEY
425        then the `founder_passwd' is NULL.
426
427    SilcHashTable user_list
428
429        All users joined on this channel.  Note that the context saved to
430        this entry shares memory with the client entrys `channels' hash
431        table.
432
433    SilcServerEntry router
434
435        This is a pointer to the server list. This is the router server 
436        whose cell this channel belongs to. This is used to route messages 
437        to this channel.
438
439    SilcCipher channel_key
440
441        The key of the channel (the cipher actually).
442
443    unsigned char *key
444    SilcUInt32 key_len
445
446        Raw key data of the channel key.
447
448    unsigned char iv[SILC_CIPHER_MAX_IV_SIZE]
449
450        Current initial vector. Initial vector is received always along
451        with the channel packet. By default this is filled with NULL.
452
453    SilcHmac hmac;
454
455        HMAC of the channel.
456
457    SilcServerChannelRekey rekey
458
459        Channel key re-key context.
460
461 */
462 struct SilcChannelEntryStruct {
463   char *channel_name;
464   SilcUInt32 mode;
465   SilcChannelID *id;
466   bool global_users;
467   char *topic;
468   char *cipher;
469   char *hmac_name;
470
471   SilcPublicKey founder_key;
472   SilcAuthMethod founder_method;
473   unsigned char *founder_passwd;
474   SilcUInt32 founder_passwd_len;
475
476   SilcUInt32 user_limit;
477   unsigned char *passphrase;
478   char *invite_list;
479   char *ban_list;
480
481   /* All users on this channel */
482   SilcHashTable user_list;
483   SilcUInt32 user_count;
484
485   /* Pointer to the router */
486   SilcServerEntry router;
487
488   /* Channel keys */
489   SilcCipher channel_key;
490   unsigned char *key;
491   SilcUInt32 key_len;
492   unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
493   SilcHmac hmac;
494
495   SilcServerChannelRekey rekey;
496
497   unsigned long created;
498   bool disabled;
499 };
500
501 /* 
502    SILC ID List object.
503
504    As for remainder these lists are defined as follows:
505
506    Entry list (cache)  Server type   List type      Contents
507    =======================================================================
508    servers             server        local list     Server itself
509    servers             server        global list    NULL
510    servers             router        local list     All servers in cell
511    servers             router        global list    All servers in SILC
512
513    clients             server        local list     All clients in server
514    clients             server        global list    NULL
515    clients             router        local list     All clients in cell
516    clients             router        global list    All clients in SILC
517
518    channels            server        local list     All channels in server
519    channels            server        global list    NULL
520    channels            router        local list     All channels in cell
521    channels            router        global list    All channels in SILC
522
523    As seen on the list normal server never defines a global list. This is
524    because of normal server don't know anything about anything global data,
525    they get it from the router if and when they need it. Routers, on the
526    other hand, always define local and global lists because routers really
527    know all the relevant data in the SILC network.
528
529    This object is used as local and global list by the server/router.
530    Above table shows how this is defined on different conditions.
531
532    This object holds pointers to the ID cache system. Every ID cache entry
533    has a specific context pointer to allocated entry (server, client or
534    channel entry).
535
536 */
537 typedef struct SilcIDListStruct {
538   SilcIDCache servers;
539   SilcIDCache clients;
540   SilcIDCache channels;
541 } *SilcIDList;
542
543 /*
544    ID Entry for Unknown connections.
545
546    This is used during authentication phases where we still don't know 
547    what kind of connection remote connection is, hence, we will use this
548    structure instead until we know what type of connection remote end is.
549
550    This is not in any list. This is always individually allocated and
551    used as such.
552
553 */
554 typedef struct {
555   /* Generic data structure. DO NOT add anything before this! */
556   SilcIDListDataStruct data;
557 } *SilcUnknownEntry;
558
559 /* Prototypes */
560 void silc_idlist_add_data(void *entry, SilcIDListData idata);
561 void silc_idlist_del_data(void *entry);
562 SILC_TASK_CALLBACK_GLOBAL(silc_idlist_purge);
563 SilcServerEntry 
564 silc_idlist_add_server(SilcIDList id_list, 
565                        char *server_name, int server_type,
566                        SilcServerID *id, SilcServerEntry router,
567                        void *connection);
568 SilcServerEntry
569 silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
570                               bool registered, SilcIDCacheEntry *ret_entry);
571 SilcServerEntry
572 silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
573                                 bool registered, SilcIDCacheEntry *ret_entry);
574 SilcServerEntry
575 silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
576                                 int port, bool registered,
577                                 SilcIDCacheEntry *ret_entry);
578 SilcServerEntry
579 silc_idlist_replace_server_id(SilcIDList id_list, SilcServerID *old_id,
580                               SilcServerID *new_id);
581 int silc_idlist_del_server(SilcIDList id_list, SilcServerEntry entry);
582 SilcClientEntry
583 silc_idlist_add_client(SilcIDList id_list, char *nickname, char *username, 
584                        char *userinfo, SilcClientID *id, 
585                        SilcServerEntry router, void *connection,
586                        int expire);
587 int silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry);
588 int silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
589                                         char *server, 
590                                         SilcClientEntry **clients,
591                                         SilcUInt32 *clients_count);
592 int silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
593                                     SilcHash md5hash,
594                                     SilcClientEntry **clients,
595                                     SilcUInt32 *clients_count);
596 SilcClientEntry
597 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id,
598                               bool registered, SilcIDCacheEntry *ret_entry);
599 SilcClientEntry
600 silc_idlist_replace_client_id(SilcIDList id_list, SilcClientID *old_id,
601                               SilcClientID *new_id, const char *nickname);
602 void silc_idlist_client_destructor(SilcIDCache cache,
603                                    SilcIDCacheEntry entry);
604 SilcChannelEntry
605 silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
606                         SilcChannelID *id, SilcServerEntry router,
607                         SilcCipher channel_key, SilcHmac hmac,
608                         int expire);
609 int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry);
610 SilcChannelEntry
611 silc_idlist_find_channel_by_name(SilcIDList id_list, char *name,
612                                  SilcIDCacheEntry *ret_entry);
613 SilcChannelEntry
614 silc_idlist_find_channel_by_id(SilcIDList id_list, SilcChannelID *id,
615                                SilcIDCacheEntry *ret_entry);
616 SilcChannelEntry
617 silc_idlist_replace_channel_id(SilcIDList id_list, SilcChannelID *old_id,
618                                SilcChannelID *new_id);
619 SilcChannelEntry *
620 silc_idlist_get_channels(SilcIDList id_list, SilcChannelID *channel_id,
621                          SilcUInt32 *channels_count);
622
623 #endif