]> git.ipfire.org Git - thirdparty/squid.git/blame - src/auth/basic/RADIUS/radius-util.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / auth / basic / RADIUS / radius-util.cc
CommitLineData
5b95b903 1/*
4ac4a490 2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
5b95b903
AJ
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
dad2cf44 9// 2008-05-14: rename to radius-util.* to avoid name clashes with squid util.*
d80aac12 10/*
11 *
f53969cc
SM
12 * RADIUS
13 * Remote Authentication Dial In User Service
d80aac12 14 *
15 *
f53969cc
SM
16 * Livingston Enterprises, Inc.
17 * 6920 Koll Center Parkway
18 * Pleasanton, CA 94566
d80aac12 19 *
f53969cc
SM
20 * Copyright 1992 Livingston Enterprises, Inc.
21 * Copyright 1997 Cistron Internet Services B.V.
d80aac12 22 *
f53969cc
SM
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.
d80aac12 31 *
f53969cc
SM
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.
d80aac12 35 *
36 */
37
38/*
f53969cc 39 * util.c Miscellanous generic functions.
d80aac12 40 *
41 */
42
43char util_sccsid[] =
26ac0430
AJ
44 "@(#)util.c 1.5 Copyright 1992 Livingston Enterprises Inc\n"
45 " 2.1 Copyright 1997 Cistron Internet Services B.V.";
d80aac12 46
f7f3304a 47#include "squid.h"
03901cf8 48#include "auth/basic/RADIUS/radius-util.h"
074d6a40 49#include "md5.h"
a8b30ac9 50
074d6a40
AJ
51#include <cctype>
52#include <csignal>
53#include <ctime>
a8b30ac9 54#if HAVE_SYS_SOCKET_H
074d6a40 55#include <sys/socket.h>
a8b30ac9 56#endif
a8b30ac9 57#if HAVE_NETINET_IN_H
074d6a40 58#include <netinet/in.h>
a8b30ac9 59#endif
a8b30ac9 60#if HAVE_NETDB_H
074d6a40 61#include <netdb.h>
a8b30ac9 62#endif
63#if HAVE_PWD_H
074d6a40 64#include <pwd.h>
a8b30ac9 65#endif
d80aac12 66
67/*
f53969cc 68 * Check for valid IP address in standard dot notation.
d80aac12 69 */
70static int good_ipaddr(char *addr)
71{
f53969cc
SM
72 int dot_count;
73 int digit_count;
d80aac12 74
26ac0430
AJ
75 dot_count = 0;
76 digit_count = 0;
77 while (*addr != '\0' && *addr != ' ') {
78 if (*addr == '.') {
eb62585f 79 ++dot_count;
26ac0430
AJ
80 digit_count = 0;
81 } else if (!isdigit(*addr)) {
82 dot_count = 5;
83 } else {
eb62585f 84 ++digit_count;
26ac0430
AJ
85 if (digit_count > 3) {
86 dot_count = 5;
87 }
88 }
eb62585f 89 ++addr;
26ac0430
AJ
90 }
91 if (dot_count != 3) {
92 return(-1);
93 } else {
94 return(0);
95 }
d80aac12 96}
97
98/*
f53969cc
SM
99 * Return an IP address in host long notation from
100 * one supplied in standard dot notation.
d80aac12 101 */
09aabd84 102static uint32_t ipstr2long(char *ip_str)
d80aac12 103{
f53969cc
SM
104 char buf[6];
105 char *ptr;
106 int i;
107 int count;
108 uint32_t ipaddr;
109 int cur_byte;
d80aac12 110
09aabd84 111 ipaddr = (uint32_t)0;
eb62585f 112 for (i = 0; i < 4; ++i) {
26ac0430
AJ
113 ptr = buf;
114 count = 0;
115 *ptr = '\0';
116 while (*ip_str != '.' && *ip_str != '\0' && count < 4) {
117 if (!isdigit(*ip_str)) {
09aabd84 118 return((uint32_t)0);
26ac0430 119 }
14942edd
FC
120 *ptr = *ip_str;
121 ++ptr;
122 ++ip_str;
eb62585f 123 ++count;
26ac0430
AJ
124 }
125 if (count >= 4 || count == 0) {
09aabd84 126 return((uint32_t)0);
26ac0430
AJ
127 }
128 *ptr = '\0';
129 cur_byte = atoi(buf);
130 if (cur_byte < 0 || cur_byte > 255) {
09aabd84 131 return((uint32_t)0);
26ac0430 132 }
eb62585f 133 ++ip_str;
09aabd84 134 ipaddr = ipaddr << 8 | (uint32_t)cur_byte;
26ac0430
AJ
135 }
136 return(ipaddr);
d80aac12 137}
138
139/*
f53969cc
SM
140 * Return an IP address in host long notation from a host
141 * name or address in dot notation.
d80aac12 142 */
09aabd84 143uint32_t get_ipaddr(char *host)
d80aac12 144{
f53969cc 145 struct hostent *hp;
d80aac12 146
26ac0430
AJ
147 if (good_ipaddr(host) == 0) {
148 return(ipstr2long(host));
149 } else if ((hp = gethostbyname(host)) == (struct hostent *)NULL) {
09aabd84 150 return((uint32_t)0);
26ac0430 151 }
09aabd84 152 return(ntohl(*(uint32_t *)hp->h_addr));
d80aac12 153}
f53969cc 154