drv_imap.c 77 KB

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