auth.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #ifdef no
  37. static const char* makebdhash(char *);
  38. #endif
  39. Auth::Auth(const char *_nick, const char *_host, struct userrec *u)
  40. {
  41. Status(AUTHING);
  42. strlcpy(nick, _nick, nick_len + 1);
  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. if (!ht_host)
  53. ht_host = hash_table_create(NULL, NULL, 50, HASH_TABLE_STRINGS);
  54. if (!ht_handle)
  55. ht_handle = hash_table_create(NULL, NULL, 50, HASH_TABLE_STRINGS);
  56. hash_table_insert(ht_host, host, this);
  57. if (user)
  58. hash_table_insert(ht_handle, handle, this);
  59. sdprintf(STR("New auth created! (%s!%s) [%s]"), nick, host, handle);
  60. authtime = atime = now;
  61. #ifdef NOTUSED
  62. bd = 0;
  63. #endif
  64. idx = -1;
  65. }
  66. Auth::~Auth()
  67. {
  68. sdprintf(STR("Removing auth: (%s!%s) [%s]"), nick, host, handle);
  69. if (user)
  70. hash_table_remove(ht_handle, handle, this);
  71. hash_table_remove(ht_host, host, this);
  72. }
  73. void Auth::MakeHash(bool bd)
  74. {
  75. make_rand_str(rand, 50);
  76. #ifdef NOTUSED
  77. if (bd)
  78. strlcpy(hash, makebdhash(rand), sizeof hash);
  79. else
  80. #endif
  81. makehash(user, rand, hash, 50);
  82. }
  83. void Auth::Done(bool _bd)
  84. {
  85. hash[0] = 0;
  86. rand[0] = 0;
  87. Status(AUTHED);
  88. #ifdef NOTUSED
  89. bd = _bd;
  90. #endif
  91. }
  92. void Auth::NewNick(const char *newnick) {
  93. strlcpy(nick, newnick, nick_len + 1);
  94. }
  95. Auth *Auth::Find(const char *_host)
  96. {
  97. if (ht_host) {
  98. Auth *auth = NULL;
  99. hash_table_find(ht_host, _host, &auth);
  100. if (auth)
  101. sdprintf(STR("Found auth: (%s!%s) [%s]"), auth->nick, auth->host, auth->handle);
  102. return auth;
  103. }
  104. return NULL;
  105. }
  106. Auth *Auth::Find(const char *handle, bool _hand)
  107. {
  108. if (ht_handle) {
  109. Auth *auth = NULL;
  110. hash_table_find(ht_handle, handle, &auth);
  111. if (auth)
  112. sdprintf(STR("Found auth (by handle): %s (%s!%s)"), handle, auth->nick, auth->host);
  113. return auth;
  114. }
  115. return NULL;
  116. }
  117. static int auth_clear_users_walk(const void *key, void *data, void *param)
  118. {
  119. Auth *auth = *(Auth **)data;
  120. if (auth->user) {
  121. sdprintf(STR("Clearing USER for auth: (%s!%s) [%s]"), auth->nick, auth->host, auth->handle);
  122. auth->user = NULL;
  123. }
  124. return 0;
  125. }
  126. void Auth::NullUsers()
  127. {
  128. hash_table_walk(ht_host, auth_clear_users_walk, NULL);
  129. }
  130. static int auth_fill_users_walk(const void *key, void *data, void *param)
  131. {
  132. Auth *auth = *(Auth **)data;
  133. if (strcmp(auth->handle, "*")) {
  134. sdprintf(STR("Filling USER for auth: (%s!%s) [%s]"), auth->nick, auth->host, auth->handle);
  135. auth->user = get_user_by_handle(userlist, auth->handle);
  136. }
  137. return 0;
  138. }
  139. void Auth::FillUsers()
  140. {
  141. hash_table_walk(ht_host, auth_fill_users_walk, NULL);
  142. }
  143. static int auth_expire_walk(const void *key, void *data, void *param)
  144. {
  145. Auth *auth = *(Auth **)data;
  146. if (auth->Authed() && ((now - auth->atime) >= (60 * 60)))
  147. delete auth;
  148. return 0;
  149. }
  150. void Auth::ExpireAuths()
  151. {
  152. if (!ischanhub())
  153. return;
  154. hash_table_walk(ht_host, auth_expire_walk, NULL);
  155. }
  156. static int auth_delete_all_walk(const void *key, void *data, void *param)
  157. {
  158. Auth *auth = *(Auth **)data;
  159. putlog(LOG_DEBUG, "*", STR("Removing (%s!%s) [%s], from auth list."), auth->nick, auth->host, auth->handle);
  160. delete auth;
  161. return 0;
  162. }
  163. void Auth::DeleteAll()
  164. {
  165. if (ischanhub()) {
  166. putlog(LOG_DEBUG, "*", STR("Removing auth entries."));
  167. hash_table_walk(ht_host, auth_delete_all_walk, NULL);
  168. }
  169. }
  170. void Auth::InitTimer()
  171. {
  172. timer_create_secs(60, STR("Auth::ExpireAuths"), (Function) Auth::ExpireAuths);
  173. }
  174. bool Auth::GetIdx(const char *chname)
  175. {
  176. sdprintf(STR("GETIDX: auth: %s, idx: %d"), nick, idx);
  177. if (idx != -1) {
  178. if (!valid_idx(idx))
  179. idx = -1;
  180. else {
  181. sdprintf(STR("FIRST FOUND: %d"), idx);
  182. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  183. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  184. return 1;
  185. }
  186. }
  187. int i = 0;
  188. for (i = 0; i < dcc_total; i++) {
  189. if (dcc[i].type && dcc[i].irc &&
  190. (((chname && chname[0]) && !strcmp(dcc[i].simulbot, chname) && !strcmp(dcc[i].nick, handle)) ||
  191. (!(chname && chname[0]) && !strcmp(dcc[i].simulbot, nick)))) {
  192. putlog(LOG_DEBUG, "*", STR("Simul found old idx for %s/%s: (%s!%s)"), nick, chname, nick, host);
  193. dcc[i].simultime = now;
  194. idx = i;
  195. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  196. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  197. return 1;
  198. }
  199. }
  200. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  201. if (idx != -1) {
  202. dcc[idx].sock = -1;
  203. dcc[idx].timeval = now;
  204. dcc[idx].irc = 1;
  205. dcc[idx].simultime = now;
  206. dcc[idx].simul = 0; /* not -1, so it's cleaned up later */
  207. dcc[idx].status = STAT_COLOR;
  208. dcc[idx].u.chat->con_flags = 0;
  209. strlcpy(dcc[idx].simulbot, chname ? chname : nick, NICKLEN);
  210. strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", 81);
  211. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  212. strlcpy(dcc[idx].nick, handle, NICKLEN);
  213. strlcpy(dcc[idx].host, host, UHOSTLEN);
  214. dcc[idx].addr = 0L;
  215. dcc[idx].user = user ? user : get_user_by_handle(userlist, handle);
  216. return 1;
  217. }
  218. return 0;
  219. }
  220. static int auth_tell_walk(const void *key, void *data, void *param)
  221. {
  222. Auth *auth = *(Auth **)data;
  223. int idx = (int) param;
  224. // dprintf(idx, "%s(%s!%s) [%s] authtime: %li, atime: %li, Status: %d\n", auth->bd ? "x " : "", auth->nick,
  225. dprintf(idx, "(%s!%s) [%s] authtime: %li, atime: %li, Status: %d\n", auth->nick,
  226. auth->host, auth->handle, (long)auth->authtime, (long)auth->atime, auth->Status());
  227. return 0;
  228. }
  229. void Auth::TellAuthed(int idx)
  230. {
  231. hash_table_walk(ht_host, auth_tell_walk, (void *) idx);
  232. }
  233. void makehash(struct userrec *u, const char *randstring, char *out, size_t out_size)
  234. {
  235. char hash[256] = "", *secpass = NULL;
  236. if (u && get_user(&USERENTRY_SECPASS, u)) {
  237. secpass = strdup((char *) get_user(&USERENTRY_SECPASS, u));
  238. secpass[strlen(secpass)] = 0;
  239. }
  240. simple_snprintf(hash, sizeof hash, "%s%s%s", randstring, (secpass && secpass[0]) ? secpass : "", auth_key);
  241. if (secpass)
  242. free(secpass);
  243. strlcpy(out, MD5(hash), out_size);
  244. OPENSSL_cleanse(hash, sizeof(hash));
  245. }
  246. #ifdef no
  247. /* This isn't even used */
  248. const char*
  249. makebdhash(char *randstring)
  250. {
  251. char hash[70] = "";
  252. const char *bdpass = STR("bdpass");
  253. simple_snprintf(hash, sizeof hash, "%s%s%s", randstring, bdpass, settings.packname);
  254. sdprintf(STR("bdhash: %s"), hash);
  255. const char* md5 = MD5(hash);
  256. OPENSSL_cleanse(hash, sizeof(hash));
  257. return md5;
  258. }
  259. #endif
  260. void check_auth_dcc(Auth *auth, const char *cmd, const char *par)
  261. {
  262. real_check_bind_dcc(cmd, auth->idx, par, auth);
  263. }