Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * OBEX Server |
| 4 | * |
Johan Hedberg | 00deeee | 2010-01-07 12:32:49 +0200 | [diff] [blame] | 5 | * Copyright (C) 2007-2010 Nokia Corporation |
Marcel Holtmann | 4174e90 | 2010-01-01 17:09:14 -0800 | [diff] [blame] | 6 | * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org> |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 7 | * |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | * |
| 23 | */ |
| 24 | |
Johan Hedberg | b8779d2 | 2012-12-07 12:46:04 +0200 | [diff] [blame] | 25 | #ifdef HAVE_CONFIG_H |
| 26 | #include <config.h> |
| 27 | #endif |
| 28 | |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 29 | #include <fcntl.h> |
| 30 | #include <stdio.h> |
| 31 | #include <errno.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <dirent.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/wait.h> |
| 38 | #include <unistd.h> |
Luiz Augusto von Dentz | 30d6274 | 2011-11-15 15:27:57 +0200 | [diff] [blame] | 39 | #include <inttypes.h> |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 40 | |
| 41 | #include <glib.h> |
| 42 | |
Marcel Holtmann | 7a72ebe | 2015-03-01 01:38:12 -0800 | [diff] [blame] | 43 | #include "obexd/src/obexd.h" |
| 44 | #include "obexd/src/plugin.h" |
| 45 | #include "obexd/src/log.h" |
| 46 | #include "obexd/src/obex.h" |
| 47 | #include "obexd/src/manager.h" |
| 48 | #include "obexd/src/mimetype.h" |
| 49 | #include "obexd/src/service.h" |
Luiz Augusto von Dentz | 4132366 | 2011-02-26 16:49:41 -0300 | [diff] [blame] | 50 | #include "ftp.h" |
Slawomir Bochenski | 6b5d954 | 2011-08-01 15:44:57 +0200 | [diff] [blame] | 51 | #include "filesystem.h" |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 52 | |
| 53 | #define LST_TYPE "x-obex/folder-listing" |
| 54 | #define CAP_TYPE "x-obex/capability" |
| 55 | |
Vinicius Costa Gomes | df9dd74 | 2010-05-13 19:26:42 -0300 | [diff] [blame] | 56 | static const uint8_t FTP_TARGET[TARGET_SIZE] = { |
| 57 | 0xF9, 0xEC, 0x7B, 0xC4, 0x95, 0x3C, 0x11, 0xD2, |
| 58 | 0x98, 0x4E, 0x52, 0x54, 0x00, 0xDC, 0x9E, 0x09 }; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 59 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 60 | struct ftp_session { |
| 61 | struct obex_session *os; |
Bharat Panda | 1e5a31d | 2014-10-30 20:29:24 +0530 | [diff] [blame] | 62 | struct obex_transfer *transfer; |
Vinicius Costa Gomes | df9dd74 | 2010-05-13 19:26:42 -0300 | [diff] [blame] | 63 | char *folder; |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static void set_folder(struct ftp_session *ftp, const char *new_folder) |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 67 | { |
Luiz Augusto von Dentz | 8e77304 | 2011-02-23 11:54:37 +0200 | [diff] [blame] | 68 | DBG("%p folder %s", ftp, new_folder); |
| 69 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 70 | g_free(ftp->folder); |
| 71 | |
| 72 | ftp->folder = new_folder ? g_strdup(new_folder) : NULL; |
| 73 | } |
| 74 | |
Vinicius Costa Gomes | df9dd74 | 2010-05-13 19:26:42 -0300 | [diff] [blame] | 75 | static int get_by_type(struct ftp_session *ftp, const char *type) |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 76 | { |
| 77 | struct obex_session *os = ftp->os; |
Luiz Augusto von Dentz | c178167 | 2011-11-02 15:09:50 +0200 | [diff] [blame] | 78 | const char *capability = obex_option_capability(); |
Vinicius Costa Gomes | 70bc46a | 2010-05-24 14:59:13 -0300 | [diff] [blame] | 79 | const char *name = obex_get_name(os); |
| 80 | char *path; |
| 81 | int err; |
Vinicius Costa Gomes | 171d3ae | 2010-02-26 16:32:13 -0300 | [diff] [blame] | 82 | |
Luiz Augusto von Dentz | 8e77304 | 2011-02-23 11:54:37 +0200 | [diff] [blame] | 83 | DBG("%p name %s type %s", ftp, name, type); |
| 84 | |
Vinicius Costa Gomes | 70bc46a | 2010-05-24 14:59:13 -0300 | [diff] [blame] | 85 | if (type == NULL && name == NULL) |
| 86 | return -EBADR; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 87 | |
Slawomir Bochenski | 1729ca8 | 2011-07-27 18:25:48 +0200 | [diff] [blame] | 88 | if (type != NULL && g_ascii_strcasecmp(type, CAP_TYPE) == 0) |
Vinicius Costa Gomes | 7c02864 | 2010-04-01 20:09:34 -0300 | [diff] [blame] | 89 | return obex_get_stream_start(os, capability); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 90 | |
Slawomir Bochenski | 6b5d954 | 2011-08-01 15:44:57 +0200 | [diff] [blame] | 91 | if (name != NULL && !is_filename(name)) |
| 92 | return -EBADR; |
| 93 | |
Vinicius Costa Gomes | 70bc46a | 2010-05-24 14:59:13 -0300 | [diff] [blame] | 94 | path = g_build_filename(ftp->folder, name, NULL); |
| 95 | err = obex_get_stream_start(os, path); |
| 96 | |
| 97 | g_free(path); |
| 98 | |
| 99 | return err; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 100 | } |
| 101 | |
Luiz Augusto von Dentz | 4132366 | 2011-02-26 16:49:41 -0300 | [diff] [blame] | 102 | void *ftp_connect(struct obex_session *os, int *err) |
Luiz Augusto von Dentz | 5bb6f1c | 2009-11-09 16:00:41 -0300 | [diff] [blame] | 103 | { |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 104 | struct ftp_session *ftp; |
| 105 | const char *root_folder; |
| 106 | |
Luiz Augusto von Dentz | 8e77304 | 2011-02-23 11:54:37 +0200 | [diff] [blame] | 107 | DBG(""); |
| 108 | |
Luiz Augusto von Dentz | c178167 | 2011-11-02 15:09:50 +0200 | [diff] [blame] | 109 | root_folder = obex_option_root_folder(); |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 110 | |
Claudio Takahasi | ac94e9b | 2010-02-25 17:24:59 -0300 | [diff] [blame] | 111 | manager_register_session(os); |
Luiz Augusto von Dentz | 5bb6f1c | 2009-11-09 16:00:41 -0300 | [diff] [blame] | 112 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 113 | ftp = g_new0(struct ftp_session, 1); |
| 114 | set_folder(ftp, root_folder); |
| 115 | ftp->os = os; |
| 116 | |
Vinicius Costa Gomes | 556447e | 2010-03-05 20:18:36 -0300 | [diff] [blame] | 117 | if (err) |
| 118 | *err = 0; |
| 119 | |
Bharat Panda | 1e5a31d | 2014-10-30 20:29:24 +0530 | [diff] [blame] | 120 | ftp->transfer = manager_register_transfer(os); |
| 121 | |
Luiz Augusto von Dentz | 8e77304 | 2011-02-23 11:54:37 +0200 | [diff] [blame] | 122 | DBG("session %p created", ftp); |
| 123 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 124 | return ftp; |
Luiz Augusto von Dentz | 5bb6f1c | 2009-11-09 16:00:41 -0300 | [diff] [blame] | 125 | } |
| 126 | |
Luiz Augusto von Dentz | 7c35898 | 2011-11-15 15:27:55 +0200 | [diff] [blame] | 127 | int ftp_get(struct obex_session *os, void *user_data) |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 128 | { |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 129 | struct ftp_session *ftp = user_data; |
Vinicius Costa Gomes | 171d3ae | 2010-02-26 16:32:13 -0300 | [diff] [blame] | 130 | const char *type = obex_get_type(os); |
Vinicius Costa Gomes | 6c4b6ba | 2010-03-23 19:36:09 -0300 | [diff] [blame] | 131 | int ret; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 132 | |
Luiz Augusto von Dentz | 8e77304 | 2011-02-23 11:54:37 +0200 | [diff] [blame] | 133 | DBG("%p", ftp); |
| 134 | |
Vinicius Costa Gomes | 6c4b6ba | 2010-03-23 19:36:09 -0300 | [diff] [blame] | 135 | if (ftp->folder == NULL) |
| 136 | return -ENOENT; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 137 | |
Vinicius Costa Gomes | 6c4b6ba | 2010-03-23 19:36:09 -0300 | [diff] [blame] | 138 | ret = get_by_type(ftp, type); |
| 139 | if (ret < 0) |
| 140 | return ret; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 141 | |
Bharat Panda | 1e5a31d | 2014-10-30 20:29:24 +0530 | [diff] [blame] | 142 | /* Only track progress of file transfer */ |
| 143 | if (type == NULL) |
| 144 | manager_emit_transfer_started(ftp->transfer); |
| 145 | |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 146 | return 0; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 147 | } |
| 148 | |
Vinicius Costa Gomes | df9dd74 | 2010-05-13 19:26:42 -0300 | [diff] [blame] | 149 | static int ftp_delete(struct ftp_session *ftp, const char *name) |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 150 | { |
Vinicius Costa Gomes | df9dd74 | 2010-05-13 19:26:42 -0300 | [diff] [blame] | 151 | char *path; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 152 | int ret = 0; |
| 153 | |
Luiz Augusto von Dentz | 8e77304 | 2011-02-23 11:54:37 +0200 | [diff] [blame] | 154 | DBG("%p name %s", ftp, name); |
| 155 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 156 | if (!(ftp->folder && name)) |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 157 | return -EINVAL; |
| 158 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 159 | path = g_build_filename(ftp->folder, name, NULL); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 160 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 161 | if (obex_remove(ftp->os, path) < 0) |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 162 | ret = -errno; |
| 163 | |
| 164 | g_free(path); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
Luiz Augusto von Dentz | 4132366 | 2011-02-26 16:49:41 -0300 | [diff] [blame] | 169 | int ftp_chkput(struct obex_session *os, void *user_data) |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 170 | { |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 171 | struct ftp_session *ftp = user_data; |
Vinicius Costa Gomes | df9dd74 | 2010-05-13 19:26:42 -0300 | [diff] [blame] | 172 | const char *name = obex_get_name(os); |
| 173 | char *path; |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 174 | int ret; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 175 | |
Luiz Augusto von Dentz | 8e77304 | 2011-02-23 11:54:37 +0200 | [diff] [blame] | 176 | DBG("%p name %s", ftp, name); |
| 177 | |
Luiz Augusto von Dentz | 38c2f10 | 2010-05-14 11:00:19 +0300 | [diff] [blame] | 178 | if (name == NULL) |
| 179 | return -EBADR; |
| 180 | |
Slawomir Bochenski | ed7441b | 2011-08-04 14:49:56 +0200 | [diff] [blame] | 181 | if (!is_filename(name)) |
| 182 | return -EBADR; |
| 183 | |
Claudio Takahasi | 0c2498c | 2010-02-26 15:51:54 -0300 | [diff] [blame] | 184 | if (obex_get_size(os) == OBJECT_SIZE_DELETE) |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 185 | return 0; |
| 186 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 187 | path = g_build_filename(ftp->folder, name, NULL); |
| 188 | |
Vinicius Costa Gomes | 7c02864 | 2010-04-01 20:09:34 -0300 | [diff] [blame] | 189 | ret = obex_put_stream_start(os, path); |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 190 | |
Bharat Panda | 1e5a31d | 2014-10-30 20:29:24 +0530 | [diff] [blame] | 191 | if (ret == 0) |
| 192 | manager_emit_transfer_started(ftp->transfer); |
| 193 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 194 | g_free(path); |
| 195 | |
| 196 | return ret; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 197 | } |
| 198 | |
Luiz Augusto von Dentz | 7c35898 | 2011-11-15 15:27:55 +0200 | [diff] [blame] | 199 | int ftp_put(struct obex_session *os, void *user_data) |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 200 | { |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 201 | struct ftp_session *ftp = user_data; |
Claudio Takahasi | 5ec3275 | 2010-02-26 14:36:27 -0300 | [diff] [blame] | 202 | const char *name = obex_get_name(os); |
| 203 | ssize_t size = obex_get_size(os); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 204 | |
Luiz Augusto von Dentz | 8e77304 | 2011-02-23 11:54:37 +0200 | [diff] [blame] | 205 | DBG("%p name %s size %zd", ftp, name, size); |
| 206 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 207 | if (ftp->folder == NULL) |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 208 | return -EPERM; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 209 | |
Vinicius Costa Gomes | d485fe8 | 2010-02-26 14:22:07 -0300 | [diff] [blame] | 210 | if (name == NULL) |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 211 | return -EBADR; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 212 | |
Slawomir Bochenski | 6b5d954 | 2011-08-01 15:44:57 +0200 | [diff] [blame] | 213 | if (!is_filename(name)) |
| 214 | return -EBADR; |
| 215 | |
Vinicius Costa Gomes | d485fe8 | 2010-02-26 14:22:07 -0300 | [diff] [blame] | 216 | if (size == OBJECT_SIZE_DELETE) |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 217 | return ftp_delete(ftp, name); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 218 | |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 219 | return 0; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 220 | } |
| 221 | |
Luiz Augusto von Dentz | 7c35898 | 2011-11-15 15:27:55 +0200 | [diff] [blame] | 222 | int ftp_setpath(struct obex_session *os, void *user_data) |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 223 | { |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 224 | struct ftp_session *ftp = user_data; |
Vinicius Costa Gomes | df9dd74 | 2010-05-13 19:26:42 -0300 | [diff] [blame] | 225 | const char *root_folder, *name; |
Luiz Augusto von Dentz | dcab04f | 2011-11-15 15:27:54 +0200 | [diff] [blame] | 226 | const uint8_t *nonhdr; |
Vinicius Costa Gomes | df9dd74 | 2010-05-13 19:26:42 -0300 | [diff] [blame] | 227 | char *fullname; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 228 | struct stat dstat; |
| 229 | gboolean root; |
| 230 | int err; |
| 231 | |
Luiz Augusto von Dentz | dcab04f | 2011-11-15 15:27:54 +0200 | [diff] [blame] | 232 | if (obex_get_non_header_data(os, &nonhdr) != 2) { |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 233 | error("Set path failed: flag and constants not found!"); |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 234 | return -EBADMSG; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 235 | } |
| 236 | |
Claudio Takahasi | 5ec3275 | 2010-02-26 14:36:27 -0300 | [diff] [blame] | 237 | name = obex_get_name(os); |
Luiz Augusto von Dentz | c178167 | 2011-11-02 15:09:50 +0200 | [diff] [blame] | 238 | root_folder = obex_option_root_folder(); |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 239 | root = g_str_equal(root_folder, ftp->folder); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 240 | |
Luiz Augusto von Dentz | 8e77304 | 2011-02-23 11:54:37 +0200 | [diff] [blame] | 241 | DBG("%p name %s", ftp, name); |
| 242 | |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 243 | /* Check flag "Backup" */ |
| 244 | if ((nonhdr[0] & 0x01) == 0x01) { |
Luiz Augusto Von Dentz | fc180e2 | 2010-06-17 12:06:08 +0300 | [diff] [blame] | 245 | DBG("Set to parent path"); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 246 | |
Claudio Takahasi | eaa0966 | 2010-02-26 12:21:04 -0300 | [diff] [blame] | 247 | if (root) |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 248 | return -EPERM; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 249 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 250 | fullname = g_path_get_dirname(ftp->folder); |
| 251 | set_folder(ftp, fullname); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 252 | g_free(fullname); |
| 253 | |
Luiz Augusto Von Dentz | fc180e2 | 2010-06-17 12:06:08 +0300 | [diff] [blame] | 254 | DBG("Set to parent path: %s", ftp->folder); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 255 | |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 256 | return 0; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 257 | } |
| 258 | |
Claudio Takahasi | eaa0966 | 2010-02-26 12:21:04 -0300 | [diff] [blame] | 259 | if (!name) { |
Luiz Augusto Von Dentz | fc180e2 | 2010-06-17 12:06:08 +0300 | [diff] [blame] | 260 | DBG("Set path failed: name missing!"); |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 261 | return -EINVAL; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 262 | } |
| 263 | |
Claudio Takahasi | eaa0966 | 2010-02-26 12:21:04 -0300 | [diff] [blame] | 264 | if (strlen(name) == 0) { |
Luiz Augusto Von Dentz | fc180e2 | 2010-06-17 12:06:08 +0300 | [diff] [blame] | 265 | DBG("Set to root"); |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 266 | set_folder(ftp, root_folder); |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 267 | return 0; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* Check and set to name path */ |
Slawomir Bochenski | 6b5d954 | 2011-08-01 15:44:57 +0200 | [diff] [blame] | 271 | if (!is_filename(name)) { |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 272 | error("Set path failed: name incorrect!"); |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 273 | return -EPERM; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 274 | } |
| 275 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 276 | fullname = g_build_filename(ftp->folder, name, NULL); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 277 | |
Luiz Augusto Von Dentz | fc180e2 | 2010-06-17 12:06:08 +0300 | [diff] [blame] | 278 | DBG("Fullname: %s", fullname); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 279 | |
Slawomir Bochenski | da050e8 | 2011-08-04 15:45:03 +0200 | [diff] [blame] | 280 | err = verify_path(fullname); |
Marek Kasik | 1610200 | 2016-06-30 17:01:27 -0400 | [diff] [blame] | 281 | if (err == -ENOENT) |
| 282 | goto not_found; |
Slawomir Bochenski | da050e8 | 2011-08-04 15:45:03 +0200 | [diff] [blame] | 283 | |
| 284 | if (err < 0) |
| 285 | goto done; |
| 286 | |
| 287 | err = stat(fullname, &dstat); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 288 | |
| 289 | if (err < 0) { |
Luiz Augusto Von Dentz | e265df7 | 2010-04-27 16:25:39 +0300 | [diff] [blame] | 290 | err = -errno; |
| 291 | |
| 292 | if (err == -ENOENT) |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 293 | goto not_found; |
| 294 | |
Slawomir Bochenski | da050e8 | 2011-08-04 15:45:03 +0200 | [diff] [blame] | 295 | DBG("stat: %s(%d)", strerror(-err), -err); |
Luiz Augusto Von Dentz | e265df7 | 2010-04-27 16:25:39 +0300 | [diff] [blame] | 296 | |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 297 | goto done; |
| 298 | } |
| 299 | |
| 300 | if (S_ISDIR(dstat.st_mode) && (dstat.st_mode & S_IRUSR) && |
| 301 | (dstat.st_mode & S_IXUSR)) { |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 302 | set_folder(ftp, fullname); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 303 | goto done; |
| 304 | } |
| 305 | |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 306 | err = -EPERM; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 307 | goto done; |
| 308 | |
| 309 | not_found: |
| 310 | if (nonhdr[0] != 0) { |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 311 | err = -ENOENT; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 312 | goto done; |
| 313 | } |
| 314 | |
| 315 | if (mkdir(fullname, 0755) < 0) { |
Luiz Augusto Von Dentz | e265df7 | 2010-04-27 16:25:39 +0300 | [diff] [blame] | 316 | err = -errno; |
Luiz Augusto Von Dentz | fc180e2 | 2010-06-17 12:06:08 +0300 | [diff] [blame] | 317 | DBG("mkdir: %s(%d)", strerror(-err), -err); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 318 | goto done; |
| 319 | } |
| 320 | |
Luiz Augusto Von Dentz | e265df7 | 2010-04-27 16:25:39 +0300 | [diff] [blame] | 321 | err = 0; |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 322 | set_folder(ftp, fullname); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 323 | |
| 324 | done: |
| 325 | g_free(fullname); |
Vinicius Costa Gomes | 84786d5 | 2010-03-02 21:03:54 -0300 | [diff] [blame] | 326 | return err; |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 327 | } |
| 328 | |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 329 | static gboolean is_valid_path(const char *path) |
| 330 | { |
Marcel Holtmann | c59ae99 | 2013-01-04 12:27:55 -0800 | [diff] [blame] | 331 | char **elements, **cur; |
Luiz Augusto von Dentz | 1f3e8d2 | 2011-11-02 15:09:55 +0200 | [diff] [blame] | 332 | int depth = 0; |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 333 | |
Luiz Augusto von Dentz | 1f3e8d2 | 2011-11-02 15:09:55 +0200 | [diff] [blame] | 334 | elements = g_strsplit(path, "/", 0); |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 335 | |
Luiz Augusto von Dentz | 1f3e8d2 | 2011-11-02 15:09:55 +0200 | [diff] [blame] | 336 | for (cur = elements; *cur != NULL; cur++) { |
| 337 | if (**cur == '\0' || strcmp(*cur, ".") == 0) |
| 338 | continue; |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 339 | |
Luiz Augusto von Dentz | 1f3e8d2 | 2011-11-02 15:09:55 +0200 | [diff] [blame] | 340 | if (strcmp(*cur, "..") == 0) { |
| 341 | depth--; |
| 342 | if (depth < 0) |
| 343 | break; |
| 344 | continue; |
| 345 | } |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 346 | |
Luiz Augusto von Dentz | 1f3e8d2 | 2011-11-02 15:09:55 +0200 | [diff] [blame] | 347 | depth++; |
| 348 | } |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 349 | |
Luiz Augusto von Dentz | 1f3e8d2 | 2011-11-02 15:09:55 +0200 | [diff] [blame] | 350 | g_strfreev(elements); |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 351 | |
| 352 | if (depth < 0) |
| 353 | return FALSE; |
| 354 | |
| 355 | return TRUE; |
| 356 | } |
| 357 | |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 358 | static char *ftp_build_filename(struct ftp_session *ftp, const char *destname) |
| 359 | { |
| 360 | char *filename; |
| 361 | |
| 362 | /* DestName can either be relative or absolute (FTP style) */ |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 363 | if (destname[0] == '/') |
Luiz Augusto von Dentz | c178167 | 2011-11-02 15:09:50 +0200 | [diff] [blame] | 364 | filename = g_build_filename(obex_option_root_folder(), |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 365 | destname, NULL); |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 366 | else |
| 367 | filename = g_build_filename(ftp->folder, destname, NULL); |
| 368 | |
Luiz Augusto von Dentz | c178167 | 2011-11-02 15:09:50 +0200 | [diff] [blame] | 369 | if (is_valid_path(filename + strlen(obex_option_root_folder()))) |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 370 | return filename; |
| 371 | |
| 372 | g_free(filename); |
| 373 | |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | static int ftp_copy(struct ftp_session *ftp, const char *name, |
| 378 | const char *destname) |
| 379 | { |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 380 | char *source, *destination, *destdir; |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 381 | int ret; |
| 382 | |
| 383 | DBG("%p name %s destination %s", ftp, name, destname); |
| 384 | |
| 385 | if (ftp->folder == NULL) { |
| 386 | error("No folder set"); |
| 387 | return -ENOENT; |
| 388 | } |
| 389 | |
| 390 | if (name == NULL || destname == NULL) |
| 391 | return -EINVAL; |
| 392 | |
| 393 | destination = ftp_build_filename(ftp, destname); |
| 394 | |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 395 | if (destination == NULL) |
| 396 | return -EBADR; |
| 397 | |
| 398 | destdir = g_path_get_dirname(destination); |
| 399 | ret = verify_path(destdir); |
| 400 | g_free(destdir); |
| 401 | |
| 402 | if (ret < 0) |
| 403 | return ret; |
| 404 | |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 405 | source = g_build_filename(ftp->folder, name, NULL); |
| 406 | |
| 407 | ret = obex_copy(ftp->os, source, destination); |
| 408 | |
| 409 | g_free(source); |
| 410 | g_free(destination); |
| 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | static int ftp_move(struct ftp_session *ftp, const char *name, |
| 416 | const char *destname) |
| 417 | { |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 418 | char *source, *destination, *destdir; |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 419 | int ret; |
| 420 | |
| 421 | DBG("%p name %s destname %s", ftp, name, destname); |
| 422 | |
| 423 | if (ftp->folder == NULL) { |
| 424 | error("No folder set"); |
| 425 | return -ENOENT; |
| 426 | } |
| 427 | |
| 428 | if (name == NULL || destname == NULL) |
| 429 | return -EINVAL; |
| 430 | |
| 431 | destination = ftp_build_filename(ftp, destname); |
| 432 | |
Slawomir Bochenski | c38a223 | 2011-08-05 08:40:31 +0200 | [diff] [blame] | 433 | if (destination == NULL) |
| 434 | return -EBADR; |
| 435 | |
| 436 | destdir = g_path_get_dirname(destination); |
| 437 | ret = verify_path(destdir); |
| 438 | g_free(destdir); |
| 439 | |
| 440 | if (ret < 0) |
| 441 | return ret; |
| 442 | |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 443 | source = g_build_filename(ftp->folder, name, NULL); |
| 444 | |
| 445 | ret = obex_move(ftp->os, source, destination); |
| 446 | |
| 447 | g_free(source); |
| 448 | g_free(destination); |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
Luiz Augusto von Dentz | 7c35898 | 2011-11-15 15:27:55 +0200 | [diff] [blame] | 453 | int ftp_action(struct obex_session *os, void *user_data) |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 454 | { |
| 455 | struct ftp_session *ftp = user_data; |
| 456 | const char *name, *destname; |
| 457 | uint8_t action_id; |
| 458 | |
| 459 | name = obex_get_name(os); |
Slawomir Bochenski | c32fc4f | 2011-08-09 10:44:39 +0200 | [diff] [blame] | 460 | if (name == NULL || !is_filename(name)) |
| 461 | return -EBADR; |
| 462 | |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 463 | destname = obex_get_destname(os); |
| 464 | action_id = obex_get_action_id(os); |
| 465 | |
| 466 | DBG("%p action 0x%x", ftp, action_id); |
| 467 | |
| 468 | switch (action_id) { |
| 469 | case 0x00: /* Copy Object */ |
| 470 | return ftp_copy(ftp, name, destname); |
| 471 | case 0x01: /* Move/Rename Object */ |
| 472 | return ftp_move(ftp, name, destname); |
| 473 | default: |
Luiz Augusto von Dentz | 93c9b5a | 2012-02-18 17:19:01 +0200 | [diff] [blame] | 474 | return -ENOSYS; |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Luiz Augusto von Dentz | 4132366 | 2011-02-26 16:49:41 -0300 | [diff] [blame] | 478 | void ftp_disconnect(struct obex_session *os, void *user_data) |
Luiz Augusto von Dentz | 5bb6f1c | 2009-11-09 16:00:41 -0300 | [diff] [blame] | 479 | { |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 480 | struct ftp_session *ftp = user_data; |
| 481 | |
Luiz Augusto von Dentz | 8e77304 | 2011-02-23 11:54:37 +0200 | [diff] [blame] | 482 | DBG("%p", ftp); |
| 483 | |
Claudio Takahasi | 815bfac | 2010-02-25 17:57:49 -0300 | [diff] [blame] | 484 | manager_unregister_session(os); |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 485 | |
Bharat Panda | 1e5a31d | 2014-10-30 20:29:24 +0530 | [diff] [blame] | 486 | manager_unregister_transfer(ftp->transfer); |
| 487 | |
Vinicius Costa Gomes | 4ce2d8b | 2010-03-15 14:41:47 -0300 | [diff] [blame] | 488 | g_free(ftp->folder); |
| 489 | g_free(ftp); |
Luiz Augusto von Dentz | 5bb6f1c | 2009-11-09 16:00:41 -0300 | [diff] [blame] | 490 | } |
| 491 | |
Bharat Panda | 1e5a31d | 2014-10-30 20:29:24 +0530 | [diff] [blame] | 492 | static void ftp_progress(struct obex_session *os, void *user_data) |
| 493 | { |
| 494 | struct ftp_session *ftp = user_data; |
| 495 | |
| 496 | manager_emit_transfer_progress(ftp->transfer); |
| 497 | } |
| 498 | |
Bharat Panda | 11703a4 | 2014-10-31 21:16:47 +0530 | [diff] [blame] | 499 | static void ftp_reset(struct obex_session *os, void *user_data) |
| 500 | { |
| 501 | struct ftp_session *ftp = user_data; |
| 502 | |
| 503 | manager_emit_transfer_completed(ftp->transfer); |
| 504 | } |
| 505 | |
Vinicius Costa Gomes | 539120c | 2010-03-31 18:09:51 -0300 | [diff] [blame] | 506 | static struct obex_service_driver ftp = { |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 507 | .name = "File Transfer server", |
| 508 | .service = OBEX_FTP, |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 509 | .target = FTP_TARGET, |
Forrest Zhao | 19e380e | 2009-11-16 15:17:47 +0800 | [diff] [blame] | 510 | .target_size = TARGET_SIZE, |
Luiz Augusto von Dentz | 5bb6f1c | 2009-11-09 16:00:41 -0300 | [diff] [blame] | 511 | .connect = ftp_connect, |
Bharat Panda | 1e5a31d | 2014-10-30 20:29:24 +0530 | [diff] [blame] | 512 | .progress = ftp_progress, |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 513 | .get = ftp_get, |
| 514 | .put = ftp_put, |
| 515 | .chkput = ftp_chkput, |
Luiz Augusto von Dentz | 5bb6f1c | 2009-11-09 16:00:41 -0300 | [diff] [blame] | 516 | .setpath = ftp_setpath, |
Luiz Augusto von Dentz | feeb8b3 | 2011-06-17 09:26:45 +0300 | [diff] [blame] | 517 | .action = ftp_action, |
Bharat Panda | 11703a4 | 2014-10-31 21:16:47 +0530 | [diff] [blame] | 518 | .disconnect = ftp_disconnect, |
| 519 | .reset = ftp_reset |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 520 | }; |
| 521 | |
| 522 | static int ftp_init(void) |
| 523 | { |
Luiz Augusto von Dentz | 4132366 | 2011-02-26 16:49:41 -0300 | [diff] [blame] | 524 | return obex_service_driver_register(&ftp); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static void ftp_exit(void) |
| 528 | { |
| 529 | obex_service_driver_unregister(&ftp); |
Luiz Augusto von Dentz | 67e2aa6 | 2009-11-03 11:05:00 -0300 | [diff] [blame] | 530 | } |
| 531 | |
Luiz Augusto von Dentz | 7aa82b3 | 2009-11-03 11:11:19 -0300 | [diff] [blame] | 532 | OBEX_PLUGIN_DEFINE(ftp, ftp_init, ftp_exit) |