annotate usr/src/cmd/ssh/libssh/common/addrmatch.c @ 11060:40daac51fc0c

6655613 resync server's conditional Match block from OpenSSH (fix lint)
author Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
date Thu, 12 Nov 2009 15:28:55 -0800
parents eacbb90c6866
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11044
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
1 /* $OpenBSD: addrmatch.c,v 1.4 2008/12/10 03:55:20 stevesk Exp $ */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
2
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
3 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
4 * Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org>
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
5 *
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
6 * Permission to use, copy, modify, and distribute this software for any
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
9 *
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
17 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
18
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
19 #include "includes.h"
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
20
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
21 #include <sys/types.h>
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
22 #include <sys/socket.h>
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
23 #include <netinet/in.h>
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
24 #include <arpa/inet.h>
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
25
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
26 #include <netdb.h>
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
27 #include <string.h>
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
28 #include <stdlib.h>
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
29 #include <stdio.h>
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
30 #include <stdarg.h>
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
31
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
32 #include "match.h"
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
33 #include "log.h"
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
34 #include "xmalloc.h"
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
35
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
36 struct xaddr {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
37 sa_family_t af;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
38 union {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
39 struct in_addr v4;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
40 struct in6_addr v6;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
41 u_int8_t addr8[16];
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
42 u_int32_t addr32[4];
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
43 } xa; /* 128-bit address */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
44 u_int32_t scope_id; /* iface scope id for v6 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
45 #define v4 xa.v4
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
46 #define v6 xa.v6
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
47 #define addr8 xa.addr8
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
48 #define addr32 xa.addr32
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
49 };
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
50
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
51 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
52 addr_unicast_masklen(int af)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
53 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
54 switch (af) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
55 case AF_INET:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
56 return 32;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
57 case AF_INET6:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
58 return 128;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
59 default:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
60 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
61 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
62 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
63
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
64 static inline int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
65 masklen_valid(int af, u_int masklen)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
66 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
67 switch (af) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
68 case AF_INET:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
69 return masklen <= 32 ? 0 : -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
70 case AF_INET6:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
71 return masklen <= 128 ? 0 : -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
72 default:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
73 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
74 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
75 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
76
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
77 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
78 * Convert struct sockaddr to struct xaddr
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
79 * Returns 0 on success, -1 on failure.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
80 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
81 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
82 addr_sa_to_xaddr(struct sockaddr *sa, socklen_t slen, struct xaddr *xa)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
83 {
11060
40daac51fc0c 6655613 resync server's conditional Match block from OpenSSH (fix lint)
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents: 11044
diff changeset
84 /* LINTED E_BAD_PTR_CAST_ALIGN */
11044
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
85 struct sockaddr_in *in4 = (struct sockaddr_in *)sa;
11060
40daac51fc0c 6655613 resync server's conditional Match block from OpenSSH (fix lint)
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents: 11044
diff changeset
86 /* LINTED E_BAD_PTR_CAST_ALIGN */
11044
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
87 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
88
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
89 memset(xa, '\0', sizeof(*xa));
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
90
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
91 switch (sa->sa_family) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
92 case AF_INET:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
93 if (slen < sizeof(*in4))
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
94 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
95 xa->af = AF_INET;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
96 memcpy(&xa->v4, &in4->sin_addr, sizeof(xa->v4));
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
97 break;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
98 case AF_INET6:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
99 if (slen < sizeof(*in6))
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
100 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
101 xa->af = AF_INET6;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
102 memcpy(&xa->v6, &in6->sin6_addr, sizeof(xa->v6));
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
103 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
104 xa->scope_id = in6->sin6_scope_id;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
105 #endif
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
106 break;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
107 default:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
108 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
109 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
110
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
111 return 0;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
112 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
113
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
114 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
115 * Calculate a netmask of length 'l' for address family 'af' and
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
116 * store it in 'n'.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
117 * Returns 0 on success, -1 on failure.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
118 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
119 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
120 addr_netmask(int af, u_int l, struct xaddr *n)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
121 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
122 int i;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
123
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
124 if (masklen_valid(af, l) != 0 || n == NULL)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
125 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
126
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
127 memset(n, '\0', sizeof(*n));
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
128 switch (af) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
129 case AF_INET:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
130 n->af = AF_INET;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
131 n->v4.s_addr = htonl((0xffffffff << (32 - l)) & 0xffffffff);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
132 return 0;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
133 case AF_INET6:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
134 n->af = AF_INET6;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
135 for (i = 0; i < 4 && l >= 32; i++, l -= 32)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
136 n->addr32[i] = 0xffffffffU;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
137 if (i < 4 && l != 0)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
138 n->addr32[i] = htonl((0xffffffff << (32 - l)) &
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
139 0xffffffff);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
140 return 0;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
141 default:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
142 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
143 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
144 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
145
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
146 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
147 * Perform logical AND of addresses 'a' and 'b', storing result in 'dst'.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
148 * Returns 0 on success, -1 on failure.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
149 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
150 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
151 addr_and(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
152 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
153 int i;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
154
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
155 if (dst == NULL || a == NULL || b == NULL || a->af != b->af)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
156 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
157
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
158 memcpy(dst, a, sizeof(*dst));
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
159 switch (a->af) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
160 case AF_INET:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
161 dst->v4.s_addr &= b->v4.s_addr;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
162 return 0;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
163 case AF_INET6:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
164 dst->scope_id = a->scope_id;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
165 for (i = 0; i < 4; i++)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
166 dst->addr32[i] &= b->addr32[i];
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
167 return 0;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
168 default:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
169 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
170 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
171 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
172
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
173 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
174 * Compare addresses 'a' and 'b'
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
175 * Return 0 if addresses are identical, -1 if (a < b) or 1 if (a > b)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
176 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
177 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
178 addr_cmp(const struct xaddr *a, const struct xaddr *b)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
179 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
180 int i;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
181
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
182 if (a->af != b->af)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
183 return a->af == AF_INET6 ? 1 : -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
184
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
185 switch (a->af) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
186 case AF_INET:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
187 if (a->v4.s_addr == b->v4.s_addr)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
188 return 0;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
189 return ntohl(a->v4.s_addr) > ntohl(b->v4.s_addr) ? 1 : -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
190 case AF_INET6:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
191 for (i = 0; i < 16; i++)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
192 if (a->addr8[i] - b->addr8[i] != 0)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
193 return a->addr8[i] > b->addr8[i] ? 1 : -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
194 if (a->scope_id == b->scope_id)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
195 return 0;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
196 return a->scope_id > b->scope_id ? 1 : -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
197 default:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
198 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
199 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
200 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
201
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
202 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
203 * Parse string address 'p' into 'n'
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
204 * Returns 0 on success, -1 on failure.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
205 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
206 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
207 addr_pton(const char *p, struct xaddr *n)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
208 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
209 struct addrinfo hints, *ai;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
210
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
211 memset(&hints, '\0', sizeof(hints));
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
212 hints.ai_flags = AI_NUMERICHOST;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
213
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
214 if (p == NULL || getaddrinfo(p, NULL, &hints, &ai) != 0)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
215 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
216
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
217 if (ai == NULL || ai->ai_addr == NULL)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
218 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
219
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
220 if (n != NULL &&
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
221 addr_sa_to_xaddr(ai->ai_addr, ai->ai_addrlen, n) == -1) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
222 freeaddrinfo(ai);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
223 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
224 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
225
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
226 freeaddrinfo(ai);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
227 return 0;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
228 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
229
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
230 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
231 * Perform bitwise negation of address
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
232 * Returns 0 on success, -1 on failure.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
233 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
234 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
235 addr_invert(struct xaddr *n)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
236 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
237 int i;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
238
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
239 if (n == NULL)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
240 return (-1);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
241
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
242 switch (n->af) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
243 case AF_INET:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
244 n->v4.s_addr = ~n->v4.s_addr;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
245 return (0);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
246 case AF_INET6:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
247 for (i = 0; i < 4; i++)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
248 n->addr32[i] = ~n->addr32[i];
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
249 return (0);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
250 default:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
251 return (-1);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
252 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
253 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
254
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
255 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
256 * Calculate a netmask of length 'l' for address family 'af' and
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
257 * store it in 'n'.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
258 * Returns 0 on success, -1 on failure.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
259 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
260 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
261 addr_hostmask(int af, u_int l, struct xaddr *n)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
262 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
263 if (addr_netmask(af, l, n) == -1 || addr_invert(n) == -1)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
264 return (-1);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
265 return (0);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
266 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
267
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
268 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
269 * Test whether address 'a' is all zeros (i.e. 0.0.0.0 or ::)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
270 * Returns 0 on if address is all-zeros, -1 if not all zeros or on failure.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
271 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
272 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
273 addr_is_all0s(const struct xaddr *a)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
274 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
275 int i;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
276
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
277 switch (a->af) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
278 case AF_INET:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
279 return (a->v4.s_addr == 0 ? 0 : -1);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
280 case AF_INET6:;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
281 for (i = 0; i < 4; i++)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
282 if (a->addr32[i] != 0)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
283 return (-1);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
284 return (0);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
285 default:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
286 return (-1);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
287 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
288 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
289
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
290 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
291 * Test whether host portion of address 'a', as determined by 'masklen'
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
292 * is all zeros.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
293 * Returns 0 on if host portion of address is all-zeros,
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
294 * -1 if not all zeros or on failure.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
295 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
296 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
297 addr_host_is_all0s(const struct xaddr *a, u_int masklen)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
298 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
299 struct xaddr tmp_addr, tmp_mask, tmp_result;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
300
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
301 memcpy(&tmp_addr, a, sizeof(tmp_addr));
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
302 if (addr_hostmask(a->af, masklen, &tmp_mask) == -1)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
303 return (-1);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
304 if (addr_and(&tmp_result, &tmp_addr, &tmp_mask) == -1)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
305 return (-1);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
306 return (addr_is_all0s(&tmp_result));
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
307 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
308
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
309 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
310 * Parse a CIDR address (x.x.x.x/y or xxxx:yyyy::/z).
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
311 * Return -1 on parse error, -2 on inconsistency or 0 on success.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
312 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
313 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
314 addr_pton_cidr(const char *p, struct xaddr *n, u_int *l)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
315 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
316 struct xaddr tmp;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
317 long unsigned int masklen = 999;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
318 char addrbuf[64], *mp, *cp;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
319
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
320 /* Don't modify argument */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
321 if (p == NULL || strlcpy(addrbuf, p, sizeof(addrbuf)) > sizeof(addrbuf))
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
322 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
323
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
324 if ((mp = strchr(addrbuf, '/')) != NULL) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
325 *mp = '\0';
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
326 mp++;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
327 masklen = strtoul(mp, &cp, 10);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
328 if (*mp == '\0' || *cp != '\0' || masklen > 128)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
329 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
330 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
331
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
332 if (addr_pton(addrbuf, &tmp) == -1)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
333 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
334
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
335 if (mp == NULL)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
336 masklen = addr_unicast_masklen(tmp.af);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
337 if (masklen_valid(tmp.af, masklen) == -1)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
338 return -2;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
339 if (addr_host_is_all0s(&tmp, masklen) != 0)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
340 return -2;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
341
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
342 if (n != NULL)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
343 memcpy(n, &tmp, sizeof(*n));
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
344 if (l != NULL)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
345 *l = masklen;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
346
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
347 return 0;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
348 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
349
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
350 static int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
351 addr_netmatch(const struct xaddr *host, const struct xaddr *net, u_int masklen)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
352 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
353 struct xaddr tmp_mask, tmp_result;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
354
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
355 if (host->af != net->af)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
356 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
357
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
358 if (addr_netmask(host->af, masklen, &tmp_mask) == -1)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
359 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
360 if (addr_and(&tmp_result, host, &tmp_mask) == -1)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
361 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
362 return addr_cmp(&tmp_result, net);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
363 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
364
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
365 /*
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
366 * Match "addr" against list pattern list "_list", which may contain a
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
367 * mix of CIDR addresses and old-school wildcards.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
368 *
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
369 * If addr is NULL, then no matching is performed, but _list is parsed
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
370 * and checked for well-formedness.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
371 *
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
372 * Returns 1 on match found (never returned when addr == NULL).
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
373 * Returns 0 on if no match found, or no errors found when addr == NULL.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
374 * Returns -1 on negated match found (never returned when addr == NULL).
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
375 * Returns -2 on invalid list entry.
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
376 */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
377 int
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
378 addr_match_list(const char *addr, const char *_list)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
379 {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
380 char *list, *cp, *o;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
381 struct xaddr try_addr, match_addr;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
382 u_int masklen, neg;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
383 int ret = 0, r;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
384
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
385 if (addr != NULL && addr_pton(addr, &try_addr) != 0) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
386 debug2("%s: couldn't parse address %.100s", __func__, addr);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
387 return 0;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
388 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
389 if ((o = list = strdup(_list)) == NULL)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
390 return -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
391 while ((cp = strsep(&list, ",")) != NULL) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
392 neg = *cp == '!';
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
393 if (neg)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
394 cp++;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
395 if (*cp == '\0') {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
396 ret = -2;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
397 break;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
398 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
399 /* Prefer CIDR address matching */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
400 r = addr_pton_cidr(cp, &match_addr, &masklen);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
401 if (r == -2) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
402 error("Inconsistent mask length for "
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
403 "network \"%.100s\"", cp);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
404 ret = -2;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
405 break;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
406 } else if (r == 0) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
407 if (addr != NULL && addr_netmatch(&try_addr,
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
408 &match_addr, masklen) == 0) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
409 foundit:
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
410 if (neg) {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
411 ret = -1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
412 break;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
413 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
414 ret = 1;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
415 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
416 continue;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
417 } else {
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
418 /* If CIDR parse failed, try wildcard string match */
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
419 if (addr != NULL && match_pattern(addr, cp) == 1)
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
420 goto foundit;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
421 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
422 }
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
423 xfree(o);
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
424
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
425 return ret;
eacbb90c6866 6655613 resync server's conditional Match block from OpenSSH
Huie-Ying Lee <Huie-Ying.Lee@Sun.COM>
parents:
diff changeset
426 }