Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 783f106

Browse files
committed
Migrate file descriptor into tac_session object
As this is obviously related to the session.
1 parent d3e089d commit 783f106

13 files changed

+116
-103
lines changed

libtac/include/libtac.h

+11-9
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ struct tac_session {
147147
uint8_t tac_authen_service;
148148
uint8_t tac_authen_type;
149149
uint8_t seq_no;
150+
int fd;
150151

151152
/* user defined stuff */
152153
uint8_t user_data[0];
@@ -171,8 +172,9 @@ HDR *_tac_req_header(struct tac_session *, u_char, bool);
171172
/* connect.c */
172173
extern int tac_timeout;
173174

174-
int tac_connect(struct addrinfo **, unsigned);
175-
int tac_connect_single(const struct addrinfo *, struct addrinfo *, int);
175+
int tac_connect(struct tac_session *, struct addrinfo **, unsigned);
176+
int tac_connect_single(struct tac_session *, const struct addrinfo *, struct addrinfo *, int);
177+
void tac_close(struct tac_session *);
176178
char *tac_ntop(const struct sockaddr *);
177179

178180
/* authen_s.c */
@@ -182,29 +184,29 @@ const char *tag_get_authen_string(uint8_t);
182184
void tac_authen_send_pkt(struct tac_session *,
183185
const char *, const char *, const char *, const char *, u_char,
184186
u_char **, unsigned *);
185-
int tac_authen_send(struct tac_session *, int,
187+
int tac_authen_send(struct tac_session *,
186188
const char *, const char *, const char *, const char *, u_char);
187189

188190
/* authen_r.c */
189191
int tac_authen_parse(struct tac_session *, struct areply *, u_char *, unsigned);
190-
int tac_authen_read(struct tac_session *, int, struct areply *);
192+
int tac_authen_read(struct tac_session *, struct areply *);
191193

192194
/* cont_s.c */
193195
void tac_cont_send_pkt(struct tac_session *, const char *,
194196
u_char **, unsigned *);
195-
int tac_cont_send(struct tac_session *, int, const char *);
197+
int tac_cont_send(struct tac_session *, const char *);
196198

197199
/* crypt.c */
198200
void _tac_crypt(const struct tac_session *, u_char *, const HDR *);
199201

200202
/* author_r.c */
201203
int tac_author_parse(struct tac_session *, u_char *, unsigned, struct areply *);
202-
int tac_author_read(struct tac_session *, int, struct areply *);
204+
int tac_author_read(struct tac_session *, struct areply *);
203205

204206
/* author_s.c */
205207
void tac_author_send_pkt(struct tac_session *, const char *, const char *,
206208
const char *, struct tac_attrib *, u_char **, unsigned *);
207-
int tac_author_send(struct tac_session *, int, const char *, const char *,
209+
int tac_author_send(struct tac_session *, const char *, const char *,
208210
const char *, struct tac_attrib *);
209211

210212
/* attrib.c */
@@ -216,13 +218,13 @@ void tac_free_attrib(struct tac_attrib **);
216218
char *tac_acct_flag2str(u_char);
217219
void tac_acct_send_pkt(struct tac_session *, u_char, const char *,
218220
const char *, const char *, struct tac_attrib *, u_char **, unsigned *);
219-
int tac_acct_send(struct tac_session *, int, u_char, const char *,
221+
int tac_acct_send(struct tac_session *, u_char, const char *,
220222
const char *, const char *, struct tac_attrib *);
221223

222224
/* acct_r.c */
223225
int tac_acct_parse(struct tac_session *, u_char *, unsigned,
224226
struct areply *);
225-
int tac_acct_read(struct tac_session *, int, struct areply *);
227+
int tac_acct_read(struct tac_session *, struct areply *);
226228

227229
/* xalloc.c */
228230
void *xcalloc(size_t, size_t);

libtac/lib/acct_r.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ int tac_acct_parse(struct tac_session *sess, u_char *pkt, unsigned pkt_total,
137137
* LIBTAC_STATUS_PROTOCOL_ERR
138138
* >= 0 : server response, see TAC_PLUS_AUTHEN_STATUS_...
139139
*/
140-
int tac_acct_read(struct tac_session *sess, int fd, struct areply *re) {
140+
int tac_acct_read(struct tac_session *sess, struct areply *re) {
141141
HDR *th;
142142
struct acct_reply *tb = NULL;
143143
size_t ulen_from_header;
@@ -148,7 +148,7 @@ int tac_acct_read(struct tac_session *sess, int fd, struct areply *re) {
148148
re->msg = re->data = NULL;
149149

150150
if (tac_readtimeout_enable &&
151-
tac_read_wait(fd, tac_timeout * 1000, TAC_PLUS_HDR_SIZE, &timeleft) < 0 ) {
151+
tac_read_wait(sess->fd, tac_timeout * 1000, TAC_PLUS_HDR_SIZE, &timeleft) < 0 ) {
152152
TACSYSLOG(LOG_ERR,\
153153
"%s: reply timeout after %u secs", __FUNCTION__, tac_timeout);
154154
re->msg = xstrdup(acct_syserr_msg);
@@ -158,7 +158,7 @@ int tac_acct_read(struct tac_session *sess, int fd, struct areply *re) {
158158

159159
th = (HDR *)xcalloc(1, TAC_PLUS_HDR_SIZE);
160160

161-
spacket_read = read(fd, (char *)th, TAC_PLUS_HDR_SIZE);
161+
spacket_read = read(sess->fd, (char *)th, TAC_PLUS_HDR_SIZE);
162162
if(spacket_read < TAC_PLUS_HDR_SIZE) {
163163
TACSYSLOG(LOG_ERR,\
164164
"%s: short reply header, read %zd of %u expected: %m", __FUNCTION__,\
@@ -186,7 +186,7 @@ int tac_acct_read(struct tac_session *sess, int fd, struct areply *re) {
186186

187187
/* read reply packet body */
188188
if (tac_readtimeout_enable &&
189-
tac_read_wait(fd, timeleft, ulen_from_header, NULL) < 0 ) {
189+
tac_read_wait(sess->fd, timeleft, ulen_from_header, NULL) < 0 ) {
190190
TACSYSLOG(LOG_ERR,\
191191
"%s: reply timeout after %u secs", __FUNCTION__, tac_timeout);
192192
re->msg = xstrdup(acct_syserr_msg);
@@ -195,7 +195,7 @@ int tac_acct_read(struct tac_session *sess, int fd, struct areply *re) {
195195
return re->status;
196196
}
197197

198-
spacket_read = read(fd, (char *)tb, ulen_from_header);
198+
spacket_read = read(sess->fd, (char *)tb, ulen_from_header);
199199
if(spacket_read < 0 || (size_t) spacket_read < ulen_from_header) {
200200
TACSYSLOG(LOG_ERR,\
201201
"%s: short reply body, read %zd of %zu: %m",\

libtac/lib/acct_s.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void tac_acct_send_pkt(struct tac_session *sess, u_char type,
135135
* LIBTAC_STATUS_WRITE_TIMEOUT (pending impl)
136136
* LIBTAC_STATUS_ASSEMBLY_ERR (pending impl)
137137
*/
138-
int tac_acct_send(struct tac_session *sess, int fd,
138+
int tac_acct_send(struct tac_session *sess,
139139
u_char type, const char *user,
140140
const char *tty, const char *r_addr, struct tac_attrib *attr) {
141141

@@ -147,7 +147,7 @@ int tac_acct_send(struct tac_session *sess, int fd,
147147
tac_acct_send_pkt(sess, type, user, tty, r_addr, attr, &pkt, &pkt_total);
148148

149149
/* write packet */
150-
w = write(fd, pkt, pkt_total);
150+
w = write(sess->fd, pkt, pkt_total);
151151

152152
if(w < 0 || (unsigned) w < pkt_total) {
153153
TACSYSLOG(LOG_ERR, "%s: short write of packet, wrote %d of %d: %m",\

libtac/lib/authen_r.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int tac_authen_parse(struct tac_session *sess, struct areply *re,
140140
* LIBTAC_STATUS_PROTOCOL_ERR
141141
* >= 0 : server response, see TAC_PLUS_AUTHEN_STATUS_...
142142
*/
143-
int tac_authen_read(struct tac_session *sess, int fd, struct areply *re) {
143+
int tac_authen_read(struct tac_session *sess, struct areply *re) {
144144
HDR *th;
145145
struct authen_reply *tb = NULL;
146146
size_t len_from_header;
@@ -150,7 +150,7 @@ int tac_authen_read(struct tac_session *sess, int fd, struct areply *re) {
150150

151151
/* read the reply header */
152152
if (tac_readtimeout_enable &&
153-
tac_read_wait(fd, tac_timeout * 1000, TAC_PLUS_HDR_SIZE, &timeleft) < 0 ) {
153+
tac_read_wait(sess->fd, tac_timeout * 1000, TAC_PLUS_HDR_SIZE, &timeleft) < 0 ) {
154154
TACSYSLOG(LOG_ERR,
155155
"%s: reply timeout after %d secs", __FUNCTION__, tac_timeout);
156156
re->status = LIBTAC_STATUS_READ_TIMEOUT;
@@ -159,7 +159,7 @@ int tac_authen_read(struct tac_session *sess, int fd, struct areply *re) {
159159

160160
th = (HDR *)xcalloc(1, TAC_PLUS_HDR_SIZE);
161161

162-
r = read(fd, th, TAC_PLUS_HDR_SIZE);
162+
r = read(sess->fd, th, TAC_PLUS_HDR_SIZE);
163163
if (r < TAC_PLUS_HDR_SIZE) {
164164
TACSYSLOG(LOG_ERR,
165165
"%s: short reply header, read %d of %u: %m", __FUNCTION__,
@@ -186,13 +186,13 @@ int tac_authen_read(struct tac_session *sess, int fd, struct areply *re) {
186186

187187
/* read reply packet body */
188188
if (tac_readtimeout_enable &&
189-
tac_read_wait(fd, timeleft, len_from_header, NULL) < 0 ) {
189+
tac_read_wait(sess->fd, timeleft, len_from_header, NULL) < 0 ) {
190190
TACSYSLOG(LOG_ERR,
191191
"%s: reply timeout after %d secs", __FUNCTION__, tac_timeout);
192192
status = LIBTAC_STATUS_READ_TIMEOUT;
193193
}
194194

195-
r = read(fd, (char *)tb, len_from_header);
195+
r = read(sess->fd, (char *)tb, len_from_header);
196196
if (r < 0 || (unsigned) r < len_from_header) {
197197
TACSYSLOG(LOG_ERR,
198198
"%s: short reply body, read %d of %zu: %m",

libtac/lib/authen_s.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void tac_authen_send_pkt(struct tac_session *sess,
207207
* LIBTAC_STATUS_WRITE_TIMEOUT
208208
* LIBTAC_STATUS_ASSEMBLY_ERR
209209
*/
210-
int tac_authen_send(struct tac_session *sess, int fd,
210+
int tac_authen_send(struct tac_session *sess,
211211
const char *user, const char *pass, const char *tty,
212212
const char *r_addr, u_char action) {
213213

@@ -219,7 +219,7 @@ int tac_authen_send(struct tac_session *sess, int fd,
219219
tac_authen_send_pkt(sess, user, pass, tty, r_addr, action, &pkt, &pkt_total);
220220

221221
/* we can now write the packet */
222-
w = write(fd, pkt, pkt_total);
222+
w = write(sess->fd, pkt, pkt_total);
223223
if (w < 0 || (unsigned) w < pkt_total) {
224224
TACSYSLOG(
225225
LOG_ERR, "%s: short write on packet, wrote %d of %u: %m", __FUNCTION__, w, pkt_total);

libtac/lib/author_r.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ int tac_author_parse(struct tac_session *sess,
228228
* LIBTAC_STATUS_PROTOCOL_ERR
229229
* >= 0 : server response, see TAC_PLUS_AUTHOR_STATUS_...
230230
*/
231-
int tac_author_read(struct tac_session *sess, int fd, struct areply *re) {
231+
int tac_author_read(struct tac_session *sess, struct areply *re) {
232232
HDR *th;
233233
struct author_reply *tb = NULL;
234234
size_t len_from_header;
@@ -238,7 +238,7 @@ int tac_author_read(struct tac_session *sess, int fd, struct areply *re) {
238238
bzero(re, sizeof(*re));
239239

240240
if (tac_readtimeout_enable
241-
&& tac_read_wait(fd, tac_timeout * 1000, TAC_PLUS_HDR_SIZE,
241+
&& tac_read_wait(sess->fd, tac_timeout * 1000, TAC_PLUS_HDR_SIZE,
242242
&timeleft) < 0) {
243243

244244
TACSYSLOG(
@@ -250,7 +250,7 @@ int tac_author_read(struct tac_session *sess, int fd, struct areply *re) {
250250

251251
th = (HDR *)xcalloc(1, TAC_PLUS_HDR_SIZE);
252252

253-
packet_read = read(fd, th, TAC_PLUS_HDR_SIZE);
253+
packet_read = read(sess->fd, th, TAC_PLUS_HDR_SIZE);
254254
if (packet_read < TAC_PLUS_HDR_SIZE) {
255255
TACSYSLOG(
256256
LOG_ERR, "%s: short reply header, read %zd of %u: %m", __FUNCTION__,
@@ -277,15 +277,15 @@ int tac_author_read(struct tac_session *sess, int fd, struct areply *re) {
277277

278278
/* read reply packet body */
279279
if (tac_readtimeout_enable
280-
&& tac_read_wait(fd, timeleft, len_from_header, NULL) < 0) {
280+
&& tac_read_wait(sess->fd, timeleft, len_from_header, NULL) < 0) {
281281
TACSYSLOG(
282282
LOG_ERR, "%s: reply timeout after %u secs", __FUNCTION__, tac_timeout);
283283
re->msg = xstrdup(author_syserr_msg);
284284
re->status = LIBTAC_STATUS_READ_TIMEOUT;
285285
free(th);
286286
return re->status;
287287
}
288-
packet_read = read(fd, tb, len_from_header);
288+
packet_read = read(sess->fd, tb, len_from_header);
289289
if (packet_read < 0 || (size_t) packet_read < len_from_header) {
290290
TACSYSLOG(
291291
LOG_ERR, "%s: short reply body, read %zd of %zu: %m", __FUNCTION__, ((packet_read >= 0) ? packet_read : 0), len_from_header);

libtac/lib/author_s.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void tac_author_send_pkt(struct tac_session *sess,
121121
* LIBTAC_STATUS_ASSEMBLY_ERR (pending impl)
122122
*/
123123
int tac_author_send(struct tac_session *sess,
124-
int fd, const char *user, const char *tty, const char *r_addr,
124+
const char *user, const char *tty, const char *r_addr,
125125
struct tac_attrib *attr) {
126126

127127
u_char *pkt = NULL;
@@ -132,7 +132,7 @@ int tac_author_send(struct tac_session *sess,
132132
tac_author_send_pkt(sess, user, tty, r_addr, attr, &pkt, &pkt_total);
133133

134134
/* write packet */
135-
w = write(fd, pkt, pkt_total);
135+
w = write(sess->fd, pkt, pkt_total);
136136
if (w < 0 || (unsigned) w < pkt_total) {
137137
TACSYSLOG(LOG_ERR, "%s: short write on packet, wrote %d of %d: %m",\
138138
__FUNCTION__, w, pkt_total);

libtac/lib/connect.c

+26-12
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,36 @@ int tac_timeout = 5;
3939
in server table.
4040
4141
* return value:
42-
* >= 0 : valid fd
42+
* == 0 : success
4343
* < 0 : error status code, see LIBTAC_STATUS_...
4444
*/
45-
int tac_connect(struct addrinfo **server, unsigned servers) {
45+
int tac_connect(struct tac_session *sess,
46+
struct addrinfo **server, unsigned servers) {
4647
unsigned tries;
47-
int fd=-1;
48+
int retval = -1;
4849

4950
if(servers == 0 || server == NULL) {
5051
TACSYSLOG(LOG_ERR, "%s: no TACACS+ servers defined", __FUNCTION__);
5152
} else {
5253
for ( tries = 0; tries < servers; tries++ ) {
53-
if((fd=tac_connect_single(server[tries], NULL, tac_timeout)) >= 0 ) {
54-
/* tac_secret was set in tac_connect_single on success */
54+
if ((retval = tac_connect_single(sess, server[tries], NULL, tac_timeout)) >= 0 ) {
5555
break;
5656
}
5757
}
5858
}
5959

6060
/* all attempts failed if fd is still < 0 */
61-
TACDEBUG(LOG_DEBUG, "%s: exit status=%d",__FUNCTION__, fd);
62-
return fd;
61+
TACDEBUG(LOG_DEBUG, "%s: exit status=%d",__FUNCTION__, retval);
62+
return retval;
6363
} /* tac_connect */
6464

6565

6666
/* return value:
67-
* >= 0 : valid fd
67+
* == 0 : success
6868
* < 0 : error status code, see LIBTAC_STATUS_...
6969
*/
70-
int tac_connect_single(const struct addrinfo *server, struct addrinfo *srcaddr, int timeout) {
70+
int tac_connect_single(struct tac_session *sess,
71+
const struct addrinfo *server, struct addrinfo *srcaddr, int timeout) {
7172
int retval = LIBTAC_STATUS_CONN_ERR; /* default retval */
7273
int fd = -1;
7374
int flags, rc;
@@ -161,17 +162,30 @@ int tac_connect_single(const struct addrinfo *server, struct addrinfo *srcaddr,
161162

162163
/* connected ok */
163164
TACDEBUG(LOG_DEBUG, "%s: connected to %s", __FUNCTION__, ip);
164-
retval = fd;
165+
retval = 0;
166+
167+
/* now stuff the fd into the tac_session */
168+
if (sess->fd >= 0)
169+
close(sess->fd);
170+
sess->fd = fd;
165171

166172
bomb:
167173
if (retval < 0 && fd != -1)
168174
close(fd);
169175

170-
TACDEBUG(LOG_DEBUG, "%s: exit status=%d (fd=%d)",\
171-
__FUNCTION__, retval < 0 ? retval:0, fd);
176+
TACDEBUG(LOG_DEBUG, "%s: exit status=%d",\
177+
__FUNCTION__, retval);
172178
return retval;
173179
} /* tac_connect_single */
174180

181+
void
182+
tac_close(struct tac_session *sess)
183+
{
184+
if (sess->fd >= 0) {
185+
close(sess->fd);
186+
sess->fd = -1;
187+
}
188+
}
175189

176190
/* return value:
177191
* ptr to char* with format IP address

libtac/lib/cont_s.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void tac_cont_send_pkt(struct tac_session *sess, const char *pass,
9595
* LIBTAC_STATUS_WRITE_TIMEOUT (pending impl)
9696
* LIBTAC_STATUS_ASSEMBLY_ERR
9797
*/
98-
int tac_cont_send(struct tac_session *sess, int fd, const char *pass) {
98+
int tac_cont_send(struct tac_session *sess, const char *pass) {
9999

100100
u_char *pkt = NULL;
101101
unsigned pkt_total = 0;
@@ -104,7 +104,7 @@ int tac_cont_send(struct tac_session *sess, int fd, const char *pass) {
104104
/* generate the packet */
105105
tac_cont_send_pkt(sess, pass, &pkt, &pkt_total);
106106

107-
w = write(fd, pkt, pkt_total);
107+
w = write(sess->fd, pkt, pkt_total);
108108
if (w < 0 || (unsigned) w < pkt_total) {
109109
TACSYSLOG(
110110
LOG_ERR, "%s: short write on packet, wrote %d of %u: %m", __FUNCTION__, w, pkt_total);

libtac/lib/header.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ HDR *_tac_req_header(struct tac_session *sess, u_char type, bool cont_session) {
4646

4747
/* preset some packet options in header */
4848
th->type=type;
49-
th->seq_no=sess->seq_no;
49+
th->seq_no=++sess->seq_no;
5050
th->encryption=TAC_PLUS_ENCRYPTED_FLAG;
5151

5252
/* make session_id from pseudo-random number */

libtac/lib/session.c

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ tac_session_alloc_extra(unsigned n)
4040
sess->tac_authen_method = TAC_PLUS_AUTHEN_METH_TACACSPLUS;
4141
sess->tac_authen_type = TAC_PLUS_AUTHEN_TYPE_PAP;
4242
sess->seq_no = 0;
43+
sess->fd = -1;
4344

4445
return sess;
4546
}
@@ -95,6 +96,7 @@ tac_session_get_user_data(struct tac_session *sess)
9596
void
9697
tac_session_free(struct tac_session *sess)
9798
{
99+
tac_close(sess);
98100
free(sess);
99101
}
100102

0 commit comments

Comments
 (0)