1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | #include <unistd.h> |
22 | #include <errno(*__errno_location ()).h> |
23 | #include <string.h> |
24 | #include <arpa/telnet.h> |
25 | |
26 | #include <ftpconn.h> |
27 | #include "priv.h" |
28 |
|
29 | |
30 | static error_t |
31 | _write (int fd, const void *buf, size_t len) |
32 | { |
33 | while (len > 0) |
34 | { |
35 | ssize_t wr = write (fd, buf, len); |
36 | if (wr < 0) |
37 | return errno(*__errno_location ()); |
38 | else if (wr == 0) |
39 | return EPIPE((0x10 << 26) | ((32) & 0x3fff)); |
40 | buf += wr; |
41 | len -= wr; |
42 | } |
43 | return 0; |
44 | } |
45 | |
46 | static error_t |
47 | _skip_write (int fd, const void *buf, size_t len, size_t *skip) |
48 | { |
49 | size_t sk = *skip; |
50 | error_t err = 0; |
51 | |
52 | if (len > sk) |
53 | { |
54 | err = _write (fd, buf + sk, len - sk); |
55 | *skip = 0; |
56 | } |
57 | else |
58 | *skip = sk - len; |
59 | |
60 | return err; |
61 | } |
62 | |
63 | |
64 | |
65 | |
66 | static error_t |
67 | _long_cmd (int fd, const char *cmd, const char *arg, size_t skip) |
68 | { |
69 | error_t err = _skip_write (fd, cmd, strlen (cmd), &skip); |
70 | if (!err && arg) |
71 | { |
72 | err = _skip_write (fd, " ", 1, &skip); |
73 | if (! err) |
74 | err = _skip_write (fd, arg, strlen (arg), &skip); |
75 | } |
76 | if (! err) |
77 | err = _skip_write (fd, "\r\n", 2, &skip); |
78 | return err; |
79 | } |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | error_t |
86 | ftp_conn_cmd (struct ftp_conn *conn, const char *cmd, const char *arg, |
87 | int *reply, const char **reply_txt) |
88 | { |
89 | error_t err = 0; |
90 | |
91 | if (conn->control < 0) |
92 | err = EPIPE((0x10 << 26) | ((32) & 0x3fff)); |
93 | else |
94 | |
95 | |
96 | { |
97 | char buf[200]; |
98 | size_t out = |
99 | snprintf (buf, sizeof buf, arg ? "%s %s\r\n" : "%s\r\n", cmd, arg); |
100 | err = _write (conn->control, buf, out); |
101 | |
102 | if (!err && conn->hooks && conn->hooks->cntl_debug) |
103 | { |
104 | buf[out - 2] = '\0'; |
105 | (* conn->hooks->cntl_debug) (conn, FTP_CONN_CNTL_DEBUG_CMD1, buf); |
106 | } |
107 | |
108 | if (!err && out == sizeof buf) |
109 | err = _long_cmd (conn->control, cmd, arg, sizeof buf); |
110 | } |
111 | |
112 | if (!err && (reply || reply_txt)) |
113 | err = ftp_conn_get_reply (conn, reply, reply_txt); |
114 | |
115 | return err; |
116 | } |
117 | |
118 | |
119 | |
120 | |
121 | error_t |
122 | ftp_conn_cmd_reopen (struct ftp_conn *conn, const char *cmd, const char *arg, |
123 | int *reply, const char **reply_txt) |
124 | { |
125 | int _reply; |
126 | error_t err; |
127 | |
128 | err = ftp_conn_cmd (conn, cmd, arg, &_reply, reply_txt); |
129 | if (err == EPIPE((0x10 << 26) | ((32) & 0x3fff)) || (!err && _reply == REPLY_CLOSED421)) |
130 | |
131 | { |
132 | err = ftp_conn_open (conn); |
133 | if (! err) |
134 | err = ftp_conn_cmd (conn, cmd, arg, reply, reply_txt); |
135 | } |
136 | else if (reply) |
137 | *reply = _reply; |
138 | |
139 | return err; |
140 | } |
141 | |
142 | |
143 | |
144 | void |
145 | ftp_conn_abort (struct ftp_conn *conn) |
146 | { |
147 | if (conn->control >= 0) |
148 | { |
149 | static const char ip[] = { IAC255, IP244, IAC255 }; |
150 | static const char abor[] = { DM242, 'a', 'b', 'o', 'r', '\r', '\n' }; |
151 | |
152 | if (conn->hooks && conn->hooks->cntl_debug) |
153 | (* conn->hooks->cntl_debug) (conn, FTP_CONN_CNTL_DEBUG_CMD1, "abor"); |
154 | |
155 | if (send (conn->control, ip, sizeof ip, MSG_OOBMSG_OOB) == sizeof ip |
156 | && write (conn->control, abor, sizeof abor) == sizeof abor) |
157 | { |
158 | int reply; |
159 | error_t err; |
160 | do |
161 | err = ftp_conn_get_raw_reply (conn, &reply, 0); |
| Value stored to 'err' is never read |
162 | while (reply == REPLY_ABORTED426); |
163 | if (reply != REPLY_TRANS_OK226 && reply != REPLY_ABORT_OK225) |
164 | ftp_conn_close (conn); |
165 | } |
166 | else |
167 | ftp_conn_close (conn); |
168 | } |
169 | } |