2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998, 2001, 2002, Juniper Networks, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
50 #include "taclib_private.h"
52 static int add_str_8(struct tac_handle *, u_int8_t *,
54 static int add_str_16(struct tac_handle *, u_int16_t *,
56 static int protocol_version(int, int, int);
57 static void close_connection(struct tac_handle *);
58 static int conn_server(struct tac_handle *);
59 static void crypt_msg(struct tac_handle *, struct tac_msg *);
60 static void *dup_str(struct tac_handle *, const struct srvr_str *,
62 static int establish_connection(struct tac_handle *);
63 static void free_str(struct clnt_str *);
64 static void generr(struct tac_handle *, const char *, ...)
66 static void gen_session_id(struct tac_msg *);
67 static int get_srvr_end(struct tac_handle *);
68 static int get_srvr_str(struct tac_handle *, const char *,
69 struct srvr_str *, size_t);
70 static void init_clnt_str(struct clnt_str *);
71 static void init_srvr_str(struct srvr_str *);
72 static int read_timed(struct tac_handle *, void *, size_t,
73 const struct timeval *);
74 static int recv_msg(struct tac_handle *);
75 static int save_str(struct tac_handle *, struct clnt_str *,
76 const void *, size_t);
77 static int send_msg(struct tac_handle *);
78 static int split(char *, char *[], int, char *, size_t);
79 static void *xmalloc(struct tac_handle *, size_t);
80 static char *xstrdup(struct tac_handle *, const char *);
81 static void clear_srvr_avs(struct tac_handle *);
82 static void create_msg(struct tac_handle *, int, int, int);
85 * Append some optional data to the current request, and store its
86 * length into the 8-bit field referenced by "fld". Returns 0 on
87 * success, or -1 on failure.
89 * This function also frees the "cs" string data and initializes it
93 add_str_8(struct tac_handle *h, u_int8_t *fld, struct clnt_str *cs)
97 if (add_str_16(h, &len, cs) == -1)
101 generr(h, "Field too long");
109 * Append some optional data to the current request, and store its
110 * length into the 16-bit field (network byte order) referenced by
111 * "fld". Returns 0 on success, or -1 on failure.
113 * This function also frees the "cs" string data and initializes it
117 add_str_16(struct tac_handle *h, u_int16_t *fld, struct clnt_str *cs)
122 if (cs->data == NULL)
128 generr(h, "Field too long");
131 offset = ntohl(h->request.length);
132 if (offset + len > BODYSIZE) {
133 generr(h, "Message too long");
136 memcpy(h->request.u.body + offset, cs->data, len);
137 h->request.length = htonl(offset + len);
145 protocol_version(int msg_type, int var, int type)
151 /* 'var' represents the 'action' */
153 case TAC_AUTHEN_LOGIN:
156 case TAC_AUTHEN_TYPE_PAP:
157 case TAC_AUTHEN_TYPE_CHAP:
158 case TAC_AUTHEN_TYPE_MSCHAP:
159 case TAC_AUTHEN_TYPE_ARAP:
169 case TAC_AUTHEN_SENDAUTH:
180 /* 'var' represents the 'method' */
183 * When new authentication methods are added, include 'method'
184 * in determining the value of 'minor'. At this point, all
185 * methods defined in this implementation (see "Authorization
186 * authentication methods" in taclib.h) are minor version 0
187 * Not all types, however, indicate minor version 0.
189 case TAC_AUTHEN_METH_NOT_SET:
190 case TAC_AUTHEN_METH_NONE:
191 case TAC_AUTHEN_METH_KRB5:
192 case TAC_AUTHEN_METH_LINE:
193 case TAC_AUTHEN_METH_ENABLE:
194 case TAC_AUTHEN_METH_LOCAL:
195 case TAC_AUTHEN_METH_TACACSPLUS:
196 case TAC_AUTHEN_METH_RCMD:
198 case TAC_AUTHEN_TYPE_PAP:
199 case TAC_AUTHEN_TYPE_CHAP:
200 case TAC_AUTHEN_TYPE_MSCHAP:
201 case TAC_AUTHEN_TYPE_ARAP:
223 return TAC_VER_MAJOR << 4 | minor;
228 close_connection(struct tac_handle *h)
237 conn_server(struct tac_handle *h)
239 const struct tac_server *srvp = &h->servers[h->cur_server];
242 if ((h->fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
243 generr(h, "Cannot create socket: %s", strerror(errno));
246 if ((flags = fcntl(h->fd, F_GETFL, 0)) == -1 ||
247 fcntl(h->fd, F_SETFL, flags | O_NONBLOCK) == -1) {
248 generr(h, "Cannot set non-blocking mode on socket: %s",
254 if (connect(h->fd, (struct sockaddr *)&srvp->addr,
255 sizeof srvp->addr) == 0)
258 if (errno == EINPROGRESS) {
262 struct sockaddr peer;
263 socklen_t errlen, peerlen;
266 /* Wait for the connection to complete. */
268 FD_SET(h->fd, &wfds);
269 tv.tv_sec = srvp->timeout;
271 nfds = select(h->fd + 1, NULL, &wfds, NULL, &tv);
273 generr(h, "select: %s", strerror(errno));
279 generr(h, "connect: timed out");
285 /* See whether we are connected now. */
286 peerlen = sizeof peer;
287 if (getpeername(h->fd, &peer, &peerlen) == 0)
290 if (errno != ENOTCONN) {
291 generr(h, "getpeername: %s", strerror(errno));
297 /* Find out why the connect failed. */
299 getsockopt(h->fd, SOL_SOCKET, SO_ERROR, &err, &errlen);
302 generr(h, "connect: %s", strerror(errno));
309 * Encrypt or decrypt a message. The operations are symmetrical.
312 crypt_msg(struct tac_handle *h, struct tac_msg *msg)
317 unsigned char md5[16];
321 secret = h->servers[h->cur_server].secret;
322 if (secret[0] == '\0')
323 msg->flags |= TAC_UNENCRYPTED;
324 if (msg->flags & TAC_UNENCRYPTED)
327 msg_len = ntohl(msg->length);
330 MD5Update(&base_ctx, msg->session_id, sizeof msg->session_id);
331 MD5Update(&base_ctx, secret, strlen(secret));
332 MD5Update(&base_ctx, &msg->version, sizeof msg->version);
333 MD5Update(&base_ctx, &msg->seq_no, sizeof msg->seq_no);
336 for (chunk = 0; chunk < msg_len; chunk += sizeof md5) {
342 if ((chunk_len = msg_len - chunk) > sizeof md5)
343 chunk_len = sizeof md5;
344 for (i = 0; i < chunk_len; i++)
345 msg->u.body[chunk + i] ^= md5[i];
348 MD5Update(&ctx, md5, sizeof md5);
353 * Return a dynamically allocated copy of the given server string.
354 * The copy is null-terminated. If "len" is non-NULL, the length of
355 * the string (excluding the terminating null byte) is stored via it.
356 * Returns NULL on failure. Empty strings are still allocated even
357 * though they have no content.
360 dup_str(struct tac_handle *h, const struct srvr_str *ss, size_t *len)
364 if ((p = (unsigned char *)xmalloc(h, ss->len + 1)) == NULL)
366 if (ss->data != NULL && ss->len != 0)
367 memcpy(p, ss->data, ss->len);
375 establish_connection(struct tac_handle *h)
379 if (h->fd >= 0) /* Already connected. */
381 if (h->num_servers == 0) {
382 generr(h, "No TACACS+ servers specified");
386 * Try the servers round-robin. We begin with the one that
387 * worked for us the last time. That way, once we find a good
388 * server, we won't waste any more time trying the bad ones.
390 for (i = 0; i < h->num_servers; i++) {
391 if (conn_server(h) == 0) {
392 h->single_connect = (h->servers[h->cur_server].flags &
393 TAC_SRVR_SINGLE_CONNECT) != 0;
396 if (++h->cur_server >= h->num_servers) /* Wrap around */
399 /* Just return whatever error was last reported by conn_server(). */
404 * Free a client string, obliterating its contents first for security.
407 free_str(struct clnt_str *cs)
409 if (cs->data != NULL) {
410 memset(cs->data, 0, cs->len);
418 generr(struct tac_handle *h, const char *format, ...)
422 va_start(ap, format);
423 vsnprintf(h->errmsg, ERRSIZE, format, ap);
428 gen_session_id(struct tac_msg *msg)
433 msg->session_id[0] = r >> 8;
434 msg->session_id[1] = r;
436 msg->session_id[2] = r >> 8;
437 msg->session_id[3] = r;
441 * Verify that we are exactly at the end of the response message.
442 * Returns 0 on success, -1 on failure.
445 get_srvr_end(struct tac_handle *h)
449 len = ntohl(h->response.length);
451 if (h->srvr_pos != len) {
452 generr(h, "Invalid length field in response "
453 "from server: end expected at %u, response length %u",
461 get_srvr_str(struct tac_handle *h, const char *field,
462 struct srvr_str *ss, size_t len)
464 if (h->srvr_pos + len > ntohl(h->response.length)) {
465 generr(h, "Invalid length field in %s response from server "
466 "(%lu > %lu)", field, (u_long)(h->srvr_pos + len),
467 (u_long)ntohl(h->response.length));
470 ss->data = len != 0 ? h->response.u.body + h->srvr_pos : NULL;
477 init_clnt_str(struct clnt_str *cs)
484 init_srvr_str(struct srvr_str *ss)
491 read_timed(struct tac_handle *h, void *buf, size_t len,
492 const struct timeval *deadline)
500 n = read(h->fd, ptr, len);
505 if (errno != EAGAIN) {
506 generr(h, "Network read error: %s",
511 /* Wait until we can read more data. */
512 gettimeofday(&tv, NULL);
513 timersub(deadline, &tv, &tv);
514 if (tv.tv_sec >= 0) {
518 FD_SET(h->fd, &rfds);
520 select(h->fd + 1, &rfds, NULL, NULL, &tv);
522 generr(h, "select: %s",
529 generr(h, "Network read timed out");
533 generr(h, "unexpected EOF from server");
544 * Receive a response from the server and decrypt it. Returns 0 on
545 * success, or -1 on failure.
548 recv_msg(struct tac_handle *h)
550 struct timeval deadline;
555 gettimeofday(&deadline, NULL);
556 deadline.tv_sec += h->servers[h->cur_server].timeout;
558 /* Read the message header and make sure it is reasonable. */
559 if (read_timed(h, msg, HDRSIZE, &deadline) == -1)
561 if (memcmp(msg->session_id, h->request.session_id,
562 sizeof msg->session_id) != 0) {
563 generr(h, "Invalid session ID in received message");
566 if (msg->type != h->request.type) {
567 generr(h, "Invalid type in received message"
568 " (got %u, expected %u)",
569 msg->type, h->request.type);
572 len = ntohl(msg->length);
573 if (len > BODYSIZE) {
574 generr(h, "Received message too large (%u > %u)",
578 if (msg->seq_no != ++h->last_seq_no) {
579 generr(h, "Invalid sequence number in received message"
580 " (got %u, expected %u)",
581 msg->seq_no, h->last_seq_no);
585 /* Read the message body. */
586 if (read_timed(h, msg->u.body, len, &deadline) == -1)
593 * Turn off single-connection mode if the server isn't amenable
596 if (!(msg->flags & TAC_SINGLE_CONNECT))
597 h->single_connect = 0;
602 save_str(struct tac_handle *h, struct clnt_str *cs, const void *data,
606 if (data != NULL && len != 0) {
607 if ((cs->data = xmalloc(h, len)) == NULL)
610 memcpy(cs->data, data, len);
616 * Send the current request, after encrypting it. Returns 0 on success,
620 send_msg(struct tac_handle *h)
622 struct timeval deadline;
627 if (h->last_seq_no & 1) {
628 generr(h, "Attempt to send message out of sequence");
632 if (establish_connection(h) == -1)
636 msg->seq_no = ++h->last_seq_no;
637 if (msg->seq_no == 1)
641 if (h->single_connect)
642 msg->flags |= TAC_SINGLE_CONNECT;
644 msg->flags &= ~TAC_SINGLE_CONNECT;
645 gettimeofday(&deadline, NULL);
646 deadline.tv_sec += h->servers[h->cur_server].timeout;
647 len = HDRSIZE + ntohl(msg->length);
652 n = write(h->fd, ptr, len);
657 if (errno != EAGAIN) {
658 generr(h, "Network write error: %s",
663 /* Wait until we can write more data. */
664 gettimeofday(&tv, NULL);
665 timersub(&deadline, &tv, &tv);
666 if (tv.tv_sec >= 0) {
670 FD_SET(h->fd, &wfds);
672 select(h->fd + 1, NULL, &wfds, NULL, &tv);
674 generr(h, "select: %s",
681 generr(h, "Network write timed out");
693 * Destructively split a string into fields separated by white space.
694 * `#' at the beginning of a field begins a comment that extends to the
695 * end of the string. Fields may be quoted with `"'. Inside quoted
696 * strings, the backslash escapes `\"' and `\\' are honored.
698 * Pointers to up to the first maxfields fields are stored in the fields
699 * array. Missing fields get NULL pointers.
701 * The return value is the actual number of fields parsed, and is always
704 * On a syntax error, places a message in the msg string, and returns -1.
707 split(char *str, char *fields[], int maxfields, char *msg, size_t msglen)
711 static const char ws[] = " \t";
713 for (i = 0; i < maxfields; i++)
719 if (*p == '#' || *p == '\0')
721 if (i >= maxfields) {
722 snprintf(msg, msglen, "line has too many fields");
733 if (*p != '"' && *p != '\\' &&
735 snprintf(msg, msglen,
736 "invalid `\\' escape");
741 snprintf(msg, msglen,
742 "unterminated quoted string");
749 if (*p != '\0' && strspn(p, ws) == 0) {
750 snprintf(msg, msglen, "quoted string not"
751 " followed by white space");
766 tac_add_server(struct tac_handle *h, const char *host, int port,
767 const char *secret, int timeout, int flags)
769 struct tac_server *srvp;
771 if (h->num_servers >= MAXSERVERS) {
772 generr(h, "Too many TACACS+ servers specified");
775 srvp = &h->servers[h->num_servers];
777 memset(&srvp->addr, 0, sizeof srvp->addr);
778 srvp->addr.sin_len = sizeof srvp->addr;
779 srvp->addr.sin_family = AF_INET;
780 if (!inet_aton(host, &srvp->addr.sin_addr)) {
781 struct hostent *hent;
783 if ((hent = gethostbyname(host)) == NULL) {
784 generr(h, "%s: host not found", host);
787 memcpy(&srvp->addr.sin_addr, hent->h_addr,
788 sizeof srvp->addr.sin_addr);
790 srvp->addr.sin_port = htons(port != 0 ? port : TACPLUS_PORT);
791 if ((srvp->secret = xstrdup(h, secret)) == NULL)
793 srvp->timeout = timeout;
800 tac_close(struct tac_handle *h)
806 for (srv = 0; srv < h->num_servers; srv++) {
807 memset(h->servers[srv].secret, 0,
808 strlen(h->servers[srv].secret));
809 free(h->servers[srv].secret);
813 free_str(&h->rem_addr);
815 free_str(&h->user_msg);
816 for (i=0; i<MAXAVPAIRS; i++)
817 free_str(&(h->avs[i]));
819 /* Clear everything else before freeing memory */
820 memset(h, 0, sizeof(struct tac_handle));
825 tac_config(struct tac_handle *h, const char *path)
828 char buf[MAXCONFLINE];
833 path = PATH_TACPLUS_CONF;
834 if ((fp = fopen(path, "r")) == NULL) {
835 generr(h, "Cannot open \"%s\": %s", path, strerror(errno));
840 while (fgets(buf, sizeof buf, fp) != NULL) {
851 unsigned long timeout;
857 /* We know len > 0, else fgets would have returned NULL. */
858 if (buf[len - 1] != '\n') {
859 if (len >= sizeof buf - 1)
860 generr(h, "%s:%d: line too long", path,
863 generr(h, "%s:%d: missing newline", path,
870 /* Extract the fields from the line. */
871 nfields = split(buf, fields, 4, msg, sizeof msg);
873 generr(h, "%s:%d: %s", path, linenum, msg);
880 generr(h, "%s:%d: missing shared secret", path,
887 timeout_str = fields[2];
888 options_str = fields[3];
890 /* Parse and validate the fields. */
892 host = strsep(&res, ":");
893 port_str = strsep(&res, ":");
894 if (port_str != NULL) {
895 port = strtoul(port_str, &end, 10);
896 if (port_str[0] == '\0' || *end != '\0') {
897 generr(h, "%s:%d: invalid port", path,
904 if (timeout_str != NULL) {
905 timeout = strtoul(timeout_str, &end, 10);
906 if (timeout_str[0] == '\0' || *end != '\0') {
907 generr(h, "%s:%d: invalid timeout", path,
915 if (options_str != NULL) {
916 if (strcmp(options_str, "single-connection") == 0)
917 options |= TAC_SRVR_SINGLE_CONNECT;
919 generr(h, "%s:%d: invalid option \"%s\"",
920 path, linenum, options_str);
926 if (tac_add_server(h, host, port, secret, timeout,
930 strcpy(msg, h->errmsg);
931 generr(h, "%s:%d: %s", path, linenum, msg);
936 /* Clear out the buffer to wipe a possible copy of a shared secret */
937 memset(buf, 0, sizeof buf);
943 tac_create_authen(struct tac_handle *h, int action, int type, int service)
945 struct tac_authen_start *as;
947 create_msg(h, TAC_AUTHEN, action, type);
949 as = &h->request.u.authen_start;
951 as->priv_lvl = TAC_PRIV_LVL_USER;
952 as->authen_type = type;
953 as->service = service;
959 tac_create_author(struct tac_handle *h, int method, int type, int service)
961 struct tac_author_request *areq;
963 create_msg(h, TAC_AUTHOR, method, type);
965 areq = &h->request.u.author_request;
966 areq->authen_meth = method;
967 areq->priv_lvl = TAC_PRIV_LVL_USER;
968 areq->authen_type = type;
969 areq->service = service;
975 tac_create_acct(struct tac_handle *h, int acct, int action, int type, int service)
977 struct tac_acct_start *as;
979 create_msg(h, TAC_ACCT, action, type);
981 as = &h->request.u.acct_start;
983 as->authen_action = action;
984 as->priv_lvl = TAC_PRIV_LVL_USER;
985 as->authen_type = type;
986 as->authen_service = service;
992 create_msg(struct tac_handle *h, int msg_type, int var, int type)
1000 msg->type = msg_type;
1001 msg->version = protocol_version(msg_type, var, type);
1002 msg->flags = 0; /* encrypted packet body */
1006 free_str(&h->rem_addr);
1008 free_str(&h->user_msg);
1010 for (i=0; i<MAXAVPAIRS; i++)
1011 free_str(&(h->avs[i]));
1015 tac_get_data(struct tac_handle *h, size_t *len)
1017 return dup_str(h, &h->srvr_data, len);
1021 tac_get_msg(struct tac_handle *h)
1023 return dup_str(h, &h->srvr_msg, NULL);
1027 * Create and initialize a tac_handle structure, and return it to the
1028 * caller. Can fail only if the necessary memory cannot be allocated.
1029 * In that case, it returns NULL.
1035 struct tac_handle *h;
1037 h = (struct tac_handle *)malloc(sizeof(struct tac_handle));
1042 h->errmsg[0] = '\0';
1043 init_clnt_str(&h->user);
1044 init_clnt_str(&h->port);
1045 init_clnt_str(&h->rem_addr);
1046 init_clnt_str(&h->data);
1047 init_clnt_str(&h->user_msg);
1048 for (i=0; i<MAXAVPAIRS; i++) {
1049 init_clnt_str(&(h->avs[i]));
1050 init_srvr_str(&(h->srvr_avs[i]));
1052 init_srvr_str(&h->srvr_msg);
1053 init_srvr_str(&h->srvr_data);
1060 tac_send_authen(struct tac_handle *h)
1062 struct tac_authen_reply *ar;
1064 if (h->num_servers == 0)
1067 if (h->last_seq_no == 0) { /* Authentication START packet */
1068 struct tac_authen_start *as;
1070 as = &h->request.u.authen_start;
1072 htonl(offsetof(struct tac_authen_start, rest[0]));
1073 if (add_str_8(h, &as->user_len, &h->user) == -1 ||
1074 add_str_8(h, &as->port_len, &h->port) == -1 ||
1075 add_str_8(h, &as->rem_addr_len, &h->rem_addr) == -1 ||
1076 add_str_8(h, &as->data_len, &h->data) == -1)
1078 } else { /* Authentication CONTINUE packet */
1079 struct tac_authen_cont *ac;
1081 ac = &h->request.u.authen_cont;
1084 htonl(offsetof(struct tac_authen_cont, rest[0]));
1085 if (add_str_16(h, &ac->user_msg_len, &h->user_msg) == -1 ||
1086 add_str_16(h, &ac->data_len, &h->data) == -1)
1090 /* Send the message and retrieve the reply. */
1091 if (send_msg(h) == -1 || recv_msg(h) == -1)
1094 /* Scan the optional fields in the reply. */
1095 ar = &h->response.u.authen_reply;
1096 h->srvr_pos = offsetof(struct tac_authen_reply, rest[0]);
1097 if (get_srvr_str(h, "msg", &h->srvr_msg, ntohs(ar->msg_len)) == -1 ||
1098 get_srvr_str(h, "data", &h->srvr_data, ntohs(ar->data_len)) == -1 ||
1099 get_srvr_end(h) == -1)
1102 if (!h->single_connect &&
1103 ar->status != TAC_AUTHEN_STATUS_GETDATA &&
1104 ar->status != TAC_AUTHEN_STATUS_GETUSER &&
1105 ar->status != TAC_AUTHEN_STATUS_GETPASS)
1106 close_connection(h);
1108 return ar->flags << 8 | ar->status;
1112 tac_send_author(struct tac_handle *h)
1116 struct tac_author_request *areq = &h->request.u.author_request;
1117 struct tac_author_response *ares = &h->response.u.author_response;
1120 htonl(offsetof(struct tac_author_request, rest[0]));
1122 /* Count each specified AV pair */
1123 for (areq->av_cnt=0, i=0; i<MAXAVPAIRS; i++)
1124 if (h->avs[i].len && h->avs[i].data)
1128 * Each AV size is a byte starting right after 'av_cnt'. Update the
1129 * offset to include these AV sizes.
1131 h->request.length = ntohl(htonl(h->request.length) + areq->av_cnt);
1133 /* Now add the string arguments from 'h' */
1134 if (add_str_8(h, &areq->user_len, &h->user) == -1 ||
1135 add_str_8(h, &areq->port_len, &h->port) == -1 ||
1136 add_str_8(h, &areq->rem_addr_len, &h->rem_addr) == -1)
1139 /* Add each AV pair, the size of each placed in areq->rest[current] */
1140 for (current=0, i=0; i<MAXAVPAIRS; i++) {
1141 if (h->avs[i].len && h->avs[i].data) {
1142 if (add_str_8(h, &areq->rest[current++],
1143 &(h->avs[i])) == -1)
1148 /* Send the message and retrieve the reply. */
1149 if (send_msg(h) == -1 || recv_msg(h) == -1)
1152 /* Update the offset in the response packet based on av pairs count */
1153 h->srvr_pos = offsetof(struct tac_author_response, rest[0]) +
1156 /* Scan the optional fields in the response. */
1157 if (get_srvr_str(h, "msg", &h->srvr_msg, ntohs(ares->msg_len)) == -1 ||
1158 get_srvr_str(h, "data", &h->srvr_data, ntohs(ares->data_len)) ==-1)
1161 /* Get each AV pair (just setting pointers, not malloc'ing) */
1163 for (i=0; i<ares->av_cnt; i++) {
1164 snprintf(dbgstr, sizeof dbgstr, "av-pair-%d", i);
1165 if (get_srvr_str(h, dbgstr, &(h->srvr_avs[i]),
1166 ares->rest[i]) == -1)
1170 /* Should have ended up at the end */
1171 if (get_srvr_end(h) == -1)
1175 if (!h->single_connect)
1176 close_connection(h);
1178 return ares->av_cnt << 8 | ares->status;
1182 tac_send_acct(struct tac_handle *h)
1184 register int i, current;
1185 struct tac_acct_start *as = &h->request.u.acct_start;
1186 struct tac_acct_reply *ar = &h->response.u.acct_reply;
1189 as = &h->request.u.acct_start;
1190 h->request.length = htonl(offsetof(struct tac_acct_start, rest[0]));
1191 for (as->av_cnt = 0, i = 0; i < MAXAVPAIRS; i++)
1192 if (h->avs[i].len && h->avs[i].data)
1194 h->request.length = ntohl(htonl(h->request.length) + as->av_cnt);
1196 if (add_str_8(h, &as->user_len, &h->user) == -1 ||
1197 add_str_8(h, &as->port_len, &h->port) == -1 ||
1198 add_str_8(h, &as->rem_addr_len, &h->rem_addr) == -1)
1201 for (i = current = 0; i < MAXAVPAIRS; i++)
1202 if (h->avs[i].len && h->avs[i].data)
1203 if (add_str_8(h, &as->rest[current++], &(h->avs[i])) == -1)
1207 if (send_msg(h) == -1 || recv_msg(h) == -1)
1211 h->srvr_pos = offsetof(struct tac_acct_reply, rest[0]);
1212 if (get_srvr_str(h, "msg", &h->srvr_msg, ntohs(ar->msg_len)) == -1 ||
1213 get_srvr_str(h, "data", &h->srvr_data, ntohs(ar->data_len)) == -1 ||
1214 get_srvr_end(h) == -1)
1218 if (!h->single_connect)
1219 close_connection(h);
1225 tac_set_rem_addr(struct tac_handle *h, const char *addr)
1227 return save_str(h, &h->rem_addr, addr, addr != NULL ? strlen(addr) : 0);
1231 tac_set_data(struct tac_handle *h, const void *data, size_t data_len)
1233 return save_str(h, &h->data, data, data_len);
1237 tac_set_msg(struct tac_handle *h, const char *msg)
1239 return save_str(h, &h->user_msg, msg, msg != NULL ? strlen(msg) : 0);
1243 tac_set_port(struct tac_handle *h, const char *port)
1245 return save_str(h, &h->port, port, port != NULL ? strlen(port) : 0);
1249 tac_set_priv(struct tac_handle *h, int priv)
1251 if (!(TAC_PRIV_LVL_MIN <= priv && priv <= TAC_PRIV_LVL_MAX)) {
1252 generr(h, "Attempt to set invalid privilege level");
1255 h->request.u.authen_start.priv_lvl = priv;
1260 tac_set_user(struct tac_handle *h, const char *user)
1262 return save_str(h, &h->user, user, user != NULL ? strlen(user) : 0);
1266 tac_set_av(struct tac_handle *h, u_int index, const char *av)
1268 if (index >= MAXAVPAIRS)
1270 return save_str(h, &(h->avs[index]), av, av != NULL ? strlen(av) : 0);
1274 tac_get_av(struct tac_handle *h, u_int index)
1276 if (index >= MAXAVPAIRS)
1278 return dup_str(h, &(h->srvr_avs[index]), NULL);
1282 tac_get_av_value(struct tac_handle *h, const char *attribute)
1285 const char *ch, *end;
1286 const char *candidate;
1288 int found_seperator;
1289 struct srvr_str srvr;
1291 if (attribute == NULL || ((len = strlen(attribute)) == 0))
1294 for (i=0; i<MAXAVPAIRS; i++) {
1295 candidate = h->srvr_avs[i].data;
1296 candidate_len = h->srvr_avs[i].len;
1299 * Valid 'srvr_avs' guaranteed to be contiguous starting at
1300 * index 0 (not necessarily the case with 'avs'). Break out
1301 * when the "end" of the list has been reached.
1306 if (len < candidate_len &&
1307 !strncmp(candidate, attribute, len)) {
1309 ch = candidate + len;
1310 end = candidate + candidate_len;
1313 * Sift out the white space between A and V (should not
1314 * be any, but don't trust implementation of server...)
1316 found_seperator = 0;
1317 while ((*ch == '=' || *ch == '*' || *ch == ' ' ||
1318 *ch == '\t') && ch != end) {
1319 if (*ch == '=' || *ch == '*')
1326 * The case of 'attribute' == "foo" and
1327 * h->srvr_avs[0] = "foobie=var1"
1328 * h->srvr_avs[1] = "foo=var2"
1331 * Note that for empty string attribute values a
1332 * 0-length string is returned in order to distinguish
1333 * against unset values.
1334 * dup_str() will handle srvr.len == 0 correctly.
1336 if (found_seperator == 1) {
1337 srvr.len = end - ch;
1339 return dup_str(h, &srvr, NULL);
1347 tac_clear_avs(struct tac_handle *h)
1350 for (i=0; i<MAXAVPAIRS; i++)
1351 save_str(h, &(h->avs[i]), NULL, 0);
1355 clear_srvr_avs(struct tac_handle *h)
1358 for (i=0; i<MAXAVPAIRS; i++)
1359 init_srvr_str(&(h->srvr_avs[i]));
1364 tac_strerror(struct tac_handle *h)
1370 xmalloc(struct tac_handle *h, size_t size)
1374 if ((r = malloc(size)) == NULL)
1375 generr(h, "Out of memory");
1380 xstrdup(struct tac_handle *h, const char *s)
1384 if ((r = strdup(s)) == NULL)
1385 generr(h, "Out of memory");