auth.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * auth.c -- handles:
  3. * auth system functions
  4. * auth system hooks
  5. */
  6. #include "eggmain.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. #ifdef S_AUTH
  32. extern char authkey[];
  33. int auth_total = 0;
  34. int max_auth = 100;
  35. struct auth_t *auth = 0;
  36. #endif /* S_AUTH */
  37. #ifdef S_AUTH
  38. void init_auth_max()
  39. {
  40. if (max_auth < 1)
  41. max_auth = 1;
  42. if (auth)
  43. auth = realloc(auth, sizeof(struct auth_t) * max_auth);
  44. else
  45. auth = malloc(sizeof(struct auth_t) * max_auth);
  46. }
  47. static void expire_auths()
  48. {
  49. int i = 0, idle = 0;
  50. if (!ischanhub()) return;
  51. for (i = 0; i < auth_total;i++) {
  52. if (auth[i].authed) {
  53. idle = now - auth[i].atime;
  54. if (idle >= (60 * 60)) {
  55. removeauth(i);
  56. }
  57. }
  58. }
  59. }
  60. #endif /* S_AUTH */
  61. void init_auth()
  62. {
  63. #ifdef S_AUTH
  64. init_auth_max();
  65. add_hook(HOOK_MINUTELY, (Function) expire_auths);
  66. #endif /* S_AUTH */
  67. }
  68. #ifdef S_AUTH
  69. char *makehash(struct userrec *u, char *rand)
  70. {
  71. char hash[256], *secpass = NULL;
  72. if (get_user(&USERENTRY_SECPASS, u)) {
  73. secpass = strdup(get_user(&USERENTRY_SECPASS, u));
  74. secpass[strlen(secpass)] = 0;
  75. }
  76. sprintf(hash, "%s%s%s", rand, (secpass && secpass[0]) ? secpass : "" , (authkey && authkey[0]) ? authkey : "");
  77. if (secpass)
  78. free(secpass);
  79. return md5(hash);
  80. }
  81. int new_auth(void)
  82. {
  83. int i = auth_total;
  84. if (auth_total == max_auth)
  85. return -1;
  86. auth_total++;
  87. egg_bzero((char *) &auth[i], sizeof(struct auth_t));
  88. return i;
  89. }
  90. /* returns 0 if not found, -1 if problem, and > 0 if found. */
  91. int findauth(char *host)
  92. {
  93. int i = 0;
  94. if (!host || !host[0])
  95. return -1;
  96. for (i = 0; i < auth_total; i++) {
  97. if (!auth[i].host) {
  98. putlog(LOG_MISC, "*", "AUTH ENTRY: %d HAS NO HOST??", i);
  99. continue;
  100. }
  101. putlog(LOG_DEBUG, "*", STR("Debug for findauth: checking: %s i: %d :: %s"), host, i, auth[i].host);
  102. if (auth[i].host && !strcmp(auth[i].host, host)) {
  103. return i;
  104. }
  105. }
  106. return -1;
  107. }
  108. void removeauth(int n)
  109. {
  110. putlog(LOG_DEBUG, "*", "Removing %s from auth list.", auth[n].host);
  111. auth_total--;
  112. if (n < auth_total)
  113. egg_memcpy(&auth[n], &auth[auth_total], sizeof(struct auth_t));
  114. else
  115. egg_bzero(&auth[n], sizeof(struct auth_t)); /* drummer */
  116. }
  117. #endif /* S_AUTH */