auth.c 2.9 KB

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