drv_imap.c 76 KB

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