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