drv_imap.c 77 KB

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