1 |
|
|
2 |
/* Copyright (C) 1999 Beau Kuiper |
/* Copyright (C) 1999 Beau Kuiper |
3 |
|
|
4 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
20 |
/* this works correctly for all cased because rootdir has already been |
/* this works correctly for all cased because rootdir has already been |
21 |
corrected using getcwd */ |
corrected using getcwd */ |
22 |
|
|
23 |
char *dir_getreal(FTPSTATE *peer) |
char *dir_getreal(FTPSTATE * peer) |
24 |
{ |
{ |
25 |
int a; |
int a; |
26 |
char *dir = getcwd2(); |
char *dir = getcwd2(); |
27 |
|
|
28 |
if (dir == NULL) |
if (dir == NULL) |
29 |
return(NULL); |
return (NULL); |
30 |
|
|
31 |
a = strlen(peer->basedir); |
a = strlen(peer->basedir); |
32 |
|
|
33 |
if (strncmp(peer->basedir, dir, a) == 0) |
if (strncmp(peer->basedir, dir, a) == 0) |
34 |
return(dir); |
return (dir); |
35 |
|
|
36 |
freewrapper(dir); |
freewrapper(dir); |
37 |
errno = EACCES; |
errno = EACCES; |
38 |
return(NULL); |
return (NULL); |
39 |
} |
} |
40 |
|
|
41 |
/* a quicker way to get the virtual dir, replaces both |
/* a quicker way to get the virtual dir, replaces both |
42 |
dir_toreal and dir_tovirtual. Note that the caller |
dir_toreal and dir_tovirtual. Note that the caller |
43 |
must copy the result if it want's to change the |
must copy the result if it want's to change the |
44 |
source */ |
source */ |
45 |
|
|
46 |
char *dir_getvirtual(FTPSTATE *peer, char *normaldir) |
char *dir_getvirtual(FTPSTATE * peer, |
47 |
|
char *normaldir) |
48 |
{ |
{ |
49 |
int a, b; |
int a, b; |
50 |
|
|
51 |
a = strlen(peer->basedir); |
a = strlen(peer->basedir); |
52 |
b = strlen(normaldir); |
b = strlen(normaldir); |
53 |
|
|
54 |
if (a == 0) |
if (a == 0) |
55 |
return(normaldir); |
return (normaldir); |
56 |
|
|
57 |
if (b == a) |
if (b == a) |
58 |
return("/"); |
return ("/"); |
59 |
|
|
60 |
return(normaldir + a); |
return (normaldir + a); |
61 |
} |
} |
62 |
|
|
63 |
void dir_combine(FTPSTATE *peer, char **pwd, char *newdir) |
void dir_combine(FTPSTATE * peer, |
64 |
|
char **pwd, |
65 |
|
char *newdir) |
66 |
{ |
{ |
67 |
char *moddir, *nextpos, *curpos; |
char *moddir, *nextpos, *curpos; |
68 |
|
|
69 |
reallocwrapper(strlen(newdir) + strlen(*pwd) |
reallocwrapper(strlen(newdir) + strlen(*pwd) |
70 |
+ strlen(peer->homedir) + 3, (void *)pwd); |
+ strlen(peer->homedir) + 3, (void *) pwd); |
71 |
|
|
72 |
moddir = *pwd + strlen(peer->basedir); |
moddir = *pwd + strlen(peer->basedir); |
73 |
|
|
74 |
/* if the directory is /, set it to nothing */ |
/* |
75 |
|
* if the directory is /, set it to nothing |
76 |
|
*/ |
77 |
if (moddir[1] == 0) |
if (moddir[1] == 0) |
78 |
moddir[0] = 0; |
moddir[0] = 0; |
79 |
|
|
80 |
if (newdir[0] == '/') /* absolute filename */ |
if (newdir[0] == '/') /* absolute filename */ |
81 |
{ |
{ |
82 |
/* make newdir relative and clear result */ |
/* |
83 |
|
* make newdir relative and clear result |
84 |
|
*/ |
85 |
newdir++; |
newdir++; |
86 |
moddir[0] = 0; |
moddir[0] = 0; |
87 |
} |
} |
92 |
if (newdir[0] == '/') |
if (newdir[0] == '/') |
93 |
newdir++; |
newdir++; |
94 |
} |
} |
95 |
|
|
96 |
curpos = newdir; |
curpos = newdir; |
97 |
|
|
98 |
do |
do |
99 |
{ |
{ |
100 |
nextpos = strchr(curpos, '/'); |
nextpos = strchr(curpos, '/'); |
101 |
if (nextpos != NULL) |
if (nextpos != NULL) |
106 |
else if (strcmp(curpos, "..") == 0) |
else if (strcmp(curpos, "..") == 0) |
107 |
{ |
{ |
108 |
char *newpos = strrchr(moddir, '/'); |
char *newpos = strrchr(moddir, '/'); |
109 |
|
|
110 |
if (newpos) |
if (newpos) |
111 |
*newpos = 0; |
*newpos = 0; |
112 |
} |
} |
115 |
strcat(moddir, "/"); |
strcat(moddir, "/"); |
116 |
strcat(moddir, curpos); |
strcat(moddir, curpos); |
117 |
} |
} |
118 |
|
|
119 |
if (nextpos) |
if (nextpos) |
120 |
{ |
{ |
121 |
/* repair earlier damage, set next component */ |
/* |
122 |
|
* repair earlier damage, set next component |
123 |
|
*/ |
124 |
*nextpos = '/'; |
*nextpos = '/'; |
125 |
curpos = nextpos + 1; |
curpos = nextpos + 1; |
126 |
} |
} |
127 |
else |
else |
128 |
curpos = NULL; |
curpos = NULL; |
129 |
} while (curpos); |
} while (curpos); |
130 |
|
|
131 |
/* if the result is nothing, then it means root dir, so say so */ |
/* |
132 |
|
* if the result is nothing, then it means root dir, so say so |
133 |
|
*/ |
134 |
if ((*pwd)[0] == 0) |
if ((*pwd)[0] == 0) |
135 |
strcpy(*pwd, "/"); |
strcpy(*pwd, "/"); |
136 |
} |
} |