irc-oper.c 12 KB

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