auth.cc 7.0 KB

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