irc-oper.c 13 KB

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