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