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