auth.cc 6.9 KB

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