drv_imap.c 51 KB

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