]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ftp/Parsing.cc
Major source layout change: Moved FTP code into servers/, clients/, and ftp/.
[thirdparty/squid.git] / src / ftp / Parsing.cc
CommitLineData
92ae4c86
AR
1/*
2 * DEBUG: section 09 File Transfer Protocol (FTP)
3 */
4
5#include "squid.h"
6#include "ftp/Parsing.h"
7#include "ip/Address.h"
8#include "MemBuf.h"
9#include "SquidConfig.h"
10
11bool
12Ftp::ParseIpPort(const char *buf, const char *forceIp, Ip::Address &addr)
13{
14 int h1, h2, h3, h4;
15 int p1, p2;
16 const int n = sscanf(buf, "%d,%d,%d,%d,%d,%d",
17 &h1, &h2, &h3, &h4, &p1, &p2);
18
19 if (n != 6 || p1 < 0 || p2 < 0 || p1 > 255 || p2 > 255)
20 return false;
21
22 if (forceIp) {
23 addr = forceIp; // but the above code still validates the IP we got
24 } else {
25 static char ipBuf[1024];
26 snprintf(ipBuf, sizeof(ipBuf), "%d.%d.%d.%d", h1, h2, h3, h4);
27 addr = ipBuf;
28
29 if (addr.isAnyAddr())
30 return false;
31 }
32
33 const int port = ((p1 << 8) + p2);
34
35 if (port <= 0)
36 return false;
37
38 if (Config.Ftp.sanitycheck && port < 1024)
39 return false;
40
41 addr.port(port);
42 return true;
43}
44
45bool
46Ftp::ParseProtoIpPort(const char *buf, Ip::Address &addr)
47{
48
49 const char delim = *buf;
50 const char *s = buf + 1;
51 const char *e = s;
52 const int proto = strtol(s, const_cast<char**>(&e), 10);
53 if ((proto != 1 && proto != 2) || *e != delim)
54 return false;
55
56 s = e + 1;
57 e = strchr(s, delim);
58 char ip[MAX_IPSTRLEN];
59 if (static_cast<size_t>(e - s) >= sizeof(ip))
60 return false;
61 strncpy(ip, s, e - s);
62 ip[e - s] = '\0';
63 addr = ip;
64
65 if (addr.isAnyAddr())
66 return false;
67
68 if ((proto == 2) != addr.isIPv6()) // proto ID mismatches address version
69 return false;
70
71 s = e + 1; // skip port delimiter
72 const int port = strtol(s, const_cast<char**>(&e), 10);
73 if (port < 0 || *e != '|')
74 return false;
75
76 if (Config.Ftp.sanitycheck && port < 1024)
77 return false;
78
79 addr.port(port);
80 return true;
81}
82
83const char *
84Ftp::UnescapeDoubleQuoted(const char *quotedPath)
85{
86 static MemBuf path;
87 path.reset();
88 const char *s = quotedPath;
89 if (*s == '"') {
90 ++s;
91 bool parseDone = false;
92 while (!parseDone) {
93 if (const char *e = strchr(s, '"')) {
94 path.append(s, e - s);
95 s = e + 1;
96 if (*s == '"') {
97 path.append(s, 1);
98 ++s;
99 } else
100 parseDone = true;
101 } else { //parse error
102 parseDone = true;
103 path.reset();
104 }
105 }
106 }
107 return path.content();
108}