| 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], buf_u[PKBUFSZ]; |
| 34 | |
| 35 | /*----- Main code ---------------------------------------------------------*/ |
| 36 | |
| 37 | /* --- @timestr@ --- * |
| 38 | * |
| 39 | * Arguments: @time_t t@ = a time to convert |
| 40 | * |
| 41 | * Returns: A pointer to a textual representation of the time. |
| 42 | * |
| 43 | * Use: Converts a time to a textual representation. Corrupts |
| 44 | * @buf_u@. |
| 45 | */ |
| 46 | |
| 47 | const char *timestr(time_t t) |
| 48 | { |
| 49 | struct tm *tm; |
| 50 | if (!t) |
| 51 | return ("NEVER"); |
| 52 | tm = localtime(&t); |
| 53 | strftime((char *)buf_u, sizeof(buf_u), "%Y-%m-%dT%H:%M:%S", tm); |
| 54 | return ((const char *)buf_u); |
| 55 | } |
| 56 | |
| 57 | /* --- @mystrieq@ --- * |
| 58 | * |
| 59 | * Arguments: @const char *x, *y@ = two strings |
| 60 | * |
| 61 | * Returns: True if @x@ and @y are equal, up to case. |
| 62 | */ |
| 63 | |
| 64 | int mystrieq(const char *x, const char *y) |
| 65 | { |
| 66 | for (;;) { |
| 67 | if (!*x && !*y) return (1); |
| 68 | if (tolower((unsigned char)*x) != tolower((unsigned char)*y)) |
| 69 | return (0); |
| 70 | x++; y++; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* --- @seq_reset@ --- * |
| 75 | * |
| 76 | * Arguments: @seqwin *s@ = sequence-checking window |
| 77 | * |
| 78 | * Returns: --- |
| 79 | * |
| 80 | * Use: Resets a sequence number window. |
| 81 | */ |
| 82 | |
| 83 | void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; } |
| 84 | |
| 85 | /* --- @seq_check@ --- * |
| 86 | * |
| 87 | * Arguments: @seqwin *s@ = sequence-checking window |
| 88 | * @uint32 q@ = sequence number to check |
| 89 | * @const char *service@ = service to report message from |
| 90 | * |
| 91 | * Returns: Zero on success, nonzero if the sequence number was bad. |
| 92 | * |
| 93 | * Use: Checks a sequence number against the window, updating things |
| 94 | * as necessary. |
| 95 | */ |
| 96 | |
| 97 | int seq_check(seqwin *s, uint32 q, const char *service) |
| 98 | { |
| 99 | uint32 qbit; |
| 100 | uint32 n; |
| 101 | |
| 102 | if (q < s->seq) { |
| 103 | a_warn(service, "replay", "old-sequence", A_END); |
| 104 | return (-1); |
| 105 | } |
| 106 | if (q >= s->seq + SEQ_WINSZ) { |
| 107 | n = q - (s->seq + SEQ_WINSZ - 1); |
| 108 | if (n < SEQ_WINSZ) |
| 109 | s->win >>= n; |
| 110 | else |
| 111 | s->win = 0; |
| 112 | s->seq += n; |
| 113 | } |
| 114 | qbit = 1 << (q - s->seq); |
| 115 | if (s->win & qbit) { |
| 116 | a_warn(service, "replay", "duplicated-sequence", A_END); |
| 117 | return (-1); |
| 118 | } |
| 119 | s->win |= qbit; |
| 120 | return (0); |
| 121 | } |
| 122 | |
| 123 | /*----- That's all, folks -------------------------------------------------*/ |