]> git.pond.sub.org Git - empserver/blob - src/client/secure.c
Update copyright notice
[empserver] / src / client / secure.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  secure.c: Check redir etc. to protect against tampering deity
29  *
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2007
32  */
33
34 #include <config.h>
35
36 #include <assert.h>
37 #include <string.h>
38 #include "ringbuf.h"
39 #include "secure.h"
40
41 static struct ring recent_input;
42 static size_t saved_bytes;
43
44 /*
45  * Remember line of input INP for a while.
46  * It must end with a newline.
47  * Return value is suitable for forget_input(): it makes it forget all
48  * input up to and including this line.
49  */
50 size_t
51 save_input(char *inp)
52 {
53     size_t len = strlen(inp);
54     int eol;
55
56     assert(len && inp[len - 1] == '\n');
57
58     while (ring_putm(&recent_input, inp, len) < 0) {
59         eol = ring_search(&recent_input, "\n");
60         assert(eol >= 0);
61         ring_discard(&recent_input, eol + 1);
62     }
63     saved_bytes += len;
64     return saved_bytes;
65 }
66
67 /*
68  * Can you still remember a line of input that ends with TAIL?
69  * It must end with a newline.
70  * Return non-zero iff TAIL can be remembered.
71  * Passing that value to forget_input() will forget all input up to
72  * and including this line.
73  */
74 size_t
75 seen_input(char *tail)
76 {
77     size_t len = strlen(tail);
78     size_t remembered = ring_len(&recent_input);
79     int dist;
80
81     assert(len && tail[len - 1] == '\n');
82
83     dist = ring_search(&recent_input, tail);
84     if (dist < 0)
85         return 0;
86
87     assert(dist + len <= remembered && remembered <= saved_bytes);
88     return saved_bytes - remembered + dist + len;
89 }
90
91 /*
92  * Forget remembered input up to SEEN.
93  * SEEN should be obtained from save_input() or seen_input().
94  */
95 void
96 forget_input(size_t seen)
97 {
98     size_t forgotten = saved_bytes - ring_len(&recent_input);
99
100     assert(seen);
101
102     if (seen > forgotten) {
103         assert(ring_peek(&recent_input, seen - forgotten - 1) == '\n');
104         ring_discard(&recent_input, seen - forgotten);
105     }
106 }