client.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2012 Alexander Barton (alex@barton.de)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #define __client_c__
  12. #include "portab.h"
  13. /**
  14. * @file
  15. * Client management.
  16. */
  17. #include "imp.h"
  18. #include <assert.h>
  19. #include <unistd.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <strings.h>
  24. #include <netdb.h>
  25. #include "defines.h"
  26. #include "conn.h"
  27. #include "exp.h"
  28. #include "client.h"
  29. #include <imp.h>
  30. #include "ngircd.h"
  31. #include "channel.h"
  32. #include "conf.h"
  33. #include "conn-func.h"
  34. #include "hash.h"
  35. #include "irc-write.h"
  36. #include "log.h"
  37. #include "messages.h"
  38. #include <exp.h>
  39. #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
  40. static CLIENT *This_Server, *My_Clients;
  41. static WHOWAS My_Whowas[MAX_WHOWAS];
  42. static int Last_Whowas = -1;
  43. static long Max_Users, My_Max_Users;
  44. static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
  45. static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
  46. static CLIENT *New_Client_Struct PARAMS(( void ));
  47. static void Generate_MyToken PARAMS(( CLIENT *Client ));
  48. static void Adjust_Counters PARAMS(( CLIENT *Client ));
  49. static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
  50. CLIENT *TopServer, int Type, const char *ID,
  51. const char *User, const char *Hostname, const char *Info,
  52. int Hops, int Token, const char *Modes,
  53. bool Idented));
  54. static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
  55. bool SendQuit));
  56. static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
  57. void *i));
  58. GLOBAL void
  59. Client_Init( void )
  60. {
  61. struct hostent *h;
  62. This_Server = New_Client_Struct( );
  63. if( ! This_Server )
  64. {
  65. Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
  66. Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
  67. exit( 1 );
  68. }
  69. /* Client-Struktur dieses Servers */
  70. This_Server->next = NULL;
  71. This_Server->type = CLIENT_SERVER;
  72. This_Server->conn_id = NONE;
  73. This_Server->introducer = This_Server;
  74. This_Server->mytoken = 1;
  75. This_Server->hops = 0;
  76. gethostname( This_Server->host, CLIENT_HOST_LEN );
  77. if (Conf_DNS) {
  78. h = gethostbyname( This_Server->host );
  79. if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
  80. }
  81. Client_SetID( This_Server, Conf_ServerName );
  82. Client_SetInfo( This_Server, Conf_ServerInfo );
  83. My_Clients = This_Server;
  84. memset( &My_Whowas, 0, sizeof( My_Whowas ));
  85. } /* Client_Init */
  86. GLOBAL void
  87. Client_Exit( void )
  88. {
  89. CLIENT *c, *next;
  90. int cnt;
  91. if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
  92. else Client_Destroy( This_Server, "Server going down.", NULL, false );
  93. cnt = 0;
  94. c = My_Clients;
  95. while( c )
  96. {
  97. cnt++;
  98. next = (CLIENT *)c->next;
  99. free( c );
  100. c = next;
  101. }
  102. if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
  103. } /* Client_Exit */
  104. GLOBAL CLIENT *
  105. Client_ThisServer( void )
  106. {
  107. return This_Server;
  108. } /* Client_ThisServer */
  109. /**
  110. * Initialize new local client; wrapper function for Init_New_Client().
  111. * @return New CLIENT structure.
  112. */
  113. GLOBAL CLIENT *
  114. Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
  115. {
  116. return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
  117. Hostname, NULL, 0, 0, NULL, Idented);
  118. } /* Client_NewLocal */
  119. /**
  120. * Initialize new remote server; wrapper function for Init_New_Client().
  121. * @return New CLIENT structure.
  122. */
  123. GLOBAL CLIENT *
  124. Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
  125. int Hops, int Token, const char *Info, bool Idented)
  126. {
  127. return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
  128. Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
  129. } /* Client_NewRemoteServer */
  130. /**
  131. * Initialize new remote client; wrapper function for Init_New_Client().
  132. * @return New CLIENT structure.
  133. */
  134. GLOBAL CLIENT *
  135. Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
  136. const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
  137. {
  138. return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
  139. User, Hostname, Info, Hops, Token, Modes, Idented);
  140. } /* Client_NewRemoteUser */
  141. /**
  142. * Initialize new client and set up the given parameters like client type,
  143. * user name, host name, introducing server etc. ...
  144. * @return New CLIENT structure.
  145. */
  146. static CLIENT *
  147. Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
  148. int Type, const char *ID, const char *User, const char *Hostname,
  149. const char *Info, int Hops, int Token, const char *Modes, bool Idented)
  150. {
  151. CLIENT *client;
  152. assert(Idx >= NONE);
  153. assert(Introducer != NULL);
  154. client = New_Client_Struct();
  155. if (!client)
  156. return NULL;
  157. client->starttime = time(NULL);
  158. client->conn_id = Idx;
  159. client->introducer = Introducer;
  160. client->topserver = TopServer;
  161. client->type = Type;
  162. if (ID)
  163. Client_SetID(client, ID);
  164. if (User) {
  165. Client_SetUser(client, User, Idented);
  166. Client_SetOrigUser(client, User);
  167. }
  168. if (Hostname)
  169. Client_SetHostname(client, Hostname);
  170. if (Info)
  171. Client_SetInfo(client, Info);
  172. client->hops = Hops;
  173. client->token = Token;
  174. if (Modes)
  175. Client_SetModes(client, Modes);
  176. if (Type == CLIENT_SERVER)
  177. Generate_MyToken(client);
  178. if (strchr(client->modes, 'a'))
  179. strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
  180. client->next = (POINTER *)My_Clients;
  181. My_Clients = client;
  182. Adjust_Counters(client);
  183. return client;
  184. } /* Init_New_Client */
  185. GLOBAL void
  186. Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
  187. {
  188. /* remove a client */
  189. CLIENT *last, *c;
  190. char msg[LINE_LEN];
  191. const char *txt;
  192. assert( Client != NULL );
  193. if( LogMsg ) txt = LogMsg;
  194. else txt = FwdMsg;
  195. if( ! txt ) txt = "Reason unknown.";
  196. /* netsplit message */
  197. if( Client->type == CLIENT_SERVER ) {
  198. strlcpy(msg, This_Server->id, sizeof (msg));
  199. strlcat(msg, " ", sizeof (msg));
  200. strlcat(msg, Client->id, sizeof (msg));
  201. }
  202. last = NULL;
  203. c = My_Clients;
  204. while( c )
  205. {
  206. if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
  207. {
  208. /*
  209. * The client that is about to be removed is a server,
  210. * the client we are checking right now is a child of that
  211. * server and thus has to be removed, too.
  212. *
  213. * Call Client_Destroy() recursively with the server as the
  214. * new "object to be removed". This starts the cycle again, until
  215. * all servers that are linked via the original server have been
  216. * removed.
  217. */
  218. Client_Destroy( c, NULL, msg, false );
  219. last = NULL;
  220. c = My_Clients;
  221. continue;
  222. }
  223. if( c == Client )
  224. {
  225. /* found the client: remove it */
  226. if( last ) last->next = c->next;
  227. else My_Clients = (CLIENT *)c->next;
  228. if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
  229. Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
  230. else if( c->type == CLIENT_SERVER )
  231. {
  232. if( c != This_Server )
  233. {
  234. if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
  235. else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
  236. }
  237. /* inform other servers */
  238. if( ! NGIRCd_SignalQuit )
  239. {
  240. if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
  241. else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
  242. }
  243. }
  244. else
  245. {
  246. if( c->conn_id != NONE )
  247. {
  248. if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
  249. else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
  250. } else {
  251. Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
  252. c->id[0] ? c->id : "(No Nick)", txt );
  253. }
  254. }
  255. free( c );
  256. break;
  257. }
  258. last = c;
  259. c = (CLIENT *)c->next;
  260. }
  261. } /* Client_Destroy */
  262. /**
  263. * Set client hostname.
  264. *
  265. * If global hostname cloaking is in effect, don't set the real hostname
  266. * but the configured one.
  267. *
  268. * @param Client The client of which the hostname should be set.
  269. * @param Hostname The new hostname.
  270. */
  271. GLOBAL void
  272. Client_SetHostname( CLIENT *Client, const char *Hostname )
  273. {
  274. assert(Client != NULL);
  275. assert(Hostname != NULL);
  276. if (strlen(Conf_CloakHost)) {
  277. char cloak[GETID_LEN];
  278. strlcpy(cloak, Hostname, GETID_LEN);
  279. strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
  280. snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
  281. LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
  282. Client_ID(Client), Client->host, cloak);
  283. strlcpy(Client->host, cloak, sizeof(Client->host));
  284. } else {
  285. LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
  286. Client_ID(Client), Client->host, Hostname);
  287. strlcpy(Client->host, Hostname, sizeof(Client->host));
  288. }
  289. } /* Client_SetHostname */
  290. GLOBAL void
  291. Client_SetID( CLIENT *Client, const char *ID )
  292. {
  293. assert( Client != NULL );
  294. assert( ID != NULL );
  295. strlcpy( Client->id, ID, sizeof( Client->id ));
  296. if (Conf_CloakUserToNick) {
  297. strlcpy( Client->user, ID, sizeof( Client->user ));
  298. strlcpy( Client->info, ID, sizeof( Client->info ));
  299. }
  300. /* Hash */
  301. Client->hash = Hash( Client->id );
  302. } /* Client_SetID */
  303. GLOBAL void
  304. Client_SetUser( CLIENT *Client, const char *User, bool Idented )
  305. {
  306. /* set clients username */
  307. assert( Client != NULL );
  308. assert( User != NULL );
  309. if (Conf_CloakUserToNick) {
  310. strlcpy(Client->user, Client->id, sizeof(Client->user));
  311. } else if (Idented) {
  312. strlcpy(Client->user, User, sizeof(Client->user));
  313. } else {
  314. Client->user[0] = '~';
  315. strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
  316. }
  317. } /* Client_SetUser */
  318. /**
  319. * Set "original" user name of a client.
  320. * This function saves the "original" user name, the user name specified by
  321. * the peer using the USER command, into the CLIENT structure. This user
  322. * name may be used for authentication, for example.
  323. * @param Client The client.
  324. * @param User User name to set.
  325. */
  326. GLOBAL void
  327. Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
  328. {
  329. assert(Client != NULL);
  330. assert(User != NULL);
  331. #if defined(PAM) && defined(IDENTAUTH)
  332. strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
  333. #endif
  334. } /* Client_SetOrigUser */
  335. GLOBAL void
  336. Client_SetInfo( CLIENT *Client, const char *Info )
  337. {
  338. /* set client hostname */
  339. assert( Client != NULL );
  340. assert( Info != NULL );
  341. if (Conf_CloakUserToNick)
  342. strlcpy(Client->info, Client->id, sizeof(Client->info));
  343. else
  344. strlcpy(Client->info, Info, sizeof(Client->info));
  345. } /* Client_SetInfo */
  346. GLOBAL void
  347. Client_SetModes( CLIENT *Client, const char *Modes )
  348. {
  349. assert( Client != NULL );
  350. assert( Modes != NULL );
  351. strlcpy(Client->modes, Modes, sizeof( Client->modes ));
  352. } /* Client_SetModes */
  353. GLOBAL void
  354. Client_SetFlags( CLIENT *Client, const char *Flags )
  355. {
  356. assert( Client != NULL );
  357. assert( Flags != NULL );
  358. strlcpy(Client->flags, Flags, sizeof(Client->flags));
  359. } /* Client_SetFlags */
  360. GLOBAL void
  361. Client_SetAway( CLIENT *Client, const char *Txt )
  362. {
  363. /* Set AWAY reason of client */
  364. assert( Client != NULL );
  365. assert( Txt != NULL );
  366. strlcpy( Client->away, Txt, sizeof( Client->away ));
  367. LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
  368. Client_Mask(Client), Txt);
  369. } /* Client_SetAway */
  370. GLOBAL void
  371. Client_SetType( CLIENT *Client, int Type )
  372. {
  373. assert( Client != NULL );
  374. Client->type = Type;
  375. if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
  376. Adjust_Counters( Client );
  377. } /* Client_SetType */
  378. GLOBAL void
  379. Client_SetHops( CLIENT *Client, int Hops )
  380. {
  381. assert( Client != NULL );
  382. Client->hops = Hops;
  383. } /* Client_SetHops */
  384. GLOBAL void
  385. Client_SetToken( CLIENT *Client, int Token )
  386. {
  387. assert( Client != NULL );
  388. Client->token = Token;
  389. } /* Client_SetToken */
  390. GLOBAL void
  391. Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
  392. {
  393. assert( Client != NULL );
  394. assert( Introducer != NULL );
  395. Client->introducer = Introducer;
  396. } /* Client_SetIntroducer */
  397. GLOBAL void
  398. Client_SetOperByMe( CLIENT *Client, bool OperByMe )
  399. {
  400. assert( Client != NULL );
  401. Client->oper_by_me = OperByMe;
  402. } /* Client_SetOperByMe */
  403. GLOBAL bool
  404. Client_ModeAdd( CLIENT *Client, char Mode )
  405. {
  406. /* Set Mode.
  407. * If Client already alread had Mode, return false.
  408. * If the Mode was newly set, return true.
  409. */
  410. char x[2];
  411. assert( Client != NULL );
  412. x[0] = Mode; x[1] = '\0';
  413. if (!strchr( Client->modes, x[0])) {
  414. strlcat( Client->modes, x, sizeof( Client->modes ));
  415. return true;
  416. }
  417. else return false;
  418. } /* Client_ModeAdd */
  419. GLOBAL bool
  420. Client_ModeDel( CLIENT *Client, char Mode )
  421. {
  422. /* Delete Mode.
  423. * If Mode was removed, return true.
  424. * If Client did not have Mode, return false.
  425. */
  426. char x[2], *p;
  427. assert( Client != NULL );
  428. x[0] = Mode; x[1] = '\0';
  429. p = strchr( Client->modes, x[0] );
  430. if( ! p ) return false;
  431. /* Client has Mode -> delete */
  432. while( *p )
  433. {
  434. *p = *(p + 1);
  435. p++;
  436. }
  437. return true;
  438. } /* Client_ModeDel */
  439. GLOBAL CLIENT *
  440. Client_Search( const char *Nick )
  441. {
  442. /* return Client-Structure that has the corresponding Nick.
  443. * If none is found, return NULL.
  444. */
  445. char search_id[CLIENT_ID_LEN], *ptr;
  446. CLIENT *c = NULL;
  447. UINT32 search_hash;
  448. assert( Nick != NULL );
  449. /* copy Nick and truncate hostmask if necessary */
  450. strlcpy( search_id, Nick, sizeof( search_id ));
  451. ptr = strchr( search_id, '!' );
  452. if( ptr ) *ptr = '\0';
  453. search_hash = Hash(search_id);
  454. c = My_Clients;
  455. while (c) {
  456. if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
  457. return c;
  458. c = (CLIENT *)c->next;
  459. }
  460. return NULL;
  461. } /* Client_Search */
  462. /**
  463. * Get client structure ("introducer") identfied by a server token.
  464. * @return CLIENT structure or NULL if none could be found.
  465. */
  466. GLOBAL CLIENT *
  467. Client_GetFromToken( CLIENT *Client, int Token )
  468. {
  469. CLIENT *c;
  470. assert( Client != NULL );
  471. if (!Token)
  472. return NULL;
  473. c = My_Clients;
  474. while (c) {
  475. if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
  476. (c->token == Token))
  477. return c;
  478. c = (CLIENT *)c->next;
  479. }
  480. return NULL;
  481. } /* Client_GetFromToken */
  482. GLOBAL int
  483. Client_Type( CLIENT *Client )
  484. {
  485. assert( Client != NULL );
  486. return Client->type;
  487. } /* Client_Type */
  488. GLOBAL CONN_ID
  489. Client_Conn( CLIENT *Client )
  490. {
  491. assert( Client != NULL );
  492. return Client->conn_id;
  493. } /* Client_Conn */
  494. GLOBAL char *
  495. Client_ID( CLIENT *Client )
  496. {
  497. assert( Client != NULL );
  498. #ifdef DEBUG
  499. if(Client->type == CLIENT_USER)
  500. assert(strlen(Client->id) < Conf_MaxNickLength);
  501. #endif
  502. if( Client->id[0] ) return Client->id;
  503. else return "*";
  504. } /* Client_ID */
  505. GLOBAL char *
  506. Client_Info( CLIENT *Client )
  507. {
  508. assert( Client != NULL );
  509. return Client->info;
  510. } /* Client_Info */
  511. GLOBAL char *
  512. Client_User( CLIENT *Client )
  513. {
  514. assert( Client != NULL );
  515. return Client->user[0] ? Client->user : "~";
  516. } /* Client_User */
  517. #ifdef PAM
  518. /**
  519. * Get the "original" user name as supplied by the USER command.
  520. * The user name as given by the client is used for authentication instead
  521. * of the one detected using IDENT requests.
  522. * @param Client The client.
  523. * @return Original user name.
  524. */
  525. GLOBAL char *
  526. Client_OrigUser(CLIENT *Client) {
  527. #ifndef IDENTAUTH
  528. char *user = Client->user;
  529. if (user[0] == '~')
  530. user++;
  531. return user;
  532. #else
  533. return Client->orig_user;
  534. #endif
  535. } /* Client_OrigUser */
  536. #endif
  537. /**
  538. * Return the hostname of a client.
  539. * @param Client Pointer to client structure
  540. * @return Pointer to client hostname
  541. */
  542. GLOBAL char *
  543. Client_Hostname(CLIENT *Client)
  544. {
  545. assert (Client != NULL);
  546. return Client->host;
  547. }
  548. /**
  549. * Return the cloaked hostname of a client, if set.
  550. * @param Client Pointer to the client structure.
  551. * @return Pointer to the cloaked hostname or NULL if not set.
  552. */
  553. GLOBAL char *
  554. Client_HostnameCloaked(CLIENT *Client)
  555. {
  556. assert(Client != NULL);
  557. return Client->cloaked;
  558. }
  559. /**
  560. * Get (potentially cloaked) hostname of a client to display it to other users.
  561. *
  562. * If the client has not enabled cloaking, the real hostname is used.
  563. * Please note that this function uses a global static buffer, so you can't
  564. * nest invocations without overwriting earlier results!
  565. *
  566. * @param Client Pointer to client structure
  567. * @return Pointer to client hostname
  568. */
  569. GLOBAL char *
  570. Client_HostnameDisplayed(CLIENT *Client)
  571. {
  572. assert(Client != NULL);
  573. /* Client isn't cloaked at all, return real hostname: */
  574. if (!Client_HasMode(Client, 'x'))
  575. return Client_Hostname(Client);
  576. /* Use an already saved cloaked hostname, if there is one */
  577. if (Client->cloaked[0])
  578. return Client->cloaked;
  579. Client_UpdateCloakedHostname(Client, NULL, NULL);
  580. return Client->cloaked;
  581. }
  582. /**
  583. * Update (and generate, if necessary) the cloaked hostname of a client.
  584. *
  585. * The newly set cloaked hostname is announced in the network using METADATA
  586. * commands to peers that support this feature.
  587. *
  588. * @param Client The client of which the cloaked hostname should be updated.
  589. * @param Origin The originator of the hostname change, or NULL if this server.
  590. * @param Hostname The new cloaked hostname, or NULL if it should be generated.
  591. */
  592. GLOBAL void
  593. Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
  594. const char *Hostname)
  595. {
  596. static char Cloak_Buffer[CLIENT_HOST_LEN];
  597. assert(Client != NULL);
  598. if (!Origin)
  599. Origin = Client_ThisServer();
  600. if (!Hostname) {
  601. /* Generate new cloaked hostname */
  602. if (*Conf_CloakHostModeX) {
  603. strlcpy(Cloak_Buffer, Client->host, CLIENT_HOST_LEN);
  604. strlcat(Cloak_Buffer, Conf_CloakHostSalt,
  605. CLIENT_HOST_LEN);
  606. snprintf(Client->cloaked, sizeof(Client->cloaked),
  607. Conf_CloakHostModeX, Hash(Cloak_Buffer));
  608. } else
  609. strlcpy(Client->cloaked, Client_ID(Client->introducer),
  610. sizeof(Client->cloaked));
  611. } else
  612. strlcpy(Client->cloaked, Hostname, sizeof(Client->cloaked));
  613. LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
  614. Client_ID(Client), Client->cloaked);
  615. /* Inform other servers in the network */
  616. IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
  617. "METADATA %s cloakhost :%s",
  618. Client_ID(Client), Client->cloaked);
  619. }
  620. GLOBAL char *
  621. Client_Modes( CLIENT *Client )
  622. {
  623. assert( Client != NULL );
  624. return Client->modes;
  625. } /* Client_Modes */
  626. GLOBAL char *
  627. Client_Flags( CLIENT *Client )
  628. {
  629. assert( Client != NULL );
  630. return Client->flags;
  631. } /* Client_Flags */
  632. GLOBAL bool
  633. Client_OperByMe( CLIENT *Client )
  634. {
  635. assert( Client != NULL );
  636. return Client->oper_by_me;
  637. } /* Client_OperByMe */
  638. GLOBAL int
  639. Client_Hops( CLIENT *Client )
  640. {
  641. assert( Client != NULL );
  642. return Client->hops;
  643. } /* Client_Hops */
  644. GLOBAL int
  645. Client_Token( CLIENT *Client )
  646. {
  647. assert( Client != NULL );
  648. return Client->token;
  649. } /* Client_Token */
  650. GLOBAL int
  651. Client_MyToken( CLIENT *Client )
  652. {
  653. assert( Client != NULL );
  654. return Client->mytoken;
  655. } /* Client_MyToken */
  656. GLOBAL CLIENT *
  657. Client_NextHop( CLIENT *Client )
  658. {
  659. CLIENT *c;
  660. assert( Client != NULL );
  661. c = Client;
  662. while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
  663. c = c->introducer;
  664. return c;
  665. } /* Client_NextHop */
  666. /**
  667. * Return ID of a client: "client!user@host"
  668. * This client ID is used for IRC prefixes, for example.
  669. * Please note that this function uses a global static buffer, so you can't
  670. * nest invocations without overwriting earlier results!
  671. * @param Client Pointer to client structure
  672. * @return Pointer to global buffer containing the client ID
  673. */
  674. GLOBAL char *
  675. Client_Mask( CLIENT *Client )
  676. {
  677. static char Mask_Buffer[GETID_LEN];
  678. assert (Client != NULL);
  679. /* Servers: return name only, there is no "mask" */
  680. if (Client->type == CLIENT_SERVER)
  681. return Client->id;
  682. snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
  683. Client->id, Client->user, Client->host);
  684. return Mask_Buffer;
  685. } /* Client_Mask */
  686. /**
  687. * Return ID of a client with cloaked hostname: "client!user@server-name"
  688. *
  689. * This client ID is used for IRC prefixes, for example.
  690. * Please note that this function uses a global static buffer, so you can't
  691. * nest invocations without overwriting earlier results!
  692. * If the client has not enabled cloaking, the real hostname is used.
  693. *
  694. * @param Client Pointer to client structure
  695. * @return Pointer to global buffer containing the client ID
  696. */
  697. GLOBAL char *
  698. Client_MaskCloaked(CLIENT *Client)
  699. {
  700. static char Mask_Buffer[GETID_LEN];
  701. assert (Client != NULL);
  702. /* Is the client using cloaking at all? */
  703. if (!Client_HasMode(Client, 'x'))
  704. return Client_Mask(Client);
  705. snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
  706. Client_HostnameDisplayed(Client));
  707. return Mask_Buffer;
  708. } /* Client_MaskCloaked */
  709. GLOBAL CLIENT *
  710. Client_Introducer( CLIENT *Client )
  711. {
  712. assert( Client != NULL );
  713. return Client->introducer;
  714. } /* Client_Introducer */
  715. GLOBAL CLIENT *
  716. Client_TopServer( CLIENT *Client )
  717. {
  718. assert( Client != NULL );
  719. return Client->topserver;
  720. } /* Client_TopServer */
  721. GLOBAL bool
  722. Client_HasMode( CLIENT *Client, char Mode )
  723. {
  724. assert( Client != NULL );
  725. return strchr( Client->modes, Mode ) != NULL;
  726. } /* Client_HasMode */
  727. GLOBAL char *
  728. Client_Away( CLIENT *Client )
  729. {
  730. assert( Client != NULL );
  731. return Client->away;
  732. } /* Client_Away */
  733. /**
  734. * Make sure that a given nickname is valid.
  735. *
  736. * If the nickname is not valid for the given client, this function sends back
  737. * the appropriate error messages.
  738. *
  739. * @param Client Client that wants to change the nickname.
  740. * @param Nick New nickname.
  741. * @returns true if nickname is valid, false otherwise.
  742. */
  743. GLOBAL bool
  744. Client_CheckNick(CLIENT *Client, char *Nick)
  745. {
  746. assert(Client != NULL);
  747. assert(Nick != NULL);
  748. if (!Client_IsValidNick(Nick)) {
  749. if (strlen(Nick ) >= Conf_MaxNickLength)
  750. IRC_WriteStrClient(Client, ERR_NICKNAMETOOLONG_MSG,
  751. Client_ID(Client), Nick,
  752. Conf_MaxNickLength - 1);
  753. else
  754. IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
  755. Client_ID(Client), Nick);
  756. return false;
  757. }
  758. if (Client_Type(Client) != CLIENT_SERVER
  759. && Client_Type(Client) != CLIENT_SERVICE) {
  760. /* Make sure that this isn't a restricted/forbidden nickname */
  761. if (Conf_NickIsBlocked(Nick)) {
  762. IRC_WriteStrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
  763. Client_ID(Client), Nick);
  764. return false;
  765. }
  766. }
  767. /* Nickname already registered? */
  768. if (Client_Search(Nick)) {
  769. IRC_WriteStrClient(Client, ERR_NICKNAMEINUSE_MSG,
  770. Client_ID(Client), Nick);
  771. return false;
  772. }
  773. return true;
  774. } /* Client_CheckNick */
  775. GLOBAL bool
  776. Client_CheckID( CLIENT *Client, char *ID )
  777. {
  778. char str[COMMAND_LEN];
  779. CLIENT *c;
  780. assert( Client != NULL );
  781. assert( Client->conn_id > NONE );
  782. assert( ID != NULL );
  783. /* ID too long? */
  784. if (strlen(ID) > CLIENT_ID_LEN) {
  785. IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
  786. return false;
  787. }
  788. /* ID already in use? */
  789. c = My_Clients;
  790. while (c) {
  791. if (strcasecmp(c->id, ID) == 0) {
  792. snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
  793. if (c->conn_id != NONE)
  794. Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
  795. else
  796. Log(LOG_ERR, "%s (via network)!", str);
  797. Conn_Close(Client->conn_id, str, str, true);
  798. return false;
  799. }
  800. c = (CLIENT *)c->next;
  801. }
  802. return true;
  803. } /* Client_CheckID */
  804. GLOBAL CLIENT *
  805. Client_First( void )
  806. {
  807. return My_Clients;
  808. } /* Client_First */
  809. GLOBAL CLIENT *
  810. Client_Next( CLIENT *c )
  811. {
  812. assert( c != NULL );
  813. return (CLIENT *)c->next;
  814. } /* Client_Next */
  815. GLOBAL long
  816. Client_UserCount( void )
  817. {
  818. return Count( CLIENT_USER );
  819. } /* Client_UserCount */
  820. GLOBAL long
  821. Client_ServiceCount( void )
  822. {
  823. return Count( CLIENT_SERVICE );;
  824. } /* Client_ServiceCount */
  825. GLOBAL long
  826. Client_ServerCount( void )
  827. {
  828. return Count( CLIENT_SERVER );
  829. } /* Client_ServerCount */
  830. GLOBAL long
  831. Client_MyUserCount( void )
  832. {
  833. return MyCount( CLIENT_USER );
  834. } /* Client_MyUserCount */
  835. GLOBAL long
  836. Client_MyServiceCount( void )
  837. {
  838. return MyCount( CLIENT_SERVICE );
  839. } /* Client_MyServiceCount */
  840. GLOBAL unsigned long
  841. Client_MyServerCount( void )
  842. {
  843. CLIENT *c;
  844. unsigned long cnt = 0;
  845. c = My_Clients;
  846. while( c )
  847. {
  848. if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
  849. c = (CLIENT *)c->next;
  850. }
  851. return cnt;
  852. } /* Client_MyServerCount */
  853. GLOBAL unsigned long
  854. Client_OperCount( void )
  855. {
  856. CLIENT *c;
  857. unsigned long cnt = 0;
  858. c = My_Clients;
  859. while( c )
  860. {
  861. if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
  862. c = (CLIENT *)c->next;
  863. }
  864. return cnt;
  865. } /* Client_OperCount */
  866. GLOBAL unsigned long
  867. Client_UnknownCount( void )
  868. {
  869. CLIENT *c;
  870. unsigned long cnt = 0;
  871. c = My_Clients;
  872. while( c )
  873. {
  874. if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
  875. c = (CLIENT *)c->next;
  876. }
  877. return cnt;
  878. } /* Client_UnknownCount */
  879. GLOBAL long
  880. Client_MaxUserCount( void )
  881. {
  882. return Max_Users;
  883. } /* Client_MaxUserCount */
  884. GLOBAL long
  885. Client_MyMaxUserCount( void )
  886. {
  887. return My_Max_Users;
  888. } /* Client_MyMaxUserCount */
  889. /**
  890. * Check that a given nickname is valid.
  891. *
  892. * @param Nick the nickname to check.
  893. * @returns true if nickname is valid, false otherwise.
  894. */
  895. GLOBAL bool
  896. Client_IsValidNick(const char *Nick)
  897. {
  898. const char *ptr;
  899. static const char goodchars[] = ";0123456789-";
  900. assert (Nick != NULL);
  901. if (strchr(goodchars, Nick[0]))
  902. return false;
  903. if (strlen(Nick ) >= Conf_MaxNickLength)
  904. return false;
  905. ptr = Nick;
  906. while (*ptr) {
  907. if (*ptr < 'A' && !strchr(goodchars, *ptr ))
  908. return false;
  909. if (*ptr > '}')
  910. return false;
  911. ptr++;
  912. }
  913. return true;
  914. } /* Client_IsValidNick */
  915. /**
  916. * Return pointer to "My_Whowas" structure.
  917. */
  918. GLOBAL WHOWAS *
  919. Client_GetWhowas( void )
  920. {
  921. return My_Whowas;
  922. } /* Client_GetWhowas */
  923. /**
  924. * Return the index of the last used WHOWAS entry.
  925. */
  926. GLOBAL int
  927. Client_GetLastWhowasIndex( void )
  928. {
  929. return Last_Whowas;
  930. } /* Client_GetLastWhowasIndex */
  931. /**
  932. * Get the start time of this client.
  933. * The result is the start time in seconds since 1970-01-01, as reported
  934. * by the C function time(NULL).
  935. */
  936. GLOBAL time_t
  937. Client_StartTime(CLIENT *Client)
  938. {
  939. assert( Client != NULL );
  940. return Client->starttime;
  941. } /* Client_Uptime */
  942. /**
  943. * Reject a client when logging in.
  944. *
  945. * This function is called when a client isn't allowed to connect to this
  946. * server. Possible reasons are bad server password, bad PAM password,
  947. * or that the client is G/K-Line'd.
  948. *
  949. * After calling this function, the client isn't connected any more.
  950. *
  951. * @param Client The client to reject.
  952. * @param Reason The reason why the client has been rejected.
  953. * @param InformClient If true, send the exact reason to the client.
  954. */
  955. GLOBAL void
  956. Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
  957. {
  958. char info[COMMAND_LEN];
  959. assert(Client != NULL);
  960. assert(Reason != NULL);
  961. if (InformClient)
  962. snprintf(info, sizeof(info), "Access denied: %s", Reason);
  963. else
  964. strcpy(info, "Access denied: Bad password?");
  965. Log(LOG_ERR,
  966. "User \"%s\" rejected (connection %d): %s!",
  967. Client_Mask(Client), Client_Conn(Client), Reason);
  968. Conn_Close(Client_Conn(Client), Reason, info, true);
  969. }
  970. /**
  971. * Introduce a new user or service client in the network.
  972. *
  973. * @param From Remote server introducing the client or NULL (local).
  974. * @param Client New client.
  975. * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
  976. */
  977. GLOBAL void
  978. Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
  979. {
  980. /* Set client type (user or service) */
  981. Client_SetType(Client, Type);
  982. if (From) {
  983. if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
  984. Client_ID(Client)))
  985. Client_SetType(Client, CLIENT_SERVICE);
  986. LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
  987. Client_TypeText(Client), Client_Mask(Client),
  988. Client_Modes(Client), Client_ID(From),
  989. Client_ID(Client_Introducer(Client)),
  990. Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
  991. } else {
  992. Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
  993. Client_TypeText(Client), Client_Mask(Client),
  994. Client_Conn(Client));
  995. Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
  996. Client_ID(Client), Client_User(Client),
  997. Client_Hostname(Client),
  998. Conn_IPA(Client_Conn(Client)),
  999. Client_TypeText(Client));
  1000. }
  1001. /* Inform other servers */
  1002. IRC_WriteStrServersPrefixFlag_CB(From,
  1003. From != NULL ? From : Client_ThisServer(),
  1004. '\0', cb_introduceClient, (void *)Client);
  1005. } /* Client_Introduce */
  1006. static unsigned long
  1007. Count( CLIENT_TYPE Type )
  1008. {
  1009. CLIENT *c;
  1010. unsigned long cnt = 0;
  1011. c = My_Clients;
  1012. while( c )
  1013. {
  1014. if( c->type == Type ) cnt++;
  1015. c = (CLIENT *)c->next;
  1016. }
  1017. return cnt;
  1018. } /* Count */
  1019. static unsigned long
  1020. MyCount( CLIENT_TYPE Type )
  1021. {
  1022. CLIENT *c;
  1023. unsigned long cnt = 0;
  1024. c = My_Clients;
  1025. while( c )
  1026. {
  1027. if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
  1028. c = (CLIENT *)c->next;
  1029. }
  1030. return cnt;
  1031. } /* MyCount */
  1032. static CLIENT *
  1033. New_Client_Struct( void )
  1034. {
  1035. /* Neue CLIENT-Struktur pre-initialisieren */
  1036. CLIENT *c;
  1037. c = (CLIENT *)malloc( sizeof( CLIENT ));
  1038. if( ! c )
  1039. {
  1040. Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
  1041. return NULL;
  1042. }
  1043. memset( c, 0, sizeof ( CLIENT ));
  1044. c->type = CLIENT_UNKNOWN;
  1045. c->conn_id = NONE;
  1046. c->oper_by_me = false;
  1047. c->hops = -1;
  1048. c->token = -1;
  1049. c->mytoken = -1;
  1050. return c;
  1051. } /* New_Client */
  1052. static void
  1053. Generate_MyToken( CLIENT *Client )
  1054. {
  1055. CLIENT *c;
  1056. int token;
  1057. c = My_Clients;
  1058. token = 2;
  1059. while( c )
  1060. {
  1061. if( c->mytoken == token )
  1062. {
  1063. /* Das Token wurde bereits vergeben */
  1064. token++;
  1065. c = My_Clients;
  1066. continue;
  1067. }
  1068. else c = (CLIENT *)c->next;
  1069. }
  1070. Client->mytoken = token;
  1071. LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
  1072. } /* Generate_MyToken */
  1073. static void
  1074. Adjust_Counters( CLIENT *Client )
  1075. {
  1076. long count;
  1077. assert( Client != NULL );
  1078. if( Client->type != CLIENT_USER ) return;
  1079. if( Client->conn_id != NONE )
  1080. {
  1081. /* Local connection */
  1082. count = Client_MyUserCount( );
  1083. if( count > My_Max_Users ) My_Max_Users = count;
  1084. }
  1085. count = Client_UserCount( );
  1086. if( count > Max_Users ) Max_Users = count;
  1087. } /* Adjust_Counters */
  1088. /**
  1089. * Register client in My_Whowas structure for further recall by WHOWAS.
  1090. * Note: Only clients that have been connected at least 30 seconds will be
  1091. * registered to prevent automated IRC bots to "destroy" a nice server
  1092. * history database.
  1093. */
  1094. GLOBAL void
  1095. Client_RegisterWhowas( CLIENT *Client )
  1096. {
  1097. int slot;
  1098. time_t now;
  1099. assert( Client != NULL );
  1100. /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
  1101. if (Conf_MorePrivacy)
  1102. return;
  1103. now = time(NULL);
  1104. /* Don't register clients that were connected less than 30 seconds. */
  1105. if( now - Client->starttime < 30 )
  1106. return;
  1107. slot = Last_Whowas + 1;
  1108. if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
  1109. #ifdef DEBUG
  1110. Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
  1111. #endif
  1112. My_Whowas[slot].time = now;
  1113. strlcpy( My_Whowas[slot].id, Client_ID( Client ),
  1114. sizeof( My_Whowas[slot].id ));
  1115. strlcpy( My_Whowas[slot].user, Client_User( Client ),
  1116. sizeof( My_Whowas[slot].user ));
  1117. strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
  1118. sizeof( My_Whowas[slot].host ));
  1119. strlcpy( My_Whowas[slot].info, Client_Info( Client ),
  1120. sizeof( My_Whowas[slot].info ));
  1121. strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
  1122. sizeof( My_Whowas[slot].server ));
  1123. Last_Whowas = slot;
  1124. } /* Client_RegisterWhowas */
  1125. GLOBAL const char *
  1126. Client_TypeText(CLIENT *Client)
  1127. {
  1128. assert(Client != NULL);
  1129. switch (Client_Type(Client)) {
  1130. case CLIENT_USER:
  1131. return "User";
  1132. break;
  1133. case CLIENT_SERVICE:
  1134. return "Service";
  1135. break;
  1136. case CLIENT_SERVER:
  1137. return "Server";
  1138. break;
  1139. default:
  1140. return "Client";
  1141. }
  1142. } /* Client_TypeText */
  1143. /**
  1144. * Destroy user or service client.
  1145. */
  1146. static void
  1147. Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
  1148. {
  1149. if(Client->conn_id != NONE) {
  1150. /* Local (directly connected) client */
  1151. Log(LOG_NOTICE,
  1152. "%s \"%s\" unregistered (connection %d): %s",
  1153. Client_TypeText(Client), Client_Mask(Client),
  1154. Client->conn_id, Txt);
  1155. Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
  1156. Client_ID(Client), Client_User(Client),
  1157. Client_Hostname(Client), Txt);
  1158. if (SendQuit) {
  1159. /* Inforam all the other servers */
  1160. if (FwdMsg)
  1161. IRC_WriteStrServersPrefix(NULL,
  1162. Client, "QUIT :%s", FwdMsg );
  1163. else
  1164. IRC_WriteStrServersPrefix(NULL,
  1165. Client, "QUIT :");
  1166. }
  1167. } else {
  1168. /* Remote client */
  1169. LogDebug("%s \"%s\" unregistered: %s",
  1170. Client_TypeText(Client), Client_Mask(Client), Txt);
  1171. if(SendQuit) {
  1172. /* Inform all the other servers, but the ones in the
  1173. * direction we got the QUIT from */
  1174. if(FwdMsg)
  1175. IRC_WriteStrServersPrefix(Client_NextHop(Client),
  1176. Client, "QUIT :%s", FwdMsg );
  1177. else
  1178. IRC_WriteStrServersPrefix(Client_NextHop(Client),
  1179. Client, "QUIT :" );
  1180. }
  1181. }
  1182. /* Unregister client from channels */
  1183. Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
  1184. /* Register client in My_Whowas structure */
  1185. Client_RegisterWhowas(Client);
  1186. } /* Destroy_UserOrService */
  1187. /**
  1188. * Introduce a new user or service client to a remote server.
  1189. *
  1190. * This function differentiates between RFC1459 and RFC2813 server links and
  1191. * generates the appropriate commands to register the new user or service.
  1192. *
  1193. * @param To The remote server to inform.
  1194. * @param Prefix Prefix for the generated commands.
  1195. * @param data CLIENT structure of the new client.
  1196. */
  1197. static void
  1198. cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
  1199. {
  1200. CLIENT *c = (CLIENT *)data;
  1201. CONN_ID conn;
  1202. char *modes, *user, *host;
  1203. modes = Client_Modes(c);
  1204. user = Client_User(c) ? Client_User(c) : "-";
  1205. host = Client_Hostname(c) ? Client_Hostname(c) : "-";
  1206. conn = Client_Conn(To);
  1207. if (Conn_Options(conn) & CONN_RFC1459) {
  1208. /* RFC 1459 mode: separate NICK and USER commands */
  1209. Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
  1210. Client_Hops(c) + 1);
  1211. Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
  1212. Client_ID(c), user, host,
  1213. Client_ID(Client_Introducer(c)), Client_Info(c));
  1214. if (modes[0])
  1215. Conn_WriteStr(conn, ":%s MODE %s +%s",
  1216. Client_ID(c), Client_ID(c), modes);
  1217. } else {
  1218. /* RFC 2813 mode: one combined NICK or SERVICE command */
  1219. if (Client_Type(c) == CLIENT_SERVICE
  1220. && strchr(Client_Flags(To), 'S'))
  1221. IRC_WriteStrClientPrefix(To, Prefix,
  1222. "SERVICE %s %d * +%s %d :%s",
  1223. Client_Mask(c),
  1224. Client_MyToken(Client_Introducer(c)),
  1225. Client_Modes(c), Client_Hops(c) + 1,
  1226. Client_Info(c));
  1227. else
  1228. IRC_WriteStrClientPrefix(To, Prefix,
  1229. "NICK %s %d %s %s %d +%s :%s",
  1230. Client_ID(c), Client_Hops(c) + 1,
  1231. user, host,
  1232. Client_MyToken(Client_Introducer(c)),
  1233. modes, Client_Info(c));
  1234. }
  1235. } /* cb_introduceClient */
  1236. #ifdef DEBUG
  1237. GLOBAL void
  1238. Client_DebugDump(void)
  1239. {
  1240. CLIENT *c;
  1241. Log(LOG_DEBUG, "Client status:");
  1242. c = My_Clients;
  1243. while (c) {
  1244. Log(LOG_DEBUG,
  1245. " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
  1246. Client_ID(c), Client_Type(c), Client_Hostname(c),
  1247. Client_User(c), Client_Conn(c), Client_StartTime(c),
  1248. Client_Flags(c));
  1249. c = (CLIENT *)c->next;
  1250. }
  1251. } /* Client_DumpClients */
  1252. #endif
  1253. /* -eof- */