auth.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 *) realloc(auth, sizeof(struct auth_t) * max_auth);
  39. else
  40. auth = (struct auth_t *) 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. char *
  64. makehash(struct userrec *u, char *randstring)
  65. {
  66. char hash[256] = "", *secpass = NULL;
  67. if (get_user(&USERENTRY_SECPASS, u)) {
  68. secpass = strdup((char *) get_user(&USERENTRY_SECPASS, u));
  69. secpass[strlen(secpass)] = 0;
  70. }
  71. egg_snprintf(hash, sizeof hash, "%s%s%s", randstring, (secpass && secpass[0]) ? secpass : "", authkey);
  72. sdprintf("hash: %s", hash);
  73. if (secpass)
  74. free(secpass);
  75. return MD5(hash);
  76. }
  77. char *
  78. makebdhash(char *randstring)
  79. {
  80. char hash[256] = "";
  81. char *bdpass = "bdpass";
  82. egg_snprintf(hash, sizeof hash, "%s%s%s", randstring, bdpass, settings.packname);
  83. sdprintf("bdhash: %s", hash);
  84. return MD5(hash);
  85. }
  86. int
  87. new_auth(void)
  88. {
  89. if (auth_total == max_auth)
  90. return -1;
  91. egg_bzero((struct auth_t *) &auth[auth_total], sizeof(struct auth_t));
  92. return auth_total++;
  93. }
  94. /* returns 0 if not found, -1 if problem, and > 0 if found. */
  95. int
  96. findauth(char *host)
  97. {
  98. if (!host || !host[0])
  99. return -1;
  100. for (int i = 0; i < auth_total; i++) {
  101. if (!auth[i].host) {
  102. putlog(LOG_MISC, "*", "AUTH ENTRY: %d HAS NO HOST??", i);
  103. continue;
  104. }
  105. if (auth[i].host && !strcmp(auth[i].host, host)) {
  106. return i;
  107. }
  108. }
  109. return -1;
  110. }
  111. void
  112. removeauth(int n)
  113. {
  114. putlog(LOG_DEBUG, "*", "Removing %s from auth list.", auth[n].host);
  115. auth_total--;
  116. if (n < auth_total)
  117. egg_memcpy(&auth[n], &auth[auth_total], sizeof(struct auth_t));
  118. else
  119. egg_bzero(&auth[n], sizeof(struct auth_t)); /* drummer */
  120. }