pcsuite.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * OBEX Server
  5. *
  6. * Copyright (C) 2007-2010 Nokia Corporation
  7. * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #include <fcntl.h>
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <dirent.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. #include <unistd.h>
  24. #include <inttypes.h>
  25. #include <glib.h>
  26. #include "gdbus/gdbus.h"
  27. #include "obexd/src/obexd.h"
  28. #include "obexd/src/plugin.h"
  29. #include "obexd/src/log.h"
  30. #include "obexd/src/obex.h"
  31. #include "obexd/src/mimetype.h"
  32. #include "obexd/src/service.h"
  33. #include "ftp.h"
  34. #define PCSUITE_CHANNEL 24
  35. #define PCSUITE_WHO_SIZE 8
  36. #define PCSUITE_RECORD "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \
  37. <record> \
  38. <attribute id=\"0x0001\"> \
  39. <sequence> \
  40. <uuid value=\"00005005-0000-1000-8000-0002ee000001\"/> \
  41. </sequence> \
  42. </attribute> \
  43. \
  44. <attribute id=\"0x0004\"> \
  45. <sequence> \
  46. <sequence> \
  47. <uuid value=\"0x0100\"/> \
  48. </sequence> \
  49. <sequence> \
  50. <uuid value=\"0x0003\"/> \
  51. <uint8 value=\"%u\" name=\"channel\"/> \
  52. </sequence> \
  53. <sequence> \
  54. <uuid value=\"0x0008\"/> \
  55. </sequence> \
  56. </sequence> \
  57. </attribute> \
  58. \
  59. <attribute id=\"0x0005\"> \
  60. <sequence> \
  61. <uuid value=\"0x1002\"/> \
  62. </sequence> \
  63. </attribute> \
  64. \
  65. <attribute id=\"0x0009\"> \
  66. <sequence> \
  67. <sequence> \
  68. <uuid value=\"00005005-0000-1000-8000-0002ee000001\"/> \
  69. <uint16 value=\"0x0100\" name=\"version\"/> \
  70. </sequence> \
  71. </sequence> \
  72. </attribute> \
  73. \
  74. <attribute id=\"0x0100\"> \
  75. <text value=\"%s\" name=\"name\"/> \
  76. </attribute> \
  77. </record>"
  78. #define BACKUP_BUS_NAME "com.nokia.backup.plugin"
  79. #define BACKUP_PATH "/com/nokia/backup"
  80. #define BACKUP_PLUGIN_INTERFACE "com.nokia.backup.plugin"
  81. #define BACKUP_DBUS_TIMEOUT (1000 * 60 * 15)
  82. static const uint8_t FTP_TARGET[TARGET_SIZE] = {
  83. 0xF9, 0xEC, 0x7B, 0xC4, 0x95, 0x3C, 0x11, 0xD2,
  84. 0x98, 0x4E, 0x52, 0x54, 0x00, 0xDC, 0x9E, 0x09 };
  85. static const uint8_t PCSUITE_WHO[PCSUITE_WHO_SIZE] = {
  86. 'P', 'C', ' ', 'S', 'u', 'i', 't', 'e' };
  87. struct pcsuite_session {
  88. struct ftp_session *ftp;
  89. char *lock_file;
  90. int fd;
  91. };
  92. static void *pcsuite_connect(struct obex_session *os, int *err)
  93. {
  94. struct pcsuite_session *pcsuite;
  95. struct ftp_session *ftp;
  96. int fd;
  97. char *filename;
  98. DBG("");
  99. ftp = ftp_connect(os, err);
  100. if (ftp == NULL)
  101. return NULL;
  102. filename = g_build_filename(g_get_home_dir(), ".pcsuite", NULL);
  103. fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
  104. if (fd < 0 && errno != EEXIST) {
  105. error("open(%s): %s(%d)", filename, strerror(errno), errno);
  106. goto fail;
  107. }
  108. /* Try to remove the file before retrying since it could be
  109. that some process left/crash without removing it */
  110. if (fd < 0) {
  111. if (remove(filename) < 0) {
  112. error("remove(%s): %s(%d)", filename, strerror(errno),
  113. errno);
  114. goto fail;
  115. }
  116. fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
  117. if (fd < 0) {
  118. error("open(%s): %s(%d)", filename, strerror(errno),
  119. errno);
  120. goto fail;
  121. }
  122. }
  123. DBG("%s created", filename);
  124. pcsuite = g_new0(struct pcsuite_session, 1);
  125. pcsuite->ftp = ftp;
  126. pcsuite->lock_file = filename;
  127. pcsuite->fd = fd;
  128. DBG("session %p created", pcsuite);
  129. if (err)
  130. *err = 0;
  131. return pcsuite;
  132. fail:
  133. if (ftp)
  134. ftp_disconnect(os, ftp);
  135. if (err)
  136. *err = -errno;
  137. g_free(filename);
  138. return NULL;
  139. }
  140. static int pcsuite_get(struct obex_session *os, void *user_data)
  141. {
  142. struct pcsuite_session *pcsuite = user_data;
  143. DBG("%p", pcsuite);
  144. return ftp_get(os, pcsuite->ftp);
  145. }
  146. static int pcsuite_chkput(struct obex_session *os, void *user_data)
  147. {
  148. struct pcsuite_session *pcsuite = user_data;
  149. DBG("%p", pcsuite);
  150. return ftp_chkput(os, pcsuite->ftp);
  151. }
  152. static int pcsuite_put(struct obex_session *os, void *user_data)
  153. {
  154. struct pcsuite_session *pcsuite = user_data;
  155. DBG("%p", pcsuite);
  156. return ftp_put(os, pcsuite->ftp);
  157. }
  158. static int pcsuite_setpath(struct obex_session *os, void *user_data)
  159. {
  160. struct pcsuite_session *pcsuite = user_data;
  161. DBG("%p", pcsuite);
  162. return ftp_setpath(os, pcsuite->ftp);
  163. }
  164. static int pcsuite_action(struct obex_session *os, void *user_data)
  165. {
  166. struct pcsuite_session *pcsuite = user_data;
  167. DBG("%p", pcsuite);
  168. return ftp_action(os, pcsuite->ftp);
  169. }
  170. static void pcsuite_disconnect(struct obex_session *os, void *user_data)
  171. {
  172. struct pcsuite_session *pcsuite = user_data;
  173. DBG("%p", pcsuite);
  174. if (pcsuite->fd >= 0)
  175. close(pcsuite->fd);
  176. if (pcsuite->lock_file) {
  177. remove(pcsuite->lock_file);
  178. g_free(pcsuite->lock_file);
  179. }
  180. if (pcsuite->ftp)
  181. ftp_disconnect(os, pcsuite->ftp);
  182. g_free(pcsuite);
  183. }
  184. static struct obex_service_driver pcsuite = {
  185. .name = "Nokia OBEX PC Suite Services",
  186. .service = OBEX_PCSUITE,
  187. .channel = PCSUITE_CHANNEL,
  188. .secure = TRUE,
  189. .record = PCSUITE_RECORD,
  190. .target = FTP_TARGET,
  191. .target_size = TARGET_SIZE,
  192. .who = PCSUITE_WHO,
  193. .who_size = PCSUITE_WHO_SIZE,
  194. .connect = pcsuite_connect,
  195. .get = pcsuite_get,
  196. .put = pcsuite_put,
  197. .chkput = pcsuite_chkput,
  198. .setpath = pcsuite_setpath,
  199. .action = pcsuite_action,
  200. .disconnect = pcsuite_disconnect
  201. };
  202. struct backup_object {
  203. char *cmd;
  204. int fd;
  205. int oflag;
  206. int error_code;
  207. mode_t mode;
  208. DBusPendingCall *pending_call;
  209. DBusConnection *conn;
  210. };
  211. static void on_backup_dbus_notify(DBusPendingCall *pending_call,
  212. void *user_data)
  213. {
  214. struct backup_object *obj = user_data;
  215. DBusMessage *reply;
  216. const char *filename;
  217. int error_code;
  218. DBG("Notification received for pending call - %s", obj->cmd);
  219. reply = dbus_pending_call_steal_reply(pending_call);
  220. if (reply && dbus_message_get_args(reply, NULL, DBUS_TYPE_INT32,
  221. &error_code, DBUS_TYPE_STRING,
  222. &filename, DBUS_TYPE_INVALID)) {
  223. obj->error_code = error_code;
  224. if (filename) {
  225. DBG("Notification - file path = %s, error_code = %d",
  226. filename, error_code);
  227. if (error_code == 0)
  228. obj->fd = open(filename,obj->oflag,obj->mode);
  229. }
  230. } else
  231. DBG("Notification timed out or connection got closed");
  232. if (reply)
  233. dbus_message_unref(reply);
  234. dbus_pending_call_unref(pending_call);
  235. obj->pending_call = NULL;
  236. dbus_connection_unref(obj->conn);
  237. obj->conn = NULL;
  238. if (obj->fd >= 0) {
  239. DBG("File opened, setting io flags, cmd = %s",
  240. obj->cmd);
  241. if (obj->oflag == O_RDONLY)
  242. obex_object_set_io_flags(user_data, G_IO_IN, 0);
  243. else
  244. obex_object_set_io_flags(user_data, G_IO_OUT, 0);
  245. } else {
  246. DBG("File open error, setting io error, cmd = %s",
  247. obj->cmd);
  248. obex_object_set_io_flags(user_data, G_IO_ERR, -EPERM);
  249. }
  250. }
  251. static gboolean send_backup_dbus_message(const char *oper,
  252. struct backup_object *obj,
  253. size_t *size)
  254. {
  255. DBusConnection *conn;
  256. DBusMessage *msg;
  257. DBusPendingCall *pending_call;
  258. gboolean ret = FALSE;
  259. dbus_uint32_t file_size;
  260. file_size = size ? *size : 0;
  261. conn = g_dbus_setup_bus(DBUS_BUS_SESSION, NULL, NULL);
  262. if (conn == NULL)
  263. return FALSE;
  264. msg = dbus_message_new_method_call(BACKUP_BUS_NAME, BACKUP_PATH,
  265. BACKUP_PLUGIN_INTERFACE,
  266. "request");
  267. if (msg == NULL) {
  268. dbus_connection_unref(conn);
  269. return FALSE;
  270. }
  271. dbus_message_append_args(msg, DBUS_TYPE_STRING, &oper,
  272. DBUS_TYPE_STRING, &obj->cmd,
  273. DBUS_TYPE_INT32, &file_size,
  274. DBUS_TYPE_INVALID);
  275. if (strcmp(oper, "open") == 0) {
  276. ret = g_dbus_send_message_with_reply(conn, msg, &pending_call,
  277. BACKUP_DBUS_TIMEOUT);
  278. dbus_message_unref(msg);
  279. if (ret) {
  280. obj->conn = conn;
  281. obj->pending_call = pending_call;
  282. ret = dbus_pending_call_set_notify(pending_call,
  283. on_backup_dbus_notify,
  284. obj, NULL);
  285. } else
  286. dbus_connection_unref(conn);
  287. } else {
  288. g_dbus_send_message(conn, msg);
  289. dbus_connection_unref(conn);
  290. }
  291. return ret;
  292. }
  293. static void *backup_open(const char *name, int oflag, mode_t mode,
  294. void *context, size_t *size, int *err)
  295. {
  296. struct backup_object *obj = g_new0(struct backup_object, 1);
  297. DBG("cmd = %s", name);
  298. obj->cmd = g_path_get_basename(name);
  299. obj->oflag = oflag;
  300. obj->mode = mode;
  301. obj->fd = -1;
  302. obj->pending_call = NULL;
  303. obj->conn = NULL;
  304. obj->error_code = 0;
  305. if (send_backup_dbus_message("open", obj, size) == FALSE) {
  306. g_free(obj);
  307. obj = NULL;
  308. }
  309. if (err)
  310. *err = 0;
  311. return obj;
  312. }
  313. static int backup_close(void *object)
  314. {
  315. struct backup_object *obj = object;
  316. size_t size = 0;
  317. DBG("cmd = %s", obj->cmd);
  318. if (obj->fd != -1)
  319. close(obj->fd);
  320. if (obj->pending_call) {
  321. dbus_pending_call_cancel(obj->pending_call);
  322. dbus_pending_call_unref(obj->pending_call);
  323. dbus_connection_unref(obj->conn);
  324. }
  325. send_backup_dbus_message("close", obj, &size);
  326. g_free(obj->cmd);
  327. g_free(obj);
  328. return 0;
  329. }
  330. static ssize_t backup_read(void *object, void *buf, size_t count)
  331. {
  332. struct backup_object *obj = object;
  333. ssize_t ret = 0;
  334. if (obj->pending_call) {
  335. DBG("cmd = %s, IN WAITING STAGE", obj->cmd);
  336. return -EAGAIN;
  337. }
  338. if (obj->fd != -1) {
  339. DBG("cmd = %s, READING DATA", obj->cmd);
  340. ret = read(obj->fd, buf, count);
  341. if (ret < 0)
  342. ret = -errno;
  343. } else {
  344. DBG("cmd = %s, PERMANENT FAILURE", obj->cmd);
  345. ret = obj->error_code ? -obj->error_code : -ENOENT;
  346. }
  347. return ret;
  348. }
  349. static ssize_t backup_write(void *object, const void *buf, size_t count)
  350. {
  351. struct backup_object *obj = object;
  352. ssize_t ret = 0;
  353. if (obj->pending_call) {
  354. DBG("cmd = %s, IN WAITING STAGE", obj->cmd);
  355. return -EAGAIN;
  356. }
  357. if (obj->fd != -1) {
  358. ret = write(obj->fd, buf, count);
  359. DBG("cmd = %s, WRITTING", obj->cmd);
  360. if (ret < 0) {
  361. error("backup: cmd = %s", obj->cmd);
  362. ret = -errno;
  363. }
  364. } else {
  365. error("backup: cmd = %s", obj->cmd);
  366. ret = obj->error_code ? -obj->error_code : -ENOENT;
  367. }
  368. return ret;
  369. }
  370. static int backup_flush(void *object)
  371. {
  372. DBG("%p", object);
  373. return 0;
  374. }
  375. static struct obex_mime_type_driver backup = {
  376. .target = FTP_TARGET,
  377. .target_size = TARGET_SIZE,
  378. .mimetype = "application/vnd.nokia-backup",
  379. .open = backup_open,
  380. .close = backup_close,
  381. .read = backup_read,
  382. .write = backup_write,
  383. .flush = backup_flush,
  384. };
  385. static int pcsuite_init(void)
  386. {
  387. int err;
  388. err = obex_service_driver_register(&pcsuite);
  389. if (err < 0)
  390. return err;
  391. err = obex_mime_type_driver_register(&backup);
  392. if (err < 0)
  393. obex_service_driver_unregister(&pcsuite);
  394. return err;
  395. }
  396. static void pcsuite_exit(void)
  397. {
  398. obex_mime_type_driver_unregister(&backup);
  399. obex_service_driver_unregister(&pcsuite);
  400. }
  401. OBEX_PLUGIN_DEFINE(pcsuite, pcsuite_init, pcsuite_exit)