auth.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* I hereby release this into the Public Domain - Bryan Drewery */
  2. /*
  3. * auth.c -- handles:
  4. * auth system functions
  5. * auth system hooks
  6. */
  7. #include "common.h"
  8. #include "auth.h"
  9. #include "misc.h"
  10. #include "main.h"
  11. #include "settings.h"
  12. #include "types.h"
  13. #include "userrec.h"
  14. #include "set.h"
  15. #include "core_binds.h"
  16. #include "egg_timer.h"
  17. #include "users.h"
  18. #include "crypt.h"
  19. #include "hash_table.h"
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include "chan.h"
  26. #include "tandem.h"
  27. #include "src/mod/server.mod/server.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. hash_table_t *Auth::ht_handle = NULL, *Auth::ht_host = NULL;
  36. Auth::Auth(const char *_nick, const char *_host, struct userrec *u)
  37. {
  38. Status(AUTHING);
  39. strlcpy(nick, _nick, nick_len + 1);
  40. strlcpy(host, _host, UHOSTLEN);
  41. if (u) {
  42. user = u;
  43. strlcpy(handle, u->handle, sizeof(handle));
  44. } else {
  45. user = NULL;
  46. handle[0] = '*';
  47. handle[1] = 0;
  48. }
  49. if (!ht_host)
  50. ht_host = hash_table_create(NULL, NULL, 50, HASH_TABLE_STRINGS);
  51. if (!ht_handle)
  52. ht_handle = hash_table_create(NULL, NULL, 50, HASH_TABLE_STRINGS);
  53. hash_table_insert(ht_host, host, this);
  54. if (user)
  55. hash_table_insert(ht_handle, handle, this);
  56. sdprintf(STR("New auth created! (%s!%s) [%s]"), nick, host, handle);
  57. authtime = atime = now;
  58. idx = -1;
  59. }
  60. Auth::~Auth()
  61. {
  62. sdprintf(STR("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()
  68. {
  69. make_rand_str(rand, 50);
  70. makehash(user, rand, hash, 50);
  71. }
  72. void Auth::Done()
  73. {
  74. hash[0] = 0;
  75. rand[0] = 0;
  76. Status(AUTHED);
  77. }
  78. void Auth::NewNick(const char *newnick) {
  79. strlcpy(nick, newnick, nick_len + 1);
  80. }
  81. Auth *Auth::Find(const char *_host)
  82. {
  83. if (ht_host) {
  84. Auth *auth = NULL;
  85. hash_table_find(ht_host, _host, &auth);
  86. if (auth)
  87. sdprintf(STR("Found auth: (%s!%s) [%s]"), auth->nick, auth->host, auth->handle);
  88. return auth;
  89. }
  90. return NULL;
  91. }
  92. Auth *Auth::Find(const char *handle, bool _hand)
  93. {
  94. if (ht_handle) {
  95. Auth *auth = NULL;
  96. hash_table_find(ht_handle, handle, &auth);
  97. if (auth)
  98. sdprintf(STR("Found auth (by handle): %s (%s!%s)"), handle, auth->nick, auth->host);
  99. return auth;
  100. }
  101. return NULL;
  102. }
  103. static int auth_clear_users_walk(const void *key, void *data, void *param)
  104. {
  105. Auth *auth = *(Auth **)data;
  106. if (auth->user) {
  107. sdprintf(STR("Clearing USER for auth: (%s!%s) [%s]"), auth->nick, auth->host, auth->handle);
  108. auth->user = NULL;
  109. }
  110. return 0;
  111. }
  112. void Auth::NullUsers()
  113. {
  114. hash_table_walk(ht_host, auth_clear_users_walk, NULL);
  115. }
  116. static int auth_fill_users_walk(const void *key, void *data, void *param)
  117. {
  118. Auth *auth = *(Auth **)data;
  119. if (strcmp(auth->handle, "*")) {
  120. sdprintf(STR("Filling USER for auth: (%s!%s) [%s]"), auth->nick, auth->host, auth->handle);
  121. auth->user = get_user_by_handle(userlist, auth->handle);
  122. }
  123. return 0;
  124. }
  125. void Auth::FillUsers()
  126. {
  127. hash_table_walk(ht_host, auth_fill_users_walk, NULL);
  128. }
  129. static int auth_expire_walk(const void *key, void *data, void *param)
  130. {
  131. Auth *auth = *(Auth **)data;
  132. if (auth->Authed() && ((now - auth->atime) >= (60 * 60)))
  133. delete auth;
  134. return 0;
  135. }
  136. void Auth::ExpireAuths()
  137. {
  138. if (!ischanhub())
  139. return;
  140. hash_table_walk(ht_host, auth_expire_walk, NULL);
  141. }
  142. static int auth_delete_all_walk(const void *key, void *data, void *param)
  143. {
  144. Auth *auth = *(Auth **)data;
  145. putlog(LOG_DEBUG, "*", STR("Removing (%s!%s) [%s], from auth list."), auth->nick, auth->host, auth->handle);
  146. delete auth;
  147. return 0;
  148. }
  149. void Auth::DeleteAll()
  150. {
  151. if (ischanhub()) {
  152. putlog(LOG_DEBUG, "*", STR("Removing auth entries."));
  153. hash_table_walk(ht_host, auth_delete_all_walk, NULL);
  154. }
  155. }
  156. void Auth::InitTimer()
  157. {
  158. timer_create_secs(60, STR("Auth::ExpireAuths"), (Function) Auth::ExpireAuths);
  159. }
  160. bool Auth::GetIdx(const char *chname)
  161. {
  162. sdprintf(STR("GETIDX: auth: %s, idx: %d"), nick, idx);
  163. if (idx != -1) {
  164. if (!valid_idx(idx))
  165. idx = -1;
  166. else {
  167. sdprintf(STR("FIRST FOUND: %d"), idx);
  168. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  169. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  170. return 1;
  171. }
  172. }
  173. int i = 0;
  174. for (i = 0; i < dcc_total; i++) {
  175. if (dcc[i].type && dcc[i].irc &&
  176. (((chname && chname[0]) && !strcmp(dcc[i].simulbot, chname) && !strcmp(dcc[i].nick, handle)) ||
  177. (!(chname && chname[0]) && !strcmp(dcc[i].simulbot, nick)))) {
  178. putlog(LOG_DEBUG, "*", STR("Simul found old idx for %s/%s: (%s!%s)"), nick, chname, nick, host);
  179. dcc[i].simultime = now;
  180. idx = i;
  181. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  182. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  183. return 1;
  184. }
  185. }
  186. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  187. if (idx != -1) {
  188. dcc[idx].sock = -1;
  189. dcc[idx].timeval = now;
  190. dcc[idx].irc = 1;
  191. dcc[idx].simultime = now;
  192. dcc[idx].simul = 0; /* not -1, so it's cleaned up later */
  193. dcc[idx].status = STAT_COLOR;
  194. dcc[idx].u.chat->con_flags = 0;
  195. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  196. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  197. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  198. strlcpy(dcc[idx].nick, handle, NICKLEN);
  199. strlcpy(dcc[idx].host, host, UHOSTLEN);
  200. dcc[idx].addr = 0L;
  201. dcc[idx].user = user ? user : get_user_by_handle(userlist, handle);
  202. return 1;
  203. }
  204. return 0;
  205. }
  206. static int auth_tell_walk(const void *key, void *data, void *param)
  207. {
  208. Auth *auth = *(Auth **)data;
  209. long lparam = (long) param;
  210. int idx = (int) lparam;
  211. dprintf(idx, "(%s!%s) [%s] authtime: %li, atime: %li, Status: %d\n", auth->nick,
  212. auth->host, auth->handle, (long)auth->authtime, (long)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 *) (long) 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. OPENSSL_cleanse(hash, sizeof(hash));
  231. }
  232. void check_auth_dcc(Auth *auth, const char *cmd, const char *par)
  233. {
  234. real_check_bind_dcc(cmd, auth->idx, par, auth);
  235. }