/
client.c
1717 lines (1403 loc) · 38.2 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* Please read the file COPYING, README and AUTHORS for more information.
*/
#define __client_c__
#include "portab.h"
/**
* @file
* Client management.
*/
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <netdb.h>
#include "conn.h"
#include "ngircd.h"
#include "channel.h"
#include "conf.h"
#include "conn-func.h"
#include "hash.h"
#include "irc-write.h"
#include "log.h"
#include "match.h"
#include "messages.h"
#define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
static CLIENT *This_Server, *My_Clients;
static WHOWAS My_Whowas[MAX_WHOWAS];
static int Last_Whowas = -1;
static long Max_Users, My_Max_Users;
static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
static CLIENT *New_Client_Struct PARAMS(( void ));
static void Generate_MyToken PARAMS(( CLIENT *Client ));
static void Adjust_Counters PARAMS(( CLIENT *Client ));
static void Free_Client PARAMS(( CLIENT **Client ));
static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
CLIENT *TopServer, int Type, const char *ID,
const char *User, const char *Hostname, const char *Info,
int Hops, int Token, const char *Modes,
bool Idented));
static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
bool SendQuit));
static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
void *i));
GLOBAL void
Client_Init( void )
{
struct hostent *h;
This_Server = New_Client_Struct( );
if( ! This_Server )
{
Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
exit( 1 );
}
/* Client structure for this server */
This_Server->next = NULL;
This_Server->type = CLIENT_SERVER;
This_Server->conn_id = NONE;
This_Server->introducer = This_Server;
This_Server->mytoken = 1;
This_Server->hops = 0;
gethostname( This_Server->host, CLIENT_HOST_LEN );
if (Conf_DNS) {
h = gethostbyname( This_Server->host );
if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
}
Client_SetID( This_Server, Conf_ServerName );
Client_SetInfo( This_Server, Conf_ServerInfo );
My_Clients = This_Server;
memset( &My_Whowas, 0, sizeof( My_Whowas ));
} /* Client_Init */
GLOBAL void
Client_Exit( void )
{
CLIENT *c, *next;
int cnt;
if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
else Client_Destroy( This_Server, "Server going down.", NULL, false );
cnt = 0;
c = My_Clients;
while(c) {
cnt++;
next = (CLIENT *)c->next;
Free_Client(&c);
c = next;
}
if (cnt)
Log(LOG_INFO, "Freed %d client structure%s.",
cnt, cnt == 1 ? "" : "s");
} /* Client_Exit */
GLOBAL CLIENT *
Client_ThisServer( void )
{
return This_Server;
} /* Client_ThisServer */
/**
* Initialize new local client; wrapper function for Init_New_Client().
* @return New CLIENT structure.
*/
GLOBAL CLIENT *
Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
{
return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
Hostname, NULL, 0, 0, NULL, Idented);
} /* Client_NewLocal */
/**
* Initialize new remote server; wrapper function for Init_New_Client().
* @return New CLIENT structure.
*/
GLOBAL CLIENT *
Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
int Hops, int Token, const char *Info, bool Idented)
{
return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
} /* Client_NewRemoteServer */
/**
* Initialize new remote client; wrapper function for Init_New_Client().
* @return New CLIENT structure.
*/
GLOBAL CLIENT *
Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
{
return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
User, Hostname, Info, Hops, Token, Modes, Idented);
} /* Client_NewRemoteUser */
/**
* Initialize new client and set up the given parameters like client type,
* user name, host name, introducing server etc. ...
* @return New CLIENT structure.
*/
static CLIENT *
Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
int Type, const char *ID, const char *User, const char *Hostname,
const char *Info, int Hops, int Token, const char *Modes, bool Idented)
{
CLIENT *client;
assert(Idx >= NONE);
assert(Introducer != NULL);
client = New_Client_Struct();
if (!client)
return NULL;
client->starttime = time(NULL);
client->conn_id = Idx;
client->introducer = Introducer;
client->topserver = TopServer;
client->type = Type;
if (ID)
Client_SetID(client, ID);
if (User) {
Client_SetUser(client, User, Idented);
Client_SetOrigUser(client, User);
}
if (Hostname)
Client_SetHostname(client, Hostname);
if (Info)
Client_SetInfo(client, Info);
client->hops = Hops;
client->token = Token;
if (Modes)
Client_SetModes(client, Modes);
if (Type == CLIENT_SERVER)
Generate_MyToken(client);
if (Client_HasMode(client, 'a'))
client->away = strndup(DEFAULT_AWAY_MSG, CLIENT_AWAY_LEN - 1);
client->next = (POINTER *)My_Clients;
My_Clients = client;
Adjust_Counters(client);
return client;
} /* Init_New_Client */
GLOBAL void
Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
{
/* remove a client */
CLIENT *last, *c;
char msg[COMMAND_LEN];
const char *txt;
assert( Client != NULL );
txt = LogMsg ? LogMsg : FwdMsg;
if (!txt)
txt = "Reason unknown";
/* netsplit message */
if( Client->type == CLIENT_SERVER ) {
strlcpy(msg, This_Server->id, sizeof (msg));
strlcat(msg, " ", sizeof (msg));
strlcat(msg, Client->id, sizeof (msg));
}
last = NULL;
c = My_Clients;
while( c )
{
if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
{
/*
* The client that is about to be removed is a server,
* the client we are checking right now is a child of that
* server and thus has to be removed, too.
*
* Call Client_Destroy() recursively with the server as the
* new "object to be removed". This starts the cycle again, until
* all servers that are linked via the original server have been
* removed.
*/
Client_Destroy( c, NULL, msg, false );
last = NULL;
c = My_Clients;
continue;
}
if( c == Client )
{
/* found the client: remove it */
if( last ) last->next = c->next;
else My_Clients = (CLIENT *)c->next;
if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
else if( c->type == CLIENT_SERVER )
{
if (c != This_Server) {
if (c->conn_id != NONE)
Log(LOG_NOTICE|LOG_snotice,
"Server \"%s\" unregistered (connection %d): %s.",
c->id, c->conn_id, txt);
else
Log(LOG_NOTICE|LOG_snotice,
"Server \"%s\" unregistered: %s.",
c->id, txt);
}
/* inform other servers */
if( ! NGIRCd_SignalQuit )
{
if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
}
}
else
{
if (c->conn_id != NONE) {
if (c->id[0])
Log(LOG_NOTICE,
"Client \"%s\" unregistered (connection %d): %s.",
c->id, c->conn_id, txt);
else
Log(LOG_NOTICE,
"Client unregistered (connection %d): %s.",
c->conn_id, txt);
} else {
Log(LOG_WARNING,
"Unregistered unknown client \"%s\": %s",
c->id[0] ? c->id : "(No Nick)", txt);
}
}
Free_Client(&c);
break;
}
last = c;
c = (CLIENT *)c->next;
}
} /* Client_Destroy */
/**
* Set client hostname.
*
* If global hostname cloaking is in effect, don't set the real hostname
* but the configured one.
*
* @param Client The client of which the hostname should be set.
* @param Hostname The new hostname.
*/
GLOBAL void
Client_SetHostname( CLIENT *Client, const char *Hostname )
{
assert(Client != NULL);
assert(Hostname != NULL);
/* Only cloak the hostmask if it has not yet been cloaked (the period
* indicates it's still an IP address). */
if (Conf_CloakHost[0] && strchr(Client->host, '.')) {
char cloak[GETID_LEN];
strlcpy(cloak, Hostname, GETID_LEN);
strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
Client_ID(Client), Client->host, cloak);
strlcpy(Client->host, cloak, sizeof(Client->host));
} else {
LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
Client_ID(Client), Client->host, Hostname);
strlcpy(Client->host, Hostname, sizeof(Client->host));
}
} /* Client_SetHostname */
/**
* Set IP address to display for a client.
*
* @param Client The client.
* @param IPAText Textual representation of the IP address or NULL to unset.
*/
GLOBAL void
Client_SetIPAText(CLIENT *Client, const char *IPAText)
{
assert(Client != NULL);
if (Client->ipa_text)
free(Client->ipa_text);
if (*IPAText)
Client->ipa_text = strndup(IPAText, CLIENT_HOST_LEN - 1);
else
Client->ipa_text = NULL;
}
GLOBAL void
Client_SetID( CLIENT *Client, const char *ID )
{
assert( Client != NULL );
assert( ID != NULL );
strlcpy( Client->id, ID, sizeof( Client->id ));
if (Conf_CloakUserToNick) {
strlcpy( Client->user, ID, sizeof( Client->user ));
strlcpy( Client->info, ID, sizeof( Client->info ));
}
/* Hash */
Client->hash = Hash( Client->id );
} /* Client_SetID */
GLOBAL void
Client_SetUser( CLIENT *Client, const char *User, bool Idented )
{
/* set clients username */
assert( Client != NULL );
assert( User != NULL );
if (Conf_CloakUserToNick) {
strlcpy(Client->user, Client->id, sizeof(Client->user));
} else if (Idented) {
strlcpy(Client->user, User, sizeof(Client->user));
} else {
Client->user[0] = '~';
strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
}
} /* Client_SetUser */
/**
* Set "original" user name of a client.
* This function saves the "original" user name, the user name specified by
* the peer using the USER command, into the CLIENT structure. This user
* name may be used for authentication, for example.
* @param Client The client.
* @param User User name to set.
*/
GLOBAL void
Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
{
assert(Client != NULL);
assert(User != NULL);
#if defined(PAM)
strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
#endif
} /* Client_SetOrigUser */
GLOBAL void
Client_SetInfo( CLIENT *Client, const char *Info )
{
/* set client hostname */
assert( Client != NULL );
assert( Info != NULL );
if (Conf_CloakUserToNick)
strlcpy(Client->info, Client->id, sizeof(Client->info));
else
strlcpy(Client->info, Info, sizeof(Client->info));
} /* Client_SetInfo */
GLOBAL void
Client_SetModes( CLIENT *Client, const char *Modes )
{
assert( Client != NULL );
assert( Modes != NULL );
strlcpy(Client->modes, Modes, sizeof( Client->modes ));
} /* Client_SetModes */
GLOBAL void
Client_SetFlags( CLIENT *Client, const char *Flags )
{
assert( Client != NULL );
assert( Flags != NULL );
strlcpy(Client->flags, Flags, sizeof(Client->flags));
} /* Client_SetFlags */
GLOBAL void
Client_SetAccountName(CLIENT *Client, const char *AccountName)
{
assert(Client != NULL);
if (Client->account_name)
free(Client->account_name);
if (*AccountName)
Client->account_name = strndup(AccountName,
CLIENT_NICK_LEN - 1);
else
Client->account_name = NULL;
}
GLOBAL void
Client_SetAway( CLIENT *Client, const char *Txt )
{
/* Set AWAY reason of client */
assert( Client != NULL );
assert( Txt != NULL );
if (Client->away)
free(Client->away);
Client->away = strndup(Txt, CLIENT_AWAY_LEN - 1);
LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
Client_Mask(Client), Txt);
} /* Client_SetAway */
GLOBAL void
Client_SetType( CLIENT *Client, int Type )
{
assert( Client != NULL );
Client->type = Type;
if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
Adjust_Counters( Client );
} /* Client_SetType */
GLOBAL void
Client_SetHops( CLIENT *Client, int Hops )
{
assert( Client != NULL );
Client->hops = Hops;
} /* Client_SetHops */
GLOBAL void
Client_SetToken( CLIENT *Client, int Token )
{
assert( Client != NULL );
Client->token = Token;
} /* Client_SetToken */
GLOBAL void
Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
{
assert( Client != NULL );
assert( Introducer != NULL );
Client->introducer = Introducer;
} /* Client_SetIntroducer */
GLOBAL bool
Client_ModeAdd( CLIENT *Client, char Mode )
{
/* Set Mode.
* If Client already had Mode, return false.
* If the Mode was newly set, return true.
*/
char x[2];
assert( Client != NULL );
x[0] = Mode; x[1] = '\0';
if (!Client_HasMode(Client, x[0])) {
strlcat( Client->modes, x, sizeof( Client->modes ));
return true;
}
else return false;
} /* Client_ModeAdd */
GLOBAL bool
Client_ModeDel( CLIENT *Client, char Mode )
{
/* Delete Mode.
* If Mode was removed, return true.
* If Client did not have Mode, return false.
*/
char x[2], *p;
assert( Client != NULL );
x[0] = Mode; x[1] = '\0';
p = strchr( Client->modes, x[0] );
if( ! p ) return false;
/* Client has Mode -> delete */
while( *p )
{
*p = *(p + 1);
p++;
}
return true;
} /* Client_ModeDel */
/**
* Search CLIENT structure of a given nick name.
*
* @return Pointer to CLIENT structure or NULL if not found.
*/
GLOBAL CLIENT *
Client_Search( const char *Nick )
{
char search_id[CLIENT_ID_LEN], *ptr;
CLIENT *c = NULL;
UINT32 search_hash;
assert( Nick != NULL );
/* copy Nick and truncate hostmask if necessary */
strlcpy( search_id, Nick, sizeof( search_id ));
ptr = strchr( search_id, '!' );
if( ptr ) *ptr = '\0';
search_hash = Hash(search_id);
c = My_Clients;
while (c) {
if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
return c;
c = (CLIENT *)c->next;
}
return NULL;
}
/**
* Search first CLIENT structure matching a given mask of a server.
*
* The order of servers is arbitrary, but this function makes sure that the
* local server is always returned if the mask matches it.
*
* @return Pointer to CLIENT structure or NULL if no server could be found.
*/
GLOBAL CLIENT *
Client_SearchServer(const char *Mask)
{
CLIENT *c;
assert(Mask != NULL);
/* First check if mask matches the local server */
if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
return Client_ThisServer();
c = My_Clients;
while (c) {
if (Client_Type(c) == CLIENT_SERVER) {
/* This is a server: check if Mask matches */
if (MatchCaseInsensitive(Mask, c->id))
return c;
}
c = (CLIENT *)c->next;
}
return NULL;
}
/**
* Get client structure ("introducer") identfied by a server token.
* @return CLIENT structure or NULL if none could be found.
*/
GLOBAL CLIENT *
Client_GetFromToken( CLIENT *Client, int Token )
{
CLIENT *c;
assert( Client != NULL );
if (!Token)
return NULL;
c = My_Clients;
while (c) {
if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
(c->token == Token))
return c;
c = (CLIENT *)c->next;
}
return NULL;
} /* Client_GetFromToken */
GLOBAL int
Client_Type( CLIENT *Client )
{
assert( Client != NULL );
return Client->type;
} /* Client_Type */
GLOBAL CONN_ID
Client_Conn( CLIENT *Client )
{
assert( Client != NULL );
return Client->conn_id;
} /* Client_Conn */
GLOBAL char *
Client_ID( CLIENT *Client )
{
assert( Client != NULL );
#ifdef DEBUG
if(Client->type == CLIENT_USER)
assert(strlen(Client->id) < Conf_MaxNickLength);
#endif
if( Client->id[0] ) return Client->id;
else return "*";
} /* Client_ID */
GLOBAL char *
Client_Info( CLIENT *Client )
{
assert( Client != NULL );
return Client->info;
} /* Client_Info */
GLOBAL char *
Client_User( CLIENT *Client )
{
assert( Client != NULL );
return Client->user[0] ? Client->user : "~";
} /* Client_User */
#ifdef PAM
/**
* Get the "original" user name as supplied by the USER command.
* The user name as given by the client is used for authentication instead
* of the one detected using IDENT requests.
* @param Client The client.
* @return Original user name.
*/
GLOBAL char *
Client_OrigUser(CLIENT *Client) {
return Client->orig_user;
} /* Client_OrigUser */
#endif
/**
* Return the hostname of a client.
* @param Client Pointer to client structure
* @return Pointer to client hostname
*/
GLOBAL char *
Client_Hostname(CLIENT *Client)
{
assert (Client != NULL);
return Client->host;
}
/**
* Return the cloaked hostname of a client, if set.
* @param Client Pointer to the client structure.
* @return Pointer to the cloaked hostname or NULL if not set.
*/
GLOBAL char *
Client_HostnameCloaked(CLIENT *Client)
{
assert(Client != NULL);
return Client->cloaked;
}
/**
* Get (potentially cloaked) hostname of a client to display it to other users.
*
* If the client has not enabled cloaking, the real hostname is used.
*
* @param Client Pointer to client structure
* @return Pointer to client hostname
*/
GLOBAL char *
Client_HostnameDisplayed(CLIENT *Client)
{
assert(Client != NULL);
/* Client isn't cloaked at all, return real hostname: */
if (!Client_HasMode(Client, 'x'))
return Client_Hostname(Client);
/* Use an already saved cloaked hostname, if there is one */
if (Client->cloaked)
return Client->cloaked;
Client_UpdateCloakedHostname(Client, NULL, NULL);
return Client->cloaked;
}
GLOBAL const char *
Client_IPAText(CLIENT *Client)
{
assert(Client != NULL);
/* Not a local client? */
if (Client_Conn(Client) <= NONE)
return "0.0.0.0";
if (!Client->ipa_text)
return Conn_GetIPAInfo(Client_Conn(Client));
else
return Client->ipa_text;
}
/**
* Update (and generate, if necessary) the cloaked hostname of a client.
*
* The newly set cloaked hostname is announced in the network using METADATA
* commands to peers that support this feature.
*
* @param Client The client of which the cloaked hostname should be updated.
* @param Origin The originator of the hostname change, or NULL if this server.
* @param Hostname The new cloaked hostname, or NULL if it should be generated.
*/
GLOBAL void
Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
const char *Hostname)
{
char Cloak_Buffer[CLIENT_HOST_LEN];
assert(Client != NULL);
if (!Origin)
Origin = Client_ThisServer();
if (!Client->cloaked) {
Client->cloaked = malloc(CLIENT_HOST_LEN);
if (!Client->cloaked)
return;
}
if (!Hostname) {
/* Generate new cloaked hostname */
if (*Conf_CloakHostModeX) {
strlcpy(Cloak_Buffer, Client->host,
sizeof(Cloak_Buffer));
strlcat(Cloak_Buffer, Conf_CloakHostSalt,
sizeof(Cloak_Buffer));
snprintf(Client->cloaked, CLIENT_HOST_LEN,
Conf_CloakHostModeX, Hash(Cloak_Buffer));
} else
strlcpy(Client->cloaked, Client_ID(Client->introducer),
CLIENT_HOST_LEN);
} else
strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
Client_ID(Client), Client->cloaked);
/* Inform other servers in the network */
IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
"METADATA %s cloakhost :%s",
Client_ID(Client), Client->cloaked);
}
GLOBAL char *
Client_Modes( CLIENT *Client )
{
assert( Client != NULL );
return Client->modes;
} /* Client_Modes */
GLOBAL char *
Client_Flags( CLIENT *Client )
{
assert( Client != NULL );
return Client->flags;
} /* Client_Flags */
GLOBAL int
Client_Hops( CLIENT *Client )
{
assert( Client != NULL );
return Client->hops;
} /* Client_Hops */
GLOBAL int
Client_Token( CLIENT *Client )
{
assert( Client != NULL );
return Client->token;
} /* Client_Token */
GLOBAL int
Client_MyToken( CLIENT *Client )
{
assert( Client != NULL );
return Client->mytoken;
} /* Client_MyToken */
GLOBAL CLIENT *
Client_NextHop( CLIENT *Client )
{
CLIENT *c;
assert( Client != NULL );
c = Client;
while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
c = c->introducer;
return c;
} /* Client_NextHop */
/**
* Return ID of a client: "client!user@host"
* This client ID is used for IRC prefixes, for example.
* Please note that this function uses a global static buffer, so you can't
* nest invocations without overwriting earlier results!
* @param Client Pointer to client structure
* @return Pointer to global buffer containing the client ID
*/
GLOBAL char *
Client_Mask( CLIENT *Client )
{
static char Mask_Buffer[GETID_LEN];
assert (Client != NULL);
/* Servers: return name only, there is no "mask" */
if (Client->type == CLIENT_SERVER)
return Client->id;
snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
Client->id, Client->user, Client->host);
return Mask_Buffer;
} /* Client_Mask */
/**
* Return ID of a client with cloaked hostname: "client!user@server-name"
*
* This client ID is used for IRC prefixes, for example.
* Please note that this function uses a global static buffer, so you can't
* nest invocations without overwriting earlier results!
* If the client has not enabled cloaking, the real hostname is used.
*
* @param Client Pointer to client structure
* @return Pointer to global buffer containing the client ID
*/
GLOBAL char *
Client_MaskCloaked(CLIENT *Client)
{
static char Mask_Buffer[GETID_LEN];
assert (Client != NULL);
/* Is the client using cloaking at all? */
if (!Client_HasMode(Client, 'x'))
return Client_Mask(Client);
snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
Client_HostnameDisplayed(Client));
return Mask_Buffer;
} /* Client_MaskCloaked */
GLOBAL CLIENT *
Client_Introducer( CLIENT *Client )
{
assert( Client != NULL );
return Client->introducer;
} /* Client_Introducer */
GLOBAL CLIENT *
Client_TopServer( CLIENT *Client )
{
assert( Client != NULL );
return Client->topserver;
} /* Client_TopServer */
GLOBAL bool
Client_HasMode( CLIENT *Client, char Mode )
{
assert( Client != NULL );
return strchr( Client->modes, Mode ) != NULL;
} /* Client_HasMode */
GLOBAL bool
Client_HasFlag( CLIENT *Client, char Flag )
{
assert( Client != NULL );
return strchr( Client->flags, Flag ) != NULL;
} /* Client_HasFlag */
GLOBAL char *
Client_Away( CLIENT *Client )
{
assert( Client != NULL );
return Client->away;
} /* Client_Away */