irc-oper.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2015 Alexander Barton (alex@barton.de) and Contributors.
  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. #include "portab.h"
  12. /**
  13. * @file
  14. * IRC operator commands
  15. */
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <signal.h>
  21. #include <time.h>
  22. #include "ngircd.h"
  23. #include "conn-func.h"
  24. #include "conf.h"
  25. #include "channel.h"
  26. #include "class.h"
  27. #include "parse.h"
  28. #include "irc.h"
  29. #include "irc-macros.h"
  30. #include "irc-write.h"
  31. #include "lists.h"
  32. #include "log.h"
  33. #include "match.h"
  34. #include "messages.h"
  35. #include "op.h"
  36. #include "irc-oper.h"
  37. /**
  38. * Handle invalid received OPER command.
  39. * Log OPER attempt and send error message to client.
  40. */
  41. static bool
  42. Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
  43. {
  44. Log(LOG_ERR|LOG_snotice, "Got invalid OPER from \"%s\": \"%s\" -- %s!",
  45. Client_Mask(Client), errtoken, errmsg);
  46. /* Increase penalty to slow down possible brute force attacks */
  47. IRC_SetPenalty(Client, 10);
  48. return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
  49. Client_ID(Client));
  50. } /* Bad_OperPass */
  51. /**
  52. * Handler for the IRC "OPER" command.
  53. *
  54. * @param Client The client from which this command has been received.
  55. * @param Req Request structure with prefix and all parameters.
  56. * @return CONNECTED or DISCONNECTED.
  57. */
  58. GLOBAL bool
  59. IRC_OPER( CLIENT *Client, REQUEST *Req )
  60. {
  61. struct Conf_Oper *op;
  62. size_t len, i;
  63. assert( Client != NULL );
  64. assert( Req != NULL );
  65. len = array_length(&Conf_Opers, sizeof(*op));
  66. op = array_start(&Conf_Opers);
  67. for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
  68. ;
  69. if (i >= len)
  70. return Bad_OperPass(Client, Req->argv[0], "not configured");
  71. if (strcmp(op[i].pwd, Req->argv[1]) != 0)
  72. return Bad_OperPass(Client, op[i].name, "bad password");
  73. if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
  74. return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
  75. if (!Client_HasMode(Client, 'o')) {
  76. Client_ModeAdd(Client, 'o');
  77. if (!IRC_WriteStrClient(Client, "MODE %s :+o",
  78. Client_ID(Client)))
  79. return DISCONNECTED;
  80. IRC_WriteStrServersPrefix(NULL, Client, "MODE %s :+o",
  81. Client_ID(Client));
  82. }
  83. Log(LOG_NOTICE|LOG_snotice,
  84. "Got valid OPER for \"%s\" from \"%s\", user is an IRC operator now.",
  85. Req->argv[0], Client_Mask(Client));
  86. return IRC_WriteStrClient(Client, RPL_YOUREOPER_MSG, Client_ID(Client));
  87. } /* IRC_OPER */
  88. /**
  89. * Handler for the IRC "DIE" command.
  90. *
  91. * @param Client The client from which this command has been received.
  92. * @param Req Request structure with prefix and all parameters.
  93. * @return CONNECTED or DISCONNECTED.
  94. */
  95. GLOBAL bool
  96. IRC_DIE(CLIENT * Client, REQUEST * Req)
  97. {
  98. /* Shut down server */
  99. CONN_ID c;
  100. CLIENT *cl;
  101. assert(Client != NULL);
  102. assert(Req != NULL);
  103. if (!Op_Check(Client, Req))
  104. return Op_NoPrivileges(Client, Req);
  105. /* Is a message given? */
  106. if (Req->argc > 0) {
  107. c = Conn_First();
  108. while (c != NONE) {
  109. cl = Conn_GetClient(c);
  110. if (Client_Type(cl) == CLIENT_USER)
  111. IRC_WriteStrClient(cl, "NOTICE %s :%s",
  112. Client_ID(cl), Req->argv[0]);
  113. c = Conn_Next(c);
  114. }
  115. }
  116. Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
  117. Client_Mask(Client));
  118. NGIRCd_SignalQuit = true;
  119. return CONNECTED;
  120. } /* IRC_DIE */
  121. /**
  122. * Handler for the IRC "REHASH" command.
  123. *
  124. * @param Client The client from which this command has been received.
  125. * @param Req Request structure with prefix and all parameters.
  126. * @return CONNECTED or DISCONNECTED.
  127. */
  128. GLOBAL bool
  129. IRC_REHASH( CLIENT *Client, REQUEST *Req )
  130. {
  131. /* Reload configuration file */
  132. assert( Client != NULL );
  133. assert( Req != NULL );
  134. if (!Op_Check(Client, Req))
  135. return Op_NoPrivileges(Client, Req);
  136. Log(LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...",
  137. Client_Mask(Client));
  138. IRC_WriteStrClient(Client, RPL_REHASHING_MSG, Client_ID(Client));
  139. raise(SIGHUP);
  140. return CONNECTED;
  141. } /* IRC_REHASH */
  142. /**
  143. * Handler for the IRC "RESTART" command.
  144. *
  145. * @param Client The client from which this command has been received.
  146. * @param Req Request structure with prefix and all parameters.
  147. * @return CONNECTED or DISCONNECTED.
  148. */
  149. GLOBAL bool
  150. IRC_RESTART( CLIENT *Client, REQUEST *Req )
  151. {
  152. /* Restart IRC server (fork a new process) */
  153. assert( Client != NULL );
  154. assert( Req != NULL );
  155. if (!Op_Check(Client, Req))
  156. return Op_NoPrivileges(Client, Req);
  157. Log(LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...",
  158. Client_Mask(Client));
  159. NGIRCd_SignalRestart = true;
  160. return CONNECTED;
  161. } /* IRC_RESTART */
  162. /**
  163. * Handler for the IRC "CONNECT" command.
  164. *
  165. * @param Client The client from which this command has been received.
  166. * @param Req Request structure with prefix and all parameters.
  167. * @return CONNECTED or DISCONNECTED.
  168. */
  169. GLOBAL bool
  170. IRC_CONNECT(CLIENT * Client, REQUEST * Req)
  171. {
  172. CLIENT *from, *target;
  173. assert(Client != NULL);
  174. assert(Req != NULL);
  175. /* Bad number of parameters? */
  176. if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
  177. Req->argc != 5 && Req->argc != 6)
  178. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  179. Client_ID(Client), Req->command);
  180. /* Invalid port number? */
  181. if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
  182. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  183. Client_ID(Client), Req->command);
  184. if (Client_Type(Client) != CLIENT_SERVER
  185. && !Client_HasMode(Client, 'o'))
  186. return Op_NoPrivileges(Client, Req);
  187. from = Client;
  188. target = Client_ThisServer();
  189. if (Req->argc == 3 || Req->argc == 6) {
  190. /* This CONNECT has a target parameter */
  191. if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
  192. from = Client_Search(Req->prefix);
  193. if (! from)
  194. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  195. Client_ID(Client), Req->prefix);
  196. target = (Req->argc == 3) ? Client_Search(Req->argv[2])
  197. : Client_Search(Req->argv[5]);
  198. if (! target || Client_Type(target) != CLIENT_SERVER)
  199. return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
  200. Client_ID(from), Req->argv[0]);
  201. }
  202. if (target != Client_ThisServer()) {
  203. /* Forward CONNECT command ... */
  204. if (Req->argc == 3)
  205. IRC_WriteStrClientPrefix(target, from,
  206. "CONNECT %s %s :%s", Req->argv[0],
  207. Req->argv[1], Req->argv[2]);
  208. else
  209. IRC_WriteStrClientPrefix(target, from,
  210. "CONNECT %s %s %s %s %s :%s", Req->argv[0],
  211. Req->argv[1], Req->argv[2], Req->argv[3],
  212. Req->argv[4], Req->argv[5]);
  213. return CONNECTED;
  214. }
  215. if (!Op_Check(from, Req))
  216. return Op_NoPrivileges(Client, Req);
  217. switch (Req->argc) {
  218. case 1:
  219. if (!Conf_EnablePassiveServer(Req->argv[0]))
  220. return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
  221. Client_ID(from),
  222. Req->argv[0]);
  223. break;
  224. case 2:
  225. case 3:
  226. /* Connect configured server */
  227. if (!Conf_EnableServer
  228. (Req->argv[0], (UINT16) atoi(Req->argv[1])))
  229. return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
  230. Client_ID(from),
  231. Req->argv[0]);
  232. break;
  233. default:
  234. /* Add server */
  235. if (!Conf_AddServer
  236. (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
  237. Req->argv[3], Req->argv[4]))
  238. return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
  239. Client_ID(from),
  240. Req->argv[0]);
  241. }
  242. Log(LOG_NOTICE | LOG_snotice,
  243. "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
  244. Req->argv[0]);
  245. IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
  246. "Received CONNECT %s from %s",
  247. Req->argv[0], Client_ID(from));
  248. return CONNECTED;
  249. } /* IRC_CONNECT */
  250. /**
  251. * Handler for the IRC "DISCONNECT" command.
  252. *
  253. * This command is not specified in the IRC RFCs, it is an extension
  254. * of ngIRCd: it shuts down and disables a configured server connection.
  255. *
  256. * @param Client The client from which this command has been received.
  257. * @param Req Request structure with prefix and all parameters.
  258. * @return CONNECTED or DISCONNECTED.
  259. */
  260. GLOBAL bool
  261. IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
  262. {
  263. CONN_ID my_conn;
  264. assert(Client != NULL);
  265. assert(Req != NULL);
  266. if (!Op_Check(Client, Req))
  267. return Op_NoPrivileges(Client, Req);
  268. IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
  269. "Received DISCONNECT %s from %s",
  270. Req->argv[0], Client_ID(Client));
  271. Log(LOG_NOTICE | LOG_snotice,
  272. "Got DISCONNECT command from \"%s\" for \"%s\".",
  273. Client_Mask(Client), Req->argv[0]);
  274. /* Save ID of this connection */
  275. my_conn = Client_Conn(Client);
  276. /* Disconnect configured server */
  277. if (!Conf_DisableServer(Req->argv[0]))
  278. return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
  279. Client_ID(Client), Req->argv[0]);
  280. /* Are we still connected or were we killed, too? */
  281. if (Conn_GetClient(my_conn))
  282. return CONNECTED;
  283. else
  284. return DISCONNECTED;
  285. } /* IRC_DISCONNECT */
  286. /**
  287. * Handler for the IRC "WALLOPS" command.
  288. *
  289. * @param Client The client from which this command has been received.
  290. * @param Req Request structure with prefix and all parameters.
  291. * @return CONNECTED or DISCONNECTED.
  292. */
  293. GLOBAL bool
  294. IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
  295. {
  296. CLIENT *from;
  297. assert( Client != NULL );
  298. assert( Req != NULL );
  299. switch (Client_Type(Client)) {
  300. case CLIENT_USER:
  301. if (!Op_Check(Client, Req))
  302. return Op_NoPrivileges(Client, Req);
  303. from = Client;
  304. break;
  305. case CLIENT_SERVER:
  306. _IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
  307. from = Client_Search(Req->prefix);
  308. break;
  309. default:
  310. return CONNECTED;
  311. }
  312. if (!from)
  313. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  314. Client_ID(Client), Req->prefix);
  315. IRC_SendWallops(Client, from, "%s", Req->argv[0]);
  316. return CONNECTED;
  317. } /* IRC_WALLOPS */
  318. /**
  319. * Handle <?>LINE commands (GLINE, KLINE).
  320. *
  321. * @param Client The client from which this command has been received.
  322. * @param Req Request structure with prefix and all parameters.
  323. * @return CONNECTED or DISCONNECTED.
  324. */
  325. GLOBAL bool
  326. IRC_xLINE(CLIENT *Client, REQUEST *Req)
  327. {
  328. CLIENT *from, *c, *c_next;
  329. char reason[COMMAND_LEN], class_c;
  330. struct list_head *list;
  331. time_t timeout;
  332. int class;
  333. assert(Client != NULL);
  334. assert(Req != NULL);
  335. /* Bad number of parameters? */
  336. if (Req->argc != 1 && Req->argc != 3)
  337. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  338. Client_ID(Client), Req->command);
  339. if (!Conf_AllowRemoteOper && Client_Type(Client) == CLIENT_SERVER) {
  340. /* Explicitely forbid remote servers to modify "x-lines" when
  341. * the "AllowRemoteOper" configuration option isn't set, even
  342. * when the command seems to originate from the remote server
  343. * itself: this prevents GLINE's to become set during server
  344. * handshake in this case (what wouldn't be possible during
  345. * regular runtime when a remote IRC Op sends the command). */
  346. from = NULL;
  347. } else
  348. from = Op_Check(Client, Req);
  349. if (!from)
  350. return Op_NoPrivileges(Client, Req);
  351. switch(Req->command[0]) {
  352. case 'g':
  353. case 'G':
  354. class = CLASS_GLINE; class_c = 'G';
  355. break;
  356. case 'k':
  357. case 'K':
  358. class = CLASS_KLINE; class_c = 'K';
  359. break;
  360. default:
  361. Log(LOG_CRIT,
  362. "IRC_xLINE() called for unknown line: %c!? Ignored.",
  363. Req->command[0]);
  364. return CONNECTED;
  365. }
  366. if (Req->argc == 1) {
  367. /* Delete mask from list */
  368. Class_DeleteMask(class, Req->argv[0]);
  369. Log(LOG_NOTICE|LOG_snotice,
  370. "\"%s\" deleted \"%s\" from %c-Line list.",
  371. Client_Mask(from), Req->argv[0], class_c);
  372. if (class == CLASS_GLINE) {
  373. /* Inform other servers */
  374. IRC_WriteStrServersPrefix(Client, from, "%s %s",
  375. Req->command, Req->argv[0]);
  376. }
  377. } else {
  378. /* Add new mask to list */
  379. timeout = atol(Req->argv[1]);
  380. if (timeout > 0)
  381. timeout += time(NULL);
  382. if (Class_AddMask(class, Req->argv[0],
  383. timeout,
  384. Req->argv[2])) {
  385. Log(LOG_NOTICE|LOG_snotice,
  386. "\"%s\" added \"%s\" to %c-Line list: \"%s\" (%ld seconds).",
  387. Client_Mask(from), Req->argv[0], class_c,
  388. Req->argv[2], atol(Req->argv[1]));
  389. if (class == CLASS_GLINE) {
  390. /* Inform other servers */
  391. IRC_WriteStrServersPrefix(Client, from,
  392. "%s %s %s :%s", Req->command,
  393. Req->argv[0], Req->argv[1],
  394. Req->argv[2]);
  395. }
  396. /* Check currently connected clients */
  397. snprintf(reason, sizeof(reason), "%c-Line by \"%s\": \"%s\"",
  398. class_c, Client_ID(from), Req->argv[2]);
  399. list = Class_GetList(class);
  400. c = Client_First();
  401. while (c) {
  402. c_next = Client_Next(c);
  403. if ((class == CLASS_GLINE || Client_Conn(c) > NONE)
  404. && Lists_Check(list, c))
  405. IRC_KillClient(Client, NULL,
  406. Client_ID(c), reason);
  407. c = c_next;
  408. }
  409. }
  410. }
  411. return CONNECTED;
  412. }
  413. /* -eof- */