drv_imap.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2008,2010-2013 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 "driver.h"
  24. #include "socket.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 <string.h>
  32. #include <ctype.h>
  33. #include <time.h>
  34. #include <sys/wait.h>
  35. #ifdef HAVE_LIBSASL
  36. # include <sasl/sasl.h>
  37. # include <sasl/saslutil.h>
  38. #endif
  39. #ifdef HAVE_LIBSSL
  40. enum { SSL_None, SSL_STARTTLS, SSL_IMAPS };
  41. #endif
  42. typedef struct imap_server_conf {
  43. struct imap_server_conf *next;
  44. char *name;
  45. server_conf_t sconf;
  46. char *user;
  47. char *pass;
  48. char *pass_cmd;
  49. int max_in_progress;
  50. string_list_t *auth_mechs;
  51. #ifdef HAVE_LIBSSL
  52. char ssl_type;
  53. #endif
  54. } imap_server_conf_t;
  55. typedef struct imap_store_conf {
  56. store_conf_t gen;
  57. imap_server_conf_t *server;
  58. char *delimiter;
  59. char use_namespace;
  60. } imap_store_conf_t;
  61. typedef struct imap_message {
  62. message_t gen;
  63. /* int seq; will be needed when expunges are tracked */
  64. } imap_message_t;
  65. #define NIL (void*)0x1
  66. #define LIST (void*)0x2
  67. typedef struct _list {
  68. struct _list *next, *child;
  69. char *val;
  70. int len;
  71. } list_t;
  72. #define MAX_LIST_DEPTH 5
  73. struct imap_store;
  74. typedef struct parse_list_state {
  75. list_t *head, **stack[MAX_LIST_DEPTH];
  76. int (*callback)( struct imap_store *ctx, list_t *list, char *cmd );
  77. int level, need_bytes;
  78. } parse_list_state_t;
  79. struct imap_cmd;
  80. typedef struct imap_store {
  81. store_t gen;
  82. const char *label; /* foreign */
  83. const char *prefix;
  84. const char *name;
  85. int ref_count;
  86. /* trash folder's existence is not confirmed yet */
  87. enum { TrashUnknown, TrashChecking, TrashKnown } trashnc;
  88. uint got_namespace:1;
  89. char *delimiter; /* hierarchy delimiter */
  90. list_t *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
  91. message_t **msgapp; /* FETCH results */
  92. uint caps; /* CAPABILITY results */
  93. string_list_t *auth_mechs;
  94. parse_list_state_t parse_list_sts;
  95. /* command queue */
  96. int nexttag, num_in_progress;
  97. struct imap_cmd *pending, **pending_append;
  98. struct imap_cmd *in_progress, **in_progress_append;
  99. int buffer_mem; /* memory currently occupied by buffers in the queue */
  100. /* Used during sequential operations like connect */
  101. enum { GreetingPending = 0, GreetingBad, GreetingOk, GreetingPreauth } greeting;
  102. int expectBYE; /* LOGOUT is in progress */
  103. int expectEOF; /* received LOGOUT's OK or unsolicited BYE */
  104. int canceling; /* imap_cancel() is in progress */
  105. union {
  106. void (*imap_open)( store_t *srv, void *aux );
  107. void (*imap_cancel)( void *aux );
  108. } callbacks;
  109. void *callback_aux;
  110. #ifdef HAVE_LIBSASL
  111. sasl_conn_t *sasl;
  112. int sasl_cont;
  113. #endif
  114. conn_t conn; /* this is BIG, so put it last */
  115. } imap_store_t;
  116. struct imap_cmd {
  117. struct imap_cmd *next;
  118. char *cmd;
  119. int tag;
  120. struct {
  121. /* Will be called on each continuation request until it resets this pointer.
  122. * Needs to invoke bad_callback and return -1 on error, otherwise return 0. */
  123. int (*cont)( imap_store_t *ctx, struct imap_cmd *cmd, const char *prompt );
  124. void (*done)( imap_store_t *ctx, struct imap_cmd *cmd, int response );
  125. char *data;
  126. int data_len;
  127. int uid; /* to identify fetch responses */
  128. char high_prio; /* if command is queued, put it at the front of the queue. */
  129. char to_trash; /* we are storing to trash, not current. */
  130. char create; /* create the mailbox if we get an error which suggests so. */
  131. char failok; /* Don't complain about NO response. */
  132. } param;
  133. };
  134. struct imap_cmd_simple {
  135. struct imap_cmd gen;
  136. void (*callback)( int sts, void *aux );
  137. void *callback_aux;
  138. };
  139. struct imap_cmd_fetch_msg {
  140. struct imap_cmd_simple gen;
  141. msg_data_t *msg_data;
  142. };
  143. struct imap_cmd_out_uid {
  144. struct imap_cmd gen;
  145. void (*callback)( int sts, int uid, void *aux );
  146. void *callback_aux;
  147. int out_uid;
  148. };
  149. struct imap_cmd_find_new {
  150. struct imap_cmd_simple gen;
  151. int uid;
  152. };
  153. struct imap_cmd_refcounted_state {
  154. void (*callback)( int sts, void *aux );
  155. void *callback_aux;
  156. int ref_count;
  157. int ret_val;
  158. };
  159. struct imap_cmd_refcounted {
  160. struct imap_cmd gen;
  161. struct imap_cmd_refcounted_state *state;
  162. };
  163. #define CAP(cap) (ctx->caps & (1 << (cap)))
  164. enum CAPABILITY {
  165. NOLOGIN = 0,
  166. #ifdef HAVE_LIBSASL
  167. SASLIR,
  168. #endif
  169. #ifdef HAVE_LIBSSL
  170. STARTTLS,
  171. #endif
  172. UIDPLUS,
  173. LITERALPLUS,
  174. MOVE,
  175. NAMESPACE,
  176. COMPRESS_DEFLATE
  177. };
  178. static const char *cap_list[] = {
  179. "LOGINDISABLED",
  180. #ifdef HAVE_LIBSASL
  181. "SASL-IR",
  182. #endif
  183. #ifdef HAVE_LIBSSL
  184. "STARTTLS",
  185. #endif
  186. "UIDPLUS",
  187. "LITERAL+",
  188. "MOVE",
  189. "NAMESPACE",
  190. "COMPRESS=DEFLATE"
  191. };
  192. #define RESP_OK 0
  193. #define RESP_NO 1
  194. #define RESP_CANCEL 2
  195. static INLINE void imap_ref( imap_store_t *ctx ) { ++ctx->ref_count; }
  196. static int imap_deref( imap_store_t *ctx );
  197. static void imap_invoke_bad_callback( imap_store_t *ctx );
  198. static const char *Flags[] = {
  199. "Draft",
  200. "Flagged",
  201. "Answered",
  202. "Seen",
  203. "Deleted",
  204. };
  205. static struct imap_cmd *
  206. new_imap_cmd( int size )
  207. {
  208. struct imap_cmd *cmd = nfmalloc( size );
  209. memset( &cmd->param, 0, sizeof(cmd->param) );
  210. return cmd;
  211. }
  212. #define INIT_IMAP_CMD(type, cmdp, cb, aux) \
  213. cmdp = (struct type *)new_imap_cmd( sizeof(*cmdp) ); \
  214. cmdp->callback = cb; \
  215. cmdp->callback_aux = aux;
  216. #define INIT_IMAP_CMD_X(type, cmdp, cb, aux) \
  217. cmdp = (struct type *)new_imap_cmd( sizeof(*cmdp) ); \
  218. cmdp->gen.callback = cb; \
  219. cmdp->gen.callback_aux = aux;
  220. static void
  221. done_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd, int response )
  222. {
  223. cmd->param.done( ctx, cmd, response );
  224. if (cmd->param.data) {
  225. free( cmd->param.data );
  226. ctx->buffer_mem -= cmd->param.data_len;
  227. }
  228. free( cmd->cmd );
  229. free( cmd );
  230. }
  231. static int
  232. send_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd )
  233. {
  234. int bufl, litplus, iovcnt = 1;
  235. const char *buffmt;
  236. conn_iovec_t iov[3];
  237. char buf[1024];
  238. cmd->tag = ++ctx->nexttag;
  239. if (!cmd->param.data) {
  240. buffmt = "%d %s\r\n";
  241. litplus = 0;
  242. } else if ((cmd->param.to_trash && ctx->trashnc == TrashUnknown) || !CAP(LITERALPLUS) || cmd->param.data_len >= 100*1024) {
  243. buffmt = "%d %s{%d}\r\n";
  244. litplus = 0;
  245. } else {
  246. buffmt = "%d %s{%d+}\r\n";
  247. litplus = 1;
  248. }
  249. bufl = nfsnprintf( buf, sizeof(buf), buffmt,
  250. cmd->tag, cmd->cmd, cmd->param.data_len );
  251. if (DFlags & DEBUG_NET) {
  252. if (ctx->num_in_progress)
  253. printf( "(%d in progress) ", ctx->num_in_progress );
  254. if (starts_with( cmd->cmd, -1, "LOGIN", 5 ))
  255. printf( "%s>>> %d LOGIN <user> <pass>\n", ctx->label, cmd->tag );
  256. else if (starts_with( cmd->cmd, -1, "AUTHENTICATE PLAIN", 18 ))
  257. printf( "%s>>> %d AUTHENTICATE PLAIN <authdata>\n", ctx->label, cmd->tag );
  258. else
  259. printf( "%s>>> %s", ctx->label, buf );
  260. fflush( stdout );
  261. }
  262. iov[0].buf = buf;
  263. iov[0].len = bufl;
  264. iov[0].takeOwn = KeepOwn;
  265. if (litplus) {
  266. iov[1].buf = cmd->param.data;
  267. iov[1].len = cmd->param.data_len;
  268. iov[1].takeOwn = GiveOwn;
  269. cmd->param.data = 0;
  270. ctx->buffer_mem -= cmd->param.data_len;
  271. iov[2].buf = "\r\n";
  272. iov[2].len = 2;
  273. iov[2].takeOwn = KeepOwn;
  274. iovcnt = 3;
  275. }
  276. if (socket_write( &ctx->conn, iov, iovcnt ) < 0)
  277. goto bail;
  278. if (cmd->param.to_trash && ctx->trashnc == TrashUnknown)
  279. ctx->trashnc = TrashChecking;
  280. cmd->next = 0;
  281. *ctx->in_progress_append = cmd;
  282. ctx->in_progress_append = &cmd->next;
  283. ctx->num_in_progress++;
  284. return 0;
  285. bail:
  286. done_imap_cmd( ctx, cmd, RESP_CANCEL );
  287. return -1;
  288. }
  289. static int
  290. cmd_submittable( imap_store_t *ctx, struct imap_cmd *cmd )
  291. {
  292. struct imap_cmd *cmdp;
  293. return !ctx->conn.write_buf &&
  294. !(ctx->in_progress &&
  295. (cmdp = (struct imap_cmd *)((char *)ctx->in_progress_append -
  296. offsetof(struct imap_cmd, next)), 1) &&
  297. (cmdp->param.cont || cmdp->param.data)) &&
  298. !(cmd->param.to_trash && ctx->trashnc == TrashChecking) &&
  299. ctx->num_in_progress < ((imap_store_conf_t *)ctx->gen.conf)->server->max_in_progress;
  300. }
  301. static int
  302. flush_imap_cmds( imap_store_t *ctx )
  303. {
  304. struct imap_cmd *cmd;
  305. while ((cmd = ctx->pending) && cmd_submittable( ctx, cmd )) {
  306. if (!(ctx->pending = cmd->next))
  307. ctx->pending_append = &ctx->pending;
  308. if (send_imap_cmd( ctx, cmd ) < 0)
  309. return -1;
  310. }
  311. return 0;
  312. }
  313. static void
  314. cancel_pending_imap_cmds( imap_store_t *ctx )
  315. {
  316. struct imap_cmd *cmd;
  317. while ((cmd = ctx->pending)) {
  318. if (!(ctx->pending = cmd->next))
  319. ctx->pending_append = &ctx->pending;
  320. done_imap_cmd( ctx, cmd, RESP_CANCEL );
  321. }
  322. }
  323. static void
  324. cancel_submitted_imap_cmds( imap_store_t *ctx )
  325. {
  326. struct imap_cmd *cmd;
  327. while ((cmd = ctx->in_progress)) {
  328. ctx->in_progress = cmd->next;
  329. /* don't update num_in_progress and in_progress_append - store is dead */
  330. done_imap_cmd( ctx, cmd, RESP_CANCEL );
  331. }
  332. }
  333. static int
  334. submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd )
  335. {
  336. assert( ctx );
  337. assert( ctx->gen.bad_callback );
  338. assert( cmd );
  339. assert( cmd->param.done );
  340. if ((ctx->pending && !cmd->param.high_prio) || !cmd_submittable( ctx, cmd )) {
  341. if (ctx->pending && cmd->param.high_prio) {
  342. cmd->next = ctx->pending;
  343. ctx->pending = cmd;
  344. } else {
  345. cmd->next = 0;
  346. *ctx->pending_append = cmd;
  347. ctx->pending_append = &cmd->next;
  348. }
  349. return 0;
  350. }
  351. return send_imap_cmd( ctx, cmd );
  352. }
  353. /* Minimal printf() replacement that supports an %\s format sequence to print backslash-escaped
  354. * string literals. Note that this does not automatically add quotes around the printed string,
  355. * so it is possible to concatenate multiple segments. */
  356. static char *
  357. imap_vprintf( const char *fmt, va_list ap )
  358. {
  359. const char *s, *es;
  360. char *d, *ed;
  361. int maxlen;
  362. char c;
  363. char buf[1024]; /* Minimal supported command buffer size per IMAP spec. */
  364. d = buf;
  365. ed = d + sizeof(buf);
  366. s = fmt;
  367. for (;;) {
  368. c = *fmt;
  369. if (!c || c == '%') {
  370. int l = fmt - s;
  371. if (d + l > ed)
  372. oob();
  373. memcpy( d, s, l );
  374. d += l;
  375. if (!c) {
  376. l = d - buf;
  377. ed = nfmalloc( l + 1 );
  378. memcpy( ed, buf, l );
  379. ed[l] = 0;
  380. return ed;
  381. }
  382. maxlen = INT_MAX;
  383. c = *++fmt;
  384. if (c == '\\') {
  385. c = *++fmt;
  386. if (c != 's') {
  387. fputs( "Fatal: unsupported escaped format specifier. Please report a bug.\n", stderr );
  388. abort();
  389. }
  390. s = va_arg( ap, const char * );
  391. while ((c = *s++)) {
  392. if (d + 2 > ed)
  393. oob();
  394. if (c == '\\' || c == '"')
  395. *d++ = '\\';
  396. *d++ = c;
  397. }
  398. } else { /* \\ cannot be combined with anything else. */
  399. if (c == '.') {
  400. c = *++fmt;
  401. if (c != '*') {
  402. fputs( "Fatal: unsupported string length specification. Please report a bug.\n", stderr );
  403. abort();
  404. }
  405. maxlen = va_arg( ap , int );
  406. c = *++fmt;
  407. }
  408. if (c == 'c') {
  409. if (d + 1 > ed)
  410. oob();
  411. *d++ = (char)va_arg( ap , int );
  412. } else if (c == 's') {
  413. s = va_arg( ap, const char * );
  414. es = memchr( s, 0, maxlen );
  415. l = es ? es - s : maxlen;
  416. if (d + l > ed)
  417. oob();
  418. memcpy( d, s, l );
  419. d += l;
  420. } else if (c == 'd') {
  421. d += nfsnprintf( d, ed - d, "%d", va_arg( ap , int ) );
  422. } else {
  423. fputs( "Fatal: unsupported format specifier. Please report a bug.\n", stderr );
  424. abort();
  425. }
  426. }
  427. s = ++fmt;
  428. } else {
  429. fmt++;
  430. }
  431. }
  432. }
  433. static int
  434. imap_exec( imap_store_t *ctx, struct imap_cmd *cmdp,
  435. void (*done)( imap_store_t *ctx, struct imap_cmd *cmd, int response ),
  436. const char *fmt, ... )
  437. {
  438. va_list ap;
  439. if (!cmdp)
  440. cmdp = new_imap_cmd( sizeof(*cmdp) );
  441. cmdp->param.done = done;
  442. va_start( ap, fmt );
  443. cmdp->cmd = imap_vprintf( fmt, ap );
  444. va_end( ap );
  445. return submit_imap_cmd( ctx, cmdp );
  446. }
  447. static void
  448. transform_box_response( int *response )
  449. {
  450. switch (*response) {
  451. case RESP_CANCEL: *response = DRV_CANCELED; break;
  452. case RESP_NO: *response = DRV_BOX_BAD; break;
  453. default: *response = DRV_OK; break;
  454. }
  455. }
  456. static void
  457. imap_done_simple_box( imap_store_t *ctx ATTR_UNUSED,
  458. struct imap_cmd *cmd, int response )
  459. {
  460. struct imap_cmd_simple *cmdp = (struct imap_cmd_simple *)cmd;
  461. transform_box_response( &response );
  462. cmdp->callback( response, cmdp->callback_aux );
  463. }
  464. static void
  465. transform_msg_response( int *response )
  466. {
  467. switch (*response) {
  468. case RESP_CANCEL: *response = DRV_CANCELED; break;
  469. case RESP_NO: *response = DRV_MSG_BAD; break;
  470. default: *response = DRV_OK; break;
  471. }
  472. }
  473. static void
  474. imap_done_simple_msg( imap_store_t *ctx ATTR_UNUSED,
  475. struct imap_cmd *cmd, int response )
  476. {
  477. struct imap_cmd_simple *cmdp = (struct imap_cmd_simple *)cmd;
  478. transform_msg_response( &response );
  479. cmdp->callback( response, cmdp->callback_aux );
  480. }
  481. static struct imap_cmd_refcounted_state *
  482. imap_refcounted_new_state( void (*cb)( int, void * ), void *aux )
  483. {
  484. struct imap_cmd_refcounted_state *sts = nfmalloc( sizeof(*sts) );
  485. sts->callback = cb;
  486. sts->callback_aux = aux;
  487. sts->ref_count = 1; /* so forced sync does not cause an early exit */
  488. sts->ret_val = DRV_OK;
  489. return sts;
  490. }
  491. static struct imap_cmd *
  492. imap_refcounted_new_cmd( struct imap_cmd_refcounted_state *sts )
  493. {
  494. struct imap_cmd_refcounted *cmd = (struct imap_cmd_refcounted *)new_imap_cmd( sizeof(*cmd) );
  495. cmd->state = sts;
  496. sts->ref_count++;
  497. return &cmd->gen;
  498. }
  499. static void
  500. imap_refcounted_done( struct imap_cmd_refcounted_state *sts )
  501. {
  502. if (!--sts->ref_count) {
  503. sts->callback( sts->ret_val, sts->callback_aux );
  504. free( sts );
  505. }
  506. }
  507. static void
  508. imap_refcounted_done_box( imap_store_t *ctx ATTR_UNUSED, struct imap_cmd *cmd, int response )
  509. {
  510. struct imap_cmd_refcounted_state *sts = ((struct imap_cmd_refcounted *)cmd)->state;
  511. switch (response) {
  512. case RESP_CANCEL:
  513. sts->ret_val = DRV_CANCELED;
  514. break;
  515. case RESP_NO:
  516. if (sts->ret_val == DRV_OK) /* Don't override cancelation. */
  517. sts->ret_val = DRV_BOX_BAD;
  518. break;
  519. }
  520. imap_refcounted_done( sts );
  521. }
  522. static const char *
  523. imap_strchr( const char *s, char tc )
  524. {
  525. for (;; s++) {
  526. char c = *s;
  527. if (c == '\\')
  528. c = *++s;
  529. if (!c)
  530. return 0;
  531. if (c == tc)
  532. return s;
  533. }
  534. }
  535. static char *
  536. next_arg( char **ps )
  537. {
  538. char *ret, *s, *d;
  539. char c;
  540. assert( ps );
  541. s = *ps;
  542. if (!s)
  543. return 0;
  544. while (isspace( (uchar)*s ))
  545. s++;
  546. if (!*s) {
  547. *ps = 0;
  548. return 0;
  549. }
  550. if (*s == '"') {
  551. s++;
  552. ret = d = s;
  553. while ((c = *s++) != '"') {
  554. if (c == '\\')
  555. c = *s++;
  556. if (!c) {
  557. *ps = 0;
  558. return 0;
  559. }
  560. *d++ = c;
  561. }
  562. *d = 0;
  563. } else {
  564. ret = s;
  565. while ((c = *s)) {
  566. if (isspace( (uchar)c )) {
  567. *s++ = 0;
  568. break;
  569. }
  570. s++;
  571. }
  572. }
  573. if (!*s)
  574. s = 0;
  575. *ps = s;
  576. return ret;
  577. }
  578. static int
  579. is_atom( list_t *list )
  580. {
  581. return list && list->val && list->val != NIL && list->val != LIST;
  582. }
  583. static int
  584. is_list( list_t *list )
  585. {
  586. return list && list->val == LIST;
  587. }
  588. static void
  589. free_list( list_t *list )
  590. {
  591. list_t *tmp;
  592. for (; list; list = tmp) {
  593. tmp = list->next;
  594. if (is_list( list ))
  595. free_list( list->child );
  596. else if (is_atom( list ))
  597. free( list->val );
  598. free( list );
  599. }
  600. }
  601. enum {
  602. LIST_OK,
  603. LIST_PARTIAL,
  604. LIST_BAD
  605. };
  606. static int
  607. parse_imap_list( imap_store_t *ctx, char **sp, parse_list_state_t *sts )
  608. {
  609. list_t *cur, **curp;
  610. char *s = *sp, *d, *p;
  611. int n, bytes;
  612. char c;
  613. assert( sts );
  614. assert( sts->level > 0 );
  615. curp = sts->stack[--sts->level];
  616. bytes = sts->need_bytes;
  617. if (bytes >= 0) {
  618. sts->need_bytes = -1;
  619. if (!bytes)
  620. goto getline;
  621. cur = (list_t *)((char *)curp - offsetof(list_t, next));
  622. s = cur->val + cur->len - bytes;
  623. goto getbytes;
  624. }
  625. if (!s)
  626. return LIST_BAD;
  627. for (;;) {
  628. while (isspace( (uchar)*s ))
  629. s++;
  630. if (sts->level && *s == ')') {
  631. s++;
  632. curp = sts->stack[--sts->level];
  633. goto next;
  634. }
  635. *curp = cur = nfmalloc( sizeof(*cur) );
  636. cur->val = 0; /* for clean bail */
  637. curp = &cur->next;
  638. *curp = 0; /* ditto */
  639. if (*s == '(') {
  640. /* sublist */
  641. if (sts->level == MAX_LIST_DEPTH)
  642. goto bail;
  643. s++;
  644. cur->val = LIST;
  645. sts->stack[sts->level++] = curp;
  646. curp = &cur->child;
  647. *curp = 0; /* for clean bail */
  648. goto next2;
  649. } else if (ctx && *s == '{') {
  650. /* literal */
  651. bytes = cur->len = strtol( s + 1, &s, 10 );
  652. if (*s != '}' || *++s)
  653. goto bail;
  654. s = cur->val = nfmalloc( cur->len + 1 );
  655. s[cur->len] = 0;
  656. getbytes:
  657. n = socket_read( &ctx->conn, s, bytes );
  658. if (n < 0) {
  659. badeof:
  660. error( "IMAP error: unexpected EOF from %s\n", ctx->conn.name );
  661. goto bail;
  662. }
  663. bytes -= n;
  664. if (bytes > 0)
  665. goto postpone;
  666. if (DFlags & DEBUG_NET_ALL) {
  667. printf( "%s=========\n", ctx->label );
  668. fwrite( cur->val, cur->len, 1, stdout );
  669. printf( "%s=========\n", ctx->label );
  670. fflush( stdout );
  671. }
  672. getline:
  673. if (!(s = socket_read_line( &ctx->conn )))
  674. goto postpone;
  675. if (s == (void *)~0)
  676. goto badeof;
  677. if (DFlags & DEBUG_NET) {
  678. printf( "%s%s\n", ctx->label, s );
  679. fflush( stdout );
  680. }
  681. } else if (*s == '"') {
  682. /* quoted string */
  683. s++;
  684. p = d = s;
  685. while ((c = *s++) != '"') {
  686. if (c == '\\')
  687. c = *s++;
  688. if (!c)
  689. goto bail;
  690. *d++ = c;
  691. }
  692. cur->len = d - p;
  693. cur->val = nfmalloc( cur->len + 1 );
  694. memcpy( cur->val, p, cur->len );
  695. cur->val[cur->len] = 0;
  696. } else {
  697. /* atom */
  698. p = s;
  699. for (; *s && !isspace( (uchar)*s ); s++)
  700. if (sts->level && *s == ')')
  701. break;
  702. cur->len = s - p;
  703. if (equals( p, cur->len, "NIL", 3 ))
  704. cur->val = NIL;
  705. else {
  706. cur->val = nfmalloc( cur->len + 1 );
  707. memcpy( cur->val, p, cur->len );
  708. cur->val[cur->len] = 0;
  709. }
  710. }
  711. next:
  712. if (!sts->level)
  713. break;
  714. next2:
  715. if (!*s)
  716. goto bail;
  717. }
  718. *sp = s;
  719. return LIST_OK;
  720. postpone:
  721. if (sts->level < MAX_LIST_DEPTH) {
  722. sts->stack[sts->level++] = curp;
  723. sts->need_bytes = bytes;
  724. return LIST_PARTIAL;
  725. }
  726. bail:
  727. free_list( sts->head );
  728. return LIST_BAD;
  729. }
  730. static void
  731. parse_list_init( parse_list_state_t *sts )
  732. {
  733. sts->need_bytes = -1;
  734. sts->level = 1;
  735. sts->head = 0;
  736. sts->stack[0] = &sts->head;
  737. }
  738. static int
  739. parse_list_continue( imap_store_t *ctx, char *s )
  740. {
  741. list_t *list;
  742. int resp;
  743. if ((resp = parse_imap_list( ctx, &s, &ctx->parse_list_sts )) != LIST_PARTIAL) {
  744. list = (resp == LIST_BAD) ? 0 : ctx->parse_list_sts.head;
  745. ctx->parse_list_sts.head = 0;
  746. resp = ctx->parse_list_sts.callback( ctx, list, s );
  747. }
  748. return resp;
  749. }
  750. static int
  751. parse_list( imap_store_t *ctx, char *s, int (*cb)( imap_store_t *ctx, list_t *list, char *s ) )
  752. {
  753. parse_list_init( &ctx->parse_list_sts );
  754. ctx->parse_list_sts.callback = cb;
  755. return parse_list_continue( ctx, s );
  756. }
  757. static int parse_namespace_rsp_p2( imap_store_t *, list_t *, char * );
  758. static int parse_namespace_rsp_p3( imap_store_t *, list_t *, char * );
  759. static int
  760. parse_namespace_rsp_fail( void )
  761. {
  762. error( "IMAP error: malformed NAMESPACE response\n" );
  763. return LIST_BAD;
  764. }
  765. static int
  766. parse_namespace_rsp( imap_store_t *ctx, list_t *list, char *s )
  767. {
  768. if (!(ctx->ns_personal = list))
  769. return parse_namespace_rsp_fail();
  770. return parse_list( ctx, s, parse_namespace_rsp_p2 );
  771. }
  772. static int
  773. parse_namespace_rsp_p2( imap_store_t *ctx, list_t *list, char *s )
  774. {
  775. if (!(ctx->ns_other = list))
  776. return parse_namespace_rsp_fail();
  777. return parse_list( ctx, s, parse_namespace_rsp_p3 );
  778. }
  779. static int
  780. parse_namespace_rsp_p3( imap_store_t *ctx, list_t *list, char *s ATTR_UNUSED )
  781. {
  782. if (!(ctx->ns_shared = list))
  783. return parse_namespace_rsp_fail();
  784. return LIST_OK;
  785. }
  786. static time_t
  787. parse_date( const char *str )
  788. {
  789. char *end;
  790. time_t date;
  791. int hours, mins;
  792. struct tm datetime;
  793. memset( &datetime, 0, sizeof(datetime) );
  794. if (!(end = strptime( str, "%d-%b-%Y %H:%M:%S ", &datetime )))
  795. return -1;
  796. if ((date = timegm( &datetime )) == -1)
  797. return -1;
  798. if (sscanf( end, "%3d%2d", &hours, &mins ) != 2)
  799. return -1;
  800. return date - (hours * 60 + mins) * 60;
  801. }
  802. static int
  803. parse_fetch_rsp( imap_store_t *ctx, list_t *list, char *s ATTR_UNUSED )
  804. {
  805. list_t *tmp, *flags;
  806. char *body = 0, *tuid = 0;
  807. imap_message_t *cur;
  808. msg_data_t *msgdata;
  809. struct imap_cmd *cmdp;
  810. int uid = 0, mask = 0, status = 0, size = 0;
  811. uint i;
  812. time_t date = 0;
  813. if (!is_list( list )) {
  814. error( "IMAP error: bogus FETCH response\n" );
  815. free_list( list );
  816. return LIST_BAD;
  817. }
  818. for (tmp = list->child; tmp; tmp = tmp->next) {
  819. if (is_atom( tmp )) {
  820. if (!strcmp( "UID", tmp->val )) {
  821. tmp = tmp->next;
  822. if (is_atom( tmp ))
  823. uid = atoi( tmp->val );
  824. else
  825. error( "IMAP error: unable to parse UID\n" );
  826. } else if (!strcmp( "FLAGS", tmp->val )) {
  827. tmp = tmp->next;
  828. if (is_list( tmp )) {
  829. for (flags = tmp->child; flags; flags = flags->next) {
  830. if (is_atom( flags )) {
  831. if (flags->val[0] == '\\') { /* ignore user-defined flags for now */
  832. if (!strcmp( "Recent", flags->val + 1)) {
  833. status |= M_RECENT;
  834. goto flagok;
  835. }
  836. for (i = 0; i < as(Flags); i++)
  837. if (!strcmp( Flags[i], flags->val + 1 )) {
  838. mask |= 1 << i;
  839. goto flagok;
  840. }
  841. if (flags->val[1] == 'X' && flags->val[2] == '-')
  842. goto flagok; /* ignore system flag extensions */
  843. error( "IMAP warning: unknown system flag %s\n", flags->val );
  844. }
  845. flagok: ;
  846. } else
  847. error( "IMAP error: unable to parse FLAGS list\n" );
  848. }
  849. status |= M_FLAGS;
  850. } else
  851. error( "IMAP error: unable to parse FLAGS\n" );
  852. } else if (!strcmp( "INTERNALDATE", tmp->val )) {
  853. tmp = tmp->next;
  854. if (is_atom( tmp )) {
  855. if ((date = parse_date( tmp->val )) == -1)
  856. error( "IMAP error: unable to parse INTERNALDATE format\n" );
  857. } else
  858. error( "IMAP error: unable to parse INTERNALDATE\n" );
  859. } else if (!strcmp( "RFC822.SIZE", tmp->val )) {
  860. tmp = tmp->next;
  861. if (is_atom( tmp ))
  862. size = atoi( tmp->val );
  863. else
  864. error( "IMAP error: unable to parse RFC822.SIZE\n" );
  865. } else if (!strcmp( "BODY[]", tmp->val )) {
  866. tmp = tmp->next;
  867. if (is_atom( tmp )) {
  868. body = tmp->val;
  869. tmp->val = 0; /* don't free together with list */
  870. size = tmp->len;
  871. } else
  872. error( "IMAP error: unable to parse BODY[]\n" );
  873. } else if (!strcmp( "BODY[HEADER.FIELDS", tmp->val )) {
  874. tmp = tmp->next;
  875. if (is_list( tmp )) {
  876. tmp = tmp->next;
  877. if (!is_atom( tmp ) || strcmp( tmp->val, "]" ))
  878. goto bfail;
  879. tmp = tmp->next;
  880. if (!is_atom( tmp ))
  881. goto bfail;
  882. if (starts_with( tmp->val, tmp->len, "X-TUID: ", 8 ))
  883. tuid = tmp->val + 8;
  884. } else {
  885. bfail:
  886. error( "IMAP error: unable to parse BODY[HEADER.FIELDS ...]\n" );
  887. }
  888. }
  889. }
  890. }
  891. if (body) {
  892. for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
  893. if (cmdp->param.uid == uid)
  894. goto gotuid;
  895. error( "IMAP error: unexpected FETCH response (UID %d)\n", uid );
  896. free_list( list );
  897. return LIST_BAD;
  898. gotuid:
  899. msgdata = ((struct imap_cmd_fetch_msg *)cmdp)->msg_data;
  900. msgdata->data = body;
  901. msgdata->len = size;
  902. msgdata->date = date;
  903. if (status & M_FLAGS)
  904. msgdata->flags = mask;
  905. } else if (uid) { /* ignore async flag updates for now */
  906. /* XXX this will need sorting for out-of-order (multiple queries) */
  907. cur = nfcalloc( sizeof(*cur) );
  908. *ctx->msgapp = &cur->gen;
  909. ctx->msgapp = &cur->gen.next;
  910. cur->gen.next = 0;
  911. cur->gen.uid = uid;
  912. cur->gen.flags = mask;
  913. cur->gen.status = status;
  914. cur->gen.size = size;
  915. cur->gen.srec = 0;
  916. if (tuid)
  917. strncpy( cur->gen.tuid, tuid, TUIDL );
  918. else
  919. cur->gen.tuid[0] = 0;
  920. if (ctx->gen.uidnext <= uid) /* in case the server sends no UIDNEXT */
  921. ctx->gen.uidnext = uid + 1;
  922. }
  923. free_list( list );
  924. return LIST_OK;
  925. }
  926. static void
  927. parse_capability( imap_store_t *ctx, char *cmd )
  928. {
  929. char *arg;
  930. uint i;
  931. free_string_list( ctx->auth_mechs );
  932. ctx->auth_mechs = 0;
  933. ctx->caps = 0x80000000;
  934. while ((arg = next_arg( &cmd ))) {
  935. if (starts_with( arg, -1, "AUTH=", 5 )) {
  936. add_string_list( &ctx->auth_mechs, arg + 5 );
  937. } else {
  938. for (i = 0; i < as(cap_list); i++)
  939. if (!strcmp( cap_list[i], arg ))
  940. ctx->caps |= 1 << i;
  941. }
  942. }
  943. if (!CAP(NOLOGIN))
  944. add_string_list( &ctx->auth_mechs, "LOGIN" );
  945. }
  946. static int
  947. parse_response_code( imap_store_t *ctx, struct imap_cmd *cmd, char *s )
  948. {
  949. char *arg, *earg, *p;
  950. if (!s || *s != '[')
  951. return RESP_OK; /* no response code */
  952. s++;
  953. if (!(p = strchr( s, ']' ))) {
  954. bad_resp:
  955. error( "IMAP error: malformed response code\n" );
  956. return RESP_CANCEL;
  957. }
  958. *p++ = 0;
  959. if (!(arg = next_arg( &s )))
  960. goto bad_resp;
  961. if (!strcmp( "UIDVALIDITY", arg )) {
  962. if (!(arg = next_arg( &s )) ||
  963. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg))
  964. {
  965. error( "IMAP error: malformed UIDVALIDITY status\n" );
  966. return RESP_CANCEL;
  967. }
  968. } else if (!strcmp( "UIDNEXT", arg )) {
  969. if (!(arg = next_arg( &s )) || !(ctx->gen.uidnext = atoi( arg ))) {
  970. error( "IMAP error: malformed NEXTUID status\n" );
  971. return RESP_CANCEL;
  972. }
  973. } else if (!strcmp( "CAPABILITY", arg )) {
  974. parse_capability( ctx, s );
  975. } else if (!strcmp( "ALERT", arg )) {
  976. /* RFC2060 says that these messages MUST be displayed
  977. * to the user
  978. */
  979. for (; isspace( (uchar)*p ); p++);
  980. error( "*** IMAP ALERT *** %s\n", p );
  981. } else if (cmd && !strcmp( "APPENDUID", arg )) {
  982. if (!(arg = next_arg( &s )) ||
  983. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg) ||
  984. !(arg = next_arg( &s )) ||
  985. !(((struct imap_cmd_out_uid *)cmd)->out_uid = atoi( arg )))
  986. {
  987. error( "IMAP error: malformed APPENDUID status\n" );
  988. return RESP_CANCEL;
  989. }
  990. }
  991. return RESP_OK;
  992. }
  993. static int parse_list_rsp_p2( imap_store_t *, list_t *, char * );
  994. static int
  995. parse_list_rsp( imap_store_t *ctx, list_t *list, char *cmd )
  996. {
  997. char *arg;
  998. list_t *lp;
  999. if (!is_list( list )) {
  1000. free_list( list );
  1001. bad_list:
  1002. error( "IMAP error: malformed LIST response\n" );
  1003. return LIST_BAD;
  1004. }
  1005. for (lp = list->child; lp; lp = lp->next)
  1006. if (is_atom( lp ) && !strcasecmp( lp->val, "\\NoSelect" )) {
  1007. free_list( list );
  1008. return LIST_OK;
  1009. }
  1010. free_list( list );
  1011. if (!(arg = next_arg( &cmd )))
  1012. goto bad_list;
  1013. if (!ctx->delimiter)
  1014. ctx->delimiter = nfstrdup( arg );
  1015. return parse_list( ctx, cmd, parse_list_rsp_p2 );
  1016. }
  1017. static int
  1018. is_inbox( imap_store_t *ctx, const char *arg, int argl )
  1019. {
  1020. int i;
  1021. char c;
  1022. if (!starts_with( arg, argl, "INBOX", 5 ))
  1023. return 0;
  1024. if (arg[5])
  1025. for (i = 0; (c = ctx->delimiter[i]); i++)
  1026. if (arg[i + 5] != c)
  1027. return 0;
  1028. return 1;
  1029. }
  1030. static int
  1031. parse_list_rsp_p2( imap_store_t *ctx, list_t *list, char *cmd ATTR_UNUSED )
  1032. {
  1033. string_list_t *narg;
  1034. char *arg;
  1035. int argl, l;
  1036. if (!is_atom( list )) {
  1037. error( "IMAP error: malformed LIST response\n" );
  1038. free_list( list );
  1039. return LIST_BAD;
  1040. }
  1041. arg = list->val;
  1042. argl = list->len;
  1043. if ((l = strlen( ctx->prefix ))) {
  1044. if (starts_with( arg, argl, ctx->prefix, l )) {
  1045. arg += l;
  1046. argl -= l;
  1047. if (is_inbox( ctx, arg, argl )) {
  1048. if (!arg[5])
  1049. warn( "IMAP warning: ignoring INBOX in %s\n", ctx->prefix );
  1050. goto skip;
  1051. }
  1052. } else if (!is_inbox( ctx, arg, argl )) {
  1053. goto skip;
  1054. }
  1055. }
  1056. if (argl >= 5 && !memcmp( arg + argl - 5, ".lock", 5 )) /* workaround broken servers */
  1057. goto skip;
  1058. if (map_name( arg, (char **)&narg, offsetof(string_list_t, string), ctx->delimiter, "/") < 0) {
  1059. warn( "IMAP warning: ignoring mailbox %s (reserved character '/' in name)\n", arg );
  1060. goto skip;
  1061. }
  1062. narg->next = ctx->gen.boxes;
  1063. ctx->gen.boxes = narg;
  1064. skip:
  1065. free_list( list );
  1066. return LIST_OK;
  1067. }
  1068. static int
  1069. prepare_name( char **buf, const imap_store_t *ctx, const char *prefix, const char *name )
  1070. {
  1071. int pl = strlen( prefix );
  1072. switch (map_name( name, buf, pl, "/", ctx->delimiter )) {
  1073. case -1:
  1074. error( "IMAP error: mailbox name %s contains server's hierarchy delimiter\n", name );
  1075. return -1;
  1076. case -2:
  1077. error( "IMAP error: server's hierarchy delimiter not known\n" );
  1078. return -1;
  1079. default:
  1080. memcpy( *buf, prefix, pl );
  1081. return 0;
  1082. }
  1083. }
  1084. static int
  1085. prepare_box( char **buf, const imap_store_t *ctx )
  1086. {
  1087. const char *name = ctx->name;
  1088. return prepare_name( buf, ctx,
  1089. (starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) ? "" : ctx->prefix, name );
  1090. }
  1091. static int
  1092. prepare_trash( char **buf, const imap_store_t *ctx )
  1093. {
  1094. return prepare_name( buf, ctx, ctx->prefix, ctx->gen.conf->trash );
  1095. }
  1096. struct imap_cmd_trycreate {
  1097. struct imap_cmd gen;
  1098. struct imap_cmd *orig_cmd;
  1099. };
  1100. static void imap_open_store_greeted( imap_store_t * );
  1101. static void get_cmd_result_p2( imap_store_t *, struct imap_cmd *, int );
  1102. static void
  1103. imap_socket_read( void *aux )
  1104. {
  1105. imap_store_t *ctx = (imap_store_t *)aux;
  1106. struct imap_cmd *cmdp, **pcmdp;
  1107. char *cmd, *arg, *arg1, *p;
  1108. int resp, resp2, tag;
  1109. conn_iovec_t iov[2];
  1110. for (;;) {
  1111. if (ctx->parse_list_sts.level) {
  1112. resp = parse_list_continue( ctx, 0 );
  1113. listret:
  1114. if (resp == LIST_PARTIAL)
  1115. return;
  1116. if (resp == LIST_BAD)
  1117. break;
  1118. continue;
  1119. }
  1120. if (!(cmd = socket_read_line( &ctx->conn )))
  1121. return;
  1122. if (cmd == (void *)~0) {
  1123. if (!ctx->expectEOF)
  1124. error( "IMAP error: unexpected EOF from %s\n", ctx->conn.name );
  1125. /* A clean shutdown sequence ends with bad_callback as well (see imap_cleanup()). */
  1126. break;
  1127. }
  1128. if (DFlags & DEBUG_NET) {
  1129. printf( "%s%s\n", ctx->label, cmd );
  1130. fflush( stdout );
  1131. }
  1132. arg = next_arg( &cmd );
  1133. if (!arg) {
  1134. error( "IMAP error: empty response\n" );
  1135. break;
  1136. }
  1137. if (*arg == '*') {
  1138. arg = next_arg( &cmd );
  1139. if (!arg) {
  1140. error( "IMAP error: malformed untagged response\n" );
  1141. break;
  1142. }
  1143. if (ctx->greeting == GreetingPending && !strcmp( "PREAUTH", arg )) {
  1144. parse_response_code( ctx, 0, cmd );
  1145. ctx->greeting = GreetingPreauth;
  1146. dogreet:
  1147. imap_ref( ctx );
  1148. imap_open_store_greeted( ctx );
  1149. if (imap_deref( ctx ))
  1150. return;
  1151. } else if (!strcmp( "OK", arg )) {
  1152. parse_response_code( ctx, 0, cmd );
  1153. if (ctx->greeting == GreetingPending) {
  1154. ctx->greeting = GreetingOk;
  1155. goto dogreet;
  1156. }
  1157. } else if (!strcmp( "BYE", arg )) {
  1158. if (!ctx->expectBYE) {
  1159. ctx->greeting = GreetingBad;
  1160. error( "IMAP error: unexpected BYE response: %s\n", cmd );
  1161. /* We just wait for the server to close the connection now. */
  1162. ctx->expectEOF = 1;
  1163. } else {
  1164. /* We still need to wait for the LOGOUT's tagged OK. */
  1165. }
  1166. } else if (ctx->greeting == GreetingPending) {
  1167. error( "IMAP error: bogus greeting response %s\n", arg );
  1168. break;
  1169. } else if (!strcmp( "NO", arg )) {
  1170. warn( "Warning from IMAP server: %s\n", cmd );
  1171. } else if (!strcmp( "BAD", arg )) {
  1172. error( "Error from IMAP server: %s\n", cmd );
  1173. } else if (!strcmp( "CAPABILITY", arg )) {
  1174. parse_capability( ctx, cmd );
  1175. } else if (!strcmp( "LIST", arg )) {
  1176. resp = parse_list( ctx, cmd, parse_list_rsp );
  1177. goto listret;
  1178. } else if (!strcmp( "NAMESPACE", arg )) {
  1179. resp = parse_list( ctx, cmd, parse_namespace_rsp );
  1180. goto listret;
  1181. } else if ((arg1 = next_arg( &cmd ))) {
  1182. if (!strcmp( "EXISTS", arg1 ))
  1183. ctx->gen.count = atoi( arg );
  1184. else if (!strcmp( "RECENT", arg1 ))
  1185. ctx->gen.recent = atoi( arg );
  1186. else if(!strcmp ( "FETCH", arg1 )) {
  1187. resp = parse_list( ctx, cmd, parse_fetch_rsp );
  1188. goto listret;
  1189. }
  1190. } else {
  1191. error( "IMAP error: unrecognized untagged response '%s'\n", arg );
  1192. break; /* this may mean anything, so prefer not to spam the log */
  1193. }
  1194. continue;
  1195. } else if (!ctx->in_progress) {
  1196. error( "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "" );
  1197. break; /* this may mean anything, so prefer not to spam the log */
  1198. } else if (*arg == '+') {
  1199. /* There can be any number of commands in flight, but only the last
  1200. * one can require a continuation, as it enforces a round-trip. */
  1201. cmdp = (struct imap_cmd *)((char *)ctx->in_progress_append -
  1202. offsetof(struct imap_cmd, next));
  1203. if (cmdp->param.data) {
  1204. if (cmdp->param.to_trash)
  1205. ctx->trashnc = TrashKnown; /* Can't get NO [TRYCREATE] any more. */
  1206. iov[0].buf = cmdp->param.data;
  1207. iov[0].len = cmdp->param.data_len;
  1208. iov[0].takeOwn = GiveOwn;
  1209. cmdp->param.data = 0;
  1210. ctx->buffer_mem -= cmdp->param.data_len;
  1211. iov[1].buf = "\r\n";
  1212. iov[1].len = 2;
  1213. iov[1].takeOwn = KeepOwn;
  1214. if (socket_write( &ctx->conn, iov, 2 ) < 0)
  1215. return;
  1216. } else if (cmdp->param.cont) {
  1217. if (cmdp->param.cont( ctx, cmdp, cmd ))
  1218. return;
  1219. } else {
  1220. error( "IMAP error: unexpected command continuation request\n" );
  1221. break;
  1222. }
  1223. } else {
  1224. tag = atoi( arg );
  1225. for (pcmdp = &ctx->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
  1226. if (cmdp->tag == tag)
  1227. goto gottag;
  1228. error( "IMAP error: unexpected tag %s\n", arg );
  1229. break;
  1230. gottag:
  1231. if (!(*pcmdp = cmdp->next))
  1232. ctx->in_progress_append = pcmdp;
  1233. ctx->num_in_progress--;
  1234. arg = next_arg( &cmd );
  1235. if (!arg) {
  1236. error( "IMAP error: malformed tagged response\n" );
  1237. break;
  1238. }
  1239. if (!strcmp( "OK", arg )) {
  1240. if (cmdp->param.to_trash)
  1241. ctx->trashnc = TrashKnown; /* Can't get NO [TRYCREATE] any more. */
  1242. resp = RESP_OK;
  1243. } else {
  1244. if (!strcmp( "NO", arg )) {
  1245. if (cmdp->param.create && cmd && starts_with( cmd, -1, "[TRYCREATE]", 11 )) { /* APPEND or UID COPY */
  1246. struct imap_cmd_trycreate *cmd2 =
  1247. (struct imap_cmd_trycreate *)new_imap_cmd( sizeof(*cmd2) );
  1248. cmd2->orig_cmd = cmdp;
  1249. cmd2->gen.param.high_prio = 1;
  1250. p = strchr( cmdp->cmd, '"' );
  1251. if (imap_exec( ctx, &cmd2->gen, get_cmd_result_p2,
  1252. "CREATE %.*s", imap_strchr( p + 1, '"' ) - p + 1, p ) < 0)
  1253. return;
  1254. continue;
  1255. }
  1256. resp = RESP_NO;
  1257. if (cmdp->param.failok)
  1258. goto doresp;
  1259. } else /*if (!strcmp( "BAD", arg ))*/
  1260. resp = RESP_CANCEL;
  1261. error( "IMAP command '%s' returned an error: %s %s\n",
  1262. !starts_with( cmdp->cmd, -1, "LOGIN", 5 ) ? cmdp->cmd : "LOGIN <user> <pass>",
  1263. arg, cmd ? cmd : "" );
  1264. }
  1265. doresp:
  1266. if ((resp2 = parse_response_code( ctx, cmdp, cmd )) > resp)
  1267. resp = resp2;
  1268. imap_ref( ctx );
  1269. if (resp == RESP_CANCEL)
  1270. imap_invoke_bad_callback( ctx );
  1271. done_imap_cmd( ctx, cmdp, resp );
  1272. if (imap_deref( ctx ))
  1273. return;
  1274. if (ctx->canceling && !ctx->in_progress) {
  1275. ctx->canceling = 0;
  1276. ctx->callbacks.imap_cancel( ctx->callback_aux );
  1277. return;
  1278. }
  1279. }
  1280. if (flush_imap_cmds( ctx ) < 0)
  1281. return;
  1282. }
  1283. imap_invoke_bad_callback( ctx );
  1284. }
  1285. static void
  1286. get_cmd_result_p2( imap_store_t *ctx, struct imap_cmd *cmd, int response )
  1287. {
  1288. struct imap_cmd_trycreate *cmdp = (struct imap_cmd_trycreate *)cmd;
  1289. struct imap_cmd *ocmd = cmdp->orig_cmd;
  1290. if (response != RESP_OK) {
  1291. done_imap_cmd( ctx, ocmd, response );
  1292. } else {
  1293. ctx->gen.uidnext = 1;
  1294. if (ocmd->param.to_trash)
  1295. ctx->trashnc = TrashKnown;
  1296. ocmd->param.create = 0;
  1297. ocmd->param.high_prio = 1;
  1298. submit_imap_cmd( ctx, ocmd );
  1299. }
  1300. }
  1301. /******************* imap_cancel_store *******************/
  1302. static void
  1303. imap_cancel_store( store_t *gctx )
  1304. {
  1305. imap_store_t *ctx = (imap_store_t *)gctx;
  1306. #ifdef HAVE_LIBSASL
  1307. sasl_dispose( &ctx->sasl );
  1308. #endif
  1309. socket_close( &ctx->conn );
  1310. cancel_submitted_imap_cmds( ctx );
  1311. cancel_pending_imap_cmds( ctx );
  1312. free_generic_messages( ctx->gen.msgs );
  1313. free_string_list( ctx->gen.boxes );
  1314. free_list( ctx->ns_personal );
  1315. free_list( ctx->ns_other );
  1316. free_list( ctx->ns_shared );
  1317. free_string_list( ctx->auth_mechs );
  1318. free( ctx->delimiter );
  1319. imap_deref( ctx );
  1320. }
  1321. static int
  1322. imap_deref( imap_store_t *ctx )
  1323. {
  1324. if (!--ctx->ref_count) {
  1325. free( ctx );
  1326. return -1;
  1327. }
  1328. return 0;
  1329. }
  1330. static void
  1331. imap_invoke_bad_callback( imap_store_t *ctx )
  1332. {
  1333. ctx->gen.bad_callback( ctx->gen.bad_callback_aux );
  1334. }
  1335. /******************* imap_disown_store *******************/
  1336. static store_t *unowned;
  1337. static void
  1338. imap_cancel_unowned( void *gctx )
  1339. {
  1340. store_t *store, **storep;
  1341. for (storep = &unowned; (store = *storep); storep = &store->next)
  1342. if (store == gctx) {
  1343. *storep = store->next;
  1344. break;
  1345. }
  1346. imap_cancel_store( gctx );
  1347. }
  1348. static void
  1349. imap_disown_store( store_t *gctx )
  1350. {
  1351. free_generic_messages( gctx->msgs );
  1352. gctx->msgs = 0;
  1353. set_bad_callback( gctx, imap_cancel_unowned, gctx );
  1354. gctx->next = unowned;
  1355. unowned = gctx;
  1356. }
  1357. /******************* imap_cleanup *******************/
  1358. static void imap_cleanup_p2( imap_store_t *, struct imap_cmd *, int );
  1359. static void
  1360. imap_cleanup( void )
  1361. {
  1362. store_t *ctx, *nctx;
  1363. for (ctx = unowned; ctx; ctx = nctx) {
  1364. nctx = ctx->next;
  1365. set_bad_callback( ctx, (void (*)(void *))imap_cancel_store, ctx );
  1366. ((imap_store_t *)ctx)->expectBYE = 1;
  1367. imap_exec( (imap_store_t *)ctx, 0, imap_cleanup_p2, "LOGOUT" );
  1368. }
  1369. }
  1370. static void
  1371. imap_cleanup_p2( imap_store_t *ctx,
  1372. struct imap_cmd *cmd ATTR_UNUSED, int response )
  1373. {
  1374. if (response == RESP_NO)
  1375. imap_cancel_store( &ctx->gen );
  1376. else if (response == RESP_OK)
  1377. ctx->expectEOF = 1;
  1378. }
  1379. /******************* imap_open_store *******************/
  1380. static void imap_open_store_connected( int, void * );
  1381. #ifdef HAVE_LIBSSL
  1382. static void imap_open_store_tlsstarted1( int, void * );
  1383. #endif
  1384. static void imap_open_store_p2( imap_store_t *, struct imap_cmd *, int );
  1385. static void imap_open_store_authenticate( imap_store_t * );
  1386. #ifdef HAVE_LIBSSL
  1387. static void imap_open_store_authenticate_p2( imap_store_t *, struct imap_cmd *, int );
  1388. static void imap_open_store_tlsstarted2( int, void * );
  1389. static void imap_open_store_authenticate_p3( imap_store_t *, struct imap_cmd *, int );
  1390. #endif
  1391. static void imap_open_store_authenticate2( imap_store_t * );
  1392. static void imap_open_store_authenticate2_p2( imap_store_t *, struct imap_cmd *, int );
  1393. static void imap_open_store_namespace( imap_store_t * );
  1394. static void imap_open_store_namespace_p2( imap_store_t *, struct imap_cmd *, int );
  1395. static void imap_open_store_namespace2( imap_store_t * );
  1396. static void imap_open_store_compress( imap_store_t * );
  1397. #ifdef HAVE_LIBZ
  1398. static void imap_open_store_compress_p2( imap_store_t *, struct imap_cmd *, int );
  1399. #endif
  1400. static void imap_open_store_finalize( imap_store_t * );
  1401. #ifdef HAVE_LIBSSL
  1402. static void imap_open_store_ssl_bail( imap_store_t * );
  1403. #endif
  1404. static void imap_open_store_bail( imap_store_t *, int );
  1405. static void
  1406. imap_open_store_bad( void *aux )
  1407. {
  1408. imap_open_store_bail( (imap_store_t *)aux, FAIL_TEMP );
  1409. }
  1410. static void
  1411. imap_open_store( store_conf_t *conf, const char *label,
  1412. void (*cb)( store_t *srv, void *aux ), void *aux )
  1413. {
  1414. imap_store_conf_t *cfg = (imap_store_conf_t *)conf;
  1415. imap_server_conf_t *srvc = cfg->server;
  1416. imap_store_t *ctx;
  1417. store_t **ctxp;
  1418. for (ctxp = &unowned; (ctx = (imap_store_t *)*ctxp); ctxp = &ctx->gen.next)
  1419. if (ctx->gen.conf == conf) {
  1420. *ctxp = ctx->gen.next;
  1421. ctx->label = label;
  1422. cb( &ctx->gen, aux );
  1423. return;
  1424. }
  1425. for (ctxp = &unowned; (ctx = (imap_store_t *)*ctxp); ctxp = &ctx->gen.next)
  1426. if (((imap_store_conf_t *)ctx->gen.conf)->server == srvc) {
  1427. *ctxp = ctx->gen.next;
  1428. ctx->label = label;
  1429. /* One could ping the server here, but given that the idle timeout
  1430. * is at least 30 minutes, this sounds pretty pointless. */
  1431. free_string_list( ctx->gen.boxes );
  1432. ctx->gen.boxes = 0;
  1433. ctx->gen.listed = 0;
  1434. ctx->gen.conf = conf;
  1435. free( ctx->delimiter );
  1436. ctx->delimiter = 0;
  1437. ctx->callbacks.imap_open = cb;
  1438. ctx->callback_aux = aux;
  1439. set_bad_callback( &ctx->gen, imap_open_store_bad, ctx );
  1440. imap_open_store_namespace( ctx );
  1441. return;
  1442. }
  1443. ctx = nfcalloc( sizeof(*ctx) );
  1444. ctx->gen.conf = conf;
  1445. ctx->label = label;
  1446. ctx->ref_count = 1;
  1447. ctx->callbacks.imap_open = cb;
  1448. ctx->callback_aux = aux;
  1449. set_bad_callback( &ctx->gen, imap_open_store_bad, ctx );
  1450. ctx->in_progress_append = &ctx->in_progress;
  1451. ctx->pending_append = &ctx->pending;
  1452. socket_init( &ctx->conn, &srvc->sconf,
  1453. (void (*)( void * ))imap_invoke_bad_callback,
  1454. imap_socket_read, (int (*)(void *))flush_imap_cmds, ctx );
  1455. socket_connect( &ctx->conn, imap_open_store_connected );
  1456. }
  1457. static void
  1458. imap_open_store_connected( int ok, void *aux )
  1459. {
  1460. imap_store_t *ctx = (imap_store_t *)aux;
  1461. #ifdef HAVE_LIBSSL
  1462. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1463. imap_server_conf_t *srvc = cfg->server;
  1464. #endif
  1465. if (!ok)
  1466. imap_open_store_bail( ctx, FAIL_WAIT );
  1467. #ifdef HAVE_LIBSSL
  1468. else if (srvc->ssl_type == SSL_IMAPS)
  1469. socket_start_tls( &ctx->conn, imap_open_store_tlsstarted1 );
  1470. #endif
  1471. }
  1472. #ifdef HAVE_LIBSSL
  1473. static void
  1474. imap_open_store_tlsstarted1( int ok, void *aux )
  1475. {
  1476. imap_store_t *ctx = (imap_store_t *)aux;
  1477. if (!ok)
  1478. imap_open_store_ssl_bail( ctx );
  1479. }
  1480. #endif
  1481. static void
  1482. imap_open_store_greeted( imap_store_t *ctx )
  1483. {
  1484. if (!ctx->caps)
  1485. imap_exec( ctx, 0, imap_open_store_p2, "CAPABILITY" );
  1486. else
  1487. imap_open_store_authenticate( ctx );
  1488. }
  1489. static void
  1490. imap_open_store_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1491. {
  1492. if (response == RESP_NO)
  1493. imap_open_store_bail( ctx, FAIL_FINAL );
  1494. else if (response == RESP_OK)
  1495. imap_open_store_authenticate( ctx );
  1496. }
  1497. static void
  1498. imap_open_store_authenticate( imap_store_t *ctx )
  1499. {
  1500. #ifdef HAVE_LIBSSL
  1501. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1502. imap_server_conf_t *srvc = cfg->server;
  1503. #endif
  1504. if (ctx->greeting != GreetingPreauth) {
  1505. #ifdef HAVE_LIBSSL
  1506. if (srvc->ssl_type == SSL_STARTTLS) {
  1507. if (CAP(STARTTLS)) {
  1508. imap_exec( ctx, 0, imap_open_store_authenticate_p2, "STARTTLS" );
  1509. return;
  1510. } else {
  1511. error( "IMAP error: SSL support not available\n" );
  1512. imap_open_store_bail( ctx, FAIL_FINAL );
  1513. return;
  1514. }
  1515. }
  1516. #endif
  1517. imap_open_store_authenticate2( ctx );
  1518. } else {
  1519. #ifdef HAVE_LIBSSL
  1520. if (srvc->ssl_type == SSL_STARTTLS) {
  1521. error( "IMAP error: SSL support not available\n" );
  1522. imap_open_store_bail( ctx, FAIL_FINAL );
  1523. return;
  1524. }
  1525. #endif
  1526. imap_open_store_namespace( ctx );
  1527. }
  1528. }
  1529. #ifdef HAVE_LIBSSL
  1530. static void
  1531. imap_open_store_authenticate_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1532. {
  1533. if (response == RESP_NO)
  1534. imap_open_store_bail( ctx, FAIL_FINAL );
  1535. else if (response == RESP_OK)
  1536. socket_start_tls( &ctx->conn, imap_open_store_tlsstarted2 );
  1537. }
  1538. static void
  1539. imap_open_store_tlsstarted2( int ok, void *aux )
  1540. {
  1541. imap_store_t *ctx = (imap_store_t *)aux;
  1542. if (!ok)
  1543. imap_open_store_ssl_bail( ctx );
  1544. else
  1545. imap_exec( ctx, 0, imap_open_store_authenticate_p3, "CAPABILITY" );
  1546. }
  1547. static void
  1548. imap_open_store_authenticate_p3( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1549. {
  1550. if (response == RESP_NO)
  1551. imap_open_store_bail( ctx, FAIL_FINAL );
  1552. else if (response == RESP_OK)
  1553. imap_open_store_authenticate2( ctx );
  1554. }
  1555. #endif
  1556. static const char *
  1557. ensure_user( imap_server_conf_t *srvc )
  1558. {
  1559. if (!srvc->user) {
  1560. error( "Skipping account %s, no user\n", srvc->name );
  1561. return 0;
  1562. }
  1563. return srvc->user;
  1564. }
  1565. static const char *
  1566. ensure_password( imap_server_conf_t *srvc )
  1567. {
  1568. char *cmd = srvc->pass_cmd;
  1569. if (cmd) {
  1570. FILE *fp;
  1571. int ret;
  1572. char buffer[80];
  1573. if (*cmd == '+') {
  1574. flushn();
  1575. cmd++;
  1576. }
  1577. if (!(fp = popen( cmd, "r" ))) {
  1578. pipeerr:
  1579. sys_error( "Skipping account %s, password command failed", srvc->name );
  1580. return 0;
  1581. }
  1582. if (!fgets( buffer, sizeof(buffer), fp ))
  1583. buffer[0] = 0;
  1584. if ((ret = pclose( fp )) < 0)
  1585. goto pipeerr;
  1586. if (ret) {
  1587. if (WIFSIGNALED( ret ))
  1588. error( "Skipping account %s, password command crashed\n", srvc->name );
  1589. else
  1590. error( "Skipping account %s, password command exited with status %d\n", srvc->name, WEXITSTATUS( ret ) );
  1591. return 0;
  1592. }
  1593. if (!buffer[0]) {
  1594. error( "Skipping account %s, password command produced no output\n", srvc->name );
  1595. return 0;
  1596. }
  1597. buffer[strcspn( buffer, "\n" )] = 0; /* Strip trailing newline */
  1598. free( srvc->pass ); /* From previous runs */
  1599. srvc->pass = nfstrdup( buffer );
  1600. } else if (!srvc->pass) {
  1601. char *pass, prompt[80];
  1602. flushn();
  1603. sprintf( prompt, "Password (%s): ", srvc->name );
  1604. pass = getpass( prompt );
  1605. if (!pass) {
  1606. perror( "getpass" );
  1607. exit( 1 );
  1608. }
  1609. if (!*pass) {
  1610. error( "Skipping account %s, no password\n", srvc->name );
  1611. return 0;
  1612. }
  1613. /* getpass() returns a pointer to a static buffer. Make a copy for long term storage. */
  1614. srvc->pass = nfstrdup( pass );
  1615. }
  1616. return srvc->pass;
  1617. }
  1618. #ifdef HAVE_LIBSASL
  1619. static sasl_callback_t sasl_callbacks[] = {
  1620. { SASL_CB_USER, NULL, NULL },
  1621. { SASL_CB_AUTHNAME, NULL, NULL },
  1622. { SASL_CB_PASS, NULL, NULL },
  1623. { SASL_CB_LIST_END, NULL, NULL }
  1624. };
  1625. static int
  1626. process_sasl_interact( sasl_interact_t *interact, imap_server_conf_t *srvc )
  1627. {
  1628. const char *val;
  1629. for (;; ++interact) {
  1630. switch (interact->id) {
  1631. case SASL_CB_LIST_END:
  1632. return 0;
  1633. case SASL_CB_USER:
  1634. case SASL_CB_AUTHNAME:
  1635. val = ensure_user( srvc );
  1636. break;
  1637. case SASL_CB_PASS:
  1638. val = ensure_password( srvc );
  1639. break;
  1640. default:
  1641. error( "Error: Unknown SASL interaction ID\n" );
  1642. return -1;
  1643. }
  1644. if (!val)
  1645. return -1;
  1646. interact->result = val;
  1647. interact->len = strlen( val );
  1648. }
  1649. }
  1650. static int
  1651. process_sasl_step( imap_store_t *ctx, int rc, const char *in, uint in_len,
  1652. sasl_interact_t *interact, const char **out, uint *out_len )
  1653. {
  1654. imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server;
  1655. while (rc == SASL_INTERACT) {
  1656. if (process_sasl_interact( interact, srvc ) < 0)
  1657. return -1;
  1658. rc = sasl_client_step( ctx->sasl, in, in_len, &interact, out, out_len );
  1659. }
  1660. if (rc == SASL_CONTINUE) {
  1661. ctx->sasl_cont = 1;
  1662. } else if (rc == SASL_OK) {
  1663. ctx->sasl_cont = 0;
  1664. } else {
  1665. error( "Error: %s\n", sasl_errdetail( ctx->sasl ) );
  1666. return -1;
  1667. }
  1668. return 0;
  1669. }
  1670. static int
  1671. decode_sasl_data( const char *prompt, char **in, uint *in_len )
  1672. {
  1673. if (prompt) {
  1674. int rc;
  1675. uint prompt_len = strlen( prompt );
  1676. /* We're decoding, the output will be shorter than prompt_len. */
  1677. *in = nfmalloc( prompt_len );
  1678. rc = sasl_decode64( prompt, prompt_len, *in, prompt_len, in_len );
  1679. if (rc != SASL_OK) {
  1680. free( *in );
  1681. error( "Error: SASL(%d): %s\n", rc, sasl_errstring( rc, NULL, NULL ) );
  1682. return -1;
  1683. }
  1684. } else {
  1685. *in = NULL;
  1686. *in_len = 0;
  1687. }
  1688. return 0;
  1689. }
  1690. static int
  1691. encode_sasl_data( const char *out, uint out_len, char **enc, uint *enc_len )
  1692. {
  1693. int rc;
  1694. uint enc_len_max = ((out_len + 2) / 3) * 4 + 1;
  1695. *enc = nfmalloc( enc_len_max );
  1696. rc = sasl_encode64( out, out_len, *enc, enc_len_max, enc_len );
  1697. if (rc != SASL_OK) {
  1698. free( *enc );
  1699. error( "Error: SASL(%d): %s\n", rc, sasl_errstring( rc, NULL, NULL ) );
  1700. return -1;
  1701. }
  1702. return 0;
  1703. }
  1704. static int
  1705. do_sasl_auth( imap_store_t *ctx, struct imap_cmd *cmdp ATTR_UNUSED, const char *prompt )
  1706. {
  1707. int rc, ret, iovcnt = 0;
  1708. uint in_len, out_len, enc_len;
  1709. const char *out;
  1710. char *in, *enc;
  1711. sasl_interact_t *interact = NULL;
  1712. conn_iovec_t iov[2];
  1713. if (!ctx->sasl_cont) {
  1714. error( "Error: IMAP wants more steps despite successful SASL authentication.\n" );
  1715. goto bail;
  1716. }
  1717. if (decode_sasl_data( prompt, &in, &in_len ) < 0)
  1718. goto bail;
  1719. rc = sasl_client_step( ctx->sasl, in, in_len, &interact, &out, &out_len );
  1720. ret = process_sasl_step( ctx, rc, in, in_len, interact, &out, &out_len );
  1721. free( in );
  1722. if (ret < 0)
  1723. goto bail;
  1724. if (out) {
  1725. if (encode_sasl_data( out, out_len, &enc, &enc_len ) < 0)
  1726. goto bail;
  1727. iov[0].buf = enc;
  1728. iov[0].len = enc_len;
  1729. iov[0].takeOwn = GiveOwn;
  1730. iovcnt = 1;
  1731. if (DFlags & DEBUG_NET) {
  1732. printf( "%s>+> %s\n", ctx->label, enc );
  1733. fflush( stdout );
  1734. }
  1735. } else {
  1736. if (DFlags & DEBUG_NET) {
  1737. printf( "%s>+>\n", ctx->label );
  1738. fflush( stdout );
  1739. }
  1740. }
  1741. iov[iovcnt].buf = "\r\n";
  1742. iov[iovcnt].len = 2;
  1743. iov[iovcnt].takeOwn = KeepOwn;
  1744. iovcnt++;
  1745. return socket_write( &ctx->conn, iov, iovcnt );
  1746. bail:
  1747. imap_open_store_bail( ctx, FAIL_FINAL );
  1748. return -1;
  1749. }
  1750. static void
  1751. done_sasl_auth( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1752. {
  1753. if (response == RESP_OK && ctx->sasl_cont) {
  1754. sasl_interact_t *interact = NULL;
  1755. const char *out;
  1756. uint out_len;
  1757. int rc = sasl_client_step( ctx->sasl, NULL, 0, &interact, &out, &out_len );
  1758. if (process_sasl_step( ctx, rc, NULL, 0, interact, &out, &out_len ) < 0)
  1759. warn( "Warning: SASL reported failure despite successful IMAP authentication. Ignoring...\n" );
  1760. else if (out)
  1761. warn( "Warning: SASL wants more steps despite successful IMAP authentication. Ignoring...\n" );
  1762. }
  1763. imap_open_store_authenticate2_p2( ctx, NULL, response );
  1764. }
  1765. #endif
  1766. static void
  1767. imap_open_store_authenticate2( imap_store_t *ctx )
  1768. {
  1769. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1770. imap_server_conf_t *srvc = cfg->server;
  1771. string_list_t *mech, *cmech;
  1772. int auth_login = 0;
  1773. #ifdef HAVE_LIBSASL
  1774. char saslmechs[1024], *saslend = saslmechs;
  1775. #endif
  1776. info( "Logging in...\n" );
  1777. for (mech = srvc->auth_mechs; mech; mech = mech->next) {
  1778. int any = !strcmp( mech->string, "*" );
  1779. for (cmech = ctx->auth_mechs; cmech; cmech = cmech->next) {
  1780. if (any || !strcasecmp( mech->string, cmech->string )) {
  1781. if (!strcasecmp( cmech->string, "LOGIN" )) {
  1782. #ifdef HAVE_LIBSSL
  1783. if (ctx->conn.ssl || !any)
  1784. #endif
  1785. auth_login = 1;
  1786. } else {
  1787. #ifdef HAVE_LIBSASL
  1788. int len = strlen( cmech->string );
  1789. if (saslend + len + 2 > saslmechs + sizeof(saslmechs))
  1790. oob();
  1791. *saslend++ = ' ';
  1792. memcpy( saslend, cmech->string, len + 1 );
  1793. saslend += len;
  1794. #else
  1795. error( "IMAP error: authentication mechanism %s is not supported\n", cmech->string );
  1796. goto bail;
  1797. #endif
  1798. }
  1799. }
  1800. }
  1801. }
  1802. #ifdef HAVE_LIBSASL
  1803. if (saslend != saslmechs) {
  1804. int rc;
  1805. uint out_len = 0;
  1806. char *enc = NULL;
  1807. const char *gotmech = NULL, *out = NULL;
  1808. sasl_interact_t *interact = NULL;
  1809. struct imap_cmd *cmd;
  1810. static int sasl_inited;
  1811. if (!sasl_inited) {
  1812. rc = sasl_client_init( sasl_callbacks );
  1813. if (rc != SASL_OK) {
  1814. saslbail:
  1815. error( "Error: SASL(%d): %s\n", rc, sasl_errstring( rc, NULL, NULL ) );
  1816. goto bail;
  1817. }
  1818. sasl_inited = 1;
  1819. }
  1820. rc = sasl_client_new( "imap", srvc->sconf.host, NULL, NULL, NULL, 0, &ctx->sasl );
  1821. if (rc != SASL_OK) {
  1822. if (!ctx->sasl)
  1823. goto saslbail;
  1824. error( "Error: %s\n", sasl_errdetail( ctx->sasl ) );
  1825. goto bail;
  1826. }
  1827. rc = sasl_client_start( ctx->sasl, saslmechs + 1, &interact, CAP(SASLIR) ? &out : NULL, &out_len, &gotmech );
  1828. if (gotmech)
  1829. info( "Authenticating with SASL mechanism %s...\n", gotmech );
  1830. /* Technically, we are supposed to loop over sasl_client_start(),
  1831. * but it just calls sasl_client_step() anyway. */
  1832. if (process_sasl_step( ctx, rc, NULL, 0, interact, CAP(SASLIR) ? &out : NULL, &out_len ) < 0)
  1833. goto bail;
  1834. if (out) {
  1835. if (!out_len)
  1836. enc = nfstrdup( "=" ); /* A zero-length initial response is encoded as padding. */
  1837. else if (encode_sasl_data( out, out_len, &enc, NULL ) < 0)
  1838. goto bail;
  1839. }
  1840. cmd = new_imap_cmd( sizeof(*cmd) );
  1841. cmd->param.cont = do_sasl_auth;
  1842. imap_exec( ctx, cmd, done_sasl_auth, enc ? "AUTHENTICATE %s %s" : "AUTHENTICATE %s", gotmech, enc );
  1843. free( enc );
  1844. return;
  1845. }
  1846. #endif
  1847. if (auth_login) {
  1848. if (!ensure_user( srvc ) || !ensure_password( srvc ))
  1849. goto bail;
  1850. #ifdef HAVE_LIBSSL
  1851. if (!ctx->conn.ssl)
  1852. #endif
  1853. warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
  1854. imap_exec( ctx, 0, imap_open_store_authenticate2_p2,
  1855. "LOGIN \"%\\s\" \"%\\s\"", srvc->user, srvc->pass );
  1856. return;
  1857. }
  1858. error( "IMAP error: server supports no acceptable authentication mechanism\n" );
  1859. bail:
  1860. imap_open_store_bail( ctx, FAIL_FINAL );
  1861. }
  1862. static void
  1863. imap_open_store_authenticate2_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1864. {
  1865. if (response == RESP_NO)
  1866. imap_open_store_bail( ctx, FAIL_FINAL );
  1867. else if (response == RESP_OK)
  1868. imap_open_store_namespace( ctx );
  1869. }
  1870. static void
  1871. imap_open_store_namespace( imap_store_t *ctx )
  1872. {
  1873. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1874. ctx->prefix = cfg->gen.path;
  1875. ctx->delimiter = cfg->delimiter ? nfstrdup( cfg->delimiter ) : 0;
  1876. if (((!ctx->prefix && cfg->use_namespace) || !cfg->delimiter) && CAP(NAMESPACE)) {
  1877. /* get NAMESPACE info */
  1878. if (!ctx->got_namespace)
  1879. imap_exec( ctx, 0, imap_open_store_namespace_p2, "NAMESPACE" );
  1880. else
  1881. imap_open_store_namespace2( ctx );
  1882. return;
  1883. }
  1884. imap_open_store_compress( ctx );
  1885. }
  1886. static void
  1887. imap_open_store_namespace_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1888. {
  1889. if (response == RESP_NO) {
  1890. imap_open_store_bail( ctx, FAIL_FINAL );
  1891. } else if (response == RESP_OK) {
  1892. ctx->got_namespace = 1;
  1893. imap_open_store_namespace2( ctx );
  1894. }
  1895. }
  1896. static void
  1897. imap_open_store_namespace2( imap_store_t *ctx )
  1898. {
  1899. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1900. list_t *nsp, *nsp_1st, *nsp_1st_ns, *nsp_1st_dl;
  1901. /* XXX for now assume 1st personal namespace */
  1902. if (is_list( (nsp = ctx->ns_personal) ) &&
  1903. is_list( (nsp_1st = nsp->child) ) &&
  1904. is_atom( (nsp_1st_ns = nsp_1st->child) ) &&
  1905. is_atom( (nsp_1st_dl = nsp_1st_ns->next) ))
  1906. {
  1907. if (!ctx->prefix && cfg->use_namespace)
  1908. ctx->prefix = nsp_1st_ns->val;
  1909. if (!ctx->delimiter)
  1910. ctx->delimiter = nfstrdup( nsp_1st_dl->val );
  1911. imap_open_store_compress( ctx );
  1912. } else {
  1913. imap_open_store_bail( ctx, FAIL_FINAL );
  1914. }
  1915. }
  1916. static void
  1917. imap_open_store_compress( imap_store_t *ctx )
  1918. {
  1919. #ifdef HAVE_LIBZ
  1920. if (CAP(COMPRESS_DEFLATE)) { /* XXX make that configurable */
  1921. imap_exec( ctx, 0, imap_open_store_compress_p2, "COMPRESS DEFLATE" );
  1922. return;
  1923. }
  1924. #endif
  1925. imap_open_store_finalize( ctx );
  1926. }
  1927. #ifdef HAVE_LIBZ
  1928. static void
  1929. imap_open_store_compress_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1930. {
  1931. if (response == RESP_NO) {
  1932. /* We already reported an error, but it's not fatal to us. */
  1933. imap_open_store_finalize( ctx );
  1934. } else if (response == RESP_OK) {
  1935. socket_start_deflate( &ctx->conn );
  1936. imap_open_store_finalize( ctx );
  1937. }
  1938. }
  1939. #endif
  1940. static void
  1941. imap_open_store_finalize( imap_store_t *ctx )
  1942. {
  1943. set_bad_callback( &ctx->gen, 0, 0 );
  1944. if (!ctx->prefix)
  1945. ctx->prefix = "";
  1946. ctx->trashnc = TrashUnknown;
  1947. ctx->callbacks.imap_open( &ctx->gen, ctx->callback_aux );
  1948. }
  1949. #ifdef HAVE_LIBSSL
  1950. static void
  1951. imap_open_store_ssl_bail( imap_store_t *ctx )
  1952. {
  1953. /* This avoids that we try to send LOGOUT to an unusable socket. */
  1954. socket_close( &ctx->conn );
  1955. imap_open_store_bail( ctx, FAIL_FINAL );
  1956. }
  1957. #endif
  1958. static void
  1959. imap_open_store_bail( imap_store_t *ctx, int failed )
  1960. {
  1961. void (*cb)( store_t *srv, void *aux ) = ctx->callbacks.imap_open;
  1962. void *aux = ctx->callback_aux;
  1963. ctx->gen.conf->failed = failed;
  1964. imap_cancel_store( &ctx->gen );
  1965. cb( 0, aux );
  1966. }
  1967. /******************* imap_open_box *******************/
  1968. static int
  1969. imap_select_box( store_t *gctx, const char *name )
  1970. {
  1971. imap_store_t *ctx = (imap_store_t *)gctx;
  1972. free_generic_messages( gctx->msgs );
  1973. gctx->msgs = 0;
  1974. ctx->msgapp = &gctx->msgs;
  1975. ctx->name = name;
  1976. return DRV_OK;
  1977. }
  1978. static void
  1979. imap_open_box( store_t *gctx,
  1980. void (*cb)( int sts, void *aux ), void *aux )
  1981. {
  1982. imap_store_t *ctx = (imap_store_t *)gctx;
  1983. struct imap_cmd_simple *cmd;
  1984. char *buf;
  1985. if (prepare_box( &buf, ctx ) < 0) {
  1986. cb( DRV_BOX_BAD, aux );
  1987. return;
  1988. }
  1989. ctx->gen.uidnext = 0;
  1990. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  1991. cmd->gen.param.failok = 1;
  1992. imap_exec( ctx, &cmd->gen, imap_done_simple_box,
  1993. "SELECT \"%\\s\"", buf );
  1994. free( buf );
  1995. }
  1996. /******************* imap_create_box *******************/
  1997. static void
  1998. imap_create_box( store_t *gctx,
  1999. void (*cb)( int sts, void *aux ), void *aux )
  2000. {
  2001. imap_store_t *ctx = (imap_store_t *)gctx;
  2002. struct imap_cmd_simple *cmd;
  2003. char *buf;
  2004. if (prepare_box( &buf, ctx ) < 0) {
  2005. cb( DRV_BOX_BAD, aux );
  2006. return;
  2007. }
  2008. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  2009. imap_exec( ctx, &cmd->gen, imap_done_simple_box,
  2010. "CREATE \"%\\s\"", buf );
  2011. free( buf );
  2012. }
  2013. /******************* imap_delete_box *******************/
  2014. static int
  2015. imap_confirm_box_empty( store_t *gctx )
  2016. {
  2017. return gctx->count ? DRV_BOX_BAD : DRV_OK;
  2018. }
  2019. static void imap_delete_box_p2( imap_store_t *, struct imap_cmd *, int );
  2020. static void
  2021. imap_delete_box( store_t *gctx,
  2022. void (*cb)( int sts, void *aux ), void *aux )
  2023. {
  2024. imap_store_t *ctx = (imap_store_t *)gctx;
  2025. struct imap_cmd_simple *cmd;
  2026. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  2027. imap_exec( ctx, &cmd->gen, imap_delete_box_p2, "CLOSE" );
  2028. }
  2029. static void
  2030. imap_delete_box_p2( imap_store_t *ctx, struct imap_cmd *gcmd, int response )
  2031. {
  2032. struct imap_cmd_simple *cmdp = (struct imap_cmd_simple *)gcmd;
  2033. struct imap_cmd_simple *cmd;
  2034. char *buf;
  2035. if (response != RESP_OK) {
  2036. imap_done_simple_box( ctx, &cmdp->gen, response );
  2037. return;
  2038. }
  2039. if (prepare_box( &buf, ctx ) < 0) {
  2040. imap_done_simple_box( ctx, &cmdp->gen, RESP_NO );
  2041. return;
  2042. }
  2043. INIT_IMAP_CMD(imap_cmd_simple, cmd, cmdp->callback, cmdp->callback_aux)
  2044. imap_exec( ctx, &cmd->gen, imap_done_simple_box,
  2045. "DELETE \"%\\s\"", buf );
  2046. free( buf );
  2047. }
  2048. static int
  2049. imap_finish_delete_box( store_t *gctx ATTR_UNUSED )
  2050. {
  2051. return DRV_OK;
  2052. }
  2053. /******************* imap_load_box *******************/
  2054. static void
  2055. imap_prepare_load_box( store_t *gctx, int opts )
  2056. {
  2057. gctx->opts = opts;
  2058. }
  2059. static int imap_submit_load( imap_store_t *, const char *, int, struct imap_cmd_refcounted_state * );
  2060. static void
  2061. imap_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int *excs, int nexcs,
  2062. void (*cb)( int sts, void *aux ), void *aux )
  2063. {
  2064. imap_store_t *ctx = (imap_store_t *)gctx;
  2065. int i, j, bl;
  2066. char buf[1000];
  2067. if (!ctx->gen.count) {
  2068. free( excs );
  2069. cb( DRV_OK, aux );
  2070. } else {
  2071. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  2072. sort_ints( excs, nexcs );
  2073. for (i = 0; i < nexcs; ) {
  2074. for (bl = 0; i < nexcs && bl < 960; i++) {
  2075. if (bl)
  2076. buf[bl++] = ',';
  2077. bl += sprintf( buf + bl, "%d", excs[i] );
  2078. j = i;
  2079. for (; i + 1 < nexcs && excs[i + 1] == excs[i] + 1; i++) {}
  2080. if (i != j)
  2081. bl += sprintf( buf + bl, ":%d", excs[i] );
  2082. }
  2083. if (imap_submit_load( ctx, buf, 0, sts ) < 0)
  2084. goto done;
  2085. }
  2086. if (maxuid == INT_MAX)
  2087. maxuid = ctx->gen.uidnext ? ctx->gen.uidnext - 1 : 1000000000;
  2088. if (maxuid >= minuid) {
  2089. if ((ctx->gen.opts & OPEN_FIND) && minuid < newuid) {
  2090. sprintf( buf, "%d:%d", minuid, newuid - 1 );
  2091. if (imap_submit_load( ctx, buf, 0, sts ) < 0)
  2092. goto done;
  2093. if (newuid > maxuid)
  2094. goto done;
  2095. sprintf( buf, "%d:%d", newuid, maxuid );
  2096. } else {
  2097. sprintf( buf, "%d:%d", minuid, maxuid );
  2098. }
  2099. imap_submit_load( ctx, buf, (ctx->gen.opts & OPEN_FIND), sts );
  2100. }
  2101. done:
  2102. free( excs );
  2103. imap_refcounted_done( sts );
  2104. }
  2105. }
  2106. static int
  2107. imap_submit_load( imap_store_t *ctx, const char *buf, int tuids, struct imap_cmd_refcounted_state *sts )
  2108. {
  2109. return imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  2110. "UID FETCH %s (UID%s%s%s)", buf,
  2111. (ctx->gen.opts & OPEN_FLAGS) ? " FLAGS" : "",
  2112. (ctx->gen.opts & OPEN_SIZE) ? " RFC822.SIZE" : "",
  2113. tuids ? " BODY.PEEK[HEADER.FIELDS (X-TUID)]" : "");
  2114. }
  2115. /******************* imap_fetch_msg *******************/
  2116. static void imap_fetch_msg_p2( imap_store_t *ctx, struct imap_cmd *gcmd, int response );
  2117. static void
  2118. imap_fetch_msg( store_t *ctx, message_t *msg, msg_data_t *data,
  2119. void (*cb)( int sts, void *aux ), void *aux )
  2120. {
  2121. struct imap_cmd_fetch_msg *cmd;
  2122. INIT_IMAP_CMD_X(imap_cmd_fetch_msg, cmd, cb, aux)
  2123. cmd->gen.gen.param.uid = msg->uid;
  2124. cmd->msg_data = data;
  2125. data->data = 0;
  2126. imap_exec( (imap_store_t *)ctx, &cmd->gen.gen, imap_fetch_msg_p2,
  2127. "UID FETCH %d (%s%sBODY.PEEK[])", msg->uid,
  2128. !(msg->status & M_FLAGS) ? "FLAGS " : "",
  2129. (data->date== -1) ? "INTERNALDATE " : "" );
  2130. }
  2131. static void
  2132. imap_fetch_msg_p2( imap_store_t *ctx, struct imap_cmd *gcmd, int response )
  2133. {
  2134. struct imap_cmd_fetch_msg *cmd = (struct imap_cmd_fetch_msg *)gcmd;
  2135. if (response == RESP_OK && !cmd->msg_data->data) {
  2136. /* The FETCH succeeded, but there is no message with this UID. */
  2137. response = RESP_NO;
  2138. }
  2139. imap_done_simple_msg( ctx, gcmd, response );
  2140. }
  2141. /******************* imap_set_msg_flags *******************/
  2142. static void imap_set_flags_p2( imap_store_t *, struct imap_cmd *, int );
  2143. static int
  2144. imap_make_flags( int flags, char *buf )
  2145. {
  2146. const char *s;
  2147. uint i, d;
  2148. for (i = d = 0; i < as(Flags); i++)
  2149. if (flags & (1 << i)) {
  2150. buf[d++] = ' ';
  2151. buf[d++] = '\\';
  2152. for (s = Flags[i]; *s; s++)
  2153. buf[d++] = *s;
  2154. }
  2155. buf[0] = '(';
  2156. buf[d++] = ')';
  2157. return d;
  2158. }
  2159. static int
  2160. imap_flags_helper( imap_store_t *ctx, int uid, char what, int flags,
  2161. struct imap_cmd_refcounted_state *sts )
  2162. {
  2163. char buf[256];
  2164. buf[imap_make_flags( flags, buf )] = 0;
  2165. return imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_set_flags_p2,
  2166. "UID STORE %d %cFLAGS.SILENT %s", uid, what, buf );
  2167. }
  2168. static void
  2169. imap_set_msg_flags( store_t *gctx, message_t *msg, int uid, int add, int del,
  2170. void (*cb)( int sts, void *aux ), void *aux )
  2171. {
  2172. imap_store_t *ctx = (imap_store_t *)gctx;
  2173. if (msg) {
  2174. uid = msg->uid;
  2175. add &= ~msg->flags;
  2176. del &= msg->flags;
  2177. msg->flags |= add;
  2178. msg->flags &= ~del;
  2179. }
  2180. if (add || del) {
  2181. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  2182. if ((add && imap_flags_helper( ctx, uid, '+', add, sts ) < 0) ||
  2183. (del && imap_flags_helper( ctx, uid, '-', del, sts ) < 0)) {}
  2184. imap_refcounted_done( sts );
  2185. } else {
  2186. cb( DRV_OK, aux );
  2187. }
  2188. }
  2189. static void
  2190. imap_set_flags_p2( imap_store_t *ctx ATTR_UNUSED, struct imap_cmd *cmd, int response )
  2191. {
  2192. struct imap_cmd_refcounted_state *sts = ((struct imap_cmd_refcounted *)cmd)->state;
  2193. switch (response) {
  2194. case RESP_CANCEL:
  2195. sts->ret_val = DRV_CANCELED;
  2196. break;
  2197. case RESP_NO:
  2198. if (sts->ret_val == DRV_OK) /* Don't override cancelation. */
  2199. sts->ret_val = DRV_MSG_BAD;
  2200. break;
  2201. }
  2202. imap_refcounted_done( sts );
  2203. }
  2204. /******************* imap_close_box *******************/
  2205. static void
  2206. imap_close_box( store_t *gctx,
  2207. void (*cb)( int sts, void *aux ), void *aux )
  2208. {
  2209. imap_store_t *ctx = (imap_store_t *)gctx;
  2210. if (ctx->gen.conf->trash && CAP(UIDPLUS)) {
  2211. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  2212. message_t *msg, *fmsg, *nmsg;
  2213. int bl;
  2214. char buf[1000];
  2215. for (msg = ctx->gen.msgs; ; ) {
  2216. for (bl = 0; msg && bl < 960; msg = msg->next) {
  2217. if (!(msg->flags & F_DELETED))
  2218. continue;
  2219. if (bl)
  2220. buf[bl++] = ',';
  2221. bl += sprintf( buf + bl, "%d", msg->uid );
  2222. fmsg = msg;
  2223. for (; (nmsg = msg->next) && (nmsg->flags & F_DELETED); msg = nmsg) {}
  2224. if (msg != fmsg)
  2225. bl += sprintf( buf + bl, ":%d", msg->uid );
  2226. }
  2227. if (!bl)
  2228. break;
  2229. if (imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  2230. "UID EXPUNGE %s", buf ) < 0)
  2231. break;
  2232. }
  2233. imap_refcounted_done( sts );
  2234. } else {
  2235. /* This is inherently racy: it may cause messages which other clients
  2236. * marked as deleted to be expunged without being trashed. */
  2237. struct imap_cmd_simple *cmd;
  2238. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  2239. imap_exec( ctx, &cmd->gen, imap_done_simple_box, "CLOSE" );
  2240. }
  2241. }
  2242. /******************* imap_trash_msg *******************/
  2243. static void
  2244. imap_trash_msg( store_t *gctx, message_t *msg,
  2245. void (*cb)( int sts, void *aux ), void *aux )
  2246. {
  2247. imap_store_t *ctx = (imap_store_t *)gctx;
  2248. struct imap_cmd_simple *cmd;
  2249. char *buf;
  2250. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  2251. cmd->gen.param.create = 1;
  2252. cmd->gen.param.to_trash = 1;
  2253. if (prepare_trash( &buf, ctx ) < 0) {
  2254. cb( DRV_BOX_BAD, aux );
  2255. return;
  2256. }
  2257. imap_exec( ctx, &cmd->gen, imap_done_simple_msg,
  2258. CAP(MOVE) ? "UID MOVE %d \"%\\s\"" : "UID COPY %d \"%\\s\"", msg->uid, buf );
  2259. free( buf );
  2260. }
  2261. /******************* imap_store_msg *******************/
  2262. static void imap_store_msg_p2( imap_store_t *, struct imap_cmd *, int );
  2263. static size_t
  2264. my_strftime( char *s, size_t max, const char *fmt, const struct tm *tm )
  2265. {
  2266. return strftime( s, max, fmt, tm );
  2267. }
  2268. static void
  2269. imap_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  2270. void (*cb)( int sts, int uid, void *aux ), void *aux )
  2271. {
  2272. imap_store_t *ctx = (imap_store_t *)gctx;
  2273. struct imap_cmd_out_uid *cmd;
  2274. char *buf;
  2275. int d;
  2276. char flagstr[128], datestr[64];
  2277. d = 0;
  2278. if (data->flags) {
  2279. d = imap_make_flags( data->flags, flagstr );
  2280. flagstr[d++] = ' ';
  2281. }
  2282. flagstr[d] = 0;
  2283. INIT_IMAP_CMD(imap_cmd_out_uid, cmd, cb, aux)
  2284. ctx->buffer_mem += data->len;
  2285. cmd->gen.param.data_len = data->len;
  2286. cmd->gen.param.data = data->data;
  2287. cmd->out_uid = -2;
  2288. if (to_trash) {
  2289. cmd->gen.param.create = 1;
  2290. cmd->gen.param.to_trash = 1;
  2291. if (prepare_trash( &buf, ctx ) < 0) {
  2292. cb( DRV_BOX_BAD, -1, aux );
  2293. return;
  2294. }
  2295. } else {
  2296. if (prepare_box( &buf, ctx ) < 0) {
  2297. cb( DRV_BOX_BAD, -1, aux );
  2298. return;
  2299. }
  2300. }
  2301. if (data->date) {
  2302. /* configure ensures that %z actually works. */
  2303. my_strftime( datestr, sizeof(datestr), "%d-%b-%Y %H:%M:%S %z", localtime( &data->date ) );
  2304. imap_exec( ctx, &cmd->gen, imap_store_msg_p2,
  2305. "APPEND \"%\\s\" %s\"%\\s\" ", buf, flagstr, datestr );
  2306. } else {
  2307. imap_exec( ctx, &cmd->gen, imap_store_msg_p2,
  2308. "APPEND \"%\\s\" %s", buf, flagstr );
  2309. }
  2310. free( buf );
  2311. }
  2312. static void
  2313. imap_store_msg_p2( imap_store_t *ctx ATTR_UNUSED, struct imap_cmd *cmd, int response )
  2314. {
  2315. struct imap_cmd_out_uid *cmdp = (struct imap_cmd_out_uid *)cmd;
  2316. transform_msg_response( &response );
  2317. cmdp->callback( response, cmdp->out_uid, cmdp->callback_aux );
  2318. }
  2319. /******************* imap_find_new_msgs *******************/
  2320. static void imap_find_new_msgs_p2( imap_store_t *, struct imap_cmd *, int );
  2321. static void
  2322. imap_find_new_msgs( store_t *gctx, int newuid,
  2323. void (*cb)( int sts, void *aux ), void *aux )
  2324. {
  2325. imap_store_t *ctx = (imap_store_t *)gctx;
  2326. struct imap_cmd_find_new *cmd;
  2327. INIT_IMAP_CMD_X(imap_cmd_find_new, cmd, cb, aux)
  2328. cmd->uid = newuid;
  2329. imap_exec( (imap_store_t *)ctx, &cmd->gen.gen, imap_find_new_msgs_p2, "CHECK" );
  2330. }
  2331. static void
  2332. imap_find_new_msgs_p2( imap_store_t *ctx, struct imap_cmd *gcmd, int response )
  2333. {
  2334. struct imap_cmd_find_new *cmdp = (struct imap_cmd_find_new *)gcmd;
  2335. struct imap_cmd_simple *cmd;
  2336. if (response != RESP_OK) {
  2337. imap_done_simple_box( ctx, gcmd, response );
  2338. return;
  2339. }
  2340. INIT_IMAP_CMD(imap_cmd_simple, cmd, cmdp->gen.callback, cmdp->gen.callback_aux)
  2341. imap_exec( (imap_store_t *)ctx, &cmd->gen, imap_done_simple_box,
  2342. "UID FETCH %d:1000000000 (UID BODY.PEEK[HEADER.FIELDS (X-TUID)])", cmdp->uid );
  2343. }
  2344. /******************* imap_list_store *******************/
  2345. static void
  2346. imap_list_store( store_t *gctx, int flags,
  2347. void (*cb)( int sts, void *aux ), void *aux )
  2348. {
  2349. imap_store_t *ctx = (imap_store_t *)gctx;
  2350. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  2351. if (((flags & LIST_PATH) &&
  2352. imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  2353. "LIST \"\" \"%\\s*\"", ctx->prefix ) < 0) ||
  2354. ((flags & LIST_INBOX) && (!(flags & LIST_PATH) || *ctx->prefix) &&
  2355. imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  2356. "LIST \"\" INBOX*" ) < 0))
  2357. {}
  2358. imap_refcounted_done( sts );
  2359. }
  2360. /******************* imap_cancel_cmds *******************/
  2361. static void
  2362. imap_cancel_cmds( store_t *gctx,
  2363. void (*cb)( void *aux ), void *aux )
  2364. {
  2365. imap_store_t *ctx = (imap_store_t *)gctx;
  2366. cancel_pending_imap_cmds( ctx );
  2367. if (ctx->in_progress) {
  2368. ctx->canceling = 1;
  2369. ctx->callbacks.imap_cancel = cb;
  2370. ctx->callback_aux = aux;
  2371. } else {
  2372. cb( aux );
  2373. }
  2374. }
  2375. /******************* imap_commit_cmds *******************/
  2376. static void
  2377. imap_commit_cmds( store_t *gctx )
  2378. {
  2379. (void)gctx;
  2380. }
  2381. /******************* imap_memory_usage *******************/
  2382. static int
  2383. imap_memory_usage( store_t *gctx )
  2384. {
  2385. imap_store_t *ctx = (imap_store_t *)gctx;
  2386. return ctx->buffer_mem + ctx->conn.buffer_mem;
  2387. }
  2388. /******************* imap_parse_store *******************/
  2389. imap_server_conf_t *servers, **serverapp = &servers;
  2390. static int
  2391. imap_parse_store( conffile_t *cfg, store_conf_t **storep )
  2392. {
  2393. imap_store_conf_t *store;
  2394. imap_server_conf_t *server, *srv, sserver;
  2395. const char *type, *name, *arg;
  2396. int acc_opt = 0;
  2397. #ifdef HAVE_LIBSSL
  2398. /* Legacy SSL options */
  2399. int require_ssl = -1, use_imaps = -1;
  2400. int use_sslv2 = -1, use_sslv3 = -1, use_tlsv1 = -1, use_tlsv11 = -1, use_tlsv12 = -1;
  2401. int require_cram = -1;
  2402. #endif
  2403. if (!strcasecmp( "IMAPAccount", cfg->cmd )) {
  2404. server = nfcalloc( sizeof(*server) );
  2405. server->name = nfstrdup( cfg->val );
  2406. *serverapp = server;
  2407. serverapp = &server->next;
  2408. store = 0;
  2409. *storep = 0;
  2410. } else if (!strcasecmp( "IMAPStore", cfg->cmd )) {
  2411. store = nfcalloc( sizeof(*store) );
  2412. store->gen.driver = &imap_driver;
  2413. store->gen.name = nfstrdup( cfg->val );
  2414. store->use_namespace = 1;
  2415. *storep = &store->gen;
  2416. memset( &sserver, 0, sizeof(sserver) );
  2417. server = &sserver;
  2418. } else
  2419. return 0;
  2420. #ifdef HAVE_LIBSSL
  2421. server->ssl_type = -1;
  2422. server->sconf.ssl_versions = -1;
  2423. server->sconf.system_certs = 1;
  2424. #endif
  2425. server->max_in_progress = INT_MAX;
  2426. while (getcline( cfg ) && cfg->cmd) {
  2427. if (!strcasecmp( "Host", cfg->cmd )) {
  2428. /* The imap[s]: syntax is just a backwards compat hack. */
  2429. arg = cfg->val;
  2430. #ifdef HAVE_LIBSSL
  2431. if (starts_with( arg, -1, "imaps:", 6 )) {
  2432. arg += 6;
  2433. server->ssl_type = SSL_IMAPS;
  2434. if (server->sconf.ssl_versions == -1)
  2435. server->sconf.ssl_versions = SSLv2 | SSLv3 | TLSv1;
  2436. } else
  2437. #endif
  2438. if (starts_with( arg, -1, "imap:", 5 ))
  2439. arg += 5;
  2440. if (starts_with( arg, -1, "//", 2 ))
  2441. arg += 2;
  2442. if (arg != cfg->val)
  2443. warn( "%s:%d: Notice: URL notation is deprecated; use a plain host name and possibly 'SSLType IMAPS' instead\n", cfg->file, cfg->line );
  2444. server->sconf.host = nfstrdup( arg );
  2445. }
  2446. else if (!strcasecmp( "User", cfg->cmd ))
  2447. server->user = nfstrdup( cfg->val );
  2448. else if (!strcasecmp( "Pass", cfg->cmd ))
  2449. server->pass = nfstrdup( cfg->val );
  2450. else if (!strcasecmp( "PassCmd", cfg->cmd ))
  2451. server->pass_cmd = nfstrdup( cfg->val );
  2452. else if (!strcasecmp( "Port", cfg->cmd ))
  2453. server->sconf.port = parse_int( cfg );
  2454. else if (!strcasecmp( "PipelineDepth", cfg->cmd )) {
  2455. if ((server->max_in_progress = parse_int( cfg )) < 1) {
  2456. error( "%s:%d: PipelineDepth must be at least 1\n", cfg->file, cfg->line );
  2457. cfg->err = 1;
  2458. }
  2459. }
  2460. #ifdef HAVE_LIBSSL
  2461. else if (!strcasecmp( "CertificateFile", cfg->cmd )) {
  2462. server->sconf.cert_file = expand_strdup( cfg->val );
  2463. if (access( server->sconf.cert_file, R_OK )) {
  2464. sys_error( "%s:%d: CertificateFile '%s'",
  2465. cfg->file, cfg->line, server->sconf.cert_file );
  2466. cfg->err = 1;
  2467. }
  2468. } else if (!strcasecmp( "SystemCertificates", cfg->cmd )) {
  2469. server->sconf.system_certs = parse_bool( cfg );
  2470. } else if (!strcasecmp( "SSLType", cfg->cmd )) {
  2471. if (!strcasecmp( "None", cfg->val )) {
  2472. server->ssl_type = SSL_None;
  2473. } else if (!strcasecmp( "STARTTLS", cfg->val )) {
  2474. server->ssl_type = SSL_STARTTLS;
  2475. } else if (!strcasecmp( "IMAPS", cfg->val )) {
  2476. server->ssl_type = SSL_IMAPS;
  2477. } else {
  2478. error( "%s:%d: Invalid SSL type\n", cfg->file, cfg->line );
  2479. cfg->err = 1;
  2480. }
  2481. } else if (!strcasecmp( "SSLVersion", cfg->cmd ) ||
  2482. !strcasecmp( "SSLVersions", cfg->cmd )) {
  2483. server->sconf.ssl_versions = 0;
  2484. arg = cfg->val;
  2485. do {
  2486. if (!strcasecmp( "SSLv2", arg )) {
  2487. server->sconf.ssl_versions |= SSLv2;
  2488. } else if (!strcasecmp( "SSLv3", arg )) {
  2489. server->sconf.ssl_versions |= SSLv3;
  2490. } else if (!strcasecmp( "TLSv1", arg )) {
  2491. server->sconf.ssl_versions |= TLSv1;
  2492. } else if (!strcasecmp( "TLSv1.1", arg )) {
  2493. server->sconf.ssl_versions |= TLSv1_1;
  2494. } else if (!strcasecmp( "TLSv1.2", arg )) {
  2495. server->sconf.ssl_versions |= TLSv1_2;
  2496. } else {
  2497. error( "%s:%d: Unrecognized SSL version\n", cfg->file, cfg->line );
  2498. cfg->err = 1;
  2499. }
  2500. } while ((arg = get_arg( cfg, ARG_OPTIONAL, 0 )));
  2501. } else if (!strcasecmp( "RequireSSL", cfg->cmd ))
  2502. require_ssl = parse_bool( cfg );
  2503. else if (!strcasecmp( "UseIMAPS", cfg->cmd ))
  2504. use_imaps = parse_bool( cfg );
  2505. else if (!strcasecmp( "UseSSLv2", cfg->cmd ))
  2506. use_sslv2 = parse_bool( cfg );
  2507. else if (!strcasecmp( "UseSSLv3", cfg->cmd ))
  2508. use_sslv3 = parse_bool( cfg );
  2509. else if (!strcasecmp( "UseTLSv1", cfg->cmd ))
  2510. use_tlsv1 = parse_bool( cfg );
  2511. else if (!strcasecmp( "UseTLSv1.1", cfg->cmd ))
  2512. use_tlsv11 = parse_bool( cfg );
  2513. else if (!strcasecmp( "UseTLSv1.2", cfg->cmd ))
  2514. use_tlsv12 = parse_bool( cfg );
  2515. else if (!strcasecmp( "AuthMech", cfg->cmd ) ||
  2516. !strcasecmp( "AuthMechs", cfg->cmd )) {
  2517. arg = cfg->val;
  2518. do
  2519. add_string_list( &server->auth_mechs, arg );
  2520. while ((arg = get_arg( cfg, ARG_OPTIONAL, 0 )));
  2521. } else if (!strcasecmp( "RequireCRAM", cfg->cmd ))
  2522. require_cram = parse_bool( cfg );
  2523. #endif
  2524. else if (!strcasecmp( "Tunnel", cfg->cmd ))
  2525. server->sconf.tunnel = nfstrdup( cfg->val );
  2526. else if (store) {
  2527. if (!strcasecmp( "Account", cfg->cmd )) {
  2528. for (srv = servers; srv; srv = srv->next)
  2529. if (srv->name && !strcmp( srv->name, cfg->val ))
  2530. goto gotsrv;
  2531. error( "%s:%d: unknown IMAP account '%s'\n", cfg->file, cfg->line, cfg->val );
  2532. cfg->err = 1;
  2533. continue;
  2534. gotsrv:
  2535. store->server = srv;
  2536. } else if (!strcasecmp( "UseNamespace", cfg->cmd ))
  2537. store->use_namespace = parse_bool( cfg );
  2538. else if (!strcasecmp( "Path", cfg->cmd ))
  2539. store->gen.path = nfstrdup( cfg->val );
  2540. else if (!strcasecmp( "PathDelimiter", cfg->cmd ))
  2541. store->delimiter = nfstrdup( cfg->val );
  2542. else
  2543. parse_generic_store( &store->gen, cfg );
  2544. continue;
  2545. } else {
  2546. error( "%s:%d: unknown/misplaced keyword '%s'\n", cfg->file, cfg->line, cfg->cmd );
  2547. cfg->err = 1;
  2548. continue;
  2549. }
  2550. acc_opt = 1;
  2551. }
  2552. if (store)
  2553. type = "IMAP store", name = store->gen.name;
  2554. else
  2555. type = "IMAP account", name = server->name;
  2556. if (!store || !store->server) {
  2557. if (!server->sconf.tunnel && !server->sconf.host) {
  2558. error( "%s '%s' has neither Tunnel nor Host\n", type, name );
  2559. cfg->err = 1;
  2560. return 1;
  2561. }
  2562. if (server->pass && server->pass_cmd) {
  2563. error( "%s '%s' has both Pass and PassCmd\n", type, name );
  2564. cfg->err = 1;
  2565. return 1;
  2566. }
  2567. #ifdef HAVE_LIBSSL
  2568. if ((use_sslv2 & use_sslv3 & use_tlsv1 & use_tlsv11 & use_tlsv12) != -1 || use_imaps >= 0 || require_ssl >= 0) {
  2569. if (server->ssl_type >= 0 || server->sconf.ssl_versions >= 0) {
  2570. error( "%s '%s': The deprecated UseSSL*, UseTLS*, UseIMAPS, and RequireSSL options are mutually exlusive with SSLType and SSLVersions.\n", type, name );
  2571. cfg->err = 1;
  2572. return 1;
  2573. }
  2574. warn( "Notice: %s '%s': UseSSL*, UseTLS*, UseIMAPS, and RequireSSL are deprecated. Use SSLType and SSLVersions instead.\n", type, name );
  2575. server->sconf.ssl_versions =
  2576. (use_sslv2 != 1 ? 0 : SSLv2) |
  2577. (use_sslv3 != 1 ? 0 : SSLv3) |
  2578. (use_tlsv1 == 0 ? 0 : TLSv1) |
  2579. (use_tlsv11 != 1 ? 0 : TLSv1_1) |
  2580. (use_tlsv12 != 1 ? 0 : TLSv1_2);
  2581. if (use_imaps == 1) {
  2582. server->ssl_type = SSL_IMAPS;
  2583. } else if (require_ssl) {
  2584. server->ssl_type = SSL_STARTTLS;
  2585. } else if (!server->sconf.ssl_versions) {
  2586. server->ssl_type = SSL_None;
  2587. } else {
  2588. warn( "Notice: %s '%s': 'RequireSSL no' is being ignored\n", type, name );
  2589. server->ssl_type = SSL_STARTTLS;
  2590. }
  2591. if (server->ssl_type != SSL_None && !server->sconf.ssl_versions) {
  2592. error( "%s '%s' requires SSL but no SSL versions enabled\n", type, name );
  2593. cfg->err = 1;
  2594. return 1;
  2595. }
  2596. } else {
  2597. if (server->sconf.ssl_versions < 0)
  2598. server->sconf.ssl_versions = TLSv1; /* Most compatible and still reasonably secure. */
  2599. if (server->ssl_type < 0)
  2600. server->ssl_type = server->sconf.tunnel ? SSL_None : SSL_STARTTLS;
  2601. }
  2602. #endif
  2603. #ifdef HAVE_LIBSSL
  2604. if (require_cram >= 0) {
  2605. if (server->auth_mechs) {
  2606. error( "%s '%s': The deprecated RequireCRAM option is mutually exlusive with AuthMech.\n", type, name );
  2607. cfg->err = 1;
  2608. return 1;
  2609. }
  2610. warn( "Notice: %s '%s': RequireCRAM is deprecated. Use AuthMech instead.\n", type, name );
  2611. if (require_cram)
  2612. add_string_list(&server->auth_mechs, "CRAM-MD5");
  2613. }
  2614. #endif
  2615. if (!server->auth_mechs)
  2616. add_string_list( &server->auth_mechs, "*" );
  2617. if (!server->sconf.port)
  2618. server->sconf.port =
  2619. #ifdef HAVE_LIBSSL
  2620. server->ssl_type == SSL_IMAPS ? 993 :
  2621. #endif
  2622. 143;
  2623. }
  2624. if (store) {
  2625. if (!store->server) {
  2626. store->server = nfmalloc( sizeof(sserver) );
  2627. memcpy( store->server, &sserver, sizeof(sserver) );
  2628. store->server->name = store->gen.name;
  2629. } else if (acc_opt) {
  2630. error( "%s '%s' has both Account and account-specific options\n", type, name );
  2631. cfg->err = 1;
  2632. }
  2633. }
  2634. return 1;
  2635. }
  2636. struct driver imap_driver = {
  2637. DRV_CRLF | DRV_VERBOSE,
  2638. imap_parse_store,
  2639. imap_cleanup,
  2640. imap_open_store,
  2641. imap_disown_store,
  2642. imap_cancel_store,
  2643. imap_list_store,
  2644. imap_select_box,
  2645. imap_create_box,
  2646. imap_open_box,
  2647. imap_confirm_box_empty,
  2648. imap_delete_box,
  2649. imap_finish_delete_box,
  2650. imap_prepare_load_box,
  2651. imap_load_box,
  2652. imap_fetch_msg,
  2653. imap_store_msg,
  2654. imap_find_new_msgs,
  2655. imap_set_msg_flags,
  2656. imap_trash_msg,
  2657. imap_close_box,
  2658. imap_cancel_cmds,
  2659. imap_commit_cmds,
  2660. imap_memory_usage,
  2661. };