File: dedot.c

package info (click to toggle)
gopher 2.3-2
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 2,364 kB
  • ctags: 2,030
  • sloc: ansic: 22,451; perl: 1,950; sh: 1,510; makefile: 397; asm: 1
file content (182 lines) | stat: -rw-r--r-- 5,382 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/********************************************************************
 * $Author: lindner $
 * $Revision: 3.10 $
 * $Date: 1995/09/25 05:02:32 $
 * $Source: /home/arcwelder/GopherSrc/CVS/gopher+/gopherd/dedot.c,v $
 * $State: Exp $
 *
 * Paul Lindner, University of Minnesota CIS.
 *
 * Copyright 1991, 1992 by the Regents of the University of Minnesota
 * see the file "Copyright" in the distribution for conditions of use.
 *********************************************************************
 * MODULE: dedot.c
 * See below.
 *********************************************************************
 * Revision History:
 * $Log: dedot.c,v $
 * Revision 3.10  1995/09/25  05:02:32  lindner
 * Convert to ANSI C
 *
 * Revision 3.9  1994/04/22  03:35:16  lindner
 * Fix for spinning gopherd processes
 *
 * Revision 3.8  1994/04/14  15:48:52  lindner
 * Fix for files with single quotes
 *
 * Revision 3.7  1994/03/04  23:25:14  lindner
 * Fix for dedot weirdness
 *
 * Revision 3.6  1993/09/18  03:26:38  lindner
 * Important Security fix
 *
 * Revision 3.5  1993/08/23  18:33:57  lindner
 * Fix off by one error
 *
 * Revision 3.4  1993/08/06  14:28:14  lindner
 * Further strip out single quotes
 *
 * Revision 3.3  1993/08/05  22:19:43  lindner
 * Fix for single quotes
 *
 * Revision 3.2  1993/08/05  20:43:07  lindner
 * Added fix for dedot to remove quotes for when using system or popen
 *
 * Revision 3.1.1.1  1993/02/11  18:02:50  lindner
 * Gopher+1.2beta release
 *
 * Revision 1.1  1992/12/10  23:13:27  lindner
 * gopher 1.1 release
 *
 *
 *********************************************************************/


#include <stdio.h>

/*
** These routines can be used to remove ../ and ./ entries from
** pathnames.  These routines do it radically, without any attempt
** at all at interpretation.  This is okay for a gopher server,
** because clients aren't supposed to ask for .. or . in a request.
**
** We want to do this so that we can avoid the chroot(), so that we
** can have symbolic links under the gopher directory to other things
** (like man pages for example) outside our structure, and still be
** safe.  Unless of course someone makes a link to / or somewhere silly.
**
** dedot1 will remove dots from one string in place, dedot2 will copy
** one to the other, removing in the process - the src and dst can be
** the same.  dedot1 can use this to advantage - it checks first if
** things have to be removed, and calls dedot2 if necessary.  This
** is a slight advantage because in most cases we won't do anything,
** and we save the expense of copying data back and forth.  This
** seems to save almost half the time (very loose testing though).
**
** John Sellens jmsellens@watdragon.waterloo.edu
*/

void dequote1(char *);
void dedot1(char *);
void dedot2(char *, char *);


void
dequote1(char *src)
{
     char *cp2; 

     /** Strip out the quotes.. **/
     while (*src != '\0') {
	  if (*src == '"' || *src == '\'') {
	       for (cp2=src; *cp2 != '\0'; cp2++) {
		    *cp2 = *(cp2+1);
	       }
	  }
	  src++;
     }
}

void
dedot1(char *src )
{
     dequote1(src);
     if ( *src == '.' ) {
	  if (src[1] == '\0' || src[1] == '/' ||
	      ( src[1] == '.' && ( src[2] == '\0' || src[2] == '/' ) ) ) {
	       dedot2( src, src );
	       return;
	  }
     }
     while ( *src ) {
	  if ( *src++ == '/' ) {
	       if ( *src == '.' ) {
		    if (src[1] == '\0' || src[1] == '/' ||
			( src[1] == '.' && ( src[2] == '\0' || src[2] == '/' ) ) ) {
			 dedot2( src, src );
			 return;
		    }
	       }
	  }
     }
     return;
}

/* copy src to dst, blindly removing (not interpreting) ./ and ../ */

void
dedot2(char *src, char *dst)
{
     /*
      ** We either have /, a filename, ./ or ../
      */
     int i;

     while ( *src ) {
          switch ( *src ) {
          case '/':
               /* copy it, and skip any extras e.g. /a///b */
               *dst++ = *src++;
               while ( *src == '/' )
                    src++;
               break;
          case '.':
               /* don't forget about trailing . and .. */

               /* Nuke any quotes */
               for (i=1; src[i] == '"' || src[i] == '\''; i++)
                    ;
               
               if ( src[i] == '/'  ) {          /* ./ */
                    src += i+1;
                    break;
               }
               if ( src[i] == '\0'  ) { /* .\0 */
                    src += i;   /* only 1 so we don't fall off the end */
                    break;
               }
               if ( src[i] == '.' &&
                    (src[i+1] == '"' || src[i+1] == '\'')) { /* ..", ..' */
                    src += i+2;
                    break;
               }
               if ( src[i] == '.' && src[i+1] == '/' ) { /* ../ */
                    src += i+2;
                    break;
               }
               if ( src[i] == '.' && src[i+1] == '\0' ) {       /* .. */
                    src += i+1; /* don't fall off the end */
                    break;
               }
               /* must be a filename - fall through */
          default:
               /* must be filename - copy it over */
               while ( *src != '\0' && *src != '/')
		    /** Note quotes are taken care of by Gpopen **/
                    *dst++ = *src++;
               break;
          }
     }
     /* and terminate it */
     *dst = '\0';
}