]> git.saurik.com Git - apple/network_cmds.git/blobdiff - alias/alias_irc.c
network_cmds-328.tar.gz
[apple/network_cmds.git] / alias / alias_irc.c
index 910e9343404ac302c68055caabc9dea55b6ec08a..4ecddb9d912512414bbe9e99251958c99c45f3fb 100644 (file)
@@ -1,3 +1,53 @@
+/*
+ * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
+ *
+ * @APPLE_LICENSE_HEADER_START@
+ * 
+ * The contents of this file constitute Original Code as defined in and
+ * are subject to the Apple Public Source License Version 1.1 (the
+ * "License").  You may not use this file except in compliance with the
+ * License.  Please obtain a copy of the License at
+ * http://www.apple.com/publicsource and read it before using this file.
+ * 
+ * This Original Code and all software distributed under the License are
+ * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ * 
+ * @APPLE_LICENSE_HEADER_END@
+ */
+/*-
+ * Copyright (c) 2001 Charles Mott <cmott@scientech.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Based upon:
+ * $FreeBSD: src/lib/libalias/alias_irc.c,v 1.5.2.4 2001/08/21 16:42:42 ru Exp $
+ */
+
 /* Alias_irc.c intercepts packages contain IRC CTCP commands, and
        changes DCC commands to export a port on the aliasing host instead
        of an aliased host.
@@ -10,9 +60,6 @@
         The handling of this is copied more-or-less verbatim from
         ftp_alias.c
 
-    This software is placed into the public domain with no restrictions
-    on its distribution.
-
         Initial version: Eivind Eklund <perhaps@yes.no> (ee) 97-01-29
 
          Version 2.1:  May, 1997 (cjm)
@@ -78,7 +125,7 @@ lFOUND_CTCP:
                 char newpacket[65536];   /* Estimate of maximum packet size :) */
                 int  copyat = i;                         /* Same */
                 int  iCopy = 0;                          /* How much data have we written to copy-back string? */
-                unsigned long org_addr;  /* Original IP address */
+                in_addr_t org_addr;  /* Original IP address */
                 unsigned short org_port; /* Original source port address */
         lCTCP_START:
                 if( i >= dlen || iCopy >= sizeof(newpacket) )
@@ -162,7 +209,7 @@ lFOUND_CTCP:
                 /* Fetch IP address */
                 org_addr = 0;
                 while(i<dlen && isdigit(sptr[i])) {
-                        if( org_addr > ULONG_MAX/10UL )        { /* Terminate on overflow */
+                        if( org_addr > 0xFFFFFFFF/10UL )       { /* Terminate on overflow */
                                 DBprintf(("DCC Address overflow (org_addr == 0x%08lx, next char %c\n", org_addr, sptr[i]));
                                 goto lBAD_CTCP;
                         }
@@ -211,13 +258,18 @@ lFOUND_CTCP:
                         true_addr.s_addr = htonl(org_addr);
                         destaddr.s_addr = 0;
 
+                        /* Sanity/Security checking */
+                        if (!org_addr || !org_port ||
+                            pip->ip_src.s_addr != true_addr.s_addr ||
+                            org_port < IPPORT_RESERVED)
+                                goto lBAD_CTCP;
+
                         /* Steal the FTP_DATA_PORT - it doesn't really matter, and this
                                 would probably allow it through at least _some_
                                 firewalls. */
-                        dcc_link = FindUdpTcpOut (true_addr,
-                                                                                       destaddr,
-                                                                                  true_port,
-                                                                                       0, IPPROTO_TCP);
+                        dcc_link = FindUdpTcpOut(true_addr, destaddr,
+                                                 true_port, 0,
+                                                 IPPROTO_TCP, 1);
                         DBprintf(("Got a DCC link\n"));
                         if ( dcc_link ) {
                                 struct in_addr alias_address;  /* Address from aliasing */
@@ -231,7 +283,7 @@ lFOUND_CTCP:
                                 alias_address = GetAliasAddress(link);
                                 iCopy += snprintf(&newpacket[iCopy],
                                                                                 sizeof(newpacket)-iCopy, 
-                                                                                "%lu ", (u_long)htonl(alias_address.s_addr));
+                                                                                "%u ", (in_addr_t)htonl(alias_address.s_addr));
                                 if( iCopy >= sizeof(newpacket) ) { /* Truncated/fit exactly - bad news */
                                         DBprintf(("DCC constructed packet overflow.\n"));
                                         goto lBAD_CTCP;