irmc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * OBEX IrMC Sync Server
  5. *
  6. * Copyright (C) 2010 Marcel Mol <marcel@mesa.nl>
  7. *
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. #define _GNU_SOURCE
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <glib.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <arpa/inet.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <inttypes.h>
  24. #include "obexd/src/obexd.h"
  25. #include "obexd/src/plugin.h"
  26. #include "obexd/src/log.h"
  27. #include "obexd/src/obex.h"
  28. #include "obexd/src/service.h"
  29. #include "obexd/src/manager.h"
  30. #include "obexd/src/mimetype.h"
  31. #include "phonebook.h"
  32. #include "filesystem.h"
  33. struct aparam_header {
  34. uint8_t tag;
  35. uint8_t len;
  36. uint8_t val[0];
  37. } __attribute__ ((packed));
  38. #define DID_LEN 18
  39. struct irmc_session {
  40. struct obex_session *os;
  41. struct apparam_field *params;
  42. uint16_t entries;
  43. GString *buffer;
  44. char sn[DID_LEN];
  45. char did[DID_LEN];
  46. char manu[DID_LEN];
  47. char model[DID_LEN];
  48. void *request;
  49. };
  50. #define IRMC_TARGET_SIZE 9
  51. static const guint8 IRMC_TARGET[IRMC_TARGET_SIZE] = {
  52. 0x49, 0x52, 0x4d, 0x43, 0x2d, 0x53, 0x59, 0x4e, 0x43 };
  53. /* FIXME:
  54. * the IrMC specs state the first vcard should be the owner
  55. * vcard. As there is no simple way to collect ownerdetails
  56. * just create an empty vcard (which is allowed according to the
  57. * specs).
  58. */
  59. static const char *owner_vcard =
  60. "BEGIN:VCARD\r\n"
  61. "VERSION:2.1\r\n"
  62. "N:\r\n"
  63. "TEL:\r\n"
  64. "X-IRMX-LUID:0\r\n"
  65. "END:VCARD\r\n";
  66. static void phonebook_size_result(const char *buffer, size_t bufsize,
  67. int vcards, int missed,
  68. gboolean lastpart, void *user_data)
  69. {
  70. struct irmc_session *irmc = user_data;
  71. DBG("vcards %d", vcards);
  72. irmc->params->maxlistcount = vcards;
  73. if (irmc->request) {
  74. phonebook_req_finalize(irmc->request);
  75. irmc->request = NULL;
  76. }
  77. }
  78. static void query_result(const char *buffer, size_t bufsize, int vcards,
  79. int missed, gboolean lastpart, void *user_data)
  80. {
  81. struct irmc_session *irmc = user_data;
  82. const char *s, *t;
  83. DBG("bufsize %zu vcards %d missed %d", bufsize, vcards, missed);
  84. if (irmc->request) {
  85. phonebook_req_finalize(irmc->request);
  86. irmc->request = NULL;
  87. }
  88. /* first add a 'owner' vcard */
  89. if (!irmc->buffer)
  90. irmc->buffer = g_string_new(owner_vcard);
  91. else
  92. irmc->buffer = g_string_append(irmc->buffer, owner_vcard);
  93. if (buffer == NULL)
  94. goto done;
  95. /* loop around buffer and add X-IRMC-LUID attribs */
  96. s = buffer;
  97. while ((t = strstr(s, "UID:")) != NULL) {
  98. /* add up to UID: into buffer */
  99. irmc->buffer = g_string_append_len(irmc->buffer, s, t-s);
  100. /*
  101. * add UID: line into buffer
  102. * Not sure if UID is still needed if X-IRMC-LUID is there
  103. */
  104. s = t;
  105. t = strstr(s, "\r\n");
  106. t += 2;
  107. irmc->buffer = g_string_append_len(irmc->buffer, s, t-s);
  108. /* add X-IRMC-LUID with same number as UID */
  109. irmc->buffer = g_string_append_len(irmc->buffer,
  110. "X-IRMC-LUID:", 12);
  111. s += 4; /* point to uid number */
  112. irmc->buffer = g_string_append_len(irmc->buffer, s, t-s);
  113. s = t;
  114. }
  115. /* add remaining bit of buffer */
  116. irmc->buffer = g_string_append(irmc->buffer, s);
  117. done:
  118. obex_object_set_io_flags(irmc, G_IO_IN, 0);
  119. }
  120. static void *irmc_connect(struct obex_session *os, int *err)
  121. {
  122. struct irmc_session *irmc;
  123. struct apparam_field *param;
  124. int ret;
  125. DBG("");
  126. manager_register_session(os);
  127. irmc = g_new0(struct irmc_session, 1);
  128. irmc->os = os;
  129. /* FIXME:
  130. * Ideally get capabilities info here and use that to define
  131. * IrMC DID and SN etc parameters.
  132. * For now lets used hostname and some 'random' value
  133. */
  134. gethostname(irmc->did, DID_LEN);
  135. strncpy(irmc->sn, "12345", sizeof(irmc->sn) - 1);
  136. strncpy(irmc->manu, "obex", sizeof(irmc->manu) - 1);
  137. strncpy(irmc->model, "mymodel", sizeof(irmc->model) - 1);
  138. /* We need to know the number of contact/cal/nt entries
  139. * somewhere so why not do it now.
  140. */
  141. param = g_new0(struct apparam_field, 1);
  142. param->maxlistcount = 0; /* to count the number of vcards... */
  143. param->filter = 0x200085; /* UID TEL N VERSION */
  144. irmc->params = param;
  145. irmc->request = phonebook_pull(PB_CONTACTS, irmc->params,
  146. phonebook_size_result, irmc, err);
  147. ret = phonebook_pull_read(irmc->request);
  148. if (err)
  149. *err = ret;
  150. return irmc;
  151. }
  152. static int irmc_get(struct obex_session *os, void *user_data)
  153. {
  154. struct irmc_session *irmc = user_data;
  155. const char *type = obex_get_type(os);
  156. const char *name = obex_get_name(os);
  157. char *path;
  158. int ret;
  159. DBG("name %s type %s irmc %p", name, type ? type : "NA", irmc);
  160. path = g_strdup(name);
  161. ret = obex_get_stream_start(os, path);
  162. g_free(path);
  163. return ret;
  164. }
  165. static void irmc_disconnect(struct obex_session *os, void *user_data)
  166. {
  167. struct irmc_session *irmc = user_data;
  168. DBG("");
  169. manager_unregister_session(os);
  170. if (irmc->params) {
  171. if (irmc->params->searchval)
  172. g_free(irmc->params->searchval);
  173. g_free(irmc->params);
  174. }
  175. if (irmc->buffer)
  176. g_string_free(irmc->buffer, TRUE);
  177. g_free(irmc);
  178. }
  179. static int irmc_chkput(struct obex_session *os, void *user_data)
  180. {
  181. DBG("");
  182. /* Reject all PUTs */
  183. return -EBADR;
  184. }
  185. static int irmc_open_devinfo(struct irmc_session *irmc)
  186. {
  187. if (!irmc->buffer)
  188. irmc->buffer = g_string_new("");
  189. g_string_append_printf(irmc->buffer,
  190. "MANU:%s\r\n"
  191. "MOD:%s\r\n"
  192. "SN:%s\r\n"
  193. "IRMC-VERSION:1.1\r\n"
  194. "PB-TYPE-TX:VCARD2.1\r\n"
  195. "PB-TYPE-RX:NONE\r\n"
  196. "CAL-TYPE-TX:NONE\r\n"
  197. "CAL-TYPE-RX:NONE\r\n"
  198. "MSG-TYPE-TX:NONE\r\n"
  199. "MSG-TYPE-RX:NONE\r\n"
  200. "NOTE-TYPE-TX:NONE\r\n"
  201. "NOTE-TYPE-RX:NONE\r\n",
  202. irmc->manu, irmc->model, irmc->sn);
  203. return 0;
  204. }
  205. static int irmc_open_pb(struct irmc_session *irmc)
  206. {
  207. int ret;
  208. /* how can we tell if the vcard count call already finished? */
  209. irmc->request = phonebook_pull(PB_CONTACTS, irmc->params,
  210. query_result, irmc, &ret);
  211. if (ret < 0) {
  212. DBG("phonebook_pull failed...");
  213. return ret;
  214. }
  215. ret = phonebook_pull_read(irmc->request);
  216. if (ret < 0) {
  217. DBG("phonebook_pull_read failed...");
  218. return ret;
  219. }
  220. return 0;
  221. }
  222. static int irmc_open_info(struct irmc_session *irmc)
  223. {
  224. if (irmc->buffer == NULL)
  225. irmc->buffer = g_string_new("");
  226. g_string_printf(irmc->buffer, "Total-Records:%d\r\n"
  227. "Maximum-Records:%d\r\n"
  228. "IEL:2\r\n"
  229. "DID:%s\r\n",
  230. irmc->params->maxlistcount,
  231. irmc->params->maxlistcount, irmc->did);
  232. return 0;
  233. }
  234. static int irmc_open_cc(struct irmc_session *irmc)
  235. {
  236. if (irmc->buffer == NULL)
  237. irmc->buffer = g_string_new("");
  238. g_string_printf(irmc->buffer, "%d\r\n", irmc->params->maxlistcount);
  239. return 0;
  240. }
  241. static int irmc_open_cal(struct irmc_session *irmc)
  242. {
  243. /* no suport yet. Just return an empty buffer. cal.vcs */
  244. DBG("unsupported, returning empty buffer");
  245. if (!irmc->buffer)
  246. irmc->buffer = g_string_new("");
  247. return 0;
  248. }
  249. static int irmc_open_nt(struct irmc_session *irmc)
  250. {
  251. /* no suport yet. Just return an empty buffer. nt.vnt */
  252. DBG("unsupported, returning empty buffer");
  253. if (!irmc->buffer)
  254. irmc->buffer = g_string_new("");
  255. return 0;
  256. }
  257. static int irmc_open_luid(struct irmc_session *irmc)
  258. {
  259. if (irmc->buffer == NULL)
  260. irmc->buffer = g_string_new("");
  261. DBG("changelog request, force whole book");
  262. g_string_printf(irmc->buffer, "SN:%s\r\n"
  263. "DID:%s\r\n"
  264. "Total-Records:%d\r\n"
  265. "Maximum-Records:%d\r\n"
  266. "*\r\n",
  267. irmc->sn, irmc->did,
  268. irmc->params->maxlistcount,
  269. irmc->params->maxlistcount);
  270. return 0;
  271. }
  272. static void *irmc_open(const char *name, int oflag, mode_t mode, void *context,
  273. size_t *size, int *err)
  274. {
  275. struct irmc_session *irmc = context;
  276. int ret = 0;
  277. char *path;
  278. DBG("name %s context %p", name, context);
  279. if (oflag != O_RDONLY) {
  280. ret = -EPERM;
  281. goto fail;
  282. }
  283. if (name == NULL) {
  284. ret = -EBADR;
  285. goto fail;
  286. }
  287. /* Always contains the absolute path */
  288. if (g_path_is_absolute(name))
  289. path = g_strdup(name);
  290. else
  291. path = g_build_filename("/", name, NULL);
  292. if (g_str_equal(path, PB_DEVINFO))
  293. ret = irmc_open_devinfo(irmc);
  294. else if (g_str_equal(path, PB_CONTACTS))
  295. ret = irmc_open_pb(irmc);
  296. else if (g_str_equal(path, PB_INFO_LOG))
  297. ret = irmc_open_info(irmc);
  298. else if (g_str_equal(path, PB_CC_LOG))
  299. ret = irmc_open_cc(irmc);
  300. else if (g_str_has_prefix(path, PB_CALENDAR_FOLDER))
  301. ret = irmc_open_cal(irmc);
  302. else if (g_str_has_prefix(path, PB_NOTES_FOLDER))
  303. ret = irmc_open_nt(irmc);
  304. else if (g_str_has_prefix(path, PB_LUID_FOLDER))
  305. ret = irmc_open_luid(irmc);
  306. else
  307. ret = -EBADR;
  308. g_free(path);
  309. if (ret == 0)
  310. return irmc;
  311. fail:
  312. if (err)
  313. *err = ret;
  314. return NULL;
  315. }
  316. static int irmc_close(void *object)
  317. {
  318. struct irmc_session *irmc = object;
  319. DBG("");
  320. if (irmc->buffer) {
  321. g_string_free(irmc->buffer, TRUE);
  322. irmc->buffer = NULL;
  323. }
  324. if (irmc->request) {
  325. phonebook_req_finalize(irmc->request);
  326. irmc->request = NULL;
  327. }
  328. return 0;
  329. }
  330. static ssize_t irmc_read(void *object, void *buf, size_t count)
  331. {
  332. struct irmc_session *irmc = object;
  333. int len;
  334. DBG("buffer %p count %zu", irmc->buffer, count);
  335. if (!irmc->buffer)
  336. return -EAGAIN;
  337. len = string_read(irmc->buffer, buf, count);
  338. DBG("returning %d bytes", len);
  339. return len;
  340. }
  341. static struct obex_mime_type_driver irmc_driver = {
  342. .target = IRMC_TARGET,
  343. .target_size = IRMC_TARGET_SIZE,
  344. .open = irmc_open,
  345. .close = irmc_close,
  346. .read = irmc_read,
  347. };
  348. static struct obex_service_driver irmc = {
  349. .name = "IRMC Sync server",
  350. .service = OBEX_IRMC,
  351. .target = IRMC_TARGET,
  352. .target_size = IRMC_TARGET_SIZE,
  353. .connect = irmc_connect,
  354. .get = irmc_get,
  355. .disconnect = irmc_disconnect,
  356. .chkput = irmc_chkput
  357. };
  358. static int irmc_init(void)
  359. {
  360. int err;
  361. DBG("");
  362. err = phonebook_init();
  363. if (err < 0)
  364. return err;
  365. err = obex_mime_type_driver_register(&irmc_driver);
  366. if (err < 0)
  367. goto fail_mime_irmc;
  368. err = obex_service_driver_register(&irmc);
  369. if (err < 0)
  370. goto fail_irmc_reg;
  371. return 0;
  372. fail_irmc_reg:
  373. obex_mime_type_driver_unregister(&irmc_driver);
  374. fail_mime_irmc:
  375. phonebook_exit();
  376. return err;
  377. }
  378. static void irmc_exit(void)
  379. {
  380. DBG("");
  381. obex_service_driver_unregister(&irmc);
  382. obex_mime_type_driver_unregister(&irmc_driver);
  383. phonebook_exit();
  384. }
  385. OBEX_PLUGIN_DEFINE(irmc, irmc_init, irmc_exit)