auth.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. MD5_CTX ctx;
  78. unsigned char md5out[MD5_HASH_LENGTH + 1];
  79. char md5string[MD5_HASH_LENGTH + 1], hash[256], *ret = NULL, *secpass = NULL;
  80. if (get_user(&USERENTRY_SECPASS, u)) {
  81. secpass = nmalloc(strlen(get_user(&USERENTRY_SECPASS, u)) + 1);
  82. strcpy(secpass, (char *) get_user(&USERENTRY_SECPASS, u));
  83. secpass[strlen(secpass)] = 0;
  84. }
  85. sprintf(hash, "%s%s%s", rand, (secpass && secpass[0]) ? secpass : "" , (authkey && authkey[0]) ? authkey : "");
  86. if (secpass)
  87. nfree(secpass);
  88. MD5_Init(&ctx);
  89. MD5_Update(&ctx, hash, strlen(hash));
  90. MD5_Final(md5out, &ctx);
  91. strncpyz(md5string, btoh(md5out, MD5_DIGEST_LENGTH), sizeof md5string);
  92. ret = md5string;
  93. return ret;
  94. }
  95. int new_auth(void)
  96. {
  97. int i = auth_total;
  98. Context;
  99. if (auth_total == max_auth)
  100. return -1;
  101. auth_total++;
  102. egg_bzero((char *) &auth[i], sizeof(struct auth_t));
  103. return i;
  104. }
  105. /* returns 0 if not found, -1 if problem, and > 0 if found. */
  106. int findauth(char *host)
  107. {
  108. int i = 0;
  109. Context;
  110. if (!host || !host[0])
  111. return -1;
  112. Context;
  113. for (i = 0; i < auth_total; i++) {
  114. Context;
  115. if (!auth[i].host) {
  116. Context;
  117. putlog(LOG_MISC, "*", "AUTH ENTRY: %d HAS NO HOST??", i);
  118. continue;
  119. }
  120. Context;
  121. putlog(LOG_DEBUG, "*", STR("Debug for findauth: checking: %s i: %d :: %s"), host, i, auth[i].host);
  122. Context;
  123. if (auth[i].host && !strcmp(auth[i].host, host)) {
  124. Context;
  125. return i;
  126. }
  127. }
  128. Context;
  129. return -1;
  130. }
  131. void removeauth(int n)
  132. {
  133. Context;
  134. putlog(LOG_DEBUG, "*", "Removing %s from auth list.", auth[n].host);
  135. auth_total--;
  136. if (n < auth_total)
  137. egg_memcpy(&auth[n], &auth[auth_total], sizeof(struct auth_t));
  138. else
  139. egg_bzero(&auth[n], sizeof(struct auth_t)); /* drummer */
  140. }
  141. #endif /* S_AUTH */