client.c 30 KB

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