1 29 30 package com.caucho.quercus.lib; 31 32 import com.caucho.quercus.QuercusModuleException; 33 import com.caucho.quercus.annotation.Optional; 34 import com.caucho.quercus.env.*; 35 import com.caucho.quercus.lib.file.SocketReadWrite; 36 import com.caucho.quercus.module.AbstractQuercusModule; 37 import com.caucho.util.IntMap; 38 import com.caucho.util.L10N; 39 40 import javax.naming.NamingEnumeration ; 41 import javax.naming.NamingException ; 42 import javax.naming.directory.Attribute ; 43 import javax.naming.directory.Attributes ; 44 import javax.naming.directory.DirContext ; 45 import javax.naming.directory.InitialDirContext ; 46 import java.io.IOException ; 47 import java.net.InetAddress ; 48 import java.net.Socket ; 49 import java.util.LinkedHashMap ; 50 import java.util.Map ; 51 import java.util.logging.Level ; 52 import java.util.logging.Logger ; 53 import java.util.regex.Pattern ; 54 55 58 public class NetworkModule extends AbstractQuercusModule { 59 private static final L10N L = new L10N(NetworkModule.class); 60 private static final Logger log 61 = Logger.getLogger(NetworkModule.class.getName()); 62 63 private static final LinkedHashMap <String , LongValue> _protoToNum 64 = new LinkedHashMap <String , LongValue>(); 65 private static final LinkedHashMap <String , ServiceNode> _servToNum 66 = new LinkedHashMap <String , ServiceNode>(); 67 68 private static final IntMap _dnsTypeMap = new IntMap(); 69 70 public static final int LOG_EMERG = 0; 71 public static final int LOG_ALERT = 1; 72 public static final int LOG_CRIT = 2; 73 public static final int LOG_ERR = 3; 74 public static final int LOG_WARNING = 4; 75 public static final int LOG_NOTICE = 5; 76 public static final int LOG_INFO = 6; 77 public static final int LOG_DEBUG = 7; 78 79 public static final int LOG_CONS = 0x01; 80 public static final int LOG_NDELAY = 0x02; 81 public static final int LOG_ODELAY = 0x04; 82 public static final int LOG_PERROR = 0x08; 83 public static final int LOG_PID = 0x10; 84 85 public static final int LOG_AUTH = 0; 86 public static final int LOG_AUTHPRIV = 1; 87 public static final int LOG_CRON = 2; 88 public static final int LOG_DAEMON = 3; 89 public static final int LOG_KERN = 4; 90 public static final int LOG_LOCAL0 = 5; 91 public static final int LOG_LOCAL1 = 6; 92 public static final int LOG_LOCAL2 = 7; 93 public static final int LOG_LOCAL3 = 8; 94 public static final int LOG_LOCAL4 = 9; 95 public static final int LOG_LOCAL5 = 10; 96 public static final int LOG_LOCAL6 = 11; 97 public static final int LOG_LOCAL7 = 12; 98 public static final int LOG_LPR = 13; 99 public static final int LOG_MAIL = 14; 100 public static final int LOG_NEWS = 15; 101 public static final int LOG_SYSLOG = 16; 102 public static final int LOG_USER = 17; 103 public static final int LOG_UUCP = 18; 104 105 public static final int DNS_A = 1; 106 public static final int DNS_CNAME = 2; 107 public static final int DNS_HINFO = 3; 108 public static final int DNS_MX = 4; 109 public static final int DNS_NS = 5; 110 public static final int DNS_PTR = 6; 111 public static final int DNS_SOA = 7; 112 public static final int DNS_TXT = 8; 113 public static final int DNS_AAAA = 9; 114 public static final int DNS_SRV = 10; 115 public static final int DNS_NAPTR = 11; 116 public static final int DNS_A6 = 12; 117 public static final int DNS_ALL = 13; 118 public static final int DNS_ANY = 14; 119 120 123 public static SocketReadWrite 124 fsockopen(Env env, 125 String host, 126 @Optional("80") int port, 127 @Optional @com.caucho.quercus.annotation.Reference Value errno, 128 @Optional @com.caucho.quercus.annotation.Reference Value errstr, 129 @Optional double timeout) 130 { 131 try { 132 Socket s = new Socket (host, port); 133 134 if (timeout > 0) 135 s.setSoTimeout((int) (timeout * 1000)); 136 else 137 s.setSoTimeout(120000); 138 139 SocketReadWrite stream; 140 stream = new SocketReadWrite(env, s, SocketReadWrite.Domain.AF_INET); 141 142 stream.init(); 143 144 return stream; 145 } catch (IOException e) { 146 log.log(Level.FINE, e.toString(), e); 147 148 if (errstr != null) 149 errstr.set(new StringValueImpl(e.toString())); 150 151 return null; 152 } 153 } 154 155 158 public static Value ip2long(String ip) 159 { 160 long v = 0; 161 162 int p = 0; 163 int len = ip.length(); 164 for (int i = 0; i < 4; i++) { 165 int digit = 0; 166 char ch = 0; 167 168 for (; p < len && '0' <= (ch = ip.charAt(p)) && ch <= '9'; p++) { 169 digit = 10 * digit + ch - '0'; 170 } 171 172 if (p < len && ch != '.') 173 return BooleanValue.FALSE; 174 else if (p == len && i < 3) 175 return BooleanValue.FALSE; 176 177 p++; 178 179 v = 256 * v + digit; 180 } 181 182 return new LongValue(v); 183 } 184 185 194 public static Value gethostbyname(String hostname) 195 { 196 198 InetAddress ip = null; 199 200 try { 201 ip = InetAddress.getByName(hostname); 202 } 203 catch (Exception e) { 204 log.log(Level.WARNING, e.toString(), e); 205 206 return StringValue.create(hostname); 207 } 208 209 return StringValue.create(ip.getHostAddress()); 210 } 211 212 221 public static Value gethostbynamel(String hostname) 222 { 223 225 InetAddress ip[] = null; 226 227 try{ 228 ip = InetAddress.getAllByName(hostname); 229 } 230 catch (Exception e) { 231 log.log(Level.WARNING, e.toString(), e); 232 233 return BooleanValue.FALSE; 234 } 235 236 ArrayValue ipArray = new ArrayValueImpl(); 237 238 for (int k = 0; k < ip.length; k++) { 239 String currentIPString = ip[k].getHostAddress(); 240 241 StringValue currentIP = new StringValueImpl((currentIPString)); 242 243 ipArray.append(currentIP); 244 } 245 246 return ipArray; 247 } 248 249 258 public static Value gethostbyaddr(Env env, String ip) 259 { 260 262 String formIPv4 = "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." + 263 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." + 264 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." + 265 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"; 266 267 CharSequence ipToCS = ip.subSequence(0, ip.length()); 268 269 if (! (Pattern.matches(formIPv4, ipToCS))) { 270 env.warning("Address is not in a.b.c.d form"); 271 272 return BooleanValue.FALSE; 273 } 274 275 String splitIP[] = null; 276 277 try { 278 splitIP = ip.split("\\."); 279 } 280 catch (Exception e) { 281 log.log(Level.WARNING, e.toString(), e); 282 283 env.warning("Regex expression invalid"); 284 285 return StringValue.create(ip); 286 } 287 288 byte addr[] = new byte[splitIP.length]; 289 290 for (int k = 0; k < splitIP.length; k++) { 291 Integer intForm = new Integer (splitIP[k]); 292 293 addr[k] = intForm.byteValue(); 294 } 295 296 InetAddress host = null; 297 298 try{ 299 host = InetAddress.getByAddress(addr); 300 } 301 catch (Exception e) { 302 log.log(Level.WARNING, e.toString(), e); 303 304 return StringValue.create(ip); 305 } 306 307 return StringValue.create(host.getHostName()); 308 } 309 310 317 public static Value getprotobyname(String protoName) 318 { 319 321 if (! (_protoToNum.containsKey(protoName))) 322 return BooleanValue.FALSE; 323 324 return LongValue.create((_protoToNum.get(protoName).toLong())); 325 } 326 327 334 public static Value getprotobynumber(int protoNumber) 335 { 336 338 for (Map.Entry<String , LongValue> entry: _protoToNum.entrySet()) 339 if (entry.getValue().toLong() == protoNumber) 340 return StringValue.create(entry.getKey()); 341 342 return BooleanValue.FALSE; 343 } 344 345 354 public static Value getservbyname(String service, String protocol) 355 { 356 358 if (! (_servToNum.containsKey(service))) 359 return BooleanValue.FALSE; 360 361 ServiceNode node = _servToNum.get(service); 362 363 if (! (node.protocolCheck(protocol))) 364 return BooleanValue.FALSE; 365 366 return node.getPort(); 367 } 368 369 379 public static Value getservbyport(int port, String protocol) 380 { 381 383 for (Map.Entry<String , ServiceNode> entry: _servToNum.entrySet()) { 384 ServiceNode node = entry.getValue(); 385 386 if (node.getPort().toLong() == port && 387 node.protocolCheck(protocol)) 388 return StringValue.create(entry.getKey()); 389 } 390 391 return BooleanValue.FALSE; 392 } 393 394 public static boolean getmxrr(Env env, 395 String hostname, 396 @com.caucho.quercus.annotation.Reference Value mxhosts, 397 @Optional @com.caucho.quercus.annotation.Reference Value weight) 398 { 399 return dns_get_mx(env, hostname, mxhosts, weight); 400 } 401 402 413 public static boolean dns_get_mx(Env env, 414 String hostname, 415 @com.caucho.quercus.annotation.Reference Value mxhosts, 416 @Optional @com.caucho.quercus.annotation.Reference Value weight) 417 { 418 try { 419 421 DirContext ictx = new InitialDirContext (); 422 Attributes atrs = ictx.getAttributes("dns:/" + hostname, new String [] {"MX"}); 423 424 ArrayValue hosts = new ArrayValueImpl(); 425 426 ArrayValue weights = new ArrayValueImpl(); 427 428 try { 429 NamingEnumeration list = atrs.getAll(); 430 431 if (! (list.hasMore())) 432 return false; 433 434 String [] tokens = list.next().toString().split("\\s"); 435 436 for (int k = 1; k < tokens.length; k++) { 437 int weightToInt = Integer.valueOf(tokens[k]).intValue(); 438 439 weights.append(LongValue.create(weightToInt)); 440 441 k++; 442 443 String uncleanHost = tokens[k]; 444 445 int numOfCharacters = 0; 446 447 if (k < tokens.length - 1) 448 numOfCharacters = uncleanHost.length() - 2; 449 else 450 numOfCharacters = uncleanHost.length() -1; 451 452 String cleanHost = uncleanHost.substring(0, numOfCharacters); 453 454 hosts.append(StringValue.create(cleanHost)); 455 } 456 } 457 catch (Exception e) { 458 log.log(Level.WARNING, e.toString(), e); 459 env.warning("An error occurred while processing the records"); 460 461 return false; 462 } 463 464 mxhosts.set(hosts); 465 weight.set(weights); 466 return true; 467 } catch (NamingException e) { 468 throw new QuercusModuleException(e); 469 } 470 } 471 472 public static boolean checkdnsrr(Env env, 473 String hostname, 474 @Optional("MX") String type) 475 { 476 return dns_check_record(env, hostname, _dnsTypeMap.get(type), null, null); 477 } 478 479 490 public static boolean dns_check_record(Env env, 491 String hostname, 492 @Optional("DNS_ALL") int type, 493 @Optional ArrayValue mxhosts, 494 @Optional ArrayValue weight) 495 { 496 498 try { 499 DirContext ictx = new InitialDirContext (); 500 Attributes atrs = ictx.getAttributes("dns:/" + hostname); 501 502 ArrayValue hosts = new ArrayValueImpl(); 503 504 ArrayValue weights = new ArrayValueImpl(); 505 506 try { 507 NamingEnumeration <? extends Attribute > e = atrs.getAll(); 508 509 while (e.hasMoreElements()) { 510 Attribute attr = e.nextElement(); 511 512 String id = attr.getID(); 513 514 String [] tokens = attr.toString().split("\\s"); 515 516 for (int k = 1; k < tokens.length; k++) { 517 int weightToInt = Integer.valueOf(tokens[k]).intValue(); 518 519 weights.append(LongValue.create(weightToInt)); 520 521 k++; 522 523 String uncleanHost = tokens[k]; 524 525 int numOfCharacters = 0; 526 527 if (k < tokens.length - 1) 528 numOfCharacters = uncleanHost.length() - 2; 529 else 530 numOfCharacters = uncleanHost.length() -1; 531 532 String cleanHost = uncleanHost.substring(0, numOfCharacters); 533 534 hosts.append(StringValue.create(cleanHost)); 535 } 536 } 537 } catch (Exception e) { 538 log.log(Level.WARNING, e.toString(), e); 539 env.warning(L.l("An error occurred while processing the records\n{0}", 540 e)); 541 542 return false; 543 } 544 545 mxhosts.set(hosts); 546 weight.set(weights); 547 548 return true; 549 } catch (NamingException e) { 550 throw new QuercusModuleException(e); 551 } 552 } 553 554 557 public static Value define_syslog_variables() 558 { 559 return NullValue.NULL; 560 } 561 562 567 public static boolean openlog(String ident, int option, int facility) 568 { 569 return true; 570 } 571 572 575 public static boolean closelog() 576 { 577 return true; 578 } 579 580 583 public static boolean syslog(Env env, int priority, String message) 584 { 585 Level level = Level.OFF; 586 587 switch (priority) { 588 case LOG_EMERG: 589 case LOG_ALERT: 590 case LOG_CRIT: 591 level = Level.SEVERE; 592 break; 593 case LOG_ERR: 594 case LOG_WARNING: 595 level = Level.WARNING; 596 break; 597 case LOG_NOTICE: 598 level = Level.CONFIG; 599 break; 600 case LOG_INFO: 601 level = Level.INFO; 602 break; 603 case LOG_DEBUG: 604 level = Level.FINE; 605 break; 606 } 607 608 env.getLogger().log(level, message); 609 610 return true; 611 } 612 613 private static class ServiceNode { 614 private LongValue _port; 615 616 private boolean _isTCP; 617 private boolean _isUDP; 618 619 ServiceNode(int port, boolean tcp, boolean udp) 620 { 621 _port = LongValue.create(port); 622 _isTCP = tcp; 623 _isUDP = udp; 624 } 625 626 public LongValue getPort() 627 { 628 return _port; 629 } 630 631 public boolean protocolCheck(String protocol) 632 { 633 if (protocol.equals("tcp")) 634 return _isTCP; 635 else if (protocol.equals("udp")) 636 return _isUDP; 637 else 638 return false; 639 } 640 641 public boolean isTCP() 642 { 643 return _isTCP; 644 } 645 646 public boolean isUDP() 647 { 648 return _isUDP; 649 } 650 } 651 652 static { 653 _protoToNum.put("ip", LongValue.create(0)); 654 _protoToNum.put("icmp", LongValue.create(1)); 655 _protoToNum.put("ggp", LongValue.create(3)); 656 _protoToNum.put("tcp", LongValue.create(6)); 657 _protoToNum.put("egp", LongValue.create(8)); 658 _protoToNum.put("pup", LongValue.create(12)); 659 _protoToNum.put("udp", LongValue.create(17)); 660 _protoToNum.put("hmp", LongValue.create(12)); 661 _protoToNum.put("xns-idp", LongValue.create(22)); 662 _protoToNum.put("rdp", LongValue.create(27)); 663 _protoToNum.put("rvd", LongValue.create(66)); 664 _servToNum.put("echo", new ServiceNode(7, true, true)); 665 _servToNum.put("discard", new ServiceNode(9, true, true)); 666 _servToNum.put("systat", new ServiceNode(11, true, true)); 667 _servToNum.put("daytime", new ServiceNode(13, true, true)); 668 _servToNum.put("qotd", new ServiceNode(17, true, true)); 669 _servToNum.put("chargen", new ServiceNode(19, true, true)); 670 _servToNum.put("ftp-data", new ServiceNode(20, true, false)); 671 _servToNum.put("ftp", new ServiceNode(21, true, false)); 672 _servToNum.put("telnet", new ServiceNode(23, true, false)); 673 _servToNum.put("smtp", new ServiceNode(25, true, false)); 674 _servToNum.put("time", new ServiceNode(37, true, true)); 675 _servToNum.put("rlp", new ServiceNode(39, false, true)); 676 _servToNum.put("nameserver", new ServiceNode(42, true, true)); 677 _servToNum.put("nicname", new ServiceNode(43, true, false)); 678 _servToNum.put("domain", new ServiceNode(53, true, true)); 679 _servToNum.put("bootps", new ServiceNode(67, false, true)); 680 _servToNum.put("bootpc", new ServiceNode(68, false, true)); 681 _servToNum.put("tftp", new ServiceNode(69, false, true)); 682 _servToNum.put("gopher", new ServiceNode(70, true, false)); 683 _servToNum.put("finger", new ServiceNode(79, true, false)); 684 _servToNum.put("http", new ServiceNode(80, true, false)); 685 _servToNum.put("kerberos", new ServiceNode(88, true, true)); 686 _servToNum.put("hostname", new ServiceNode(101, true, false)); 687 _servToNum.put("iso-tsap", new ServiceNode(102, true, false)); 688 _servToNum.put("rtelnet", new ServiceNode(107, true, false)); 689 _servToNum.put("pop2", new ServiceNode(109, true, false)); 690 _servToNum.put("pop3", new ServiceNode(110, true, false)); 691 _servToNum.put("sunrpc", new ServiceNode(111, true, true)); 692 _servToNum.put("auth", new ServiceNode(113, true, false)); 693 _servToNum.put("uucp-path", new ServiceNode(117, true, false)); 694 _servToNum.put("nntp", new ServiceNode(119, true, false)); 695 _servToNum.put("ntp", new ServiceNode(123, false, true)); 696 _servToNum.put("epmap", new ServiceNode(135, true, true)); 697 _servToNum.put("netbios-ns", new ServiceNode(137, true, true)); 698 _servToNum.put("netbios-dgm", new ServiceNode(138, false, true)); 699 _servToNum.put("netbios-ssn", new ServiceNode(139, true, false)); 700 _servToNum.put("imap", new ServiceNode(143, true, false)); 701 _servToNum.put("pcmail-srv", new ServiceNode(158, true, false)); 702 _servToNum.put("snmp", new ServiceNode(161, false, true)); 703 _servToNum.put("snmptrap", new ServiceNode(162, false, true)); 704 _servToNum.put("print-srv", new ServiceNode(170, true, false)); 705 _servToNum.put("bgp", new ServiceNode(179, true, false)); 706 _servToNum.put("irc", new ServiceNode(194, true, false)); 707 _servToNum.put("ipx", new ServiceNode(213, false, true)); 708 _servToNum.put("ldap", new ServiceNode(389, true, false)); 709 _servToNum.put("https", new ServiceNode(443, true, true)); 710 _servToNum.put("microsoft-ds", new ServiceNode(445, true, true)); 711 _servToNum.put("kpasswd", new ServiceNode(464, true, true)); 712 _servToNum.put("isakmp", new ServiceNode(500, false, true)); 713 _servToNum.put("exec", new ServiceNode(512, true, false)); 714 _servToNum.put("biff", new ServiceNode(512, false, true)); 715 _servToNum.put("login", new ServiceNode(513, true, false)); 716 _servToNum.put("who", new ServiceNode(513, false, true)); 717 _servToNum.put("cmd", new ServiceNode(514, true, false)); 718 _servToNum.put("syslog", new ServiceNode(514, false, true)); 719 _servToNum.put("printer", new ServiceNode(515, true, false)); 720 _servToNum.put("talk", new ServiceNode(517, false, true)); 721 _servToNum.put("ntalk", new ServiceNode(518, false, true)); 722 _servToNum.put("efs", new ServiceNode(520, true, false)); 723 _servToNum.put("router", new ServiceNode(520, false, true)); 724 _servToNum.put("timed", new ServiceNode(525, false, true)); 725 _servToNum.put("tempo", new ServiceNode(526, true, false)); 726 _servToNum.put("courier", new ServiceNode(530, true, false)); 727 _servToNum.put("conference", new ServiceNode(531, true, false)); 728 _servToNum.put("netnews", new ServiceNode(532, true, false)); 729 _servToNum.put("netwall", new ServiceNode(533, false, true)); 730 _servToNum.put("uucp", new ServiceNode(540, true, false)); 731 _servToNum.put("klogin", new ServiceNode(543, true, false)); 732 _servToNum.put("kshell", new ServiceNode(544, true, false)); 733 _servToNum.put("new-rwho", new ServiceNode(550, false, true)); 734 _servToNum.put("remotefs", new ServiceNode(556, true, false)); 735 _servToNum.put("rmonitor", new ServiceNode(560, false, true)); 736 _servToNum.put("monitor", new ServiceNode(561, false, true)); 737 _servToNum.put("ldaps", new ServiceNode(636, true, false)); 738 _servToNum.put("doom", new ServiceNode(666, true, true)); 739 _servToNum.put("kerberos-adm", new ServiceNode(749, true, true)); 740 _servToNum.put("kerberos-iv", new ServiceNode(750, false, true)); 741 _servToNum.put("kpop", new ServiceNode(1109, true, false)); 742 _servToNum.put("phone", new ServiceNode(1167, false, true)); 743 _servToNum.put("ms-sql-s", new ServiceNode(1433, true, true)); 744 _servToNum.put("ms-sql-m", new ServiceNode(1434, true, true)); 745 _servToNum.put("wins", new ServiceNode(1512, true, true)); 746 _servToNum.put("ingreslock", new ServiceNode(1524, true, false)); 747 _servToNum.put("12tp", new ServiceNode(1701, false, true)); 748 _servToNum.put("pptp", new ServiceNode(1723, true, false)); 749 _servToNum.put("radius", new ServiceNode(1812, false, true)); 750 _servToNum.put("radacct", new ServiceNode(1813, false, true)); 751 _servToNum.put("nfsd", new ServiceNode(2049, false, true)); 752 _servToNum.put("knetd", new ServiceNode(2053, true, false)); 753 _servToNum.put("man", new ServiceNode(9535, true, false)); 754 755 _dnsTypeMap.put("A", DNS_A); 756 _dnsTypeMap.put("MX", DNS_MX); 757 _dnsTypeMap.put("NS", DNS_NS); 758 _dnsTypeMap.put("SOA", DNS_SOA); 759 _dnsTypeMap.put("PTR", DNS_PTR); 760 _dnsTypeMap.put("CNAME", DNS_CNAME); 761 _dnsTypeMap.put("AAAA", DNS_AAAA); 762 _dnsTypeMap.put("A6", DNS_A6); 763 _dnsTypeMap.put("SRV", DNS_SRV); 764 _dnsTypeMap.put("NAPTR", DNS_NAPTR); 765 _dnsTypeMap.put("ANY", DNS_ANY); 766 } 767 } 768 769 | Popular Tags |