auth.c 6.5 KB

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