1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/* connect.c - Open connection to server.
*
* Copyright (C) 2010, Pawel Krawczyk <kravietz@ceti.pl> and
* Jeroen Nijhof <jeroen@nijhofnet.nl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program - see the file COPYING.
*
* See `CHANGES' file for revision history.
*/
#include <signal.h>
#include <arpa/inet.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#ifdef _AIX
#include <sys/socket.h>
#endif
#include "tacplus.h"
#include "libtac.h"
/* Pointer to TACACS+ connection timeout */
int tac_timeout = 5;
/* Returns file descriptor of open connection
to the first available server from list passed
in server table.
*/
int tac_connect(struct addrinfo **server, int servers) {
int tries = 0;
int fd, flags, retval;
fd_set readfds, writefds;
struct timeval tv;
if(!servers) {
syslog(LOG_ERR, "%s: no TACACS+ servers defined", __FUNCTION__);
return(-1);
}
while(tries < servers) {
if((fd=socket(server[tries]->ai_family, server[tries]->ai_socktype, server[tries]->ai_protocol)) == -1) {
syslog(LOG_WARNING,
"%s: socket creation error", __FUNCTION__);
tries++;
continue;
}
/* put socket in non blocking mode for timeout support */
flags = fcntl(fd, F_GETFL, 0);
if(fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
syslog(LOG_WARNING, "%s: cannot set socket non blocking",
__FUNCTION__);
tries++;
continue;
}
retval = connect(fd, server[tries]->ai_addr, server[tries]->ai_addrlen);
if((retval == -1) && (errno != EINPROGRESS)) {
syslog(LOG_WARNING,
"%s: connection to %s failed: %m", __FUNCTION__,
tac_ntop(server[tries]->ai_addr, server[tries]->ai_addrlen));
if(fcntl(fd, F_SETFL, flags)) {
syslog(LOG_WARNING, "%s: cannot restore socket flags",
__FUNCTION__);
}
tries++;
continue;
}
/* set fds for select */
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
writefds = readfds;
/* set timeout seconds */
tv.tv_sec = tac_timeout;
tv.tv_usec = 0;
/* check if socket is ready for read or write */
if(!select(fd+1, &readfds, &writefds, NULL, &tv)) {
syslog(LOG_WARNING,
"%s: connection timeout with %s : %m", __FUNCTION__,
tac_ntop(server[tries]->ai_addr, server[tries]->ai_addrlen));
if(fcntl(fd, F_SETFL, flags)) {
syslog(LOG_WARNING, "%s: cannot restore socket flags",
__FUNCTION__);
}
tries++;
continue;
}
/* connected ok */
if(fcntl(fd, F_SETFL, flags)) {
syslog(LOG_WARNING, "%s: cannot restore socket flags",
__FUNCTION__);
}
TACDEBUG((LOG_DEBUG, "%s: connected to %s", __FUNCTION__, \
tac_ntop(server[tries]->ai_addr, server[tries]->ai_addrlen)));
return(fd);
}
/* all attempts failed */
syslog(LOG_ERR, "%s: all possible TACACS+ servers failed", __FUNCTION__);
return(-1);
} /* tac_connect */
int tac_connect_single(struct addrinfo *server) {
struct addrinfo *temp[1];
temp[0] = server;
return(tac_connect(temp, 1));
} /* tac_connect_single */
char *tac_ntop(const struct sockaddr *sa, size_t ai_addrlen) {
char *str = (char *) xcalloc(1, ai_addrlen);
switch(sa->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
str, ai_addrlen);
break;
case AF_INET6:
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
str, ai_addrlen);
break;
default:
strncpy(str, "Unknown AF", ai_addrlen);
}
return str;
} /* tac_ntop */
|