auth.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * auth.c -- handles:
  3. * auth system functions
  4. * auth system hooks
  5. */
  6. #include "common.h"
  7. #include "auth.h"
  8. #include "misc.h"
  9. #include "main.h"
  10. #include "settings.h"
  11. #include "types.h"
  12. #include "userrec.h"
  13. #include "set.h"
  14. #include "core_binds.h"
  15. #include "egg_timer.h"
  16. #include "users.h"
  17. #include "crypt.h"
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include "chan.h"
  24. #include "tandem.h"
  25. #include "src/mod/server.mod/server.h"
  26. #include <bdlib/src/String.h>
  27. #include <bdlib/src/HashTable.h>
  28. #include <pwd.h>
  29. #include <errno.h>
  30. #include <net/if.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/socket.h>
  33. #include <signal.h>
  34. #include "stat.h"
  35. bd::HashTable<bd::String, Auth*> Auth::ht_host(10);
  36. bd::HashTable<RfcString, Auth*> Auth::ht_nick(10);
  37. Auth::Auth(const RfcString& _nick, const char *_host, struct userrec *u)
  38. {
  39. Status(AUTHING);
  40. nick = _nick;
  41. strlcpy(host, _host, UHOSTLEN);
  42. user = u;
  43. ht_host[host] = this;
  44. ht_nick[_nick] = this;
  45. sdprintf(STR("New auth created! (%s!%s) [%s]"), nick.c_str(), host,
  46. u ? u->handle : "*");
  47. authtime = atime = now;
  48. idx = -1;
  49. }
  50. Auth::~Auth()
  51. {
  52. sdprintf(STR("Removing auth: (%s!%s) [%s]"), nick.c_str(), host,
  53. user ? user->handle : "*");
  54. ht_host.remove(host);
  55. ht_nick.remove(nick);
  56. }
  57. void Auth::MakeHash() noexcept
  58. {
  59. make_rand_str(rand, 50);
  60. makehash(user, rand, hash, 50);
  61. }
  62. void Auth::Done() noexcept
  63. {
  64. hash[0] = 0;
  65. rand[0] = 0;
  66. Status(AUTHED);
  67. }
  68. void Auth::NewNick(const RfcString& newnick) noexcept
  69. {
  70. if (ht_nick.contains(nick)) {
  71. ht_nick.remove(nick);
  72. }
  73. nick = newnick;
  74. ht_nick[newnick] = this;
  75. }
  76. Auth *Auth::Find(const char *_host) noexcept
  77. {
  78. if (ht_host.contains(_host)) {
  79. Auth *auth = ht_host[_host];
  80. sdprintf(STR("Found auth: (%s!%s) [%s]"), auth->nick.c_str(), auth->host,
  81. auth->user ? auth->user->handle : "*");
  82. return auth;
  83. }
  84. return NULL;
  85. }
  86. static void auth_clear_users_block(const bd::String& key, Auth* auth)
  87. {
  88. if (auth->user) {
  89. sdprintf(STR("Clearing USER for auth: (%s!%s) [%s]"), auth->nick.c_str(),
  90. auth->host, auth->user ? auth->user->handle : "*");
  91. auth->user = NULL;
  92. }
  93. }
  94. void Auth::NullUsers(const RfcString& nick) noexcept
  95. {
  96. if (!ht_nick.contains(nick))
  97. return;
  98. auto auth = ht_nick[nick];
  99. auth_clear_users_block(nick, auth);
  100. }
  101. void Auth::NullUsers(void) noexcept
  102. {
  103. for (auto& kv : ht_host) {
  104. auth_clear_users_block(kv.first, kv.second);
  105. }
  106. }
  107. static void auth_fill_users_block(const bd::String& key, Auth* auth)
  108. {
  109. char from[NICKLEN + UHOSTLEN];
  110. sdprintf(STR("Filling USER for auth: (%s!%s) [%s]"), auth->nick.c_str(), auth->host,
  111. auth->user ? auth->user->handle : "*");
  112. simple_snprintf(from, sizeof(from), "%s!%s", auth->nick.c_str(), auth->host);
  113. auth->user = get_user_by_host(from);
  114. }
  115. void Auth::FillUsers(void) noexcept
  116. {
  117. for (auto& kv : ht_host) {
  118. auth_fill_users_block(kv.first, kv.second);
  119. }
  120. }
  121. void Auth::ExpireAuths() noexcept
  122. {
  123. if (!ischanhub() || ht_host.size() == 0)
  124. return;
  125. std::vector<bd::String> expired_hosts;
  126. expired_hosts.reserve(ht_host.size());
  127. std::vector<const Auth*> delete_auths;
  128. delete_auths.reserve(ht_host.size());
  129. for (const auto& kv : ht_host) {
  130. const bd::String& host = kv.first;
  131. const auto& auth = kv.second;
  132. if (auth->Authed() && ((now - auth->atime) >= (1 * 60))) {
  133. putlog(LOG_DEBUG, "*", STR("Auth (%s!%s) [%s] expired."),
  134. auth->nick.c_str(), auth->host, auth->user ? auth->user->handle : "*");
  135. expired_hosts.push_back(host);
  136. ht_nick.remove(auth->nick);
  137. delete_auths.push_back(auth);
  138. }
  139. }
  140. for (const auto& host : expired_hosts) {
  141. ht_host.remove(host);
  142. }
  143. for (const auto& auth : delete_auths) {
  144. delete auth;
  145. }
  146. }
  147. void Auth::DeleteAll() noexcept
  148. {
  149. if (!ischanhub())
  150. return;
  151. putlog(LOG_DEBUG, "*", STR("Removing %zd auth entries."), ht_host.size());
  152. std::vector<const Auth*> delete_auths;
  153. delete_auths.reserve(ht_host.size());
  154. for (const auto& kv : ht_host) {
  155. const auto& auth = kv.second;
  156. putlog(LOG_DEBUG, "*", STR("Removing (%s!%s) [%s], from auth list."),
  157. auth->nick.c_str(), auth->host, auth->user ? auth->user->handle : "*");
  158. delete_auths.push_back(auth);
  159. }
  160. ht_host.clear();
  161. ht_nick.clear();
  162. for (const auto& auth : delete_auths) {
  163. delete auth;
  164. }
  165. }
  166. void Auth::InitTimer() noexcept
  167. {
  168. timer_create_secs(60, STR("Auth::ExpireAuths"), (Function) Auth::ExpireAuths);
  169. }
  170. bool Auth::GetIdx(const char *chname) noexcept
  171. {
  172. sdprintf(STR("GETIDX: auth: %s, idx: %d"), nick.c_str(), idx);
  173. if (idx != -1) {
  174. if (!valid_idx(idx))
  175. idx = -1;
  176. else if (!dcc[idx].irc || dcc[idx].simul == -1)
  177. idx = -1;
  178. else if (user && strcmp(dcc[idx].nick, user->handle))
  179. idx = -1;
  180. else {
  181. sdprintf(STR("FIRST FOUND: %d"), idx);
  182. strlcpy(dcc[idx].simulbot, nick.c_str(), sizeof(dcc[idx].simulbot));
  183. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", sizeof(dcc[idx].u.chat->con_chan));
  184. return 1;
  185. }
  186. }
  187. int i = 0;
  188. for (i = 0; i < dcc_total; i++) {
  189. if (dcc[i].type && dcc[i].irc &&
  190. (((chname && chname[0]) && !strcmp(dcc[i].simulbot, chname) &&
  191. user && !strcmp(dcc[i].nick, user->handle)) ||
  192. (!(chname && chname[0]) && !strcmp(dcc[i].simulbot, nick.c_str())))) {
  193. putlog(LOG_DEBUG, "*", STR("Simul found old idx for %s/%s: (%s!%s)"), nick.c_str(), chname, nick.c_str(), host);
  194. dcc[i].simultime = now;
  195. idx = i;
  196. strlcpy(dcc[idx].simulbot, nick.c_str(), sizeof(dcc[idx].simulbot));
  197. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", sizeof(dcc[idx].u.chat->con_chan));
  198. return 1;
  199. }
  200. }
  201. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  202. if (idx != -1) {
  203. char from[NICKLEN + UHOSTLEN];
  204. dcc[idx].sock = -1;
  205. dcc[idx].timeval = now;
  206. dcc[idx].irc = 1;
  207. dcc[idx].simultime = now;
  208. dcc[idx].simul = 0; /* not -1, so it's cleaned up later */
  209. dcc[idx].status = STAT_COLOR;
  210. dcc[idx].u.chat->con_flags = 0;
  211. strlcpy(dcc[idx].simulbot, nick.c_str(), sizeof(dcc[idx].simulbot));
  212. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", sizeof(dcc[idx].u.chat->con_chan));
  213. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  214. strlcpy(dcc[idx].nick, user ? user->handle : "*", sizeof(dcc[idx].nick));
  215. strlcpy(dcc[idx].host, host, sizeof(dcc[idx].host));
  216. dcc[idx].addr = 0L;
  217. simple_snprintf(from, sizeof(from), "%s!%s", nick.c_str(), host);
  218. dcc[idx].user = user ? user : get_user_by_host(from);
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. void Auth::TellAuthed(int idx) noexcept
  224. {
  225. for (const auto& kv : ht_host) {
  226. const Auth* auth = kv.second;
  227. dprintf(idx, "(%s!%s) [%s] authtime: %li, atime: %li, Status: %d\n",
  228. auth->nick.c_str(),
  229. auth->host, auth->user ? auth->user->handle : "*",
  230. (long)auth->authtime, (long)auth->atime, auth->Status());
  231. }
  232. }
  233. void makehash(struct userrec *u, const char *randstring, char *out, size_t out_size)
  234. {
  235. char hash[256] = "", *secpass = NULL;
  236. if (u && get_user(&USERENTRY_SECPASS, u)) {
  237. secpass = strdup((char *) get_user(&USERENTRY_SECPASS, u));
  238. secpass[strlen(secpass)] = 0;
  239. }
  240. simple_snprintf(hash, sizeof hash, "%s%s%s", randstring, (secpass && secpass[0]) ? secpass : "", auth_key);
  241. if (secpass)
  242. free(secpass);
  243. strlcpy(out, MD5(hash), out_size);
  244. OPENSSL_cleanse(hash, sizeof(hash));
  245. }
  246. int check_auth_dcc(Auth *auth, const char *cmd, const char *par)
  247. {
  248. return real_check_bind_dcc(cmd, auth->idx, par, auth);
  249. }
  250. /* vim: set sts=2 sw=2 ts=8 et: */