drv_imap.c 76 KB

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