drv_imap.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2008,2010-2012 Oswald Buddenhagen <ossi@users.sf.net>
  5. * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * As a special exception, mbsync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #include "isync.h"
  24. #include <assert.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <stddef.h>
  29. #include <limits.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. typedef struct imap_server_conf {
  33. struct imap_server_conf *next;
  34. char *name;
  35. server_conf_t sconf;
  36. char *user;
  37. char *pass;
  38. int max_in_progress;
  39. #ifdef HAVE_LIBSSL
  40. unsigned require_ssl:1;
  41. unsigned require_cram:1;
  42. #endif
  43. } imap_server_conf_t;
  44. typedef struct imap_store_conf {
  45. store_conf_t gen;
  46. imap_server_conf_t *server;
  47. unsigned use_namespace:1;
  48. char delimiter;
  49. } imap_store_conf_t;
  50. typedef struct imap_message {
  51. message_t gen;
  52. /* int seq; will be needed when expunges are tracked */
  53. } imap_message_t;
  54. #define NIL (void*)0x1
  55. #define LIST (void*)0x2
  56. typedef struct _list {
  57. struct _list *next, *child;
  58. char *val;
  59. int len;
  60. } list_t;
  61. #define MAX_LIST_DEPTH 5
  62. typedef struct parse_list_state {
  63. list_t *head, **stack[MAX_LIST_DEPTH];
  64. int level, need_bytes;
  65. } parse_list_state_t;
  66. struct imap_cmd;
  67. typedef struct imap_store {
  68. store_t gen;
  69. const char *prefix;
  70. int ref_count;
  71. /* trash folder's existence is not confirmed yet */
  72. enum { TrashUnknown, TrashChecking, TrashKnown } trashnc;
  73. unsigned got_namespace:1;
  74. char delimiter; /* hierarchy delimiter */
  75. list_t *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
  76. message_t **msgapp; /* FETCH results */
  77. unsigned caps; /* CAPABILITY results */
  78. parse_list_state_t parse_list_sts;
  79. /* command queue */
  80. int nexttag, num_in_progress;
  81. struct imap_cmd *pending, **pending_append;
  82. struct imap_cmd *in_progress, **in_progress_append;
  83. /* Used during sequential operations like connect */
  84. enum { GreetingPending = 0, GreetingBad, GreetingOk, GreetingPreauth } greeting;
  85. int canceling; /* imap_cancel() is in progress */
  86. union {
  87. void (*imap_open)( store_t *srv, void *aux );
  88. void (*imap_cancel)( void *aux );
  89. } callbacks;
  90. void *callback_aux;
  91. conn_t conn; /* this is BIG, so put it last */
  92. } imap_store_t;
  93. struct imap_cmd {
  94. struct imap_cmd *next;
  95. char *cmd;
  96. int tag;
  97. struct {
  98. /* Will be called on each continuation request until it resets this pointer.
  99. * Needs to invoke bad_callback and return -1 on error, otherwise return 0. */
  100. int (*cont)( imap_store_t *ctx, struct imap_cmd *cmd, const char *prompt );
  101. void (*done)( imap_store_t *ctx, struct imap_cmd *cmd, int response );
  102. char *data;
  103. int data_len;
  104. int uid; /* to identify fetch responses */
  105. unsigned
  106. high_prio:1, /* if command is queued, put it at the front of the queue. */
  107. to_trash:1, /* we are storing to trash, not current. */
  108. create:1, /* create the mailbox if we get an error ... */
  109. trycreate:1; /* ... but only if this is true or the server says so. */
  110. } param;
  111. };
  112. struct imap_cmd_simple {
  113. struct imap_cmd gen;
  114. void (*callback)( int sts, void *aux );
  115. void *callback_aux;
  116. };
  117. struct imap_cmd_fetch_msg {
  118. struct imap_cmd_simple gen;
  119. msg_data_t *msg_data;
  120. };
  121. struct imap_cmd_out_uid {
  122. struct imap_cmd gen;
  123. void (*callback)( int sts, int uid, void *aux );
  124. void *callback_aux;
  125. int out_uid;
  126. };
  127. struct imap_cmd_refcounted_state {
  128. void (*callback)( int sts, void *aux );
  129. void *callback_aux;
  130. int ref_count;
  131. int ret_val;
  132. };
  133. struct imap_cmd_refcounted {
  134. struct imap_cmd gen;
  135. struct imap_cmd_refcounted_state *state;
  136. };
  137. #define CAP(cap) (ctx->caps & (1 << (cap)))
  138. enum CAPABILITY {
  139. NOLOGIN = 0,
  140. #ifdef HAVE_LIBSSL
  141. CRAM,
  142. STARTTLS,
  143. #endif
  144. UIDPLUS,
  145. LITERALPLUS,
  146. NAMESPACE
  147. };
  148. static const char *cap_list[] = {
  149. "LOGINDISABLED",
  150. #ifdef HAVE_LIBSSL
  151. "AUTH=CRAM-MD5",
  152. "STARTTLS",
  153. #endif
  154. "UIDPLUS",
  155. "LITERAL+",
  156. "NAMESPACE"
  157. };
  158. #define RESP_OK 0
  159. #define RESP_NO 1
  160. #define RESP_CANCEL 2
  161. static INLINE void imap_ref( imap_store_t *ctx ) { ++ctx->ref_count; }
  162. static int imap_deref( imap_store_t *ctx );
  163. static void imap_invoke_bad_callback( imap_store_t *ctx );
  164. static const char *Flags[] = {
  165. "Draft",
  166. "Flagged",
  167. "Answered",
  168. "Seen",
  169. "Deleted",
  170. };
  171. static struct imap_cmd *
  172. new_imap_cmd( int size )
  173. {
  174. struct imap_cmd *cmd = nfmalloc( size );
  175. memset( &cmd->param, 0, sizeof(cmd->param) );
  176. return cmd;
  177. }
  178. #define INIT_IMAP_CMD(type, cmdp, cb, aux) \
  179. cmdp = (struct type *)new_imap_cmd( sizeof(*cmdp) ); \
  180. cmdp->callback = cb; \
  181. cmdp->callback_aux = aux;
  182. #define INIT_IMAP_CMD_X(type, cmdp, cb, aux) \
  183. cmdp = (struct type *)new_imap_cmd( sizeof(*cmdp) ); \
  184. cmdp->gen.callback = cb; \
  185. cmdp->gen.callback_aux = aux;
  186. static void
  187. done_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd, int response )
  188. {
  189. cmd->param.done( ctx, cmd, response );
  190. free( cmd->param.data );
  191. free( cmd->cmd );
  192. free( cmd );
  193. }
  194. static int
  195. send_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd )
  196. {
  197. int bufl, litplus;
  198. const char *buffmt;
  199. char buf[1024];
  200. cmd->tag = ++ctx->nexttag;
  201. if (!cmd->param.data) {
  202. buffmt = "%d %s\r\n";
  203. litplus = 0;
  204. } else if ((cmd->param.to_trash && ctx->trashnc == TrashUnknown) || !CAP(LITERALPLUS)) {
  205. buffmt = "%d %s{%d}\r\n";
  206. litplus = 0;
  207. } else {
  208. buffmt = "%d %s{%d+}\r\n";
  209. litplus = 1;
  210. }
  211. bufl = nfsnprintf( buf, sizeof(buf), buffmt,
  212. cmd->tag, cmd->cmd, cmd->param.data_len );
  213. if (DFlags & VERBOSE) {
  214. if (ctx->num_in_progress)
  215. printf( "(%d in progress) ", ctx->num_in_progress );
  216. if (memcmp( cmd->cmd, "LOGIN", 5 ))
  217. printf( ">>> %s", buf );
  218. else
  219. printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag );
  220. fflush( stdout );
  221. }
  222. if (socket_write( &ctx->conn, buf, bufl, KeepOwn ) < 0)
  223. goto bail;
  224. if (litplus) {
  225. char *p = cmd->param.data;
  226. cmd->param.data = 0;
  227. if (socket_write( &ctx->conn, p, cmd->param.data_len, GiveOwn ) < 0 ||
  228. socket_write( &ctx->conn, "\r\n", 2, KeepOwn ) < 0)
  229. goto bail;
  230. }
  231. if (cmd->param.to_trash && ctx->trashnc == TrashUnknown)
  232. ctx->trashnc = TrashChecking;
  233. cmd->next = 0;
  234. *ctx->in_progress_append = cmd;
  235. ctx->in_progress_append = &cmd->next;
  236. ctx->num_in_progress++;
  237. return 0;
  238. bail:
  239. done_imap_cmd( ctx, cmd, RESP_CANCEL );
  240. return -1;
  241. }
  242. static int
  243. cmd_submittable( imap_store_t *ctx, struct imap_cmd *cmd )
  244. {
  245. struct imap_cmd *cmdp;
  246. return !ctx->conn.write_buf &&
  247. !(ctx->in_progress &&
  248. (cmdp = (struct imap_cmd *)((char *)ctx->in_progress_append -
  249. offsetof(struct imap_cmd, next)), 1) &&
  250. (cmdp->param.cont || cmdp->param.data)) &&
  251. !(cmd->param.to_trash && ctx->trashnc == TrashChecking) &&
  252. ctx->num_in_progress < ((imap_store_conf_t *)ctx->gen.conf)->server->max_in_progress;
  253. }
  254. static int
  255. flush_imap_cmds( imap_store_t *ctx )
  256. {
  257. struct imap_cmd *cmd;
  258. while ((cmd = ctx->pending) && cmd_submittable( ctx, cmd )) {
  259. if (!(ctx->pending = cmd->next))
  260. ctx->pending_append = &ctx->pending;
  261. if (send_imap_cmd( ctx, cmd ) < 0)
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. static void
  267. cancel_pending_imap_cmds( imap_store_t *ctx )
  268. {
  269. struct imap_cmd *cmd;
  270. while ((cmd = ctx->pending)) {
  271. if (!(ctx->pending = cmd->next))
  272. ctx->pending_append = &ctx->pending;
  273. done_imap_cmd( ctx, cmd, RESP_CANCEL );
  274. }
  275. }
  276. static void
  277. cancel_submitted_imap_cmds( imap_store_t *ctx )
  278. {
  279. struct imap_cmd *cmd;
  280. while ((cmd = ctx->in_progress)) {
  281. ctx->in_progress = cmd->next;
  282. /* don't update num_in_progress and in_progress_append - store is dead */
  283. done_imap_cmd( ctx, cmd, RESP_CANCEL );
  284. }
  285. }
  286. static int
  287. submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd )
  288. {
  289. assert( ctx );
  290. assert( ctx->gen.bad_callback );
  291. assert( cmd );
  292. assert( cmd->param.done );
  293. if ((ctx->pending && !cmd->param.high_prio) || !cmd_submittable( ctx, cmd )) {
  294. if (ctx->pending && cmd->param.high_prio) {
  295. cmd->next = ctx->pending;
  296. ctx->pending = cmd;
  297. } else {
  298. cmd->next = 0;
  299. *ctx->pending_append = cmd;
  300. ctx->pending_append = &cmd->next;
  301. }
  302. return 0;
  303. }
  304. return send_imap_cmd( ctx, cmd );
  305. }
  306. static int
  307. imap_exec( imap_store_t *ctx, struct imap_cmd *cmdp,
  308. void (*done)( imap_store_t *ctx, struct imap_cmd *cmd, int response ),
  309. const char *fmt, ... )
  310. {
  311. va_list ap;
  312. if (!cmdp)
  313. cmdp = new_imap_cmd( sizeof(*cmdp) );
  314. cmdp->param.done = done;
  315. va_start( ap, fmt );
  316. nfvasprintf( &cmdp->cmd, fmt, ap );
  317. va_end( ap );
  318. return submit_imap_cmd( ctx, cmdp );
  319. }
  320. static void
  321. transform_box_response( int *response )
  322. {
  323. switch (*response) {
  324. case RESP_CANCEL: *response = DRV_CANCELED; break;
  325. case RESP_NO: *response = DRV_BOX_BAD; break;
  326. default: *response = DRV_OK; break;
  327. }
  328. }
  329. static void
  330. imap_done_simple_box( imap_store_t *ctx ATTR_UNUSED,
  331. struct imap_cmd *cmd, int response )
  332. {
  333. struct imap_cmd_simple *cmdp = (struct imap_cmd_simple *)cmd;
  334. transform_box_response( &response );
  335. cmdp->callback( response, cmdp->callback_aux );
  336. }
  337. static void
  338. transform_msg_response( int *response )
  339. {
  340. switch (*response) {
  341. case RESP_CANCEL: *response = DRV_CANCELED; break;
  342. case RESP_NO: *response = DRV_MSG_BAD; break;
  343. default: *response = DRV_OK; break;
  344. }
  345. }
  346. static void
  347. imap_done_simple_msg( imap_store_t *ctx ATTR_UNUSED,
  348. struct imap_cmd *cmd, int response )
  349. {
  350. struct imap_cmd_simple *cmdp = (struct imap_cmd_simple *)cmd;
  351. transform_msg_response( &response );
  352. cmdp->callback( response, cmdp->callback_aux );
  353. }
  354. static struct imap_cmd_refcounted_state *
  355. imap_refcounted_new_state( void (*cb)( int, void * ), void *aux )
  356. {
  357. struct imap_cmd_refcounted_state *sts = nfmalloc( sizeof(*sts) );
  358. sts->callback = cb;
  359. sts->callback_aux = aux;
  360. sts->ref_count = 1; /* so forced sync does not cause an early exit */
  361. sts->ret_val = DRV_OK;
  362. return sts;
  363. }
  364. static struct imap_cmd *
  365. imap_refcounted_new_cmd( struct imap_cmd_refcounted_state *sts )
  366. {
  367. struct imap_cmd_refcounted *cmd = (struct imap_cmd_refcounted *)new_imap_cmd( sizeof(*cmd) );
  368. cmd->state = sts;
  369. sts->ref_count++;
  370. return &cmd->gen;
  371. }
  372. static void
  373. imap_refcounted_done( struct imap_cmd_refcounted_state *sts )
  374. {
  375. if (!--sts->ref_count) {
  376. sts->callback( sts->ret_val, sts->callback_aux );
  377. free( sts );
  378. }
  379. }
  380. static void
  381. imap_refcounted_done_box( imap_store_t *ctx ATTR_UNUSED, struct imap_cmd *cmd, int response )
  382. {
  383. struct imap_cmd_refcounted_state *sts = ((struct imap_cmd_refcounted *)cmd)->state;
  384. switch (response) {
  385. case RESP_CANCEL:
  386. sts->ret_val = DRV_CANCELED;
  387. break;
  388. case RESP_NO:
  389. if (sts->ret_val == DRV_OK) /* Don't override cancelation. */
  390. sts->ret_val = DRV_BOX_BAD;
  391. break;
  392. }
  393. imap_refcounted_done( sts );
  394. }
  395. static char *
  396. next_arg( char **s )
  397. {
  398. char *ret;
  399. if (!s || !*s)
  400. return 0;
  401. while (isspace( (unsigned char) **s ))
  402. (*s)++;
  403. if (!**s) {
  404. *s = 0;
  405. return 0;
  406. }
  407. if (**s == '"') {
  408. ++*s;
  409. ret = *s;
  410. *s = strchr( *s, '"' );
  411. } else {
  412. ret = *s;
  413. while (**s && !isspace( (unsigned char) **s ))
  414. (*s)++;
  415. }
  416. if (*s) {
  417. if (**s)
  418. *(*s)++ = 0;
  419. if (!**s)
  420. *s = 0;
  421. }
  422. return ret;
  423. }
  424. static int
  425. is_atom( list_t *list )
  426. {
  427. return list && list->val && list->val != NIL && list->val != LIST;
  428. }
  429. static int
  430. is_list( list_t *list )
  431. {
  432. return list && list->val == LIST;
  433. }
  434. static void
  435. free_list( list_t *list )
  436. {
  437. list_t *tmp;
  438. for (; list; list = tmp) {
  439. tmp = list->next;
  440. if (is_list( list ))
  441. free_list( list->child );
  442. else if (is_atom( list ))
  443. free( list->val );
  444. free( list );
  445. }
  446. }
  447. enum {
  448. LIST_OK,
  449. LIST_PARTIAL,
  450. LIST_BAD
  451. };
  452. static int
  453. parse_imap_list( imap_store_t *ctx, char **sp, parse_list_state_t *sts )
  454. {
  455. list_t *cur, **curp;
  456. char *s = *sp, *p;
  457. int bytes;
  458. assert( sts );
  459. assert( sts->level > 0 );
  460. curp = sts->stack[--sts->level];
  461. bytes = sts->need_bytes;
  462. if (bytes >= 0) {
  463. sts->need_bytes = -1;
  464. if (!bytes)
  465. goto getline;
  466. cur = (list_t *)((char *)curp - offsetof(list_t, next));
  467. s = cur->val + cur->len - bytes;
  468. goto getbytes;
  469. }
  470. if (!s)
  471. return LIST_BAD;
  472. for (;;) {
  473. while (isspace( (unsigned char)*s ))
  474. s++;
  475. if (sts->level && *s == ')') {
  476. s++;
  477. curp = sts->stack[--sts->level];
  478. goto next;
  479. }
  480. *curp = cur = nfmalloc( sizeof(*cur) );
  481. cur->val = 0; /* for clean bail */
  482. curp = &cur->next;
  483. *curp = 0; /* ditto */
  484. if (*s == '(') {
  485. /* sublist */
  486. if (sts->level == MAX_LIST_DEPTH)
  487. goto bail;
  488. s++;
  489. cur->val = LIST;
  490. sts->stack[sts->level++] = curp;
  491. curp = &cur->child;
  492. *curp = 0; /* for clean bail */
  493. goto next2;
  494. } else if (ctx && *s == '{') {
  495. /* literal */
  496. bytes = cur->len = strtol( s + 1, &s, 10 );
  497. if (*s != '}' || *++s)
  498. goto bail;
  499. s = cur->val = nfmalloc( cur->len );
  500. getbytes:
  501. bytes -= socket_read( &ctx->conn, s, bytes );
  502. if (bytes > 0)
  503. goto postpone;
  504. if (DFlags & XVERBOSE) {
  505. puts( "=========" );
  506. fwrite( cur->val, cur->len, 1, stdout );
  507. puts( "=========" );
  508. fflush( stdout );
  509. }
  510. getline:
  511. if (!(s = socket_read_line( &ctx->conn )))
  512. goto postpone;
  513. } else if (*s == '"') {
  514. /* quoted string */
  515. s++;
  516. p = s;
  517. for (; *s != '"'; s++)
  518. if (!*s)
  519. goto bail;
  520. cur->len = s - p;
  521. s++;
  522. cur->val = nfmalloc( cur->len + 1 );
  523. memcpy( cur->val, p, cur->len );
  524. cur->val[cur->len] = 0;
  525. } else {
  526. /* atom */
  527. p = s;
  528. for (; *s && !isspace( (unsigned char)*s ); s++)
  529. if (sts->level && *s == ')')
  530. break;
  531. cur->len = s - p;
  532. if (cur->len == 3 && !memcmp ("NIL", p, 3))
  533. cur->val = NIL;
  534. else {
  535. cur->val = nfmalloc( cur->len + 1 );
  536. memcpy( cur->val, p, cur->len );
  537. cur->val[cur->len] = 0;
  538. }
  539. }
  540. next:
  541. if (!sts->level)
  542. break;
  543. next2:
  544. if (!*s)
  545. goto bail;
  546. }
  547. *sp = s;
  548. return LIST_OK;
  549. postpone:
  550. if (sts->level < MAX_LIST_DEPTH) {
  551. sts->stack[sts->level++] = curp;
  552. sts->need_bytes = bytes;
  553. return LIST_PARTIAL;
  554. }
  555. bail:
  556. free_list( sts->head );
  557. return LIST_BAD;
  558. }
  559. static void
  560. parse_list_init( parse_list_state_t *sts )
  561. {
  562. sts->need_bytes = -1;
  563. sts->level = 1;
  564. sts->head = 0;
  565. sts->stack[0] = &sts->head;
  566. }
  567. static list_t *
  568. parse_list( char **sp )
  569. {
  570. parse_list_state_t sts;
  571. parse_list_init( &sts );
  572. if (parse_imap_list( 0, sp, &sts ) == LIST_OK)
  573. return sts.head;
  574. return NULL;
  575. }
  576. static int
  577. parse_fetch( imap_store_t *ctx, list_t *list )
  578. {
  579. list_t *tmp, *flags;
  580. char *body = 0, *tuid = 0;
  581. imap_message_t *cur;
  582. msg_data_t *msgdata;
  583. struct imap_cmd *cmdp;
  584. int uid = 0, mask = 0, status = 0, size = 0;
  585. unsigned i;
  586. if (!is_list( list )) {
  587. error( "IMAP error: bogus FETCH response\n" );
  588. free_list( list );
  589. return -1;
  590. }
  591. for (tmp = list->child; tmp; tmp = tmp->next) {
  592. if (is_atom( tmp )) {
  593. if (!strcmp( "UID", tmp->val )) {
  594. tmp = tmp->next;
  595. if (is_atom( tmp ))
  596. uid = atoi( tmp->val );
  597. else
  598. error( "IMAP error: unable to parse UID\n" );
  599. } else if (!strcmp( "FLAGS", tmp->val )) {
  600. tmp = tmp->next;
  601. if (is_list( tmp )) {
  602. for (flags = tmp->child; flags; flags = flags->next) {
  603. if (is_atom( flags )) {
  604. if (flags->val[0] == '\\') { /* ignore user-defined flags for now */
  605. if (!strcmp( "Recent", flags->val + 1)) {
  606. status |= M_RECENT;
  607. goto flagok;
  608. }
  609. for (i = 0; i < as(Flags); i++)
  610. if (!strcmp( Flags[i], flags->val + 1 )) {
  611. mask |= 1 << i;
  612. goto flagok;
  613. }
  614. if (flags->val[1] == 'X' && flags->val[2] == '-')
  615. goto flagok; /* ignore system flag extensions */
  616. error( "IMAP warning: unknown system flag %s\n", flags->val );
  617. }
  618. flagok: ;
  619. } else
  620. error( "IMAP error: unable to parse FLAGS list\n" );
  621. }
  622. status |= M_FLAGS;
  623. } else
  624. error( "IMAP error: unable to parse FLAGS\n" );
  625. } else if (!strcmp( "RFC822.SIZE", tmp->val )) {
  626. tmp = tmp->next;
  627. if (is_atom( tmp ))
  628. size = atoi( tmp->val );
  629. else
  630. error( "IMAP error: unable to parse RFC822.SIZE\n" );
  631. } else if (!strcmp( "BODY[]", tmp->val )) {
  632. tmp = tmp->next;
  633. if (is_atom( tmp )) {
  634. body = tmp->val;
  635. tmp->val = 0; /* don't free together with list */
  636. size = tmp->len;
  637. } else
  638. error( "IMAP error: unable to parse BODY[]\n" );
  639. } else if (!strcmp( "BODY[HEADER.FIELDS", tmp->val )) {
  640. tmp = tmp->next;
  641. if (is_list( tmp )) {
  642. tmp = tmp->next;
  643. if (!is_atom( tmp ) || strcmp( tmp->val, "]" ))
  644. goto bfail;
  645. tmp = tmp->next;
  646. if (!is_atom( tmp ) || memcmp( tmp->val, "X-TUID: ", 8 ))
  647. goto bfail;
  648. tuid = tmp->val + 8;
  649. } else {
  650. bfail:
  651. error( "IMAP error: unable to parse BODY[HEADER.FIELDS ...]\n" );
  652. }
  653. }
  654. }
  655. }
  656. if (body) {
  657. for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
  658. if (cmdp->param.uid == uid)
  659. goto gotuid;
  660. error( "IMAP error: unexpected FETCH response (UID %d)\n", uid );
  661. free_list( list );
  662. return -1;
  663. gotuid:
  664. msgdata = ((struct imap_cmd_fetch_msg *)cmdp)->msg_data;
  665. msgdata->data = body;
  666. msgdata->len = size;
  667. if (status & M_FLAGS)
  668. msgdata->flags = mask;
  669. } else if (uid) { /* ignore async flag updates for now */
  670. /* XXX this will need sorting for out-of-order (multiple queries) */
  671. cur = nfcalloc( sizeof(*cur) );
  672. *ctx->msgapp = &cur->gen;
  673. ctx->msgapp = &cur->gen.next;
  674. cur->gen.next = 0;
  675. cur->gen.uid = uid;
  676. cur->gen.flags = mask;
  677. cur->gen.status = status;
  678. cur->gen.size = size;
  679. cur->gen.srec = 0;
  680. if (tuid)
  681. strncpy( cur->gen.tuid, tuid, TUIDL );
  682. else
  683. cur->gen.tuid[0] = 0;
  684. if (ctx->gen.uidnext <= uid) /* in case the server sends no UIDNEXT */
  685. ctx->gen.uidnext = uid + 1;
  686. }
  687. free_list( list );
  688. return 0;
  689. }
  690. static void
  691. parse_capability( imap_store_t *ctx, char *cmd )
  692. {
  693. char *arg;
  694. unsigned i;
  695. ctx->caps = 0x80000000;
  696. while ((arg = next_arg( &cmd )))
  697. for (i = 0; i < as(cap_list); i++)
  698. if (!strcmp( cap_list[i], arg ))
  699. ctx->caps |= 1 << i;
  700. }
  701. static int
  702. parse_response_code( imap_store_t *ctx, struct imap_cmd *cmd, char *s )
  703. {
  704. char *arg, *earg, *p;
  705. if (!s || *s != '[')
  706. return RESP_OK; /* no response code */
  707. s++;
  708. if (!(p = strchr( s, ']' ))) {
  709. error( "IMAP error: malformed response code\n" );
  710. return RESP_CANCEL;
  711. }
  712. *p++ = 0;
  713. arg = next_arg( &s );
  714. if (!strcmp( "UIDVALIDITY", arg )) {
  715. if (!(arg = next_arg( &s )) ||
  716. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg))
  717. {
  718. error( "IMAP error: malformed UIDVALIDITY status\n" );
  719. return RESP_CANCEL;
  720. }
  721. } else if (!strcmp( "UIDNEXT", arg )) {
  722. if (!(arg = next_arg( &s )) || !(ctx->gen.uidnext = atoi( arg ))) {
  723. error( "IMAP error: malformed NEXTUID status\n" );
  724. return RESP_CANCEL;
  725. }
  726. } else if (!strcmp( "CAPABILITY", arg )) {
  727. parse_capability( ctx, s );
  728. } else if (!strcmp( "ALERT", arg )) {
  729. /* RFC2060 says that these messages MUST be displayed
  730. * to the user
  731. */
  732. for (; isspace( (unsigned char)*p ); p++);
  733. error( "*** IMAP ALERT *** %s\n", p );
  734. } else if (cmd && !strcmp( "APPENDUID", arg )) {
  735. if (!(arg = next_arg( &s )) ||
  736. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg) ||
  737. !(arg = next_arg( &s )) ||
  738. !(((struct imap_cmd_out_uid *)cmd)->out_uid = atoi( arg )))
  739. {
  740. error( "IMAP error: malformed APPENDUID status\n" );
  741. return RESP_CANCEL;
  742. }
  743. }
  744. return RESP_OK;
  745. }
  746. static int
  747. parse_list_rsp( imap_store_t *ctx, char *cmd )
  748. {
  749. char *arg;
  750. list_t *list, *lp;
  751. int l;
  752. if (!(list = parse_list( &cmd )))
  753. return -1;
  754. if (list->val == LIST)
  755. for (lp = list->child; lp; lp = lp->next)
  756. if (is_atom( lp ) && !strcasecmp( lp->val, "\\NoSelect" )) {
  757. free_list( list );
  758. return 0;
  759. }
  760. free_list( list );
  761. arg = next_arg( &cmd );
  762. if (!ctx->delimiter)
  763. ctx->delimiter = *arg;
  764. arg = next_arg( &cmd );
  765. if (memcmp( arg, "INBOX", 5 ) || (arg[5] && arg[5] != ctx->delimiter)) {
  766. l = strlen( ctx->gen.conf->path );
  767. if (memcmp( arg, ctx->gen.conf->path, l ))
  768. return 0;
  769. arg += l;
  770. if (!memcmp( arg, "INBOX", 5 ) && (!arg[5] || arg[5] == ctx->delimiter)) {
  771. if (!arg[5])
  772. warn( "IMAP warning: ignoring INBOX in %s\n", ctx->gen.conf->path );
  773. return 0;
  774. }
  775. }
  776. if (!memcmp( arg + strlen( arg ) - 5, ".lock", 5 )) /* workaround broken servers */
  777. return 0;
  778. if (map_name( arg, ctx->delimiter, '/') < 0) {
  779. warn( "IMAP warning: ignoring mailbox %s (reserved character '/' in name)\n", arg );
  780. return 0;
  781. }
  782. add_string_list( &ctx->gen.boxes, arg );
  783. return 0;
  784. }
  785. static int
  786. prepare_name( char *buf, const imap_store_t *ctx, const char *prefix, const char *name )
  787. {
  788. int pl;
  789. nfsnprintf( buf, 1024, "%s%n%s", prefix, &pl, name );
  790. switch (map_name( buf + pl, '/', ctx->delimiter )) {
  791. case -1:
  792. error( "IMAP error: mailbox name %s contains server's hierarchy delimiter\n", buf + pl );
  793. return -1;
  794. case -2:
  795. error( "IMAP error: server's hierarchy delimiter not known\n" );
  796. return -1;
  797. default:
  798. return 0;
  799. }
  800. }
  801. static int
  802. prepare_box( char *buf, const imap_store_t *ctx )
  803. {
  804. const char *name = ctx->gen.name;
  805. return prepare_name( buf, ctx,
  806. (!memcmp( name, "INBOX", 5 ) && (!name[5] || name[5] == '/')) ? "" : ctx->prefix, name );
  807. }
  808. static int
  809. prepare_trash( char *buf, const imap_store_t *ctx )
  810. {
  811. return prepare_name( buf, ctx, ctx->prefix, ctx->gen.conf->trash );
  812. }
  813. struct imap_cmd_trycreate {
  814. struct imap_cmd gen;
  815. struct imap_cmd *orig_cmd;
  816. };
  817. static void imap_open_store_greeted( imap_store_t * );
  818. static void get_cmd_result_p2( imap_store_t *, struct imap_cmd *, int );
  819. static void
  820. imap_socket_read( void *aux )
  821. {
  822. imap_store_t *ctx = (imap_store_t *)aux;
  823. struct imap_cmd *cmdp, **pcmdp;
  824. char *cmd, *arg, *arg1, *p;
  825. int resp, resp2, tag, greeted;
  826. greeted = ctx->greeting;
  827. if (ctx->parse_list_sts.level) {
  828. cmd = 0;
  829. goto do_fetch;
  830. }
  831. for (;;) {
  832. if (!(cmd = socket_read_line( &ctx->conn )))
  833. return;
  834. arg = next_arg( &cmd );
  835. if (!arg) {
  836. error( "IMAP error: empty response\n" );
  837. break;
  838. }
  839. if (*arg == '*') {
  840. arg = next_arg( &cmd );
  841. if (!arg) {
  842. error( "IMAP error: malformed untagged response\n" );
  843. break;
  844. }
  845. if (!strcmp( "NAMESPACE", arg )) {
  846. if (!(ctx->ns_personal = parse_list( &cmd )) ||
  847. !(ctx->ns_other = parse_list( &cmd )) ||
  848. !(ctx->ns_shared = parse_list( &cmd ))) {
  849. error( "IMAP error: malformed NAMESPACE response\n" );
  850. break;
  851. }
  852. } else if (ctx->greeting == GreetingPending && !strcmp( "PREAUTH", arg )) {
  853. ctx->greeting = GreetingPreauth;
  854. parse_response_code( ctx, 0, cmd );
  855. } else if (!strcmp( "OK", arg )) {
  856. ctx->greeting = GreetingOk;
  857. parse_response_code( ctx, 0, cmd );
  858. } else if (!strcmp( "BAD", arg ) || !strcmp( "NO", arg ) || !strcmp( "BYE", arg )) {
  859. ctx->greeting = GreetingBad;
  860. parse_response_code( ctx, 0, cmd );
  861. } else if (!strcmp( "CAPABILITY", arg )) {
  862. parse_capability( ctx, cmd );
  863. } else if (!strcmp( "LIST", arg )) {
  864. if (parse_list_rsp( ctx, cmd ) < 0) {
  865. error( "IMAP error: malformed LIST response\n" );
  866. break;
  867. }
  868. } else if ((arg1 = next_arg( &cmd ))) {
  869. if (!strcmp( "EXISTS", arg1 ))
  870. ctx->gen.count = atoi( arg );
  871. else if (!strcmp( "RECENT", arg1 ))
  872. ctx->gen.recent = atoi( arg );
  873. else if(!strcmp ( "FETCH", arg1 )) {
  874. parse_list_init( &ctx->parse_list_sts );
  875. do_fetch:
  876. if ((resp = parse_imap_list( ctx, &cmd, &ctx->parse_list_sts )) == LIST_BAD)
  877. break; /* stream is likely to be useless now */
  878. if (resp == LIST_PARTIAL)
  879. return;
  880. if (parse_fetch( ctx, ctx->parse_list_sts.head ) < 0)
  881. break; /* this may mean anything, so prefer not to spam the log */
  882. }
  883. } else {
  884. error( "IMAP error: unrecognized untagged response '%s'\n", arg );
  885. break; /* this may mean anything, so prefer not to spam the log */
  886. }
  887. if (greeted == GreetingPending) {
  888. imap_ref( ctx );
  889. imap_open_store_greeted( ctx );
  890. if (imap_deref( ctx ))
  891. return;
  892. }
  893. continue;
  894. } else if (!ctx->in_progress) {
  895. error( "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "" );
  896. break; /* this may mean anything, so prefer not to spam the log */
  897. } else if (*arg == '+') {
  898. /* This can happen only with the last command underway, as
  899. it enforces a round-trip. */
  900. cmdp = ctx->in_progress;
  901. if (cmdp->param.data) {
  902. if (cmdp->param.to_trash)
  903. ctx->trashnc = TrashKnown; /* Can't get NO [TRYCREATE] any more. */
  904. p = cmdp->param.data;
  905. cmdp->param.data = 0;
  906. if (socket_write( &ctx->conn, p, cmdp->param.data_len, GiveOwn ) < 0)
  907. return;
  908. } else if (cmdp->param.cont) {
  909. if (cmdp->param.cont( ctx, cmdp, cmd ))
  910. return;
  911. } else {
  912. error( "IMAP error: unexpected command continuation request\n" );
  913. break;
  914. }
  915. if (socket_write( &ctx->conn, "\r\n", 2, KeepOwn ) < 0)
  916. return;
  917. } else {
  918. tag = atoi( arg );
  919. for (pcmdp = &ctx->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
  920. if (cmdp->tag == tag)
  921. goto gottag;
  922. error( "IMAP error: unexpected tag %s\n", arg );
  923. break;
  924. gottag:
  925. if (!(*pcmdp = cmdp->next))
  926. ctx->in_progress_append = pcmdp;
  927. ctx->num_in_progress--;
  928. arg = next_arg( &cmd );
  929. if (!arg) {
  930. error( "IMAP error: malformed tagged response\n" );
  931. break;
  932. }
  933. if (!strcmp( "OK", arg )) {
  934. if (cmdp->param.to_trash)
  935. ctx->trashnc = TrashKnown; /* Can't get NO [TRYCREATE] any more. */
  936. resp = RESP_OK;
  937. } else {
  938. if (!strcmp( "NO", arg )) {
  939. if (cmdp->param.create &&
  940. (cmdp->param.trycreate ||
  941. (cmd && !memcmp( cmd, "[TRYCREATE]", 11 ))))
  942. { /* SELECT, APPEND or UID COPY */
  943. struct imap_cmd_trycreate *cmd2 =
  944. (struct imap_cmd_trycreate *)new_imap_cmd( sizeof(*cmd2) );
  945. cmd2->orig_cmd = cmdp;
  946. cmd2->gen.param.high_prio = 1;
  947. p = strchr( cmdp->cmd, '"' );
  948. if (imap_exec( ctx, &cmd2->gen, get_cmd_result_p2,
  949. "CREATE %.*s", strchr( p + 1, '"' ) - p + 1, p ) < 0)
  950. return;
  951. continue;
  952. }
  953. resp = RESP_NO;
  954. } else /*if (!strcmp( "BAD", arg ))*/
  955. resp = RESP_CANCEL;
  956. error( "IMAP command '%s' returned an error: %s %s\n",
  957. memcmp( cmdp->cmd, "LOGIN", 5 ) ? cmdp->cmd : "LOGIN <user> <pass>",
  958. arg, cmd ? cmd : "" );
  959. }
  960. if ((resp2 = parse_response_code( ctx, cmdp, cmd )) > resp)
  961. resp = resp2;
  962. imap_ref( ctx );
  963. if (resp == RESP_CANCEL)
  964. imap_invoke_bad_callback( ctx );
  965. done_imap_cmd( ctx, cmdp, resp );
  966. if (imap_deref( ctx ))
  967. return;
  968. if (ctx->canceling && !ctx->in_progress) {
  969. ctx->canceling = 0;
  970. ctx->callbacks.imap_cancel( ctx->callback_aux );
  971. return;
  972. }
  973. }
  974. if (flush_imap_cmds( ctx ) < 0)
  975. return;
  976. }
  977. imap_invoke_bad_callback( ctx );
  978. }
  979. static void
  980. get_cmd_result_p2( imap_store_t *ctx, struct imap_cmd *cmd, int response )
  981. {
  982. struct imap_cmd_trycreate *cmdp = (struct imap_cmd_trycreate *)cmd;
  983. struct imap_cmd *ocmd = cmdp->orig_cmd;
  984. if (response != RESP_OK) {
  985. done_imap_cmd( ctx, ocmd, response );
  986. } else {
  987. ctx->gen.uidnext = 1;
  988. if (ocmd->param.to_trash)
  989. ctx->trashnc = TrashKnown;
  990. ocmd->param.create = 0;
  991. ocmd->param.high_prio = 1;
  992. submit_imap_cmd( ctx, ocmd );
  993. }
  994. }
  995. /******************* imap_cancel_store *******************/
  996. static void
  997. imap_cancel_store( store_t *gctx )
  998. {
  999. imap_store_t *ctx = (imap_store_t *)gctx;
  1000. socket_close( &ctx->conn );
  1001. cancel_submitted_imap_cmds( ctx );
  1002. cancel_pending_imap_cmds( ctx );
  1003. free_generic_messages( ctx->gen.msgs );
  1004. free_string_list( ctx->gen.boxes );
  1005. free_list( ctx->ns_personal );
  1006. free_list( ctx->ns_other );
  1007. free_list( ctx->ns_shared );
  1008. imap_deref( ctx );
  1009. }
  1010. static int
  1011. imap_deref( imap_store_t *ctx )
  1012. {
  1013. if (!--ctx->ref_count) {
  1014. free( ctx );
  1015. return -1;
  1016. }
  1017. return 0;
  1018. }
  1019. static void
  1020. imap_invoke_bad_callback( imap_store_t *ctx )
  1021. {
  1022. ctx->gen.bad_callback( ctx->gen.bad_callback_aux );
  1023. }
  1024. /******************* imap_disown_store & imap_own_store *******************/
  1025. static store_t *unowned;
  1026. static void
  1027. imap_cancel_unowned( void *gctx )
  1028. {
  1029. store_t *store, **storep;
  1030. for (storep = &unowned; (store = *storep); storep = &store->next)
  1031. if (store == gctx) {
  1032. *storep = store->next;
  1033. break;
  1034. }
  1035. imap_cancel_store( gctx );
  1036. }
  1037. static void
  1038. imap_disown_store( store_t *gctx )
  1039. {
  1040. free_generic_messages( gctx->msgs );
  1041. gctx->msgs = 0;
  1042. set_bad_callback( gctx, imap_cancel_unowned, gctx );
  1043. gctx->next = unowned;
  1044. unowned = gctx;
  1045. }
  1046. static store_t *
  1047. imap_own_store( store_conf_t *conf )
  1048. {
  1049. store_t *store, **storep;
  1050. for (storep = &unowned; (store = *storep); storep = &store->next)
  1051. if (store->conf == conf) {
  1052. *storep = store->next;
  1053. return store;
  1054. }
  1055. return 0;
  1056. }
  1057. /******************* imap_cleanup *******************/
  1058. static void imap_cleanup_p2( imap_store_t *, struct imap_cmd *, int );
  1059. static void
  1060. imap_cleanup( void )
  1061. {
  1062. store_t *ctx, *nctx;
  1063. for (ctx = unowned; ctx; ctx = nctx) {
  1064. nctx = ctx->next;
  1065. set_bad_callback( ctx, (void (*)(void *))imap_cancel_store, ctx );
  1066. imap_exec( (imap_store_t *)ctx, 0, imap_cleanup_p2, "LOGOUT" );
  1067. }
  1068. }
  1069. static void
  1070. imap_cleanup_p2( imap_store_t *ctx,
  1071. struct imap_cmd *cmd ATTR_UNUSED, int response )
  1072. {
  1073. if (response != RESP_CANCEL)
  1074. imap_cancel_store( &ctx->gen );
  1075. }
  1076. /******************* imap_open_store *******************/
  1077. #ifdef HAVE_LIBSSL
  1078. static int
  1079. do_cram_auth( imap_store_t *ctx, struct imap_cmd *cmdp, const char *prompt )
  1080. {
  1081. imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server;
  1082. char *resp;
  1083. int l;
  1084. cmdp->param.cont = 0;
  1085. cram( prompt, srvc->user, srvc->pass, &resp, &l );
  1086. if (DFlags & VERBOSE) {
  1087. printf( ">+> %s\n", resp );
  1088. fflush( stdout );
  1089. }
  1090. return socket_write( &ctx->conn, resp, l, GiveOwn );
  1091. }
  1092. #endif
  1093. static void imap_open_store_connected( int, void * );
  1094. #ifdef HAVE_LIBSSL
  1095. static void imap_open_store_tlsstarted1( int, void * );
  1096. #endif
  1097. static void imap_open_store_p2( imap_store_t *, struct imap_cmd *, int );
  1098. static void imap_open_store_authenticate( imap_store_t * );
  1099. #ifdef HAVE_LIBSSL
  1100. static void imap_open_store_authenticate_p2( imap_store_t *, struct imap_cmd *, int );
  1101. static void imap_open_store_tlsstarted2( int, void * );
  1102. static void imap_open_store_authenticate_p3( imap_store_t *, struct imap_cmd *, int );
  1103. #endif
  1104. static void imap_open_store_authenticate2( imap_store_t * );
  1105. static void imap_open_store_authenticate2_p2( imap_store_t *, struct imap_cmd *, int );
  1106. static void imap_open_store_namespace( imap_store_t * );
  1107. static void imap_open_store_namespace_p2( imap_store_t *, struct imap_cmd *, int );
  1108. static void imap_open_store_namespace2( imap_store_t * );
  1109. static void imap_open_store_finalize( imap_store_t * );
  1110. #ifdef HAVE_LIBSSL
  1111. static void imap_open_store_ssl_bail( imap_store_t * );
  1112. #endif
  1113. static void imap_open_store_bail( imap_store_t * );
  1114. static void
  1115. imap_open_store( store_conf_t *conf,
  1116. void (*cb)( store_t *srv, void *aux ), void *aux )
  1117. {
  1118. imap_store_conf_t *cfg = (imap_store_conf_t *)conf;
  1119. imap_server_conf_t *srvc = cfg->server;
  1120. imap_store_t *ctx;
  1121. store_t **ctxp;
  1122. for (ctxp = &unowned; (ctx = (imap_store_t *)*ctxp); ctxp = &ctx->gen.next)
  1123. if (((imap_store_conf_t *)ctx->gen.conf)->server == srvc) {
  1124. *ctxp = ctx->gen.next;
  1125. /* One could ping the server here, but given that the idle timeout
  1126. * is at least 30 minutes, this sounds pretty pointless. */
  1127. free_string_list( ctx->gen.boxes );
  1128. ctx->gen.boxes = 0;
  1129. ctx->gen.listed = 0;
  1130. ctx->gen.conf = conf;
  1131. ctx->delimiter = 0;
  1132. ctx->callbacks.imap_open = cb;
  1133. ctx->callback_aux = aux;
  1134. set_bad_callback( &ctx->gen, (void (*)(void *))imap_open_store_bail, ctx );
  1135. imap_open_store_namespace( ctx );
  1136. return;
  1137. }
  1138. ctx = nfcalloc( sizeof(*ctx) );
  1139. ctx->gen.conf = conf;
  1140. ctx->ref_count = 1;
  1141. ctx->callbacks.imap_open = cb;
  1142. ctx->callback_aux = aux;
  1143. set_bad_callback( &ctx->gen, (void (*)(void *))imap_open_store_bail, ctx );
  1144. ctx->in_progress_append = &ctx->in_progress;
  1145. ctx->pending_append = &ctx->pending;
  1146. socket_init( &ctx->conn, &srvc->sconf,
  1147. (void (*)( void * ))imap_invoke_bad_callback,
  1148. imap_socket_read, (int (*)(void *))flush_imap_cmds, ctx );
  1149. socket_connect( &ctx->conn, imap_open_store_connected );
  1150. }
  1151. static void
  1152. imap_open_store_connected( int ok, void *aux )
  1153. {
  1154. imap_store_t *ctx = (imap_store_t *)aux;
  1155. #ifdef HAVE_LIBSSL
  1156. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1157. imap_server_conf_t *srvc = cfg->server;
  1158. #endif
  1159. if (!ok)
  1160. imap_open_store_bail( ctx );
  1161. #ifdef HAVE_LIBSSL
  1162. else if (srvc->sconf.use_imaps)
  1163. socket_start_tls( &ctx->conn, imap_open_store_tlsstarted1 );
  1164. #endif
  1165. }
  1166. #ifdef HAVE_LIBSSL
  1167. static void
  1168. imap_open_store_tlsstarted1( int ok, void *aux )
  1169. {
  1170. imap_store_t *ctx = (imap_store_t *)aux;
  1171. if (!ok)
  1172. imap_open_store_ssl_bail( ctx );
  1173. }
  1174. #endif
  1175. static void
  1176. imap_open_store_greeted( imap_store_t *ctx )
  1177. {
  1178. if (ctx->greeting == GreetingBad) {
  1179. error( "IMAP error: unknown greeting response\n" );
  1180. imap_open_store_bail( ctx );
  1181. return;
  1182. }
  1183. if (!ctx->caps)
  1184. imap_exec( ctx, 0, imap_open_store_p2, "CAPABILITY" );
  1185. else
  1186. imap_open_store_authenticate( ctx );
  1187. }
  1188. static void
  1189. imap_open_store_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1190. {
  1191. if (response != RESP_OK)
  1192. imap_open_store_bail( ctx );
  1193. else
  1194. imap_open_store_authenticate( ctx );
  1195. }
  1196. static void
  1197. imap_open_store_authenticate( imap_store_t *ctx )
  1198. {
  1199. if (ctx->greeting != GreetingPreauth) {
  1200. #ifdef HAVE_LIBSSL
  1201. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1202. imap_server_conf_t *srvc = cfg->server;
  1203. if (!srvc->sconf.use_imaps &&
  1204. (srvc->sconf.use_sslv2 || srvc->sconf.use_sslv3 || srvc->sconf.use_tlsv1)) {
  1205. /* always try to select SSL support if available */
  1206. if (CAP(STARTTLS)) {
  1207. imap_exec( ctx, 0, imap_open_store_authenticate_p2, "STARTTLS" );
  1208. return;
  1209. } else {
  1210. if (srvc->require_ssl) {
  1211. error( "IMAP error: SSL support not available\n" );
  1212. imap_open_store_bail( ctx );
  1213. return;
  1214. } else {
  1215. warn( "IMAP warning: SSL support not available\n" );
  1216. }
  1217. }
  1218. }
  1219. #endif
  1220. imap_open_store_authenticate2( ctx );
  1221. } else {
  1222. imap_open_store_namespace( ctx );
  1223. }
  1224. }
  1225. #ifdef HAVE_LIBSSL
  1226. static void
  1227. imap_open_store_authenticate_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1228. {
  1229. if (response != RESP_OK)
  1230. imap_open_store_bail( ctx );
  1231. else
  1232. socket_start_tls( &ctx->conn, imap_open_store_tlsstarted2 );
  1233. }
  1234. static void
  1235. imap_open_store_tlsstarted2( int ok, void *aux )
  1236. {
  1237. imap_store_t *ctx = (imap_store_t *)aux;
  1238. if (!ok)
  1239. imap_open_store_ssl_bail( ctx );
  1240. else
  1241. imap_exec( ctx, 0, imap_open_store_authenticate_p3, "CAPABILITY" );
  1242. }
  1243. static void
  1244. imap_open_store_authenticate_p3( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1245. {
  1246. if (response != RESP_OK)
  1247. imap_open_store_bail( ctx );
  1248. else
  1249. imap_open_store_authenticate2( ctx );
  1250. }
  1251. #endif
  1252. static void
  1253. imap_open_store_authenticate2( imap_store_t *ctx )
  1254. {
  1255. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1256. imap_server_conf_t *srvc = cfg->server;
  1257. char *arg;
  1258. info ("Logging in...\n");
  1259. if (!srvc->user) {
  1260. error( "Skipping account %s, no user\n", srvc->name );
  1261. goto bail;
  1262. }
  1263. if (!srvc->pass) {
  1264. char prompt[80];
  1265. sprintf( prompt, "Password (%s): ", srvc->name );
  1266. arg = getpass( prompt );
  1267. if (!arg) {
  1268. perror( "getpass" );
  1269. exit( 1 );
  1270. }
  1271. if (!*arg) {
  1272. error( "Skipping account %s, no password\n", srvc->name );
  1273. goto bail;
  1274. }
  1275. /*
  1276. * getpass() returns a pointer to a static buffer. make a copy
  1277. * for long term storage.
  1278. */
  1279. srvc->pass = nfstrdup( arg );
  1280. }
  1281. #ifdef HAVE_LIBSSL
  1282. if (CAP(CRAM)) {
  1283. struct imap_cmd *cmd = new_imap_cmd( sizeof(*cmd) );
  1284. info( "Authenticating with CRAM-MD5\n" );
  1285. cmd->param.cont = do_cram_auth;
  1286. imap_exec( ctx, cmd, imap_open_store_authenticate2_p2, "AUTHENTICATE CRAM-MD5" );
  1287. return;
  1288. }
  1289. if (srvc->require_cram) {
  1290. error( "IMAP error: CRAM-MD5 authentication is not supported by server\n" );
  1291. goto bail;
  1292. }
  1293. #endif
  1294. if (CAP(NOLOGIN)) {
  1295. error( "Skipping account %s, server forbids LOGIN\n", srvc->name );
  1296. goto bail;
  1297. }
  1298. #ifdef HAVE_LIBSSL
  1299. if (!ctx->conn.ssl)
  1300. #endif
  1301. warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
  1302. imap_exec( ctx, 0, imap_open_store_authenticate2_p2,
  1303. "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass );
  1304. return;
  1305. bail:
  1306. imap_open_store_bail( ctx );
  1307. }
  1308. static void
  1309. imap_open_store_authenticate2_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1310. {
  1311. if (response != RESP_OK)
  1312. imap_open_store_bail( ctx );
  1313. else
  1314. imap_open_store_namespace( ctx );
  1315. }
  1316. static void
  1317. imap_open_store_namespace( imap_store_t *ctx )
  1318. {
  1319. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1320. ctx->prefix = cfg->gen.path;
  1321. ctx->delimiter = cfg->delimiter;
  1322. if (((!*ctx->prefix && cfg->use_namespace) || !cfg->delimiter) && CAP(NAMESPACE)) {
  1323. /* get NAMESPACE info */
  1324. if (!ctx->got_namespace)
  1325. imap_exec( ctx, 0, imap_open_store_namespace_p2, "NAMESPACE" );
  1326. else
  1327. imap_open_store_namespace2( ctx );
  1328. return;
  1329. }
  1330. imap_open_store_finalize( ctx );
  1331. }
  1332. static void
  1333. imap_open_store_namespace_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1334. {
  1335. if (response != RESP_OK) {
  1336. imap_open_store_bail( ctx );
  1337. } else {
  1338. ctx->got_namespace = 1;
  1339. imap_open_store_namespace2( ctx );
  1340. }
  1341. }
  1342. static void
  1343. imap_open_store_namespace2( imap_store_t *ctx )
  1344. {
  1345. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1346. list_t *nsp, *nsp_1st, *nsp_1st_ns, *nsp_1st_dl;
  1347. /* XXX for now assume 1st personal namespace */
  1348. if (is_list( (nsp = ctx->ns_personal) ) &&
  1349. is_list( (nsp_1st = nsp->child) ) &&
  1350. is_atom( (nsp_1st_ns = nsp_1st->child) ) &&
  1351. is_atom( (nsp_1st_dl = nsp_1st_ns->next) ))
  1352. {
  1353. if (!*ctx->prefix && cfg->use_namespace)
  1354. ctx->prefix = nsp_1st_ns->val;
  1355. if (!ctx->delimiter)
  1356. ctx->delimiter = *nsp_1st_dl->val;
  1357. }
  1358. imap_open_store_finalize( ctx );
  1359. }
  1360. static void
  1361. imap_open_store_finalize( imap_store_t *ctx )
  1362. {
  1363. set_bad_callback( &ctx->gen, 0, 0 );
  1364. ctx->trashnc = TrashUnknown;
  1365. ctx->callbacks.imap_open( &ctx->gen, ctx->callback_aux );
  1366. }
  1367. #ifdef HAVE_LIBSSL
  1368. static void
  1369. imap_open_store_ssl_bail( imap_store_t *ctx )
  1370. {
  1371. /* This avoids that we try to send LOGOUT to an unusable socket. */
  1372. socket_close( &ctx->conn );
  1373. imap_open_store_bail( ctx );
  1374. }
  1375. #endif
  1376. static void
  1377. imap_open_store_bail( imap_store_t *ctx )
  1378. {
  1379. void (*cb)( store_t *srv, void *aux ) = ctx->callbacks.imap_open;
  1380. void *aux = ctx->callback_aux;
  1381. imap_cancel_store( &ctx->gen );
  1382. cb( 0, aux );
  1383. }
  1384. /******************* imap_prepare_opts *******************/
  1385. static void
  1386. imap_prepare_opts( store_t *gctx, int opts )
  1387. {
  1388. gctx->opts = opts;
  1389. }
  1390. /******************* imap_select *******************/
  1391. static void
  1392. imap_select( store_t *gctx, int create,
  1393. void (*cb)( int sts, void *aux ), void *aux )
  1394. {
  1395. imap_store_t *ctx = (imap_store_t *)gctx;
  1396. struct imap_cmd_simple *cmd;
  1397. char buf[1024];
  1398. free_generic_messages( gctx->msgs );
  1399. gctx->msgs = 0;
  1400. if (prepare_box( buf, ctx ) < 0) {
  1401. cb( DRV_BOX_BAD, aux );
  1402. return;
  1403. }
  1404. ctx->gen.uidnext = 0;
  1405. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  1406. cmd->gen.param.create = create;
  1407. cmd->gen.param.trycreate = 1;
  1408. imap_exec( ctx, &cmd->gen, imap_done_simple_box,
  1409. "SELECT \"%s\"", buf );
  1410. }
  1411. /******************* imap_load *******************/
  1412. static int imap_submit_load( imap_store_t *, const char *, int, struct imap_cmd_refcounted_state * );
  1413. static void
  1414. imap_load( store_t *gctx, int minuid, int maxuid, int newuid, int *excs, int nexcs,
  1415. void (*cb)( int sts, void *aux ), void *aux )
  1416. {
  1417. imap_store_t *ctx = (imap_store_t *)gctx;
  1418. int i, j, bl;
  1419. char buf[1000];
  1420. if (!ctx->gen.count) {
  1421. free( excs );
  1422. cb( DRV_OK, aux );
  1423. } else {
  1424. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  1425. ctx->msgapp = &ctx->gen.msgs;
  1426. sort_ints( excs, nexcs );
  1427. for (i = 0; i < nexcs; ) {
  1428. for (bl = 0; i < nexcs && bl < 960; i++) {
  1429. if (bl)
  1430. buf[bl++] = ',';
  1431. bl += sprintf( buf + bl, "%d", excs[i] );
  1432. j = i;
  1433. for (; i + 1 < nexcs && excs[i + 1] == excs[i] + 1; i++) {}
  1434. if (i != j)
  1435. bl += sprintf( buf + bl, ":%d", excs[i] );
  1436. }
  1437. if (imap_submit_load( ctx, buf, 0, sts ) < 0)
  1438. goto done;
  1439. }
  1440. if (maxuid == INT_MAX)
  1441. maxuid = ctx->gen.uidnext ? ctx->gen.uidnext - 1 : 1000000000;
  1442. if (maxuid >= minuid) {
  1443. if ((ctx->gen.opts & OPEN_FIND) && minuid < newuid) {
  1444. sprintf( buf, "%d:%d", minuid, newuid - 1 );
  1445. if (imap_submit_load( ctx, buf, 0, sts ) < 0)
  1446. goto done;
  1447. sprintf( buf, "%d:%d", newuid, maxuid );
  1448. } else {
  1449. sprintf( buf, "%d:%d", minuid, maxuid );
  1450. }
  1451. imap_submit_load( ctx, buf, (ctx->gen.opts & OPEN_FIND), sts );
  1452. }
  1453. done:
  1454. free( excs );
  1455. imap_refcounted_done( sts );
  1456. }
  1457. }
  1458. static int
  1459. imap_submit_load( imap_store_t *ctx, const char *buf, int tuids, struct imap_cmd_refcounted_state *sts )
  1460. {
  1461. return imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  1462. "UID FETCH %s (UID%s%s%s)", buf,
  1463. (ctx->gen.opts & OPEN_FLAGS) ? " FLAGS" : "",
  1464. (ctx->gen.opts & OPEN_SIZE) ? " RFC822.SIZE" : "",
  1465. tuids ? " BODY.PEEK[HEADER.FIELDS (X-TUID)]" : "");
  1466. }
  1467. /******************* imap_fetch_msg *******************/
  1468. static void
  1469. imap_fetch_msg( store_t *ctx, message_t *msg, msg_data_t *data,
  1470. void (*cb)( int sts, void *aux ), void *aux )
  1471. {
  1472. struct imap_cmd_fetch_msg *cmd;
  1473. INIT_IMAP_CMD_X(imap_cmd_fetch_msg, cmd, cb, aux)
  1474. cmd->gen.gen.param.uid = msg->uid;
  1475. cmd->msg_data = data;
  1476. imap_exec( (imap_store_t *)ctx, &cmd->gen.gen, imap_done_simple_msg,
  1477. "UID FETCH %d (%sBODY.PEEK[])",
  1478. msg->uid, (msg->status & M_FLAGS) ? "" : "FLAGS " );
  1479. }
  1480. /******************* imap_set_flags *******************/
  1481. static void imap_set_flags_p2( imap_store_t *, struct imap_cmd *, int );
  1482. static int
  1483. imap_make_flags( int flags, char *buf )
  1484. {
  1485. const char *s;
  1486. unsigned i, d;
  1487. for (i = d = 0; i < as(Flags); i++)
  1488. if (flags & (1 << i)) {
  1489. buf[d++] = ' ';
  1490. buf[d++] = '\\';
  1491. for (s = Flags[i]; *s; s++)
  1492. buf[d++] = *s;
  1493. }
  1494. buf[0] = '(';
  1495. buf[d++] = ')';
  1496. return d;
  1497. }
  1498. static int
  1499. imap_flags_helper( imap_store_t *ctx, int uid, char what, int flags,
  1500. struct imap_cmd_refcounted_state *sts )
  1501. {
  1502. char buf[256];
  1503. buf[imap_make_flags( flags, buf )] = 0;
  1504. return imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_set_flags_p2,
  1505. "UID STORE %d %cFLAGS.SILENT %s", uid, what, buf );
  1506. }
  1507. static void
  1508. imap_set_flags( store_t *gctx, message_t *msg, int uid, int add, int del,
  1509. void (*cb)( int sts, void *aux ), void *aux )
  1510. {
  1511. imap_store_t *ctx = (imap_store_t *)gctx;
  1512. if (msg) {
  1513. uid = msg->uid;
  1514. add &= ~msg->flags;
  1515. del &= msg->flags;
  1516. msg->flags |= add;
  1517. msg->flags &= ~del;
  1518. }
  1519. if (add || del) {
  1520. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  1521. if ((add && imap_flags_helper( ctx, uid, '+', add, sts ) < 0) ||
  1522. (del && imap_flags_helper( ctx, uid, '-', del, sts ) < 0)) {}
  1523. imap_refcounted_done( sts );
  1524. } else {
  1525. cb( DRV_OK, aux );
  1526. }
  1527. }
  1528. static void
  1529. imap_set_flags_p2( imap_store_t *ctx ATTR_UNUSED, struct imap_cmd *cmd, int response )
  1530. {
  1531. struct imap_cmd_refcounted_state *sts = ((struct imap_cmd_refcounted *)cmd)->state;
  1532. switch (response) {
  1533. case RESP_CANCEL:
  1534. sts->ret_val = DRV_CANCELED;
  1535. break;
  1536. case RESP_NO:
  1537. if (sts->ret_val == DRV_OK) /* Don't override cancelation. */
  1538. sts->ret_val = DRV_MSG_BAD;
  1539. break;
  1540. }
  1541. imap_refcounted_done( sts );
  1542. }
  1543. /******************* imap_close *******************/
  1544. static void
  1545. imap_close( store_t *gctx,
  1546. void (*cb)( int sts, void *aux ), void *aux )
  1547. {
  1548. imap_store_t *ctx = (imap_store_t *)gctx;
  1549. if (CAP(UIDPLUS)) {
  1550. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  1551. message_t *msg, *fmsg, *nmsg;
  1552. int bl;
  1553. char buf[1000];
  1554. for (msg = ctx->gen.msgs; ; ) {
  1555. for (bl = 0; msg && bl < 960; msg = msg->next) {
  1556. if (!(msg->flags & F_DELETED))
  1557. continue;
  1558. if (bl)
  1559. buf[bl++] = ',';
  1560. bl += sprintf( buf + bl, "%d", msg->uid );
  1561. fmsg = msg;
  1562. for (; (nmsg = msg->next) && (nmsg->flags & F_DELETED); msg = nmsg) {}
  1563. if (msg != fmsg)
  1564. bl += sprintf( buf + bl, ":%d", msg->uid );
  1565. }
  1566. if (!bl)
  1567. break;
  1568. if (imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  1569. "UID EXPUNGE %s", buf ) < 0)
  1570. break;
  1571. }
  1572. imap_refcounted_done( sts );
  1573. } else {
  1574. /* This is inherently racy: it may cause messages which other clients
  1575. * marked as deleted to be expunged without being trashed. */
  1576. struct imap_cmd_simple *cmd;
  1577. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  1578. imap_exec( ctx, &cmd->gen, imap_done_simple_box, "CLOSE" );
  1579. }
  1580. }
  1581. /******************* imap_trash_msg *******************/
  1582. static void
  1583. imap_trash_msg( store_t *gctx, message_t *msg,
  1584. void (*cb)( int sts, void *aux ), void *aux )
  1585. {
  1586. imap_store_t *ctx = (imap_store_t *)gctx;
  1587. struct imap_cmd_simple *cmd;
  1588. char buf[1024];
  1589. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  1590. cmd->gen.param.create = 1;
  1591. cmd->gen.param.to_trash = 1;
  1592. if (prepare_trash( buf, ctx ) < 0) {
  1593. cb( DRV_BOX_BAD, aux );
  1594. return;
  1595. }
  1596. imap_exec( ctx, &cmd->gen, imap_done_simple_msg,
  1597. "UID COPY %d \"%s\"", msg->uid, buf );
  1598. }
  1599. /******************* imap_store_msg *******************/
  1600. static void imap_store_msg_p2( imap_store_t *, struct imap_cmd *, int );
  1601. static void
  1602. imap_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1603. void (*cb)( int sts, int uid, void *aux ), void *aux )
  1604. {
  1605. imap_store_t *ctx = (imap_store_t *)gctx;
  1606. struct imap_cmd_out_uid *cmd;
  1607. int d;
  1608. char flagstr[128], buf[1024];
  1609. d = 0;
  1610. if (data->flags) {
  1611. d = imap_make_flags( data->flags, flagstr );
  1612. flagstr[d++] = ' ';
  1613. }
  1614. flagstr[d] = 0;
  1615. INIT_IMAP_CMD(imap_cmd_out_uid, cmd, cb, aux)
  1616. cmd->gen.param.data_len = data->len;
  1617. cmd->gen.param.data = data->data;
  1618. cmd->out_uid = -2;
  1619. if (to_trash) {
  1620. cmd->gen.param.create = 1;
  1621. cmd->gen.param.to_trash = 1;
  1622. if (prepare_trash( buf, ctx ) < 0) {
  1623. cb( DRV_BOX_BAD, -1, aux );
  1624. return;
  1625. }
  1626. } else {
  1627. if (prepare_box( buf, ctx ) < 0) {
  1628. cb( DRV_BOX_BAD, -1, aux );
  1629. return;
  1630. }
  1631. }
  1632. imap_exec( ctx, &cmd->gen, imap_store_msg_p2,
  1633. "APPEND \"%s\" %s", buf, flagstr );
  1634. }
  1635. static void
  1636. imap_store_msg_p2( imap_store_t *ctx ATTR_UNUSED, struct imap_cmd *cmd, int response )
  1637. {
  1638. struct imap_cmd_out_uid *cmdp = (struct imap_cmd_out_uid *)cmd;
  1639. transform_msg_response( &response );
  1640. cmdp->callback( response, cmdp->out_uid, cmdp->callback_aux );
  1641. }
  1642. /******************* imap_find_new_msgs *******************/
  1643. static void
  1644. imap_find_new_msgs( store_t *gctx,
  1645. void (*cb)( int sts, void *aux ), void *aux )
  1646. {
  1647. imap_store_t *ctx = (imap_store_t *)gctx;
  1648. struct imap_cmd_simple *cmd;
  1649. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  1650. imap_exec( (imap_store_t *)ctx, &cmd->gen, imap_done_simple_box,
  1651. "UID FETCH %d:1000000000 (UID BODY.PEEK[HEADER.FIELDS (X-TUID)])", ctx->gen.uidnext );
  1652. }
  1653. /******************* imap_list *******************/
  1654. static void
  1655. imap_list( store_t *gctx, int flags,
  1656. void (*cb)( int sts, void *aux ), void *aux )
  1657. {
  1658. imap_store_t *ctx = (imap_store_t *)gctx;
  1659. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  1660. if (((flags & LIST_PATH) &&
  1661. imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  1662. "LIST \"\" \"%s*\"", ctx->prefix ) < 0) ||
  1663. ((flags & LIST_INBOX) && (!(flags & LIST_PATH) || *ctx->prefix) &&
  1664. imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  1665. "LIST \"\" INBOX*" ) < 0))
  1666. {}
  1667. imap_refcounted_done( sts );
  1668. }
  1669. /******************* imap_cancel *******************/
  1670. static void
  1671. imap_cancel( store_t *gctx,
  1672. void (*cb)( void *aux ), void *aux )
  1673. {
  1674. imap_store_t *ctx = (imap_store_t *)gctx;
  1675. cancel_pending_imap_cmds( ctx );
  1676. if (ctx->in_progress) {
  1677. ctx->canceling = 1;
  1678. ctx->callbacks.imap_cancel = cb;
  1679. ctx->callback_aux = aux;
  1680. } else {
  1681. cb( aux );
  1682. }
  1683. }
  1684. /******************* imap_commit *******************/
  1685. static void
  1686. imap_commit( store_t *gctx )
  1687. {
  1688. (void)gctx;
  1689. }
  1690. /******************* imap_parse_store *******************/
  1691. imap_server_conf_t *servers, **serverapp = &servers;
  1692. static int
  1693. imap_parse_store( conffile_t *cfg, store_conf_t **storep )
  1694. {
  1695. imap_store_conf_t *store;
  1696. imap_server_conf_t *server, *srv, sserver;
  1697. int acc_opt = 0;
  1698. if (!strcasecmp( "IMAPAccount", cfg->cmd )) {
  1699. server = nfcalloc( sizeof(*server) );
  1700. server->name = nfstrdup( cfg->val );
  1701. *serverapp = server;
  1702. serverapp = &server->next;
  1703. store = 0;
  1704. *storep = 0;
  1705. } else if (!strcasecmp( "IMAPStore", cfg->cmd )) {
  1706. store = nfcalloc( sizeof(*store) );
  1707. store->gen.driver = &imap_driver;
  1708. store->gen.name = nfstrdup( cfg->val );
  1709. store->use_namespace = 1;
  1710. *storep = &store->gen;
  1711. memset( &sserver, 0, sizeof(sserver) );
  1712. server = &sserver;
  1713. } else
  1714. return 0;
  1715. #ifdef HAVE_LIBSSL
  1716. /* this will probably annoy people, but its the best default just in
  1717. * case people forget to turn it on
  1718. */
  1719. server->require_ssl = 1;
  1720. server->sconf.use_tlsv1 = 1;
  1721. #endif
  1722. server->max_in_progress = INT_MAX;
  1723. while (getcline( cfg ) && cfg->cmd) {
  1724. if (!strcasecmp( "Host", cfg->cmd )) {
  1725. /* The imap[s]: syntax is just a backwards compat hack. */
  1726. #ifdef HAVE_LIBSSL
  1727. if (!memcmp( "imaps:", cfg->val, 6 )) {
  1728. cfg->val += 6;
  1729. server->sconf.use_imaps = 1;
  1730. server->sconf.use_sslv2 = 1;
  1731. server->sconf.use_sslv3 = 1;
  1732. } else
  1733. #endif
  1734. {
  1735. if (!memcmp( "imap:", cfg->val, 5 ))
  1736. cfg->val += 5;
  1737. }
  1738. if (!memcmp( "//", cfg->val, 2 ))
  1739. cfg->val += 2;
  1740. server->sconf.host = nfstrdup( cfg->val );
  1741. }
  1742. else if (!strcasecmp( "User", cfg->cmd ))
  1743. server->user = nfstrdup( cfg->val );
  1744. else if (!strcasecmp( "Pass", cfg->cmd ))
  1745. server->pass = nfstrdup( cfg->val );
  1746. else if (!strcasecmp( "Port", cfg->cmd ))
  1747. server->sconf.port = parse_int( cfg );
  1748. else if (!strcasecmp( "PipelineDepth", cfg->cmd )) {
  1749. if ((server->max_in_progress = parse_int( cfg )) < 1) {
  1750. error( "%s:%d: PipelineDepth must be at least 1\n", cfg->file, cfg->line );
  1751. cfg->err = 1;
  1752. }
  1753. }
  1754. #ifdef HAVE_LIBSSL
  1755. else if (!strcasecmp( "CertificateFile", cfg->cmd )) {
  1756. server->sconf.cert_file = expand_strdup( cfg->val );
  1757. if (access( server->sconf.cert_file, R_OK )) {
  1758. sys_error( "%s:%d: CertificateFile '%s'",
  1759. cfg->file, cfg->line, server->sconf.cert_file );
  1760. cfg->err = 1;
  1761. }
  1762. } else if (!strcasecmp( "RequireSSL", cfg->cmd ))
  1763. server->require_ssl = parse_bool( cfg );
  1764. else if (!strcasecmp( "UseIMAPS", cfg->cmd ))
  1765. server->sconf.use_imaps = parse_bool( cfg );
  1766. else if (!strcasecmp( "UseSSLv2", cfg->cmd ))
  1767. server->sconf.use_sslv2 = parse_bool( cfg );
  1768. else if (!strcasecmp( "UseSSLv3", cfg->cmd ))
  1769. server->sconf.use_sslv3 = parse_bool( cfg );
  1770. else if (!strcasecmp( "UseTLSv1", cfg->cmd ))
  1771. server->sconf.use_tlsv1 = parse_bool( cfg );
  1772. else if (!strcasecmp( "RequireCRAM", cfg->cmd ))
  1773. server->require_cram = parse_bool( cfg );
  1774. #endif
  1775. else if (!strcasecmp( "Tunnel", cfg->cmd ))
  1776. server->sconf.tunnel = nfstrdup( cfg->val );
  1777. else if (store) {
  1778. if (!strcasecmp( "Account", cfg->cmd )) {
  1779. for (srv = servers; srv; srv = srv->next)
  1780. if (srv->name && !strcmp( srv->name, cfg->val ))
  1781. goto gotsrv;
  1782. error( "%s:%d: unknown IMAP account '%s'\n", cfg->file, cfg->line, cfg->val );
  1783. cfg->err = 1;
  1784. continue;
  1785. gotsrv:
  1786. store->server = srv;
  1787. } else if (!strcasecmp( "UseNamespace", cfg->cmd ))
  1788. store->use_namespace = parse_bool( cfg );
  1789. else if (!strcasecmp( "Path", cfg->cmd ))
  1790. store->gen.path = nfstrdup( cfg->val );
  1791. else if (!strcasecmp( "PathDelimiter", cfg->cmd ))
  1792. store->delimiter = *cfg->val;
  1793. else
  1794. parse_generic_store( &store->gen, cfg );
  1795. continue;
  1796. } else {
  1797. error( "%s:%d: unknown/misplaced keyword '%s'\n", cfg->file, cfg->line, cfg->cmd );
  1798. cfg->err = 1;
  1799. continue;
  1800. }
  1801. acc_opt = 1;
  1802. }
  1803. if (!store || !store->server) {
  1804. if (!server->sconf.tunnel && !server->sconf.host) {
  1805. if (store)
  1806. error( "IMAP store '%s' has incomplete/missing connection details\n", store->gen.name );
  1807. else
  1808. error( "IMAP account '%s' has incomplete/missing connection details\n", server->name );
  1809. cfg->err = 1;
  1810. return 1;
  1811. }
  1812. }
  1813. if (store) {
  1814. if (!store->server) {
  1815. store->server = nfmalloc( sizeof(sserver) );
  1816. memcpy( store->server, &sserver, sizeof(sserver) );
  1817. store->server->name = store->gen.name;
  1818. } else if (acc_opt) {
  1819. error( "IMAP store '%s' has both Account and account-specific options\n", store->gen.name );
  1820. cfg->err = 1;
  1821. }
  1822. }
  1823. return 1;
  1824. }
  1825. struct driver imap_driver = {
  1826. DRV_CRLF,
  1827. imap_parse_store,
  1828. imap_cleanup,
  1829. imap_open_store,
  1830. imap_disown_store,
  1831. imap_own_store,
  1832. imap_cancel_store,
  1833. imap_list,
  1834. imap_prepare_opts,
  1835. imap_select,
  1836. imap_load,
  1837. imap_fetch_msg,
  1838. imap_store_msg,
  1839. imap_find_new_msgs,
  1840. imap_set_flags,
  1841. imap_trash_msg,
  1842. imap_close,
  1843. imap_cancel,
  1844. imap_commit,
  1845. };