auth.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 "hash_table.h"
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <sys/wait.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include "chan.h"
  25. #include "tandem.h"
  26. #include "src/mod/server.mod/server.h"
  27. #include <pwd.h>
  28. #include <errno.h>
  29. #include <net/if.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/socket.h>
  32. #include <signal.h>
  33. #include "stat.h"
  34. hash_table_t *Auth::ht_handle = NULL, *Auth::ht_host = NULL;
  35. Auth::Auth(const char *_nick, const char *_host, struct userrec *u)
  36. {
  37. Status(AUTHING);
  38. strlcpy(nick, _nick, nick_len + 1);
  39. strlcpy(host, _host, UHOSTLEN);
  40. if (u) {
  41. user = u;
  42. strlcpy(handle, u->handle, sizeof(handle));
  43. } else {
  44. user = NULL;
  45. handle[0] = '*';
  46. handle[1] = 0;
  47. }
  48. if (!ht_host)
  49. ht_host = hash_table_create(NULL, NULL, 50, HASH_TABLE_STRINGS);
  50. if (!ht_handle)
  51. ht_handle = hash_table_create(NULL, NULL, 50, HASH_TABLE_STRINGS);
  52. hash_table_insert(ht_host, host, this);
  53. if (user)
  54. hash_table_insert(ht_handle, handle, this);
  55. sdprintf("New auth created! (%s!%s) [%s]", nick, host, handle);
  56. authtime = atime = now;
  57. bd = 0;
  58. idx = -1;
  59. }
  60. Auth::~Auth()
  61. {
  62. sdprintf("Removing auth: (%s!%s) [%s]", nick, host, handle);
  63. if (user)
  64. hash_table_remove(ht_handle, handle, this);
  65. hash_table_remove(ht_host, host, this);
  66. }
  67. void Auth::MakeHash(bool bd)
  68. {
  69. make_rand_str(rand, 50);
  70. if (bd)
  71. strlcpy(hash, makebdhash(rand), sizeof hash);
  72. else
  73. makehash(user, rand, hash, 50);
  74. }
  75. void Auth::Done(bool _bd)
  76. {
  77. hash[0] = 0;
  78. rand[0] = 0;
  79. Status(AUTHED);
  80. bd = _bd;
  81. }
  82. void Auth::NewNick(const char *newnick) {
  83. strlcpy(nick, newnick, nick_len + 1);
  84. }
  85. Auth *Auth::Find(const char *_host)
  86. {
  87. if (ht_host) {
  88. Auth *auth = NULL;
  89. hash_table_find(ht_host, _host, &auth);
  90. if (auth)
  91. sdprintf("Found auth: (%s!%s) [%s]", auth->nick, auth->host, auth->handle);
  92. return auth;
  93. }
  94. return NULL;
  95. }
  96. Auth *Auth::Find(const char *handle, bool _hand)
  97. {
  98. if (ht_handle) {
  99. Auth *auth = NULL;
  100. hash_table_find(ht_handle, handle, &auth);
  101. if (auth)
  102. sdprintf("Found auth (by handle): %s (%s!%s)", handle, auth->nick, auth->host);
  103. return auth;
  104. }
  105. return NULL;
  106. }
  107. static int auth_clear_users_walk(const void *key, void *data, void *param)
  108. {
  109. Auth *auth = *(Auth **)data;
  110. if (auth->user) {
  111. sdprintf("Clearing USER for auth: (%s!%s) [%s]", auth->nick, auth->host, auth->handle);
  112. auth->user = NULL;
  113. }
  114. return 0;
  115. }
  116. void Auth::NullUsers()
  117. {
  118. hash_table_walk(ht_host, auth_clear_users_walk, NULL);
  119. }
  120. static int auth_fill_users_walk(const void *key, void *data, void *param)
  121. {
  122. Auth *auth = *(Auth **)data;
  123. if (strcmp(auth->handle, "*")) {
  124. sdprintf("Filling USER for auth: (%s!%s) [%s]", auth->nick, auth->host, auth->handle);
  125. auth->user = get_user_by_handle(userlist, auth->handle);
  126. }
  127. return 0;
  128. }
  129. void Auth::FillUsers()
  130. {
  131. hash_table_walk(ht_host, auth_fill_users_walk, NULL);
  132. }
  133. static int auth_expire_walk(const void *key, void *data, void *param)
  134. {
  135. Auth *auth = *(Auth **)data;
  136. if (auth->Authed() && ((now - auth->atime) >= (60 * 60)))
  137. delete auth;
  138. return 0;
  139. }
  140. void Auth::ExpireAuths()
  141. {
  142. if (!ischanhub())
  143. return;
  144. hash_table_walk(ht_host, auth_expire_walk, NULL);
  145. }
  146. static int auth_delete_all_walk(const void *key, void *data, void *param)
  147. {
  148. Auth *auth = *(Auth **)data;
  149. putlog(LOG_DEBUG, "*", "Removing (%s!%s) [%s], from auth list.", auth->nick, auth->host, auth->handle);
  150. delete auth;
  151. return 0;
  152. }
  153. void Auth::DeleteAll()
  154. {
  155. if (ischanhub()) {
  156. putlog(LOG_DEBUG, "*", "Removing auth entries.");
  157. hash_table_walk(ht_host, auth_delete_all_walk, NULL);
  158. }
  159. }
  160. void Auth::InitTimer()
  161. {
  162. timer_create_secs(60, "Auth::ExpireAuths", (Function) Auth::ExpireAuths);
  163. }
  164. bool Auth::GetIdx(const char *chname)
  165. {
  166. sdprintf("GETIDX: auth: %s, idx: %d", nick, idx);
  167. if (idx != -1) {
  168. if (!valid_idx(idx))
  169. idx = -1;
  170. else {
  171. sdprintf("FIRST FOUND: %d", idx);
  172. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  173. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  174. return 1;
  175. }
  176. }
  177. int i = 0;
  178. for (i = 0; i < dcc_total; i++) {
  179. if (dcc[i].type && dcc[i].irc &&
  180. (((chname && chname[0]) && !strcmp(dcc[i].simulbot, chname) && !strcmp(dcc[i].nick, handle)) ||
  181. (!(chname && chname[0]) && !strcmp(dcc[i].simulbot, nick)))) {
  182. putlog(LOG_DEBUG, "*", "Simul found old idx for %s/%s: (%s!%s)", nick, chname, nick, host);
  183. dcc[i].simultime = now;
  184. idx = i;
  185. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  186. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  187. return 1;
  188. }
  189. }
  190. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  191. if (idx != -1) {
  192. dcc[idx].sock = -1;
  193. dcc[idx].timeval = now;
  194. dcc[idx].irc = 1;
  195. dcc[idx].simultime = now;
  196. dcc[idx].simul = 0; /* not -1, so it's cleaned up later */
  197. dcc[idx].status = STAT_COLOR;
  198. dcc[idx].u.chat->con_flags = 0;
  199. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  200. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  201. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  202. strlcpy(dcc[idx].nick, handle, NICKLEN);
  203. strlcpy(dcc[idx].host, host, UHOSTLEN);
  204. dcc[idx].addr = 0L;
  205. dcc[idx].user = user ? user : get_user_by_handle(userlist, handle);
  206. return 1;
  207. }
  208. return 0;
  209. }
  210. static int auth_tell_walk(const void *key, void *data, void *param)
  211. {
  212. Auth *auth = *(Auth **)data;
  213. int idx = (int) param;
  214. dprintf(idx, "%s(%s!%s) [%s] authtime: %li, atime: %li, Status: %d\n", auth->bd ? "x " : "", auth->nick,
  215. auth->host, auth->handle, auth->authtime, auth->atime, auth->Status());
  216. return 0;
  217. }
  218. void Auth::TellAuthed(int idx)
  219. {
  220. hash_table_walk(ht_host, auth_tell_walk, (void *) 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. const char*
  236. makebdhash(char *randstring)
  237. {
  238. char hash[70] = "";
  239. char *bdpass = "bdpass";
  240. simple_snprintf(hash, sizeof hash, "%s%s%s", randstring, bdpass, settings.packname);
  241. sdprintf("bdhash: %s", hash);
  242. const char* md5 = MD5(hash);
  243. OPENSSL_cleanse(hash, sizeof(hash));
  244. return md5;
  245. }
  246. void check_auth_dcc(Auth *auth, const char *cmd, const char *par)
  247. {
  248. real_check_bind_dcc(cmd, auth->idx, par, auth);
  249. }