auth.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "types.h"
  10. #include "users.h"
  11. #include "crypt.h"
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. #include "chan.h"
  18. #include "tandem.h"
  19. #include "modules.h"
  20. #include <pwd.h>
  21. #include <errno.h>
  22. #include <net/if.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/socket.h>
  25. #include <signal.h>
  26. #include "stat.h"
  27. extern struct userrec *userlist;
  28. extern struct dcc_t *dcc;
  29. extern struct chanset_t *chanset;
  30. extern time_t now;
  31. #if defined(S_AUTHHASH) || defined(S_DCCAUTH)
  32. extern char authkey[];
  33. #endif /* S_AUTHHASH || S_DCCAUTH */
  34. #ifdef S_AUTHCMDS
  35. int auth_total = 0;
  36. int max_auth = 100;
  37. struct auth_t *auth = NULL;
  38. #endif /* S_AUTHCMDS */
  39. #ifdef S_AUTHCMDS
  40. void init_auth_max()
  41. {
  42. if (max_auth < 1)
  43. max_auth = 1;
  44. if (auth)
  45. auth = realloc(auth, sizeof(struct auth_t) * max_auth);
  46. else
  47. auth = calloc(1, sizeof(struct auth_t) * max_auth);
  48. }
  49. static void expire_auths()
  50. {
  51. int i = 0, idle = 0;
  52. if (!ischanhub()) return;
  53. for (i = 0; i < auth_total;i++) {
  54. if (auth[i].authed) {
  55. idle = now - auth[i].atime;
  56. if (idle >= (60 * 60)) {
  57. removeauth(i);
  58. }
  59. }
  60. }
  61. }
  62. #endif /* S_AUTHCMDS */
  63. void init_auth()
  64. {
  65. #ifdef S_AUTHCMDS
  66. init_auth_max();
  67. add_hook(HOOK_MINUTELY, (Function) expire_auths);
  68. #endif /* S_AUTHCMDS */
  69. }
  70. #if defined(S_AUTHHASH) || defined(S_DCCAUTH)
  71. char *makehash(struct userrec *u, char *rand)
  72. {
  73. char hash[256] = "", *secpass = NULL;
  74. if (get_user(&USERENTRY_SECPASS, u)) {
  75. secpass = strdup(get_user(&USERENTRY_SECPASS, u));
  76. secpass[strlen(secpass)] = 0;
  77. }
  78. egg_snprintf(hash, sizeof hash, "%s%s%s", rand, (secpass && secpass[0]) ? secpass : "" , (authkey && authkey[0]) ? authkey : "");
  79. if (secpass)
  80. free(secpass);
  81. return md5(hash);
  82. }
  83. #endif /* S_AUTHHASH || S_DCCAUTH */
  84. #ifdef S_AUTHCMDS
  85. int new_auth(void)
  86. {
  87. int i = auth_total;
  88. if (auth_total == max_auth)
  89. return -1;
  90. auth_total++;
  91. egg_bzero((char *) &auth[i], sizeof(struct auth_t));
  92. return i;
  93. }
  94. /* returns 0 if not found, -1 if problem, and > 0 if found. */
  95. int findauth(char *host)
  96. {
  97. int i = 0;
  98. if (!host || !host[0])
  99. return -1;
  100. for (i = 0; i < auth_total; i++) {
  101. if (!auth[i].host) {
  102. putlog(LOG_MISC, "*", "AUTH ENTRY: %d HAS NO HOST??", i);
  103. continue;
  104. }
  105. putlog(LOG_DEBUG, "*", STR("Debug for findauth: checking: %s i: %d :: %s"), host, i, auth[i].host);
  106. if (auth[i].host && !strcmp(auth[i].host, host)) {
  107. return i;
  108. }
  109. }
  110. return -1;
  111. }
  112. void removeauth(int n)
  113. {
  114. putlog(LOG_DEBUG, "*", "Removing %s from auth list.", auth[n].host);
  115. auth_total--;
  116. if (n < auth_total)
  117. egg_memcpy(&auth[n], &auth[auth_total], sizeof(struct auth_t));
  118. else
  119. egg_bzero(&auth[n], sizeof(struct auth_t)); /* drummer */
  120. }
  121. #endif /* S_AUTHCMDS */