diff options
Diffstat (limited to 'lib/libtinytac/lnetwork.c')
| -rw-r--r-- | lib/libtinytac/lnetwork.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/lib/libtinytac/lnetwork.c b/lib/libtinytac/lnetwork.c new file mode 100644 index 0000000..5b4ea63 --- /dev/null +++ b/lib/libtinytac/lnetwork.c | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | /* | ||
| 2 | * Tiny TACACS+ Client Library | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * 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. | ||
| 15 | * * Neither the name of David M. Syzdek nor the | ||
| 16 | * names of its contributors may be used to endorse or promote products | ||
| 17 | * derived from this software without specific prior written permission. | ||
| 18 | * | ||
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR | ||
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 29 | * SUCH DAMAGE. | ||
| 30 | */ | ||
| 31 | #define _LIB_LIBTINYTAC_LNETWORK_C 1 | ||
| 32 | #include "lnetwork.h" | ||
| 33 | |||
| 34 | |||
| 35 | /////////////// | ||
| 36 | // // | ||
| 37 | // Headers // | ||
| 38 | // // | ||
| 39 | /////////////// | ||
| 40 | #pragma mark - Headers | ||
| 41 | |||
| 42 | #include <stdlib.h> | ||
| 43 | #include <sys/socket.h> | ||
| 44 | #include <string.h> | ||
| 45 | #include <errno.h> | ||
| 46 | #include <assert.h> | ||
| 47 | |||
| 48 | |||
| 49 | /////////////////// | ||
| 50 | // // | ||
| 51 | // Definitions // | ||
| 52 | // // | ||
| 53 | /////////////////// | ||
| 54 | #pragma mark - Definitions | ||
| 55 | |||
| 56 | |||
| 57 | ////////////////// | ||
| 58 | // // | ||
| 59 | // Prototypes // | ||
| 60 | // // | ||
| 61 | ////////////////// | ||
| 62 | #pragma mark - Prototypes | ||
| 63 | |||
| 64 | int | ||
| 65 | tinytac_recv( | ||
| 66 | int s, | ||
| 67 | char * key, | ||
| 68 | tinytac_pckt_t ** pcktp ) | ||
| 69 | { | ||
| 70 | size_t pckt_len; | ||
| 71 | ssize_t rc; | ||
| 72 | tinytac_pckt_t * pckt; | ||
| 73 | void * ptr; | ||
| 74 | |||
| 75 | pckt_len = sizeof(tinytac_pckt_t); | ||
| 76 | |||
| 77 | if ((pckt = malloc(pckt_len)) == NULL) | ||
| 78 | return(-1); | ||
| 79 | |||
| 80 | if ((rc = recv(s, pckt, pckt_len, 0)) == -1) | ||
| 81 | { | ||
| 82 | free(pckt); | ||
| 83 | return(-1); | ||
| 84 | }; | ||
| 85 | if (((size_t)rc) != pckt_len) | ||
| 86 | { | ||
| 87 | free(pckt); | ||
| 88 | errno = EBADMSG; | ||
| 89 | return(-1); | ||
| 90 | }; | ||
| 91 | |||
| 92 | pckt_len = ntohl(pckt->pckt_length) + sizeof(tinytac_pckt_t); | ||
| 93 | if ((ptr = realloc(pckt, pckt_len)) == NULL) | ||
| 94 | { | ||
| 95 | free(pckt); | ||
| 96 | return(-1); | ||
| 97 | }; | ||
| 98 | pckt = ptr; | ||
| 99 | |||
| 100 | if ((rc = recv(s, pckt->pckt_body, ntohl(pckt->pckt_length), 0)) == -1) | ||
| 101 | { | ||
| 102 | free(pckt); | ||
| 103 | return(-1); | ||
| 104 | }; | ||
| 105 | if (((size_t)rc) != ntohl(pckt->pckt_length)) | ||
| 106 | { | ||
| 107 | free(pckt); | ||
| 108 | errno = EBADMSG; | ||
| 109 | return(-1); | ||
| 110 | }; | ||
| 111 | |||
| 112 | tinytac_pckt_obfuscate(pckt, key, strlen(key), TTAC_NO); | ||
| 113 | |||
| 114 | *pcktp = pckt; | ||
| 115 | |||
| 116 | return(0); | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | int | ||
| 121 | tinytac_send( | ||
| 122 | int s, | ||
| 123 | char * key, | ||
| 124 | tinytac_pckt_t * pckt ) | ||
| 125 | { | ||
| 126 | size_t pckt_len; | ||
| 127 | ssize_t rc; | ||
| 128 | tinytac_pckt_obfuscate(pckt, key, strlen(key), TTAC_YES); | ||
| 129 | pckt_len = ntohl(pckt->pckt_length) + sizeof(tinytac_pckt_t); | ||
| 130 | if ((rc = send(s, pckt, pckt_len, 0)) == -1) | ||
| 131 | return(-1); | ||
| 132 | if (((size_t)rc) != pckt_len) | ||
| 133 | { | ||
| 134 | errno = EBADMSG; | ||
| 135 | return(-1); | ||
| 136 | }; | ||
| 137 | return(0); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | /* end of source */ | ||
