auth.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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<bd::String, Auth*> Auth::ht_nick(10);
  37. Auth::Auth(const char *_nick, const char *_host, struct userrec *u)
  38. {
  39. Status(AUTHING);
  40. strlcpy(nick, _nick, NICKLEN);
  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, 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, 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 char *newnick) noexcept
  69. {
  70. if (ht_nick.contains(nick)) {
  71. Auth::ht_nick.remove(nick);
  72. }
  73. strlcpy(nick, newnick, NICKLEN);
  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, 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, void *param)
  87. {
  88. if (auth->user) {
  89. sdprintf(STR("Clearing USER for auth: (%s!%s) [%s]"), auth->nick,
  90. auth->host, auth->user ? auth->user->handle : "*");
  91. auth->user = NULL;
  92. }
  93. }
  94. void Auth::NullUsers(const char *nick) noexcept
  95. {
  96. if (nick == NULL) {
  97. ht_host.each(auth_clear_users_block);
  98. } else {
  99. if (ht_nick.contains(nick)) {
  100. Auth *auth = ht_nick[nick];
  101. auth_clear_users_block(nick, auth, NULL);
  102. }
  103. }
  104. }
  105. static void auth_fill_users_block(const bd::String key, Auth* auth, void* param)
  106. {
  107. char from[NICKLEN + UHOSTLEN];
  108. sdprintf(STR("Filling USER for auth: (%s!%s) [%s]"), auth->nick, auth->host,
  109. auth->user ? auth->user->handle : "*");
  110. simple_snprintf(from, sizeof(from), "%s!%s", auth->nick, auth->host);
  111. auth->user = get_user_by_host(from);
  112. }
  113. void Auth::FillUsers(const char *nick) noexcept
  114. {
  115. if (nick == NULL) {
  116. ht_host.each(auth_fill_users_block);
  117. } else {
  118. if (ht_nick.contains(nick)) {
  119. Auth *auth = ht_nick[nick];
  120. auth_fill_users_block(nick, auth, NULL);
  121. }
  122. }
  123. }
  124. static void auth_expire_block(const bd::String key, Auth* auth, void* param)
  125. {
  126. if (auth->Authed() && ((now - auth->atime) >= (60 * 60))) {
  127. Auth::ht_host.remove(key);
  128. Auth::ht_nick.remove(auth->nick);
  129. delete auth;
  130. }
  131. }
  132. void Auth::ExpireAuths() noexcept
  133. {
  134. if (!ischanhub())
  135. return;
  136. ht_host.each(auth_expire_block);
  137. }
  138. static void auth_delete_all_block(const bd::String, Auth* auth, void* param)
  139. {
  140. putlog(LOG_DEBUG, "*", STR("Removing (%s!%s) [%s], from auth list."),
  141. auth->nick, auth->host, auth->user ? auth->user->handle : "*");
  142. delete auth;
  143. }
  144. void Auth::DeleteAll() noexcept
  145. {
  146. if (ischanhub()) {
  147. putlog(LOG_DEBUG, "*", STR("Removing auth entries."));
  148. ht_host.each(auth_delete_all_block);
  149. ht_host.clear();
  150. ht_nick.clear();
  151. }
  152. }
  153. void Auth::InitTimer() noexcept
  154. {
  155. timer_create_secs(60, STR("Auth::ExpireAuths"), (Function) Auth::ExpireAuths);
  156. }
  157. bool Auth::GetIdx(const char *chname) noexcept
  158. {
  159. sdprintf(STR("GETIDX: auth: %s, idx: %d"), nick, idx);
  160. if (idx != -1) {
  161. if (!valid_idx(idx))
  162. idx = -1;
  163. else if (!dcc[idx].irc || dcc[idx].simul == -1)
  164. idx = -1;
  165. else if (user && strcmp(dcc[idx].nick, user->handle))
  166. idx = -1;
  167. else {
  168. sdprintf(STR("FIRST FOUND: %d"), idx);
  169. strlcpy(dcc[idx].simulbot, nick, sizeof(dcc[idx].simulbot));
  170. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", sizeof(dcc[idx].u.chat->con_chan));
  171. return 1;
  172. }
  173. }
  174. int i = 0;
  175. for (i = 0; i < dcc_total; i++) {
  176. if (dcc[i].type && dcc[i].irc &&
  177. (((chname && chname[0]) && !strcmp(dcc[i].simulbot, chname) &&
  178. user && !strcmp(dcc[i].nick, user->handle)) ||
  179. (!(chname && chname[0]) && !strcmp(dcc[i].simulbot, nick)))) {
  180. putlog(LOG_DEBUG, "*", STR("Simul found old idx for %s/%s: (%s!%s)"), nick, chname, nick, host);
  181. dcc[i].simultime = now;
  182. idx = i;
  183. strlcpy(dcc[idx].simulbot, nick, sizeof(dcc[idx].simulbot));
  184. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", sizeof(dcc[idx].u.chat->con_chan));
  185. return 1;
  186. }
  187. }
  188. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  189. if (idx != -1) {
  190. char from[NICKLEN + UHOSTLEN];
  191. dcc[idx].sock = -1;
  192. dcc[idx].timeval = now;
  193. dcc[idx].irc = 1;
  194. dcc[idx].simultime = now;
  195. dcc[idx].simul = 0; /* not -1, so it's cleaned up later */
  196. dcc[idx].status = STAT_COLOR;
  197. dcc[idx].u.chat->con_flags = 0;
  198. strlcpy(dcc[idx].simulbot, nick, sizeof(dcc[idx].simulbot));
  199. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", sizeof(dcc[idx].u.chat->con_chan));
  200. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  201. strlcpy(dcc[idx].nick, user ? user->handle : "*", sizeof(dcc[idx].nick));
  202. strlcpy(dcc[idx].host, host, sizeof(dcc[idx].host));
  203. dcc[idx].addr = 0L;
  204. simple_snprintf(from, sizeof(from), "%s!%s", nick, host);
  205. dcc[idx].user = user ? user : get_user_by_host(from);
  206. return 1;
  207. }
  208. return 0;
  209. }
  210. static void auth_tell_block(const bd::String key, Auth* auth, void* param)
  211. {
  212. long lparam = (long) param;
  213. int idx = (int) lparam;
  214. dprintf(idx, "(%s!%s) [%s] authtime: %li, atime: %li, Status: %d\n", auth->nick,
  215. auth->host, auth->user ? auth->user->handle : "*",
  216. (long)auth->authtime, (long)auth->atime, auth->Status());
  217. }
  218. void Auth::TellAuthed(int idx) noexcept
  219. {
  220. ht_host.each(auth_tell_block, (void *) (long) idx);
  221. }
  222. void makehash(struct userrec *u, const char *randstring, char *out, size_t out_size)
  223. {
  224. char hash[256] = "", *secpass = NULL;
  225. if (u && get_user(&USERENTRY_SECPASS, u)) {
  226. secpass = strdup((char *) get_user(&USERENTRY_SECPASS, u));
  227. secpass[strlen(secpass)] = 0;
  228. }
  229. simple_snprintf(hash, sizeof hash, "%s%s%s", randstring, (secpass && secpass[0]) ? secpass : "", auth_key);
  230. if (secpass)
  231. free(secpass);
  232. strlcpy(out, MD5(hash), out_size);
  233. OPENSSL_cleanse(hash, sizeof(hash));
  234. }
  235. int check_auth_dcc(Auth *auth, const char *cmd, const char *par)
  236. {
  237. return real_check_bind_dcc(cmd, auth->idx, par, auth);
  238. }
  239. /* vim: set sts=2 sw=2 ts=8 et: */