chiark / gitweb /
server/: Replace the Diffie--Hellman group abstraction.
[tripe] / server / servutil.c
CommitLineData
410c8acf 1/* -*-c-*-
410c8acf 2 *
3 * Various handy server-only utilities
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 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.
e04c2d50 16 *
410c8acf 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.
e04c2d50 21 *
410c8acf 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
410c8acf 27/*----- Header files ------------------------------------------------------*/
28
29#include "tripe.h"
30
df9dfccf 31/*----- Global variables --------------------------------------------------*/
32
a4b808b0 33octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
df9dfccf 34
410c8acf 35/*----- Main code ---------------------------------------------------------*/
36
a7880467 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
a4b808b0 44 * @buf_u@.
a7880467 45 */
46
47const char *timestr(time_t t)
48{
49 struct tm *tm;
50 if (!t)
51 return ("NEVER");
52 tm = localtime(&t);
a4b808b0
MW
53 strftime((char *)buf_u, sizeof(buf_u), "%Y-%m-%dT%H:%M:%S", tm);
54 return ((const char *)buf_u);
410c8acf 55}
56
52b86648
MW
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
64int 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
37941236 74/* --- @seq_reset@ --- *
75 *
76 * Arguments: @seqwin *s@ = sequence-checking window
77 *
78 * Returns: ---
79 *
80 * Use: Resets a sequence number window.
81 */
82
83void 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
f43df819 89 * @const char *service@ = service to report message from
37941236 90 *
f43df819 91 * Returns: Zero on success, nonzero if the sequence number was bad.
37941236 92 *
93 * Use: Checks a sequence number against the window, updating things
94 * as necessary.
95 */
96
f43df819 97int seq_check(seqwin *s, uint32 q, const char *service)
37941236 98{
99 uint32 qbit;
100 uint32 n;
101
f43df819
MW
102 if (q < s->seq) {
103 a_warn(service, "replay", "old-sequence", A_END);
104 return (-1);
105 }
37941236 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);
f43df819
MW
115 if (s->win & qbit) {
116 a_warn(service, "replay", "duplicated-sequence", A_END);
117 return (-1);
118 }
37941236 119 s->win |= qbit;
120 return (0);
121}
122
410c8acf 123/*----- That's all, folks -------------------------------------------------*/