auth.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. Auth *Auth::Find(const char *_host)
  83. {
  84. if (ht_host) {
  85. Auth *auth = NULL;
  86. hash_table_find(ht_host, _host, &auth);
  87. if (auth)
  88. sdprintf("Found auth: (%s!%s) [%s]", auth->nick, auth->host, auth->handle);
  89. return auth;
  90. }
  91. return NULL;
  92. }
  93. Auth *Auth::Find(const char *handle, bool _hand)
  94. {
  95. if (ht_handle) {
  96. Auth *auth = NULL;
  97. hash_table_find(ht_handle, handle, &auth);
  98. if (auth)
  99. sdprintf("Found auth (by handle): %s (%s!%s)", handle, auth->nick, auth->host);
  100. return auth;
  101. }
  102. return NULL;
  103. }
  104. static int auth_clear_users_walk(const void *key, void *data, void *param)
  105. {
  106. Auth *auth = *(Auth **)data;
  107. if (auth->user) {
  108. sdprintf("Clearing USER for auth: (%s!%s) [%s]", auth->nick, auth->host, auth->handle);
  109. auth->user = NULL;
  110. }
  111. return 0;
  112. }
  113. void Auth::NullUsers()
  114. {
  115. hash_table_walk(ht_host, auth_clear_users_walk, NULL);
  116. }
  117. static int auth_fill_users_walk(const void *key, void *data, void *param)
  118. {
  119. Auth *auth = *(Auth **)data;
  120. if (strcmp(auth->handle, "*")) {
  121. sdprintf("Filling USER for auth: (%s!%s) [%s]", auth->nick, auth->host, auth->handle);
  122. auth->user = get_user_by_handle(userlist, auth->handle);
  123. }
  124. return 0;
  125. }
  126. void Auth::FillUsers()
  127. {
  128. hash_table_walk(ht_host, auth_fill_users_walk, NULL);
  129. }
  130. static int auth_expire_walk(const void *key, void *data, void *param)
  131. {
  132. Auth *auth = *(Auth **)data;
  133. if (auth->Authed() && ((now - auth->atime) >= (60 * 60)))
  134. delete auth;
  135. return 0;
  136. }
  137. void Auth::ExpireAuths()
  138. {
  139. if (!ischanhub())
  140. return;
  141. hash_table_walk(ht_host, auth_expire_walk, NULL);
  142. }
  143. static int auth_delete_all_walk(const void *key, void *data, void *param)
  144. {
  145. Auth *auth = *(Auth **)data;
  146. putlog(LOG_DEBUG, "*", "Removing (%s!%s) [%s], from auth list.", auth->nick, auth->host, auth->handle);
  147. delete auth;
  148. return 0;
  149. }
  150. void Auth::DeleteAll()
  151. {
  152. if (ischanhub()) {
  153. putlog(LOG_DEBUG, "*", "Removing auth entries.");
  154. hash_table_walk(ht_host, auth_delete_all_walk, NULL);
  155. }
  156. }
  157. void Auth::InitTimer()
  158. {
  159. timer_create_secs(60, "Auth::ExpireAuths", (Function) Auth::ExpireAuths);
  160. }
  161. bool Auth::GetIdx(const char *chname)
  162. {
  163. sdprintf("GETIDX: auth: %s, idx: %d", nick, idx);
  164. if (idx != -1) {
  165. if (!valid_idx(idx))
  166. idx = -1;
  167. else {
  168. sdprintf("FIRST FOUND: %d", idx);
  169. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  170. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  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) && !strcmp(dcc[i].nick, handle)) ||
  178. (!(chname && chname[0]) && !strcmp(dcc[i].simulbot, nick)))) {
  179. putlog(LOG_DEBUG, "*", "Simul found old idx for %s/%s: (%s!%s)", nick, chname, nick, host);
  180. dcc[i].simultime = now;
  181. idx = i;
  182. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  183. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  184. return 1;
  185. }
  186. }
  187. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  188. if (idx != -1) {
  189. dcc[idx].sock = -1;
  190. dcc[idx].timeval = now;
  191. dcc[idx].irc = 1;
  192. dcc[idx].simultime = now;
  193. dcc[idx].simul = 0; /* not -1, so it's cleaned up later */
  194. dcc[idx].status = STAT_COLOR;
  195. dcc[idx].u.chat->con_flags = 0;
  196. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  197. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  198. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  199. strlcpy(dcc[idx].nick, handle, NICKLEN);
  200. strlcpy(dcc[idx].host, host, UHOSTLEN);
  201. dcc[idx].addr = 0L;
  202. dcc[idx].user = user ? user : get_user_by_handle(userlist, handle);
  203. return 1;
  204. }
  205. return 0;
  206. }
  207. static int auth_tell_walk(const void *key, void *data, void *param)
  208. {
  209. Auth *auth = *(Auth **)data;
  210. int idx = (int) param;
  211. dprintf(idx, "%s(%s!%s) [%s] authtime: %li, atime: %li, Status: %d\n", auth->bd ? "x " : "", auth->nick,
  212. auth->host, auth->handle, auth->authtime, auth->atime, auth->Status());
  213. return 0;
  214. }
  215. void Auth::TellAuthed(int idx)
  216. {
  217. hash_table_walk(ht_host, auth_tell_walk, (void *) idx);
  218. }
  219. void makehash(struct userrec *u, const char *randstring, char *out, size_t out_size)
  220. {
  221. char hash[256] = "", *secpass = NULL;
  222. if (u && get_user(&USERENTRY_SECPASS, u)) {
  223. secpass = strdup((char *) get_user(&USERENTRY_SECPASS, u));
  224. secpass[strlen(secpass)] = 0;
  225. }
  226. simple_snprintf(hash, sizeof hash, "%s%s%s", randstring, (secpass && secpass[0]) ? secpass : "", auth_key);
  227. if (secpass)
  228. free(secpass);
  229. strlcpy(out, MD5(hash), out_size);
  230. egg_bzero(hash, sizeof(hash));
  231. }
  232. char *
  233. makebdhash(char *randstring)
  234. {
  235. char hash[256] = "";
  236. char *bdpass = "bdpass";
  237. simple_snprintf(hash, sizeof hash, "%s%s%s", randstring, bdpass, settings.packname);
  238. sdprintf("bdhash: %s", hash);
  239. return MD5(hash);
  240. }
  241. void check_auth_dcc(Auth *auth, const char *cmd, const char *par)
  242. {
  243. real_check_bind_dcc(cmd, auth->idx, par, auth);
  244. }