| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Various handy server-only utilities |
| 4 | * |
| 5 | * (c) 2001 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Trivial IP Encryption (TrIPE). |
| 11 | * |
| 12 | * TrIPE is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * TrIPE is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with TrIPE; if not, write to the Free Software Foundation, |
| 24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | /*----- Header files ------------------------------------------------------*/ |
| 28 | |
| 29 | #include "tripe.h" |
| 30 | |
| 31 | /*----- Global variables --------------------------------------------------*/ |
| 32 | |
| 33 | octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ]; |
| 34 | |
| 35 | /*----- Main code ---------------------------------------------------------*/ |
| 36 | |
| 37 | /* --- @mpstr@ --- * |
| 38 | * |
| 39 | * Arguments: @mp *m@ = a multiprecision integer |
| 40 | * |
| 41 | * Returns: A pointer to the integer's textual representation. |
| 42 | * |
| 43 | * Use: Converts a multiprecision integer to a string. Corrupts |
| 44 | * @buf_t@. |
| 45 | */ |
| 46 | |
| 47 | const char *mpstr(mp *m) |
| 48 | { |
| 49 | if (mp_writestring(m, (char *)buf_t, sizeof(buf_t), 10)) |
| 50 | return ("<failed>"); |
| 51 | return ((const char *)buf_t); |
| 52 | } |
| 53 | |
| 54 | /* --- @gestr@ --- * |
| 55 | * |
| 56 | * Arguments: @group *g@ = a group |
| 57 | * @ge *x@ = a group element |
| 58 | * |
| 59 | * Returns: A pointer to the element's textual representation. |
| 60 | * |
| 61 | * Use: Converts a group element to a string. Corrupts |
| 62 | * @buf_t@. |
| 63 | */ |
| 64 | |
| 65 | const char *gestr(group *g, ge *x) |
| 66 | { |
| 67 | if (group_writestring(g, x, (char *)buf_t, sizeof(buf_t))) |
| 68 | return ("<failed>"); |
| 69 | return ((const char *)buf_t); |
| 70 | } |
| 71 | |
| 72 | /* --- @timestr@ --- * |
| 73 | * |
| 74 | * Arguments: @time_t t@ = a time to convert |
| 75 | * |
| 76 | * Returns: A pointer to a textual representation of the time. |
| 77 | * |
| 78 | * Use: Converts a time to a textual representation. Corrupts |
| 79 | * @buf_t@. |
| 80 | */ |
| 81 | |
| 82 | const char *timestr(time_t t) |
| 83 | { |
| 84 | struct tm *tm; |
| 85 | if (!t) |
| 86 | return ("NEVER"); |
| 87 | tm = localtime(&t); |
| 88 | strftime((char *)buf_t, sizeof(buf_t), "%Y-%m-%dT%H:%M:%S", tm); |
| 89 | return ((const char *)buf_t); |
| 90 | } |
| 91 | |
| 92 | /* --- @mystrieq@ --- * |
| 93 | * |
| 94 | * Arguments: @const char *x, *y@ = two strings |
| 95 | * |
| 96 | * Returns: True if @x@ and @y are equal, up to case. |
| 97 | */ |
| 98 | |
| 99 | int mystrieq(const char *x, const char *y) |
| 100 | { |
| 101 | for (;;) { |
| 102 | if (!*x && !*y) return (1); |
| 103 | if (tolower((unsigned char)*x) != tolower((unsigned char)*y)) |
| 104 | return (0); |
| 105 | x++; y++; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /* --- @seq_reset@ --- * |
| 110 | * |
| 111 | * Arguments: @seqwin *s@ = sequence-checking window |
| 112 | * |
| 113 | * Returns: --- |
| 114 | * |
| 115 | * Use: Resets a sequence number window. |
| 116 | */ |
| 117 | |
| 118 | void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; } |
| 119 | |
| 120 | /* --- @seq_check@ --- * |
| 121 | * |
| 122 | * Arguments: @seqwin *s@ = sequence-checking window |
| 123 | * @uint32 q@ = sequence number to check |
| 124 | * @const char *service@ = service to report message from |
| 125 | * |
| 126 | * Returns: Zero on success, nonzero if the sequence number was bad. |
| 127 | * |
| 128 | * Use: Checks a sequence number against the window, updating things |
| 129 | * as necessary. |
| 130 | */ |
| 131 | |
| 132 | int seq_check(seqwin *s, uint32 q, const char *service) |
| 133 | { |
| 134 | uint32 qbit; |
| 135 | uint32 n; |
| 136 | |
| 137 | if (q < s->seq) { |
| 138 | a_warn(service, "replay", "old-sequence", A_END); |
| 139 | return (-1); |
| 140 | } |
| 141 | if (q >= s->seq + SEQ_WINSZ) { |
| 142 | n = q - (s->seq + SEQ_WINSZ - 1); |
| 143 | if (n < SEQ_WINSZ) |
| 144 | s->win >>= n; |
| 145 | else |
| 146 | s->win = 0; |
| 147 | s->seq += n; |
| 148 | } |
| 149 | qbit = 1 << (q - s->seq); |
| 150 | if (s->win & qbit) { |
| 151 | a_warn(service, "replay", "duplicated-sequence", A_END); |
| 152 | return (-1); |
| 153 | } |
| 154 | s->win |= qbit; |
| 155 | return (0); |
| 156 | } |
| 157 | |
| 158 | /*----- That's all, folks -------------------------------------------------*/ |