drv_imap.c 49 KB

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