]> git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/basic/RADIUS/radius-util.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / auth / basic / RADIUS / radius-util.cc
1 /*
2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 // 2008-05-14: rename to radius-util.* to avoid name clashes with squid util.*
10 /*
11 *
12 * RADIUS
13 * Remote Authentication Dial In User Service
14 *
15 *
16 * Livingston Enterprises, Inc.
17 * 6920 Koll Center Parkway
18 * Pleasanton, CA 94566
19 *
20 * Copyright 1992 Livingston Enterprises, Inc.
21 * Copyright 1997 Cistron Internet Services B.V.
22 *
23 * Permission to use, copy, modify, and distribute this software for any
24 * purpose and without fee is hereby granted, provided that this
25 * copyright and permission notice appear on all copies and supporting
26 * documentation, the name of Livingston Enterprises, Inc. not be used
27 * in advertising or publicity pertaining to distribution of the
28 * program without specific prior permission, and notice be given
29 * in supporting documentation that copying and distribution is by
30 * permission of Livingston Enterprises, Inc.
31 *
32 * Livingston Enterprises, Inc. makes no representations about
33 * the suitability of this software for any purpose. It is
34 * provided "as is" without express or implied warranty.
35 *
36 */
37
38 /*
39 * util.c Miscellaneous generic functions.
40 *
41 */
42
43 char util_sccsid[] =
44 "@(#)util.c 1.5 Copyright 1992 Livingston Enterprises Inc\n"
45 " 2.1 Copyright 1997 Cistron Internet Services B.V.";
46
47 #include "squid.h"
48 #include "auth/basic/RADIUS/radius-util.h"
49 #include "md5.h"
50
51 #include <cctype>
52 #include <csignal>
53 #include <ctime>
54 #if HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
56 #endif
57 #if HAVE_NETINET_IN_H
58 #include <netinet/in.h>
59 #endif
60 #if HAVE_NETDB_H
61 #include <netdb.h>
62 #endif
63 #if HAVE_PWD_H
64 #include <pwd.h>
65 #endif
66
67 /*
68 * Check for valid IP address in standard dot notation.
69 */
70 static int good_ipaddr(char *addr)
71 {
72 int dot_count;
73 int digit_count;
74
75 dot_count = 0;
76 digit_count = 0;
77 while (*addr != '\0' && *addr != ' ') {
78 if (*addr == '.') {
79 ++dot_count;
80 digit_count = 0;
81 } else if (!isdigit(*addr)) {
82 dot_count = 5;
83 } else {
84 ++digit_count;
85 if (digit_count > 3) {
86 dot_count = 5;
87 }
88 }
89 ++addr;
90 }
91 if (dot_count != 3) {
92 return(-1);
93 } else {
94 return(0);
95 }
96 }
97
98 /*
99 * Return an IP address in host long notation from
100 * one supplied in standard dot notation.
101 */
102 static uint32_t ipstr2long(char *ip_str)
103 {
104 char buf[6];
105 char *ptr;
106 int i;
107 int count;
108 uint32_t ipaddr;
109 int cur_byte;
110
111 ipaddr = (uint32_t)0;
112 for (i = 0; i < 4; ++i) {
113 ptr = buf;
114 count = 0;
115 *ptr = '\0';
116 while (*ip_str != '.' && *ip_str != '\0' && count < 4) {
117 if (!isdigit(*ip_str)) {
118 return((uint32_t)0);
119 }
120 *ptr = *ip_str;
121 ++ptr;
122 ++ip_str;
123 ++count;
124 }
125 if (count >= 4 || count == 0) {
126 return((uint32_t)0);
127 }
128 *ptr = '\0';
129 cur_byte = atoi(buf);
130 if (cur_byte < 0 || cur_byte > 255) {
131 return((uint32_t)0);
132 }
133 ++ip_str;
134 ipaddr = ipaddr << 8 | (uint32_t)cur_byte;
135 }
136 return(ipaddr);
137 }
138
139 /*
140 * Return an IP address in host long notation from a host
141 * name or address in dot notation.
142 */
143 uint32_t get_ipaddr(char *host)
144 {
145 struct hostent *hp;
146
147 if (good_ipaddr(host) == 0) {
148 return(ipstr2long(host));
149 } else if ((hp = gethostbyname(host)) == (struct hostent *)NULL) {
150 return((uint32_t)0);
151 }
152 return(ntohl(*(uint32_t *)hp->h_addr));
153 }
154