auth.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * auth.c -- handles:
  3. * auth system functions
  4. * auth system hooks
  5. */
  6. #include "main.h"
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include "chan.h"
  13. #include "tandem.h"
  14. #include "modules.h"
  15. #include <pwd.h>
  16. #include <errno.h>
  17. #include <net/if.h>
  18. #include <sys/ioctl.h>
  19. #include <sys/socket.h>
  20. #include <signal.h>
  21. #include "stat.h"
  22. #include "bg.h"
  23. extern struct userrec *userlist;
  24. extern struct dcc_t *dcc;
  25. extern struct chanset_t *chanset;
  26. extern time_t now;
  27. #ifdef S_AUTH
  28. extern char authkey[];
  29. int auth_total = 0;
  30. int max_auth = 100;
  31. struct auth_t *auth = 0;
  32. #endif /* S_AUTH */
  33. /* Expected memory usage
  34. */
  35. int expmem_auth()
  36. {
  37. int tot = 0;
  38. #ifdef S_AUTH
  39. tot += sizeof(struct auth_t) * max_auth;
  40. #endif /* S_AUTH */
  41. return tot;
  42. }
  43. #ifdef S_AUTH
  44. void init_auth_max()
  45. {
  46. if (max_auth < 1)
  47. max_auth = 1;
  48. if (auth)
  49. auth = nrealloc(auth, sizeof(struct auth_t) * max_auth);
  50. else
  51. auth = nmalloc(sizeof(struct auth_t) * max_auth);
  52. }
  53. static void expire_auths()
  54. {
  55. int i = 0, idle = 0;
  56. if (!ischanhub()) return;
  57. for (i = 0; i < auth_total;i++) {
  58. if (auth[i].authed) {
  59. idle = now - auth[i].atime;
  60. if (idle >= 60*60) {
  61. removeauth(i);
  62. }
  63. }
  64. }
  65. }
  66. #endif /* S_AUTH */
  67. void init_auth()
  68. {
  69. #ifdef S_AUTH
  70. init_auth_max();
  71. add_hook(HOOK_MINUTELY, (Function) expire_auths);
  72. #endif /* S_AUTH */
  73. }
  74. #ifdef S_AUTH
  75. char *makehash(struct userrec *u, char *rand)
  76. {
  77. int i = 0;
  78. MD5_CTX ctx;
  79. unsigned char md5out[33];
  80. char md5string[33], hash[500], *ret = NULL;
  81. Context;
  82. sprintf(hash, "%s%s%s", rand, (char *) get_user(&USERENTRY_SECPASS, u), authkey ? authkey : "");
  83. // putlog(LOG_DEBUG, "*", STR("Making hash from %s %s: %s"), rand, get_user(&USERENTRY_SECPASS, u), hash);
  84. MD5_Init(&ctx);
  85. MD5_Update(&ctx, hash, strlen(hash));
  86. MD5_Final(md5out, &ctx);
  87. for(i=0; i<16; i++)
  88. sprintf(md5string + (i*2), "%.2x", md5out[i]);
  89. // putlog(LOG_DEBUG, "*", STR("MD5 of hash: %s"), md5string);
  90. ret = md5string;
  91. return ret;
  92. }
  93. int new_auth(void)
  94. {
  95. int i = auth_total;
  96. Context;
  97. if (auth_total == max_auth)
  98. return -1;
  99. auth_total++;
  100. egg_bzero((char *) &auth[i], sizeof(struct auth_t));
  101. return i;
  102. }
  103. /* returns 0 if not found, -1 if problem, and > 0 if found. */
  104. int findauth(char *host)
  105. {
  106. int i = 0;
  107. Context;
  108. if (!host || !host[0])
  109. return -1;
  110. Context;
  111. for (i = 0; i < auth_total; i++) {
  112. Context;
  113. if (!auth[i].host) {
  114. Context;
  115. putlog(LOG_MISC, "*", "AUTH ENTRY: %d HAS NO HOST??", i);
  116. continue;
  117. }
  118. Context;
  119. putlog(LOG_DEBUG, "*", STR("Debug for findauth: checking: %s i: %d :: %s"), host, i, auth[i].host);
  120. Context;
  121. if (auth[i].host && !strcmp(auth[i].host, host)) {
  122. Context;
  123. return i;
  124. }
  125. }
  126. Context;
  127. return -1;
  128. }
  129. void removeauth(int n)
  130. {
  131. Context;
  132. putlog(LOG_DEBUG, "*", "Removing %s from auth list.", auth[n].host);
  133. auth_total--;
  134. if (n < auth_total)
  135. egg_memcpy(&auth[n], &auth[auth_total], sizeof(struct auth_t));
  136. else
  137. egg_bzero(&auth[n], sizeof(struct auth_t)); /* drummer */
  138. }
  139. #endif /* S_AUTH */