gdipinet.pl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/usr/bin/perl
  2. #####################################################
  3. # gdipinet.pl
  4. #
  5. # This is the GnuDIP (X)INETD server daemon.
  6. #
  7. # See COPYING for licensing information.
  8. #
  9. # Derived from GnuDIP 2.1.2 written by:
  10. #
  11. # Mike Machado
  12. #
  13. #####################################################
  14. # PERL modules
  15. use strict;
  16. use Getopt::Std;
  17. use POSIX qw(strftime);
  18. use Socket;
  19. # global variables
  20. use vars qw($conf $gnudipdir $logger $ip);
  21. # locate ourselves
  22. use FindBin;
  23. BEGIN {
  24. $gnudipdir = '';
  25. if ($FindBin::Bin =~ /(.*)\/.+?/) {
  26. $gnudipdir = $1;
  27. }
  28. }
  29. use lib "$gnudipdir/lib";
  30. # GnuDIP common subroutines
  31. use gdipdaemon;
  32. use gdiplib;
  33. use dbusers;
  34. # process command line
  35. sub usage {
  36. print STDERR <<"EOQ";
  37. usage: gdipinet.pl [ -h | -e STDERR_file ]
  38. usage: GnuDIP (X)INETD Daemon.
  39. usage: -h: Print this usage message.
  40. usage: -e: Specify filename prefix for STDERR output. The file name
  41. usage: will be this prefix followed by the process ID.
  42. EOQ
  43. }
  44. use vars qw/ $opt_h $opt_e /;
  45. if (!getopts('he:')) {
  46. usage();
  47. exit 1;
  48. }
  49. if ($opt_h) {
  50. usage();
  51. exit;
  52. }
  53. if (@ARGV ne 0) {
  54. usage();
  55. exit 1;
  56. }
  57. # redirect error messages?
  58. if ($opt_e) {
  59. # trust the prefix
  60. if ($opt_e =~ /^(.*)$/) {
  61. $opt_e = $1;
  62. }
  63. open (STDERR, ">$opt_e$$");
  64. }
  65. # get preferences from config file
  66. $conf = getconf();
  67. if (!$conf) {
  68. print STDERR "gdipinet.pl has exited - getconf returned nothing\n";
  69. exit;
  70. }
  71. # logger command
  72. $logger = $$conf{'logger_inet'};
  73. if (!$logger) {
  74. print STDERR "Configuration parameter \"logger_inet\" not defined";
  75. exit;
  76. }
  77. # suppress error messages?
  78. if (!$opt_e) {
  79. open (STDERR, ">/dev/null");
  80. }
  81. # seconds to wait for response to prompt
  82. my $timeout = $$conf{'timeout'};
  83. if (!$timeout) {
  84. writelog(
  85. "Configuration parameter \"timeout\" not defined"
  86. );
  87. exit;
  88. }
  89. # get IP address of remote end
  90. my $client_addr = getpeername(STDIN);
  91. if (! $client_addr) {
  92. my $msg = 'Could not get IP address of client';
  93. writelog($msg);
  94. print STDERR "$msg\n";
  95. print "$msg\n";
  96. exit;
  97. }
  98. my ($port, $packed_ip) = sockaddr_in($client_addr);
  99. $ip = inet_ntoa($packed_ip);
  100. # flush after each print
  101. select(STDOUT);
  102. $| = 1;
  103. # send the salt
  104. my $salt = randomsalt();
  105. print STDOUT "$salt\n";
  106. # only wait $timeout seconds for data before disconnecting
  107. my $sin = '';
  108. vec($sin, fileno(STDIN), 1) = 1;
  109. my $found = select($sin, undef, undef, $timeout);
  110. # timed out?
  111. if (!$found) {
  112. writelog("Timed out receiving session data from $ip");
  113. print STDOUT "1\n";
  114. exit;
  115. }
  116. # get the response
  117. my $data = '';
  118. chomp($data = <STDIN>);
  119. my ($clientuser, $clientpass, $clientdomain, $clientaction, $clientip) = split(/:/, $data);
  120. # got a response?
  121. if ($data eq '') {
  122. writelog("Empty response from $ip");
  123. print STDOUT "1\n";
  124. exit;
  125. }
  126. # sensible request?
  127. if($clientaction ne '0' && $clientaction ne '1' && $clientaction ne '2') {
  128. writelog("Invalid request from $ip");
  129. print STDOUT "1\n";
  130. exit;
  131. }
  132. # "dummy" request?
  133. if ($clientaction eq '2' and
  134. $$conf{'dummyuser'} and
  135. $$conf{'dummydomn'} and
  136. $$conf{'dummypswd'}) {
  137. # massage host template into valid Perl regular expression
  138. my $check = $$conf{'dummyuser'};
  139. $check =~ s/\*/\(\.\*\)/g;
  140. $check =~ s/\?/\(\.\)/g;
  141. # check for a match
  142. if ($clientuser =~ /^$check\b/ and
  143. $clientdomain eq $$conf{'dummydomn'} and
  144. $clientpass eq md5_hex(md5_hex($$conf{'dummypswd'}) . ".$salt")) {
  145. writelog(
  146. "Dummy request processed for user $clientuser from ip $ip");
  147. print STDOUT "0:$ip\n";
  148. exit;
  149. }
  150. }
  151. # retrieve user information
  152. my $userinfo = getuser($clientuser, $clientdomain);
  153. # bad login?
  154. if (!$userinfo or
  155. $clientuser ne $$userinfo{'username'} or
  156. $clientdomain ne $$userinfo{'domain'} or
  157. $clientpass ne md5_hex("$$userinfo{'password'}.$salt")
  158. ) {
  159. writelog("Invalid login attempt from $ip: user $clientuser.$clientdomain");
  160. print STDOUT "1\n";
  161. exit;
  162. }
  163. # use IP address client connected from?
  164. $clientip = $ip if $clientaction eq '2';
  165. # client passed an IP address?
  166. if ($clientaction eq '0' and (!defined($clientip) or $clientip eq '')) {
  167. writelog("No IP address passed from $ip: user $clientuser.$clientdomain");
  168. $clientip = $ip;
  169. if (defined $$conf{'require_address'} and
  170. $$conf{'require_address'} = 'yes') {
  171. print STDOUT "1\n";
  172. exit;
  173. }
  174. }
  175. # invalid IP address?
  176. if($clientaction ne '1' && !validip($clientip)) {
  177. writelog(
  178. "Unserviceable IP address $clientip for user $clientuser.$clientdomain");
  179. print STDOUT "1\n";
  180. exit;
  181. }
  182. # TTL value
  183. my $TTL = 0;
  184. $TTL = $$conf{'TTL'} if $$conf{'TTL'};
  185. $TTL = $$conf{"TTL.$clientdomain"}
  186. if $$conf{"TTL.$clientdomain"};
  187. $TTL = $$conf{"TTL.$clientuser.$clientdomain"}
  188. if $$conf{"TTL.$clientuser.$clientdomain"};
  189. # a modify request?
  190. if ($clientaction eq '0' or $clientaction eq '2') {
  191. # IP address unchanged?
  192. if ($$userinfo{'currentip'} eq $clientip) {
  193. writelog(
  194. "User $clientuser.$clientdomain remains at ip $clientip");
  195. updateuser($userinfo);
  196. # do the update
  197. } else {
  198. donsupdate($clientdomain,
  199. "update delete $clientuser.$clientdomain. A",
  200. "update add $clientuser.$clientdomain. $TTL A $clientip");
  201. writelog(
  202. "User $clientuser.$clientdomain successful update to ip $clientip");
  203. $$userinfo{'currentip'} = $clientip;
  204. updateuser($userinfo);
  205. }
  206. if ($clientaction eq '2') {
  207. print STDOUT "0:$clientip\n";
  208. } else {
  209. print STDOUT "0\n";
  210. }
  211. # an offline request
  212. } else {
  213. # IP address unchanged?
  214. if ($$userinfo{'currentip'} eq '0.0.0.0') {
  215. writelog(
  216. "User $clientuser.$clientdomain remains removed");
  217. updateuser($userinfo);
  218. # do the update
  219. } else {
  220. donsupdate($clientdomain,
  221. "update delete $clientuser.$clientdomain. A");
  222. writelog(
  223. "User $clientuser.$clientdomain successful remove from ip $$userinfo{'currentip'}");
  224. $$userinfo{'currentip'} = '0.0.0.0';
  225. updateuser($userinfo);
  226. }
  227. print STDOUT "2\n";
  228. }
  229. exit;