auth.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. static void
  33. expire_auths()
  34. {
  35. if (!ischanhub())
  36. return;
  37. time_t idle = 0;
  38. for (int i = 0; i < auth_total; i++) {
  39. if (auth[i].authed) {
  40. idle = now - auth[i].atime;
  41. if (idle >= (60 * 60)) {
  42. removeauth(i);
  43. }
  44. }
  45. }
  46. }
  47. void
  48. init_auth()
  49. {
  50. if (max_auth < 1)
  51. max_auth = 1;
  52. if (auth)
  53. auth = (struct auth_t *) my_realloc(auth, sizeof(struct auth_t) * max_auth);
  54. else
  55. auth = (struct auth_t *) my_calloc(1, sizeof(struct auth_t) * max_auth);
  56. timer_create_secs(60, "expire_auths", (Function) expire_auths);
  57. }
  58. void makehash(int idx, int authi, char *randstring)
  59. {
  60. char hash[256] = "", *secpass = NULL;
  61. struct userrec *u = NULL;
  62. if (idx != -1)
  63. u = dcc[idx].user;
  64. else if (authi != -1)
  65. u = auth[authi].user;
  66. if (get_user(&USERENTRY_SECPASS, u)) {
  67. secpass = strdup((char *) get_user(&USERENTRY_SECPASS, u));
  68. secpass[strlen(secpass)] = 0;
  69. }
  70. egg_snprintf(hash, sizeof hash, "%s%s%s", randstring, (secpass && secpass[0]) ? secpass : "", authkey);
  71. if (secpass)
  72. free(secpass);
  73. if (idx != -1)
  74. strlcpy(dcc[idx].hash, MD5(hash), sizeof dcc[idx].hash);
  75. else if (authi != -1)
  76. strlcpy(auth[authi].hash, MD5(hash), sizeof auth[authi].hash);
  77. egg_bzero(hash, sizeof(hash));
  78. }
  79. char *
  80. makebdhash(char *randstring)
  81. {
  82. char hash[256] = "";
  83. char *bdpass = "bdpass";
  84. egg_snprintf(hash, sizeof hash, "%s%s%s", randstring, bdpass, settings.packname);
  85. sdprintf("bdhash: %s", hash);
  86. return MD5(hash);
  87. }
  88. int
  89. new_auth(void)
  90. {
  91. if (auth_total == max_auth)
  92. return -1;
  93. egg_bzero((struct auth_t *) &auth[auth_total], sizeof(struct auth_t));
  94. return auth_total++;
  95. }
  96. /* returns 0 if not found, -1 if problem, and > 0 if found. */
  97. int
  98. findauth(char *host)
  99. {
  100. if (!host || !host[0])
  101. return -1;
  102. for (int i = 0; i < auth_total; i++) {
  103. if (!auth[i].host) {
  104. putlog(LOG_MISC, "*", "AUTH ENTRY: %d HAS NO HOST??", i);
  105. continue;
  106. }
  107. if (auth[i].host && !strcmp(auth[i].host, host)) {
  108. return i;
  109. }
  110. }
  111. return -1;
  112. }
  113. void
  114. removeauth(int n)
  115. {
  116. putlog(LOG_DEBUG, "*", "Removing %s from auth list.", auth[n].host);
  117. auth_total--;
  118. if (n < auth_total)
  119. egg_memcpy(&auth[n], &auth[auth_total], sizeof(struct auth_t));
  120. else
  121. egg_bzero(&auth[n], sizeof(struct auth_t)); /* drummer */
  122. }