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