commands/rlogind/setup.c

http://www.minix3.org/ · C · 92 lines · 77 code · 11 blank · 4 comment · 11 complexity · 6fcd5a04d8b7434a7e6d97aa0ca977d4 MD5 · raw file

  1. /*
  2. setup.c
  3. */
  4. #include <sys/types.h>
  5. #include <sys/ioctl.h>
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <net/netlib.h>
  12. #include <net/gen/in.h>
  13. #include <net/gen/inet.h>
  14. #include <net/gen/tcp.h>
  15. #include <net/gen/tcp_io.h>
  16. #include <net/gen/netdb.h>
  17. #include <net/gen/socket.h>
  18. #include "rlogind.h"
  19. static void getstr(char *buf, int cnt, char *errmsg);
  20. void authenticate(void)
  21. {
  22. int result;
  23. struct nwio_tcpconf tcpconf;
  24. struct hostent *hostent;
  25. char c;
  26. /* Let's lookup the hostname for the connection. */
  27. result= ioctl (0, NWIOGTCPCONF, &tcpconf);
  28. if (result<0)
  29. {
  30. fprintf(stderr, "%s: ioctl(NWIOTCPCONF): %s\r\n",
  31. prog_name, strerror(errno));
  32. exit(1);
  33. }
  34. hostent= gethostbyaddr((char *)&tcpconf.nwtc_remaddr,
  35. sizeof(tcpconf.nwtc_remaddr), AF_INET);
  36. if (hostent)
  37. {
  38. strncpy(hostname, hostent->h_name, sizeof(hostname)-1);
  39. hostname[sizeof(hostname)-1]= '\0';
  40. }
  41. else
  42. {
  43. strcpy(hostname, inet_ntoa(tcpconf.nwtc_remaddr));
  44. }
  45. authenticated = 0;
  46. getstr(&c, 1, "protocol violation");
  47. getstr(rusername, sizeof(rusername), "remuser too long");
  48. getstr(lusername, sizeof(lusername), "locuser too long");
  49. strcpy(term, "TERM=");
  50. getstr(term+5, sizeof(term)-5, "Terminal type too long");
  51. #if DEBUG
  52. fprintf(stderr, "got lu= %s, ru= %s, te= %s\r\n", lusername, rusername,
  53. term);
  54. #endif
  55. if (iruserok(tcpconf.nwtc_remaddr, 0, rusername, lusername) == 0)
  56. authenticated = 1;
  57. }
  58. static void getstr(char *buf, int cnt, char *errmsg)
  59. {
  60. char c;
  61. errno= 0;
  62. do
  63. {
  64. if (read(0, &c, 1) != 1)
  65. fatal(1, "read failed", errno);
  66. cnt--;
  67. if (cnt < 0)
  68. fatal(1, errmsg, 0);
  69. *buf++= c;
  70. } while(c != 0);
  71. }
  72. void tcp_urg(int fd, int on)
  73. {
  74. struct nwio_tcpopt tcpopt;
  75. tcpopt.nwto_flags= on ? (NWTO_BSD_URG | NWTO_SND_URG) : NWTO_SND_NOTURG;
  76. if (ioctl(1, NWIOSTCPOPT, &tcpopt) == -1)
  77. {
  78. fprintf(stderr, "rlogind: NWIOSTCPOPT failed: %s\r\n",
  79. strerror(errno));
  80. }
  81. }