auth.c 6.6 KB

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