auth.c 2.9 KB

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