diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-05-01 00:19:47 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-05-01 00:19:47 -0800 |
| commit | 77499b0a8d300ac1fa094fb35e012a40069f1bee (patch) | |
| tree | c088dce387bb769d1ca953f0c160f1df4fc0a00a /lib | |
| parent | d824cb828d265911dc594712748c2f28066af8e3 (diff) | |
| download | tinytac-77499b0a8d300ac1fa094fb35e012a40069f1bee.tar.gz tinytac-77499b0a8d300ac1fa094fb35e012a40069f1bee.tar.bz2 tinytac-77499b0a8d300ac1fa094fb35e012a40069f1bee.tar.xz tinytac-77499b0a8d300ac1fa094fb35e012a40069f1bee.zip | |
libtinytac: adding initial network functions
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/libtinytac/libtinytac.sym | 4 | ||||
| -rw-r--r-- | lib/libtinytac/lnetwork.c | 141 | ||||
| -rw-r--r-- | lib/libtinytac/lnetwork.h | 61 |
3 files changed, 206 insertions, 0 deletions
diff --git a/lib/libtinytac/libtinytac.sym b/lib/libtinytac/libtinytac.sym index 206db9d..16ddb84 100644 --- a/lib/libtinytac/libtinytac.sym +++ b/lib/libtinytac/libtinytac.sym | |||
| @@ -34,6 +34,10 @@ | |||
| 34 | tinytac_free | 34 | tinytac_free |
| 35 | tinytac_initialize | 35 | tinytac_initialize |
| 36 | # | 36 | # |
| 37 | # network functions | ||
| 38 | tinytac_recv | ||
| 39 | tinytac_send | ||
| 40 | # | ||
| 37 | # protocol functions | 41 | # protocol functions |
| 38 | tinytac_pckt_md5pad | 42 | tinytac_pckt_md5pad |
| 39 | tinytac_pckt_obfuscate | 43 | tinytac_pckt_obfuscate |
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 */ | ||
diff --git a/lib/libtinytac/lnetwork.h b/lib/libtinytac/lnetwork.h new file mode 100644 index 0000000..0937556 --- /dev/null +++ b/lib/libtinytac/lnetwork.h | |||
| @@ -0,0 +1,61 @@ | |||
| 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 | #ifndef _LIB_LIBTINYTAC_LNETWORK_H | ||
| 32 | #define _LIB_LIBTINYTAC_LNETWORK_H 1 | ||
| 33 | |||
| 34 | |||
| 35 | /////////////// | ||
| 36 | // // | ||
| 37 | // Headers // | ||
| 38 | // // | ||
| 39 | /////////////// | ||
| 40 | #pragma mark - Headers | ||
| 41 | |||
| 42 | #include "libtinytac.h" | ||
| 43 | |||
| 44 | |||
| 45 | /////////////////// | ||
| 46 | // // | ||
| 47 | // Definitions // | ||
| 48 | // // | ||
| 49 | /////////////////// | ||
| 50 | #pragma mark - Definitions | ||
| 51 | |||
| 52 | |||
| 53 | ////////////////// | ||
| 54 | // // | ||
| 55 | // Prototypes // | ||
| 56 | // // | ||
| 57 | ////////////////// | ||
| 58 | #pragma mark - Prototypes | ||
| 59 | |||
| 60 | |||
| 61 | #endif /* end of header */ | ||
