irc-oper.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2008 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. * IRC operator commands
  12. */
  13. #include "portab.h"
  14. #include "imp.h"
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <signal.h>
  20. #include "ngircd.h"
  21. #include "conn-func.h"
  22. #include "conf.h"
  23. #include "channel.h"
  24. #include "irc-write.h"
  25. #include "log.h"
  26. #include "match.h"
  27. #include "messages.h"
  28. #include "parse.h"
  29. #include "op.h"
  30. #include <exp.h>
  31. #include "irc-oper.h"
  32. /**
  33. * Handle invalid received OPER command.
  34. * Log OPER attempt and send error message to client.
  35. */
  36. static bool
  37. Bad_OperPass(CLIENT *Client, char *errtoken, char *errmsg)
  38. {
  39. Log(LOG_WARNING, "Got invalid OPER from \"%s\": \"%s\" -- %s",
  40. Client_Mask(Client), errtoken, errmsg);
  41. IRC_SetPenalty(Client, 3);
  42. return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
  43. Client_ID(Client));
  44. } /* Bad_OperPass */
  45. GLOBAL bool
  46. IRC_OPER( CLIENT *Client, REQUEST *Req )
  47. {
  48. struct Conf_Oper *op;
  49. size_t len, i;
  50. assert( Client != NULL );
  51. assert( Req != NULL );
  52. if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  53. len = array_length(&Conf_Opers, sizeof(*op));
  54. op = array_start(&Conf_Opers);
  55. for (i = 0; i < len && strcmp(op[i].name, Req->argv[0]); i++)
  56. ;
  57. if (i >= len)
  58. return Bad_OperPass(Client, Req->argv[0], "not configured");
  59. if (strcmp(op[i].pwd, Req->argv[1]) != 0)
  60. return Bad_OperPass(Client, op[i].name, "bad password");
  61. if (op[i].mask && (!Match(op[i].mask, Client_Mask(Client))))
  62. return Bad_OperPass(Client, op[i].mask, "hostmask check failed");
  63. if( ! Client_HasMode( Client, 'o' ))
  64. {
  65. Client_ModeAdd( Client, 'o' );
  66. if( ! IRC_WriteStrClient( Client, "MODE %s :+o", Client_ID( Client ))) return DISCONNECTED;
  67. IRC_WriteStrServersPrefix( NULL, Client, "MODE %s :+o", Client_ID( Client ));
  68. }
  69. if( ! Client_OperByMe( Client )) Log( LOG_NOTICE|LOG_snotice, "Got valid OPER from \"%s\", user is an IRC operator now.", Client_Mask( Client ));
  70. Client_SetOperByMe( Client, true);
  71. return IRC_WriteStrClient( Client, RPL_YOUREOPER_MSG, Client_ID( Client ));
  72. } /* IRC_OPER */
  73. GLOBAL bool
  74. IRC_DIE(CLIENT * Client, REQUEST * Req)
  75. {
  76. /* Shut down server */
  77. CONN_ID c;
  78. CLIENT *cl;
  79. assert(Client != NULL);
  80. assert(Req != NULL);
  81. if (!Op_Check(Client, Req))
  82. return Op_NoPrivileges(Client, Req);
  83. /* Bad number of parameters? */
  84. #ifdef STRICT_RFC
  85. if (Req->argc != 0)
  86. #else
  87. if (Req->argc > 1)
  88. #endif
  89. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  90. Client_ID(Client), Req->command);
  91. /* Is a message given? */
  92. if (Req->argc > 0) {
  93. c = Conn_First();
  94. while (c != NONE) {
  95. cl = Conn_GetClient(c);
  96. if (Client_Type(cl) == CLIENT_USER)
  97. IRC_WriteStrClient(cl, "NOTICE %s :%s",
  98. Client_ID(cl), Req->argv[0]);
  99. c = Conn_Next(c);
  100. }
  101. }
  102. Log(LOG_NOTICE | LOG_snotice, "Got DIE command from \"%s\" ...",
  103. Client_Mask(Client));
  104. NGIRCd_SignalQuit = true;
  105. return CONNECTED;
  106. } /* IRC_DIE */
  107. GLOBAL bool
  108. IRC_REHASH( CLIENT *Client, REQUEST *Req )
  109. {
  110. /* Reload configuration file */
  111. assert( Client != NULL );
  112. assert( Req != NULL );
  113. if (!Op_Check(Client, Req))
  114. return Op_NoPrivileges(Client, Req);
  115. /* Bad number of parameters? */
  116. if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  117. Log( LOG_NOTICE|LOG_snotice, "Got REHASH command from \"%s\" ...", Client_Mask( Client ));
  118. raise(SIGHUP);
  119. return CONNECTED;
  120. } /* IRC_REHASH */
  121. GLOBAL bool
  122. IRC_RESTART( CLIENT *Client, REQUEST *Req )
  123. {
  124. /* Restart IRC server (fork a new process) */
  125. assert( Client != NULL );
  126. assert( Req != NULL );
  127. if (!Op_Check(Client, Req))
  128. return Op_NoPrivileges(Client, Req);
  129. /* Bad number of parameters? */
  130. if( Req->argc != 0 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  131. Log( LOG_NOTICE|LOG_snotice, "Got RESTART command from \"%s\" ...", Client_Mask( Client ));
  132. NGIRCd_SignalRestart = true;
  133. return CONNECTED;
  134. } /* IRC_RESTART */
  135. /**
  136. * Connect configured or new server.
  137. */
  138. GLOBAL bool
  139. IRC_CONNECT(CLIENT * Client, REQUEST * Req)
  140. {
  141. CLIENT *from, *target;
  142. assert(Client != NULL);
  143. assert(Req != NULL);
  144. if (Client_Type(Client) != CLIENT_SERVER
  145. && !Client_HasMode(Client, 'o'))
  146. return Op_NoPrivileges(Client, Req);
  147. /* Bad number of parameters? */
  148. if (Req->argc != 1 && Req->argc != 2 && Req->argc != 3 &&
  149. Req->argc != 5 && Req->argc != 6)
  150. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  151. Client_ID(Client), Req->command);
  152. /* Invalid port number? */
  153. if ((Req->argc > 1) && atoi(Req->argv[1]) < 1)
  154. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  155. Client_ID(Client), Req->command);
  156. from = Client;
  157. target = Client_ThisServer();
  158. if (Req->argc == 3 || Req->argc == 6) {
  159. /* This CONNECT has a target parameter */
  160. if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
  161. from = Client_Search(Req->prefix);
  162. if (! from)
  163. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  164. Client_ID(Client), Req->prefix);
  165. target = (Req->argc == 3) ? Client_Search(Req->argv[2])
  166. : Client_Search(Req->argv[5]);
  167. if (! target || Client_Type(target) != CLIENT_SERVER)
  168. return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
  169. Client_ID(from), Req->argv[0]);
  170. }
  171. if (target != Client_ThisServer()) {
  172. /* Forward CONNECT command ... */
  173. if (Req->argc == 3)
  174. IRC_WriteStrClientPrefix(target, from,
  175. "CONNECT %s %s :%s", Req->argv[0],
  176. Req->argv[1], Req->argv[2]);
  177. else
  178. IRC_WriteStrClientPrefix(target, from,
  179. "CONNECT %s %s %s %s %s :%s", Req->argv[0],
  180. Req->argv[1], Req->argv[2], Req->argv[3],
  181. Req->argv[4], Req->argv[5]);
  182. return CONNECTED;
  183. }
  184. if (!Op_Check(from, Req))
  185. return Op_NoPrivileges(Client, Req);
  186. switch (Req->argc) {
  187. case 1:
  188. if (!Conf_EnablePassiveServer(Req->argv[0]))
  189. return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
  190. Client_ID(from),
  191. Req->argv[0]);
  192. break;
  193. case 2:
  194. case 3:
  195. /* Connect configured server */
  196. if (!Conf_EnableServer
  197. (Req->argv[0], (UINT16) atoi(Req->argv[1])))
  198. return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
  199. Client_ID(from),
  200. Req->argv[0]);
  201. break;
  202. default:
  203. /* Add server */
  204. if (!Conf_AddServer
  205. (Req->argv[0], (UINT16) atoi(Req->argv[1]), Req->argv[2],
  206. Req->argv[3], Req->argv[4]))
  207. return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
  208. Client_ID(from),
  209. Req->argv[0]);
  210. }
  211. Log(LOG_NOTICE | LOG_snotice,
  212. "Got CONNECT command from \"%s\" for \"%s\".", Client_Mask(from),
  213. Req->argv[0]);
  214. IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
  215. "Received CONNECT %s from %s",
  216. Req->argv[0], Client_ID(from));
  217. return CONNECTED;
  218. } /* IRC_CONNECT */
  219. /**
  220. * Disconnect (and disable) configured server.
  221. */
  222. GLOBAL bool
  223. IRC_DISCONNECT(CLIENT * Client, REQUEST * Req)
  224. {
  225. CONN_ID my_conn;
  226. assert(Client != NULL);
  227. assert(Req != NULL);
  228. if (!Op_Check(Client, Req))
  229. return Op_NoPrivileges(Client, Req);
  230. /* Bad number of parameters? */
  231. if (Req->argc != 1)
  232. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  233. Client_ID(Client), Req->command);
  234. IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
  235. "Received DISCONNECT %s from %s",
  236. Req->argv[0], Client_ID(Client));
  237. Log(LOG_NOTICE | LOG_snotice,
  238. "Got DISCONNECT command from \"%s\" for \"%s\".",
  239. Client_Mask(Client), Req->argv[0]);
  240. /* Save ID of this connection */
  241. my_conn = Client_Conn(Client);
  242. /* Disconnect configured server */
  243. if (!Conf_DisableServer(Req->argv[0]))
  244. return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
  245. Client_ID(Client), Req->argv[0]);
  246. /* Are we still connected or were we killed, too? */
  247. if (Conn_GetClient(my_conn))
  248. return CONNECTED;
  249. else
  250. return DISCONNECTED;
  251. } /* IRC_DISCONNECT */
  252. GLOBAL bool
  253. IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
  254. {
  255. CLIENT *from;
  256. assert( Client != NULL );
  257. assert( Req != NULL );
  258. if (Req->argc != 1)
  259. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
  260. switch (Client_Type(Client)) {
  261. case CLIENT_USER:
  262. if (!Client_OperByMe(Client))
  263. return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG, Client_ID(Client));
  264. from = Client;
  265. break;
  266. case CLIENT_SERVER:
  267. from = Client_Search(Req->prefix);
  268. break;
  269. default:
  270. return CONNECTED;
  271. }
  272. if (!from)
  273. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
  274. IRC_SendWallops(Client, from, "%s", Req->argv[0]);
  275. return CONNECTED;
  276. } /* IRC_WALLOPS */
  277. /* -eof- */