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