drv_imap.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2008 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, write to the Free Software Foundation,
  19. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. *
  21. * As a special exception, mbsync may be linked with the OpenSSL library,
  22. * despite that library's more restrictive license.
  23. */
  24. /* This must come before isync.h to avoid our #define S messing up
  25. * blowfish.h on MacOS X. */
  26. #include <config.h>
  27. #ifdef HAVE_LIBSSL
  28. # include <openssl/ssl.h>
  29. # include <openssl/err.h>
  30. # include <openssl/hmac.h>
  31. #endif
  32. #include "isync.h"
  33. #include <assert.h>
  34. #include <unistd.h>
  35. #include <sys/mman.h>
  36. #include <sys/time.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <stddef.h>
  40. #include <limits.h>
  41. #include <errno.h>
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include <sys/socket.h>
  45. #include <sys/ioctl.h>
  46. #ifdef HAVE_SYS_FILIO_H
  47. # include <sys/filio.h>
  48. #endif
  49. #include <netinet/in.h>
  50. #include <netinet/tcp.h>
  51. #include <arpa/inet.h>
  52. #include <netdb.h>
  53. typedef struct imap_server_conf {
  54. struct imap_server_conf *next;
  55. char *name;
  56. char *tunnel;
  57. char *host;
  58. int port;
  59. char *user;
  60. char *pass;
  61. #ifdef HAVE_LIBSSL
  62. char *cert_file;
  63. unsigned use_imaps:1;
  64. unsigned require_ssl:1;
  65. unsigned use_sslv2:1;
  66. unsigned use_sslv3:1;
  67. unsigned use_tlsv1:1;
  68. unsigned require_cram:1;
  69. X509_STORE *cert_store;
  70. #endif
  71. } imap_server_conf_t;
  72. typedef struct imap_store_conf {
  73. store_conf_t gen;
  74. imap_server_conf_t *server;
  75. unsigned use_namespace:1;
  76. } imap_store_conf_t;
  77. typedef struct imap_message {
  78. message_t gen;
  79. /* int seq; will be needed when expunges are tracked */
  80. } imap_message_t;
  81. #define NIL (void*)0x1
  82. #define LIST (void*)0x2
  83. typedef struct _list {
  84. struct _list *next, *child;
  85. char *val;
  86. int len;
  87. } list_t;
  88. typedef struct {
  89. int fd;
  90. #ifdef HAVE_LIBSSL
  91. SSL *ssl;
  92. unsigned int use_ssl:1;
  93. #endif
  94. } Socket_t;
  95. typedef struct {
  96. Socket_t sock;
  97. int bytes;
  98. int offset;
  99. char buf[1024];
  100. } buffer_t;
  101. struct imap_cmd;
  102. #define max_in_progress 50 /* make this configurable? */
  103. typedef struct imap_store {
  104. store_t gen;
  105. const char *prefix;
  106. unsigned /*currentnc:1,*/ trashnc:1;
  107. int uidnext; /* from SELECT responses */
  108. unsigned got_namespace:1;
  109. list_t *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
  110. message_t **msgapp; /* FETCH results */
  111. unsigned caps, rcaps; /* CAPABILITY results */
  112. /* command queue */
  113. int nexttag, num_in_progress, literal_pending;
  114. struct imap_cmd *in_progress, **in_progress_append;
  115. #ifdef HAVE_LIBSSL
  116. SSL_CTX *SSLContext;
  117. #endif
  118. buffer_t buf; /* this is BIG, so put it last */
  119. } imap_store_t;
  120. struct imap_cmd {
  121. struct imap_cmd *next;
  122. char *cmd;
  123. int tag;
  124. struct {
  125. int (*cont)( imap_store_t *ctx, struct imap_cmd *cmd, const char *prompt );
  126. void (*done)( imap_store_t *ctx, struct imap_cmd *cmd, int response );
  127. void *aux;
  128. char *data;
  129. int data_len;
  130. int uid; /* to identify fetch responses */
  131. unsigned
  132. create:1, /* create the mailbox if we get an error ... */
  133. trycreate:1; /* ... but only if this is true or the server says so. */
  134. } param;
  135. };
  136. #define CAP(cap) (ctx->caps & (1 << (cap)))
  137. enum CAPABILITY {
  138. NOLOGIN = 0,
  139. UIDPLUS,
  140. LITERALPLUS,
  141. NAMESPACE,
  142. #ifdef HAVE_LIBSSL
  143. CRAM,
  144. STARTTLS,
  145. #endif
  146. };
  147. static const char *cap_list[] = {
  148. "LOGINDISABLED",
  149. "UIDPLUS",
  150. "LITERAL+",
  151. "NAMESPACE",
  152. #ifdef HAVE_LIBSSL
  153. "AUTH=CRAM-MD5",
  154. "STARTTLS",
  155. #endif
  156. };
  157. #define RESP_OK 0
  158. #define RESP_NO 1
  159. #define RESP_BAD 2
  160. static int get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd );
  161. static const char *Flags[] = {
  162. "Draft",
  163. "Flagged",
  164. "Answered",
  165. "Seen",
  166. "Deleted",
  167. };
  168. #ifdef HAVE_LIBSSL
  169. /* Some of this code is inspired by / lifted from mutt. */
  170. static int
  171. compare_certificates( X509 *cert, X509 *peercert,
  172. unsigned char *peermd, unsigned peermdlen )
  173. {
  174. unsigned char md[EVP_MAX_MD_SIZE];
  175. unsigned mdlen;
  176. /* Avoid CPU-intensive digest calculation if the certificates are
  177. * not even remotely equal. */
  178. if (X509_subject_name_cmp( cert, peercert ) ||
  179. X509_issuer_name_cmp( cert, peercert ))
  180. return -1;
  181. if (!X509_digest( cert, EVP_sha1(), md, &mdlen ) ||
  182. peermdlen != mdlen || memcmp( peermd, md, mdlen ))
  183. return -1;
  184. return 0;
  185. }
  186. #if OPENSSL_VERSION_NUMBER >= 0x00904000L
  187. #define READ_X509_KEY(fp, key) PEM_read_X509( fp, key, 0, 0 )
  188. #else
  189. #define READ_X509_KEY(fp, key) PEM_read_X509( fp, key, 0 )
  190. #endif
  191. /* this gets called when a certificate is to be verified */
  192. static int
  193. verify_cert( imap_store_t *ctx )
  194. {
  195. imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server;
  196. SSL *ssl = ctx->buf.sock.ssl;
  197. X509 *cert, *lcert;
  198. BIO *bio;
  199. FILE *fp;
  200. int err;
  201. unsigned n, i;
  202. X509_STORE_CTX xsc;
  203. char buf[256];
  204. unsigned char md[EVP_MAX_MD_SIZE];
  205. cert = SSL_get_peer_certificate( ssl );
  206. if (!cert) {
  207. error( "Error, no server certificate\n" );
  208. return -1;
  209. }
  210. while (srvc->cert_file) { // So break works
  211. if (X509_cmp_current_time( X509_get_notBefore( cert )) >= 0) {
  212. error( "Server certificate is not yet valid" );
  213. break;
  214. }
  215. if (X509_cmp_current_time( X509_get_notAfter( cert )) <= 0) {
  216. error( "Server certificate has expired" );
  217. break;
  218. }
  219. if (!X509_digest( cert, EVP_sha1(), md, &n )) {
  220. error( "*** Unable to calculate digest\n" );
  221. break;
  222. }
  223. if (!(fp = fopen( srvc->cert_file, "rt" ))) {
  224. error( "Unable to load CertificateFile '%s': %s\n",
  225. srvc->cert_file, strerror( errno ) );
  226. return 0;
  227. }
  228. err = -1;
  229. for (lcert = 0; READ_X509_KEY( fp, &lcert ); )
  230. if (!(err = compare_certificates( lcert, cert, md, n )))
  231. break;
  232. X509_free( lcert );
  233. fclose( fp );
  234. if (!err)
  235. return 0;
  236. break;
  237. }
  238. if (!srvc->cert_store) {
  239. if (!(srvc->cert_store = X509_STORE_new())) {
  240. error( "Error creating certificate store\n" );
  241. return -1;
  242. }
  243. if (!X509_STORE_set_default_paths( srvc->cert_store ))
  244. warn( "Error while loading default certificate files: %s\n",
  245. ERR_error_string( ERR_get_error(), 0 ) );
  246. if (!srvc->cert_file) {
  247. info( "Note: CertificateFile not defined\n" );
  248. } else if (!X509_STORE_load_locations( srvc->cert_store, srvc->cert_file, 0 )) {
  249. error( "Error while loading certificate file '%s': %s\n",
  250. srvc->cert_file, ERR_error_string( ERR_get_error(), 0 ) );
  251. return -1;
  252. }
  253. }
  254. X509_STORE_CTX_init( &xsc, srvc->cert_store, cert, 0 );
  255. err = X509_verify_cert( &xsc ) > 0 ? 0 : X509_STORE_CTX_get_error( &xsc );
  256. X509_STORE_CTX_cleanup( &xsc );
  257. if (!err)
  258. return 0;
  259. error( "Error, can't verify certificate: %s (%d)\n",
  260. X509_verify_cert_error_string( err ), err );
  261. X509_NAME_oneline( X509_get_subject_name( cert ), buf, sizeof(buf) );
  262. info( "\nSubject: %s\n", buf );
  263. X509_NAME_oneline( X509_get_issuer_name( cert ), buf, sizeof(buf) );
  264. info( "Issuer: %s\n", buf );
  265. bio = BIO_new( BIO_s_mem() );
  266. ASN1_TIME_print( bio, X509_get_notBefore( cert ) );
  267. memset( buf, 0, sizeof(buf) );
  268. BIO_read( bio, buf, sizeof(buf) - 1 );
  269. info( "Valid from: %s\n", buf );
  270. ASN1_TIME_print( bio, X509_get_notAfter( cert ) );
  271. memset( buf, 0, sizeof(buf) );
  272. BIO_read( bio, buf, sizeof(buf) - 1 );
  273. BIO_free( bio );
  274. info( " to: %s\n", buf );
  275. if (!X509_digest( cert, EVP_md5(), md, &n )) {
  276. error( "*** Unable to calculate fingerprint\n" );
  277. } else {
  278. info( "Fingerprint: " );
  279. for (i = 0; i < n; i += 2)
  280. info( "%02X%02X ", md[i], md[i + 1] );
  281. info( "\n" );
  282. }
  283. fputs( "\nAccept certificate? [y/N]: ", stderr );
  284. if (fgets( buf, sizeof(buf), stdin ) && (buf[0] == 'y' || buf[0] == 'Y'))
  285. return 0;
  286. return -1;
  287. }
  288. static int
  289. init_ssl_ctx( imap_store_t *ctx )
  290. {
  291. imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server;
  292. const SSL_METHOD *method;
  293. int options = 0;
  294. if (srvc->use_tlsv1 && !srvc->use_sslv2 && !srvc->use_sslv3)
  295. method = TLSv1_client_method();
  296. else
  297. method = SSLv23_client_method();
  298. ctx->SSLContext = SSL_CTX_new( method );
  299. if (!srvc->use_sslv2)
  300. options |= SSL_OP_NO_SSLv2;
  301. if (!srvc->use_sslv3)
  302. options |= SSL_OP_NO_SSLv3;
  303. if (!srvc->use_tlsv1)
  304. options |= SSL_OP_NO_TLSv1;
  305. SSL_CTX_set_options( ctx->SSLContext, options );
  306. /* we check the result of the verification after SSL_connect() */
  307. SSL_CTX_set_verify( ctx->SSLContext, SSL_VERIFY_NONE, 0 );
  308. return 0;
  309. }
  310. #endif /* HAVE_LIBSSL */
  311. static void
  312. socket_perror( const char *func, Socket_t *sock, int ret )
  313. {
  314. #ifdef HAVE_LIBSSL
  315. int err;
  316. if (sock->use_ssl) {
  317. switch ((err = SSL_get_error( sock->ssl, ret ))) {
  318. case SSL_ERROR_SYSCALL:
  319. case SSL_ERROR_SSL:
  320. if ((err = ERR_get_error()) == 0) {
  321. if (ret == 0)
  322. error( "SSL_%s: got EOF\n", func );
  323. else
  324. error( "SSL_%s: %s\n", func, strerror(errno) );
  325. } else
  326. error( "SSL_%s: %s\n", func, ERR_error_string( err, 0 ) );
  327. return;
  328. default:
  329. error( "SSL_%s: unhandled SSL error %d\n", func, err );
  330. break;
  331. }
  332. return;
  333. }
  334. #else
  335. (void)sock;
  336. #endif
  337. if (ret < 0)
  338. perror( func );
  339. else
  340. error( "%s: unexpected EOF\n", func );
  341. }
  342. static int
  343. socket_read( Socket_t *sock, char *buf, int len )
  344. {
  345. int n;
  346. assert( sock->fd >= 0 );
  347. n =
  348. #ifdef HAVE_LIBSSL
  349. sock->use_ssl ? SSL_read( sock->ssl, buf, len ) :
  350. #endif
  351. read( sock->fd, buf, len );
  352. if (n <= 0) {
  353. socket_perror( "read", sock, n );
  354. close( sock->fd );
  355. sock->fd = -1;
  356. }
  357. return n;
  358. }
  359. static int
  360. socket_write( Socket_t *sock, char *buf, int len )
  361. {
  362. int n;
  363. assert( sock->fd >= 0 );
  364. n =
  365. #ifdef HAVE_LIBSSL
  366. sock->use_ssl ? SSL_write( sock->ssl, buf, len ) :
  367. #endif
  368. write( sock->fd, buf, len );
  369. if (n != len) {
  370. socket_perror( "write", sock, n );
  371. close( sock->fd );
  372. sock->fd = -1;
  373. }
  374. return n;
  375. }
  376. static int
  377. socket_pending( Socket_t *sock )
  378. {
  379. int num = -1;
  380. if (ioctl( sock->fd, FIONREAD, &num ) < 0)
  381. return -1;
  382. if (num > 0)
  383. return num;
  384. #ifdef HAVE_LIBSSL
  385. if (sock->use_ssl)
  386. return SSL_pending( sock->ssl );
  387. #endif
  388. return 0;
  389. }
  390. /* simple line buffering */
  391. static int
  392. buffer_gets( buffer_t * b, char **s )
  393. {
  394. int n;
  395. int start = b->offset;
  396. *s = b->buf + start;
  397. for (;;) {
  398. /* make sure we have enough data to read the \r\n sequence */
  399. if (b->offset + 1 >= b->bytes) {
  400. if (start) {
  401. /* shift down used bytes */
  402. *s = b->buf;
  403. assert( start <= b->bytes );
  404. n = b->bytes - start;
  405. if (n)
  406. memmove( b->buf, b->buf + start, n );
  407. b->offset -= start;
  408. b->bytes = n;
  409. start = 0;
  410. }
  411. n = socket_read( &b->sock, b->buf + b->bytes,
  412. sizeof(b->buf) - b->bytes );
  413. if (n <= 0)
  414. return -1;
  415. b->bytes += n;
  416. }
  417. if (b->buf[b->offset] == '\r') {
  418. assert( b->offset + 1 < b->bytes );
  419. if (b->buf[b->offset + 1] == '\n') {
  420. b->buf[b->offset] = 0; /* terminate the string */
  421. b->offset += 2; /* next line */
  422. if (DFlags & VERBOSE)
  423. puts( *s );
  424. return 0;
  425. }
  426. }
  427. b->offset++;
  428. }
  429. /* not reached */
  430. }
  431. static struct imap_cmd *
  432. new_imap_cmd( void )
  433. {
  434. struct imap_cmd *cmd = nfmalloc( sizeof(*cmd) );
  435. memset( &cmd->param, 0, sizeof(cmd->param) );
  436. return cmd;
  437. }
  438. static struct imap_cmd *
  439. v_submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd,
  440. const char *fmt, va_list ap )
  441. {
  442. int n, bufl;
  443. char buf[1024];
  444. while (ctx->literal_pending)
  445. get_cmd_result( ctx, 0 );
  446. if (!cmd)
  447. cmd = new_imap_cmd();
  448. cmd->tag = ++ctx->nexttag;
  449. nfvasprintf( &cmd->cmd, fmt, ap );
  450. bufl = nfsnprintf( buf, sizeof(buf), cmd->param.data ? CAP(LITERALPLUS) ?
  451. "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
  452. cmd->tag, cmd->cmd, cmd->param.data_len );
  453. if (DFlags & VERBOSE) {
  454. if (ctx->num_in_progress)
  455. printf( "(%d in progress) ", ctx->num_in_progress );
  456. if (memcmp( cmd->cmd, "LOGIN", 5 ))
  457. printf( ">>> %s", buf );
  458. else
  459. printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag );
  460. }
  461. if (socket_write( &ctx->buf.sock, buf, bufl ) != bufl) {
  462. free( cmd->param.data );
  463. free( cmd->cmd );
  464. free( cmd );
  465. return NULL;
  466. }
  467. if (cmd->param.data) {
  468. if (CAP(LITERALPLUS)) {
  469. n = socket_write( &ctx->buf.sock, cmd->param.data, cmd->param.data_len );
  470. free( cmd->param.data );
  471. if (n != cmd->param.data_len ||
  472. (n = socket_write( &ctx->buf.sock, "\r\n", 2 )) != 2)
  473. {
  474. free( cmd->cmd );
  475. free( cmd );
  476. return NULL;
  477. }
  478. cmd->param.data = 0;
  479. } else
  480. ctx->literal_pending = 1;
  481. } else if (cmd->param.cont)
  482. ctx->literal_pending = 1;
  483. cmd->next = 0;
  484. *ctx->in_progress_append = cmd;
  485. ctx->in_progress_append = &cmd->next;
  486. ctx->num_in_progress++;
  487. return cmd;
  488. }
  489. static struct imap_cmd *
  490. submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd, const char *fmt, ... )
  491. {
  492. struct imap_cmd *ret;
  493. va_list ap;
  494. va_start( ap, fmt );
  495. ret = v_submit_imap_cmd( ctx, cmd, fmt, ap );
  496. va_end( ap );
  497. return ret;
  498. }
  499. static int
  500. imap_exec( imap_store_t *ctx, struct imap_cmd *cmdp, const char *fmt, ... )
  501. {
  502. va_list ap;
  503. va_start( ap, fmt );
  504. cmdp = v_submit_imap_cmd( ctx, cmdp, fmt, ap );
  505. va_end( ap );
  506. if (!cmdp)
  507. return RESP_BAD;
  508. return get_cmd_result( ctx, cmdp );
  509. }
  510. static int
  511. imap_exec_b( imap_store_t *ctx, struct imap_cmd *cmdp, const char *fmt, ... )
  512. {
  513. va_list ap;
  514. va_start( ap, fmt );
  515. cmdp = v_submit_imap_cmd( ctx, cmdp, fmt, ap );
  516. va_end( ap );
  517. if (!cmdp)
  518. return DRV_STORE_BAD;
  519. switch (get_cmd_result( ctx, cmdp )) {
  520. case RESP_BAD: return DRV_STORE_BAD;
  521. case RESP_NO: return DRV_BOX_BAD;
  522. default: return DRV_OK;
  523. }
  524. }
  525. static int
  526. imap_exec_m( imap_store_t *ctx, struct imap_cmd *cmdp, const char *fmt, ... )
  527. {
  528. va_list ap;
  529. va_start( ap, fmt );
  530. cmdp = v_submit_imap_cmd( ctx, cmdp, fmt, ap );
  531. va_end( ap );
  532. if (!cmdp)
  533. return DRV_STORE_BAD;
  534. switch (get_cmd_result( ctx, cmdp )) {
  535. case RESP_BAD: return DRV_STORE_BAD;
  536. case RESP_NO: return DRV_MSG_BAD;
  537. default: return DRV_OK;
  538. }
  539. }
  540. /*
  541. static void
  542. drain_imap_replies( imap_store_t *ctx )
  543. {
  544. while (ctx->num_in_progress)
  545. get_cmd_result( ctx, 0 );
  546. }
  547. */
  548. static void
  549. process_imap_replies( imap_store_t *ctx )
  550. {
  551. while (ctx->num_in_progress > max_in_progress ||
  552. socket_pending( &ctx->buf.sock ))
  553. get_cmd_result( ctx, 0 );
  554. }
  555. static int
  556. is_atom( list_t *list )
  557. {
  558. return list && list->val && list->val != NIL && list->val != LIST;
  559. }
  560. static int
  561. is_list( list_t *list )
  562. {
  563. return list && list->val == LIST;
  564. }
  565. static void
  566. free_list( list_t *list )
  567. {
  568. list_t *tmp;
  569. for (; list; list = tmp) {
  570. tmp = list->next;
  571. if (is_list( list ))
  572. free_list( list->child );
  573. else if (is_atom( list ))
  574. free( list->val );
  575. free( list );
  576. }
  577. }
  578. static int
  579. parse_imap_list_l( imap_store_t *ctx, char **sp, list_t **curp, int level )
  580. {
  581. list_t *cur;
  582. char *s = *sp, *p;
  583. int n, bytes;
  584. for (;;) {
  585. while (isspace( (unsigned char)*s ))
  586. s++;
  587. if (level && *s == ')') {
  588. s++;
  589. break;
  590. }
  591. *curp = cur = nfmalloc( sizeof(*cur) );
  592. curp = &cur->next;
  593. cur->val = 0; /* for clean bail */
  594. if (*s == '(') {
  595. /* sublist */
  596. s++;
  597. cur->val = LIST;
  598. if (parse_imap_list_l( ctx, &s, &cur->child, level + 1 ))
  599. goto bail;
  600. } else if (ctx && *s == '{') {
  601. /* literal */
  602. bytes = cur->len = strtol( s + 1, &s, 10 );
  603. if (*s != '}')
  604. goto bail;
  605. s = cur->val = nfmalloc( cur->len );
  606. /* dump whats left over in the input buffer */
  607. n = ctx->buf.bytes - ctx->buf.offset;
  608. if (n > bytes)
  609. /* the entire message fit in the buffer */
  610. n = bytes;
  611. memcpy( s, ctx->buf.buf + ctx->buf.offset, n );
  612. s += n;
  613. bytes -= n;
  614. /* mark that we used part of the buffer */
  615. ctx->buf.offset += n;
  616. /* now read the rest of the message */
  617. while (bytes > 0) {
  618. if ((n = socket_read( &ctx->buf.sock, s, bytes )) <= 0)
  619. goto bail;
  620. s += n;
  621. bytes -= n;
  622. }
  623. if (DFlags & XVERBOSE) {
  624. puts( "=========" );
  625. fwrite( cur->val, cur->len, 1, stdout );
  626. puts( "=========" );
  627. }
  628. if (buffer_gets( &ctx->buf, &s ))
  629. goto bail;
  630. } else if (*s == '"') {
  631. /* quoted string */
  632. s++;
  633. p = s;
  634. for (; *s != '"'; s++)
  635. if (!*s)
  636. goto bail;
  637. cur->len = s - p;
  638. s++;
  639. cur->val = nfmalloc( cur->len + 1 );
  640. memcpy( cur->val, p, cur->len );
  641. cur->val[cur->len] = 0;
  642. } else {
  643. /* atom */
  644. p = s;
  645. for (; *s && !isspace( (unsigned char)*s ); s++)
  646. if (level && *s == ')')
  647. break;
  648. cur->len = s - p;
  649. if (cur->len == 3 && !memcmp ("NIL", p, 3))
  650. cur->val = NIL;
  651. else {
  652. cur->val = nfmalloc( cur->len + 1 );
  653. memcpy( cur->val, p, cur->len );
  654. cur->val[cur->len] = 0;
  655. }
  656. }
  657. if (!level)
  658. break;
  659. if (!*s)
  660. goto bail;
  661. }
  662. *sp = s;
  663. *curp = 0;
  664. return 0;
  665. bail:
  666. *curp = 0;
  667. return -1;
  668. }
  669. static list_t *
  670. parse_imap_list( imap_store_t *ctx, char **sp )
  671. {
  672. list_t *head;
  673. if (!parse_imap_list_l( ctx, sp, &head, 0 ))
  674. return head;
  675. free_list( head );
  676. return NULL;
  677. }
  678. static list_t *
  679. parse_list( char **sp )
  680. {
  681. return parse_imap_list( 0, sp );
  682. }
  683. static int
  684. parse_fetch( imap_store_t *ctx, char *cmd ) /* move this down */
  685. {
  686. list_t *tmp, *list, *flags;
  687. char *body = 0;
  688. imap_message_t *cur;
  689. msg_data_t *msgdata;
  690. struct imap_cmd *cmdp;
  691. int uid = 0, mask = 0, status = 0, size = 0;
  692. unsigned i;
  693. list = parse_imap_list( ctx, &cmd );
  694. if (!is_list( list )) {
  695. error( "IMAP error: bogus FETCH response\n" );
  696. free_list( list );
  697. return -1;
  698. }
  699. for (tmp = list->child; tmp; tmp = tmp->next) {
  700. if (is_atom( tmp )) {
  701. if (!strcmp( "UID", tmp->val )) {
  702. tmp = tmp->next;
  703. if (is_atom( tmp ))
  704. uid = atoi( tmp->val );
  705. else
  706. error( "IMAP error: unable to parse UID\n" );
  707. } else if (!strcmp( "FLAGS", tmp->val )) {
  708. tmp = tmp->next;
  709. if (is_list( tmp )) {
  710. for (flags = tmp->child; flags; flags = flags->next) {
  711. if (is_atom( flags )) {
  712. if (flags->val[0] == '\\') { /* ignore user-defined flags for now */
  713. if (!strcmp( "Recent", flags->val + 1)) {
  714. status |= M_RECENT;
  715. goto flagok;
  716. }
  717. for (i = 0; i < as(Flags); i++)
  718. if (!strcmp( Flags[i], flags->val + 1 )) {
  719. mask |= 1 << i;
  720. goto flagok;
  721. }
  722. if (flags->val[1] == 'X' && flags->val[2] == '-')
  723. goto flagok; /* ignore system flag extensions */
  724. error( "IMAP warning: unknown system flag %s\n", flags->val );
  725. }
  726. flagok: ;
  727. } else
  728. error( "IMAP error: unable to parse FLAGS list\n" );
  729. }
  730. status |= M_FLAGS;
  731. } else
  732. error( "IMAP error: unable to parse FLAGS\n" );
  733. } else if (!strcmp( "RFC822.SIZE", tmp->val )) {
  734. tmp = tmp->next;
  735. if (is_atom( tmp ))
  736. size = atoi( tmp->val );
  737. else
  738. error( "IMAP error: unable to parse RFC822.SIZE\n" );
  739. } else if (!strcmp( "BODY[]", tmp->val )) {
  740. tmp = tmp->next;
  741. if (is_atom( tmp )) {
  742. body = tmp->val;
  743. tmp->val = 0; /* don't free together with list */
  744. size = tmp->len;
  745. } else
  746. error( "IMAP error: unable to parse BODY[]\n" );
  747. }
  748. }
  749. }
  750. if (body) {
  751. for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
  752. if (cmdp->param.uid == uid)
  753. goto gotuid;
  754. error( "IMAP error: unexpected FETCH response (UID %d)\n", uid );
  755. free_list( list );
  756. return -1;
  757. gotuid:
  758. msgdata = (msg_data_t *)cmdp->param.aux;
  759. msgdata->data = body;
  760. msgdata->len = size;
  761. if (status & M_FLAGS)
  762. msgdata->flags = mask;
  763. } else if (uid) { /* ignore async flag updates for now */
  764. /* XXX this will need sorting for out-of-order (multiple queries) */
  765. cur = nfcalloc( sizeof(*cur) );
  766. *ctx->msgapp = &cur->gen;
  767. ctx->msgapp = &cur->gen.next;
  768. cur->gen.next = 0;
  769. cur->gen.uid = uid;
  770. cur->gen.flags = mask;
  771. cur->gen.status = status;
  772. cur->gen.size = size;
  773. }
  774. free_list( list );
  775. return 0;
  776. }
  777. static void
  778. parse_capability( imap_store_t *ctx, char *cmd )
  779. {
  780. char *arg;
  781. unsigned i;
  782. ctx->caps = 0x80000000;
  783. while ((arg = next_arg( &cmd )))
  784. for (i = 0; i < as(cap_list); i++)
  785. if (!strcmp( cap_list[i], arg ))
  786. ctx->caps |= 1 << i;
  787. ctx->rcaps = ctx->caps;
  788. }
  789. static int
  790. parse_response_code( imap_store_t *ctx, struct imap_cmd *cmd, char *s )
  791. {
  792. char *arg, *earg, *p;
  793. if (*s != '[')
  794. return RESP_OK; /* no response code */
  795. s++;
  796. if (!(p = strchr( s, ']' ))) {
  797. error( "IMAP error: malformed response code\n" );
  798. return RESP_BAD;
  799. }
  800. *p++ = 0;
  801. arg = next_arg( &s );
  802. if (!strcmp( "UIDVALIDITY", arg )) {
  803. if (!(arg = next_arg( &s )) ||
  804. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg))
  805. {
  806. error( "IMAP error: malformed UIDVALIDITY status\n" );
  807. return RESP_BAD;
  808. }
  809. } else if (!strcmp( "UIDNEXT", arg )) {
  810. if (!(arg = next_arg( &s )) || (ctx->uidnext = strtol( arg, &p, 10 ), *p)) {
  811. error( "IMAP error: malformed NEXTUID status\n" );
  812. return RESP_BAD;
  813. }
  814. } else if (!strcmp( "CAPABILITY", arg )) {
  815. parse_capability( ctx, s );
  816. } else if (!strcmp( "ALERT", arg )) {
  817. /* RFC2060 says that these messages MUST be displayed
  818. * to the user
  819. */
  820. for (; isspace( (unsigned char)*p ); p++);
  821. error( "*** IMAP ALERT *** %s\n", p );
  822. } else if (cmd && cmd->param.aux && !strcmp( "APPENDUID", arg )) {
  823. if (!(arg = next_arg( &s )) ||
  824. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg) ||
  825. !(arg = next_arg( &s )) || !(*(int *)cmd->param.aux = atoi( arg )))
  826. {
  827. error( "IMAP error: malformed APPENDUID status\n" );
  828. return RESP_BAD;
  829. }
  830. }
  831. return RESP_OK;
  832. }
  833. static void
  834. parse_search( imap_store_t *ctx, char *cmd )
  835. {
  836. char *arg;
  837. struct imap_cmd *cmdp;
  838. int uid;
  839. if (!(arg = next_arg( &cmd )))
  840. uid = -1;
  841. else if (!(uid = atoi( arg ))) {
  842. error( "IMAP error: malformed SEARCH response\n" );
  843. return;
  844. } else if (next_arg( &cmd )) {
  845. warn( "IMAP warning: SEARCH returns multiple matches\n" );
  846. uid = -1; /* to avoid havoc */
  847. }
  848. /* Find the first command that expects a UID - this is guaranteed
  849. * to come in-order, as there are no other means to identify which
  850. * SEARCH response belongs to which request.
  851. */
  852. for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
  853. if (cmdp->param.uid == -1) {
  854. *(int *)cmdp->param.aux = uid;
  855. return;
  856. }
  857. error( "IMAP error: unexpected SEARCH response (UID %u)\n", uid );
  858. }
  859. static void
  860. parse_list_rsp( imap_store_t *ctx, char *cmd )
  861. {
  862. char *arg;
  863. list_t *list, *lp;
  864. int l;
  865. list = parse_list( &cmd );
  866. if (list->val == LIST)
  867. for (lp = list->child; lp; lp = lp->next)
  868. if (is_atom( lp ) && !strcasecmp( lp->val, "\\NoSelect" )) {
  869. free_list( list );
  870. return;
  871. }
  872. free_list( list );
  873. (void) next_arg( &cmd ); /* skip delimiter */
  874. arg = next_arg( &cmd );
  875. l = strlen( ctx->gen.conf->path );
  876. if (memcmp( arg, ctx->gen.conf->path, l ))
  877. return;
  878. arg += l;
  879. if (!memcmp( arg + strlen( arg ) - 5, ".lock", 5 )) /* workaround broken servers */
  880. return;
  881. add_string_list( &ctx->gen.boxes, arg );
  882. }
  883. static int
  884. get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
  885. {
  886. struct imap_cmd *cmdp, **pcmdp, *ncmdp;
  887. char *cmd, *arg, *arg1, *p;
  888. int n, resp, resp2, tag;
  889. for (;;) {
  890. if (buffer_gets( &ctx->buf, &cmd ))
  891. return RESP_BAD;
  892. arg = next_arg( &cmd );
  893. if (*arg == '*') {
  894. arg = next_arg( &cmd );
  895. if (!arg) {
  896. error( "IMAP error: unable to parse untagged response\n" );
  897. return RESP_BAD;
  898. }
  899. if (!strcmp( "NAMESPACE", arg )) {
  900. ctx->ns_personal = parse_list( &cmd );
  901. ctx->ns_other = parse_list( &cmd );
  902. ctx->ns_shared = parse_list( &cmd );
  903. } else if (!strcmp( "OK", arg ) || !strcmp( "BAD", arg ) ||
  904. !strcmp( "NO", arg ) || !strcmp( "BYE", arg )) {
  905. if ((resp = parse_response_code( ctx, 0, cmd )) != RESP_OK)
  906. return resp;
  907. } else if (!strcmp( "CAPABILITY", arg ))
  908. parse_capability( ctx, cmd );
  909. else if (!strcmp( "LIST", arg ))
  910. parse_list_rsp( ctx, cmd );
  911. else if (!strcmp( "SEARCH", arg ))
  912. parse_search( ctx, cmd );
  913. else if ((arg1 = next_arg( &cmd ))) {
  914. if (!strcmp( "EXISTS", arg1 ))
  915. ctx->gen.count = atoi( arg );
  916. else if (!strcmp( "RECENT", arg1 ))
  917. ctx->gen.recent = atoi( arg );
  918. else if(!strcmp ( "FETCH", arg1 )) {
  919. if (parse_fetch( ctx, cmd ))
  920. return RESP_BAD;
  921. }
  922. } else {
  923. error( "IMAP error: unable to parse untagged response\n" );
  924. return RESP_BAD;
  925. }
  926. } else if (!ctx->in_progress) {
  927. error( "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "" );
  928. return RESP_BAD;
  929. } else if (*arg == '+') {
  930. /* This can happen only with the last command underway, as
  931. it enforces a round-trip. */
  932. cmdp = ctx->in_progress;
  933. if (cmdp->param.data) {
  934. n = socket_write( &ctx->buf.sock, cmdp->param.data, cmdp->param.data_len );
  935. free( cmdp->param.data );
  936. cmdp->param.data = 0;
  937. if (n != (int)cmdp->param.data_len)
  938. return RESP_BAD;
  939. } else if (cmdp->param.cont) {
  940. if (cmdp->param.cont( ctx, cmdp, cmd ))
  941. return RESP_BAD;
  942. } else {
  943. error( "IMAP error: unexpected command continuation request\n" );
  944. return RESP_BAD;
  945. }
  946. if (socket_write( &ctx->buf.sock, "\r\n", 2 ) != 2)
  947. return RESP_BAD;
  948. if (!cmdp->param.cont)
  949. ctx->literal_pending = 0;
  950. if (!tcmd)
  951. return DRV_OK;
  952. } else {
  953. tag = atoi( arg );
  954. for (pcmdp = &ctx->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
  955. if (cmdp->tag == tag)
  956. goto gottag;
  957. error( "IMAP error: unexpected tag %s\n", arg );
  958. return RESP_BAD;
  959. gottag:
  960. if (!(*pcmdp = cmdp->next))
  961. ctx->in_progress_append = pcmdp;
  962. ctx->num_in_progress--;
  963. if (cmdp->param.cont || cmdp->param.data)
  964. ctx->literal_pending = 0;
  965. arg = next_arg( &cmd );
  966. if (!strcmp( "OK", arg ))
  967. resp = DRV_OK;
  968. else {
  969. if (!strcmp( "NO", arg )) {
  970. if (cmdp->param.create && cmd && (cmdp->param.trycreate || !memcmp( cmd, "[TRYCREATE]", 11 ))) { /* SELECT, APPEND or UID COPY */
  971. p = strchr( cmdp->cmd, '"' );
  972. if (!submit_imap_cmd( ctx, 0, "CREATE %.*s", strchr( p + 1, '"' ) - p + 1, p )) {
  973. resp = RESP_BAD;
  974. goto normal;
  975. }
  976. /* not waiting here violates the spec, but a server that does not
  977. grok this nonetheless violates it too. */
  978. ncmdp = nfmalloc( sizeof(*ncmdp) );
  979. memcpy( &ncmdp->param, &cmdp->param, sizeof(cmdp->param) );
  980. ncmdp->param.create = 0;
  981. if (!submit_imap_cmd( ctx, ncmdp, "%s", cmdp->cmd )) {
  982. resp = RESP_BAD;
  983. goto normal;
  984. }
  985. free( cmdp->cmd );
  986. free( cmdp );
  987. if (!tcmd)
  988. return 0; /* ignored */
  989. if (cmdp == tcmd)
  990. tcmd = ncmdp;
  991. continue;
  992. }
  993. resp = RESP_NO;
  994. } else /*if (!strcmp( "BAD", arg ))*/
  995. resp = RESP_BAD;
  996. error( "IMAP command '%s' returned an error: %s %s\n",
  997. memcmp( cmdp->cmd, "LOGIN", 5 ) ? cmdp->cmd : "LOGIN <user> <pass>",
  998. arg, cmd ? cmd : "" );
  999. }
  1000. if ((resp2 = parse_response_code( ctx, cmdp, cmd )) > resp)
  1001. resp = resp2;
  1002. normal:
  1003. if (cmdp->param.done)
  1004. cmdp->param.done( ctx, cmdp, resp );
  1005. free( cmdp->param.data );
  1006. free( cmdp->cmd );
  1007. free( cmdp );
  1008. if (!tcmd || tcmd == cmdp)
  1009. return resp;
  1010. }
  1011. }
  1012. /* not reached */
  1013. }
  1014. static void
  1015. imap_cancel_store( store_t *gctx )
  1016. {
  1017. imap_store_t *ctx = (imap_store_t *)gctx;
  1018. free_generic_messages( gctx->msgs );
  1019. free_string_list( ctx->gen.boxes );
  1020. if (ctx->buf.sock.fd >= 0)
  1021. close( ctx->buf.sock.fd );
  1022. #ifdef HAVE_LIBSSL
  1023. if (ctx->buf.sock.ssl)
  1024. SSL_free( ctx->buf.sock.ssl );
  1025. if (ctx->SSLContext)
  1026. SSL_CTX_free( ctx->SSLContext );
  1027. #endif
  1028. free_list( ctx->ns_personal );
  1029. free_list( ctx->ns_other );
  1030. free_list( ctx->ns_shared );
  1031. free( ctx );
  1032. }
  1033. static store_t *unowned;
  1034. static void
  1035. imap_disown_store( store_t *gctx )
  1036. {
  1037. free_generic_messages( gctx->msgs );
  1038. gctx->msgs = 0;
  1039. gctx->next = unowned;
  1040. unowned = gctx;
  1041. }
  1042. static store_t *
  1043. imap_own_store( store_conf_t *conf )
  1044. {
  1045. store_t *store, **storep;
  1046. for (storep = &unowned; (store = *storep); storep = &store->next)
  1047. if (store->conf == conf) {
  1048. *storep = store->next;
  1049. return store;
  1050. }
  1051. return 0;
  1052. }
  1053. static void
  1054. imap_cleanup( void )
  1055. {
  1056. store_t *ctx, *nctx;
  1057. for (ctx = unowned; ctx; ctx = nctx) {
  1058. nctx = ctx->next;
  1059. imap_exec( (imap_store_t *)ctx, 0, "LOGOUT" );
  1060. imap_cancel_store( ctx );
  1061. }
  1062. }
  1063. #ifdef HAVE_LIBSSL
  1064. static int
  1065. start_tls( imap_store_t *ctx )
  1066. {
  1067. int ret;
  1068. static int ssl_inited;
  1069. if (!ssl_inited) {
  1070. SSL_library_init();
  1071. SSL_load_error_strings();
  1072. ssl_inited = 1;
  1073. }
  1074. if (init_ssl_ctx( ctx ))
  1075. return 1;
  1076. ctx->buf.sock.ssl = SSL_new( ctx->SSLContext );
  1077. SSL_set_fd( ctx->buf.sock.ssl, ctx->buf.sock.fd );
  1078. if ((ret = SSL_connect( ctx->buf.sock.ssl )) <= 0) {
  1079. socket_perror( "connect", &ctx->buf.sock, ret );
  1080. return 1;
  1081. }
  1082. /* verify the server certificate */
  1083. if (verify_cert( ctx ))
  1084. return 1;
  1085. ctx->buf.sock.use_ssl = 1;
  1086. info( "Connection is now encrypted\n" );
  1087. return 0;
  1088. }
  1089. #define ENCODED_SIZE(n) (4*((n+2)/3))
  1090. static char
  1091. hexchar( unsigned int b )
  1092. {
  1093. if (b < 10)
  1094. return '0' + b;
  1095. return 'a' + (b - 10);
  1096. }
  1097. static void
  1098. cram( const char *challenge, const char *user, const char *pass, char **_final, int *_finallen )
  1099. {
  1100. unsigned char *response, *final;
  1101. unsigned hashlen;
  1102. int i, clen, rlen, blen, flen, olen;
  1103. unsigned char hash[16];
  1104. char buf[256], hex[33];
  1105. HMAC_CTX hmac;
  1106. HMAC_Init( &hmac, (unsigned char *)pass, strlen( pass ), EVP_md5() );
  1107. clen = strlen( challenge );
  1108. /* response will always be smaller than challenge because we are decoding. */
  1109. response = nfcalloc( 1 + clen );
  1110. rlen = EVP_DecodeBlock( response, (unsigned char *)challenge, clen );
  1111. HMAC_Update( &hmac, response, rlen );
  1112. free( response );
  1113. hashlen = sizeof(hash);
  1114. HMAC_Final( &hmac, hash, &hashlen );
  1115. assert( hashlen == sizeof(hash) );
  1116. hex[32] = 0;
  1117. for (i = 0; i < 16; i++) {
  1118. hex[2 * i] = hexchar( (hash[i] >> 4) & 0xf );
  1119. hex[2 * i + 1] = hexchar( hash[i] & 0xf );
  1120. }
  1121. blen = nfsnprintf( buf, sizeof(buf), "%s %s", user, hex );
  1122. flen = ENCODED_SIZE( blen );
  1123. final = nfmalloc( flen + 1 );
  1124. final[flen] = 0;
  1125. olen = EVP_EncodeBlock( (unsigned char *)final, (unsigned char *)buf, blen );
  1126. assert( olen == flen );
  1127. *_final = (char *)final;
  1128. *_finallen = flen;
  1129. }
  1130. static int
  1131. do_cram_auth( imap_store_t *ctx, struct imap_cmd *cmdp, const char *prompt )
  1132. {
  1133. imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server;
  1134. char *resp;
  1135. int n, l;
  1136. cram( prompt, srvc->user, srvc->pass, &resp, &l );
  1137. if (DFlags & VERBOSE)
  1138. printf( ">+> %s\n", resp );
  1139. n = socket_write( &ctx->buf.sock, resp, l );
  1140. free( resp );
  1141. if (n != l)
  1142. return -1;
  1143. cmdp->param.cont = 0;
  1144. return 0;
  1145. }
  1146. #endif
  1147. static void
  1148. imap_open_store( store_conf_t *conf,
  1149. void (*cb)( store_t *srv, void *aux ), void *aux )
  1150. {
  1151. imap_store_conf_t *cfg = (imap_store_conf_t *)conf;
  1152. imap_server_conf_t *srvc = cfg->server;
  1153. imap_store_t *ctx;
  1154. store_t **ctxp;
  1155. char *arg, *rsp;
  1156. struct hostent *he;
  1157. struct sockaddr_in addr;
  1158. int s, a[2], preauth;
  1159. #ifdef HAVE_LIBSSL
  1160. int use_ssl;
  1161. #endif
  1162. for (ctxp = &unowned; (ctx = (imap_store_t *)*ctxp); ctxp = &ctx->gen.next)
  1163. if (((imap_store_conf_t *)ctx->gen.conf)->server == srvc) {
  1164. *ctxp = ctx->gen.next;
  1165. /* One could ping the server here, but given that the idle timeout
  1166. * is at least 30 minutes, this sounds pretty pointless. */
  1167. free_string_list( ctx->gen.boxes );
  1168. ctx->gen.boxes = 0;
  1169. ctx->gen.listed = 0;
  1170. ctx->gen.conf = conf;
  1171. goto final;
  1172. }
  1173. ctx = nfcalloc( sizeof(*ctx) );
  1174. ctx->gen.conf = conf;
  1175. ctx->buf.sock.fd = -1;
  1176. ctx->in_progress_append = &ctx->in_progress;
  1177. /* open connection to IMAP server */
  1178. #ifdef HAVE_LIBSSL
  1179. use_ssl = 0;
  1180. #endif
  1181. if (srvc->tunnel) {
  1182. infon( "Starting tunnel '%s'... ", srvc->tunnel );
  1183. if (socketpair( PF_UNIX, SOCK_STREAM, 0, a )) {
  1184. perror( "socketpair" );
  1185. exit( 1 );
  1186. }
  1187. if (fork() == 0) {
  1188. if (dup2( a[0], 0 ) == -1 || dup2( a[0], 1 ) == -1)
  1189. _exit( 127 );
  1190. close( a[0] );
  1191. close( a[1] );
  1192. execl( "/bin/sh", "sh", "-c", srvc->tunnel, (char *)0 );
  1193. _exit( 127 );
  1194. }
  1195. close (a[0]);
  1196. ctx->buf.sock.fd = a[1];
  1197. info( "ok\n" );
  1198. } else {
  1199. memset( &addr, 0, sizeof(addr) );
  1200. addr.sin_port = srvc->port ? htons( srvc->port ) :
  1201. #ifdef HAVE_LIBSSL
  1202. srvc->use_imaps ? htons( 993 ) :
  1203. #endif
  1204. htons( 143 );
  1205. addr.sin_family = AF_INET;
  1206. infon( "Resolving %s... ", srvc->host );
  1207. he = gethostbyname( srvc->host );
  1208. if (!he) {
  1209. error( "IMAP error: Cannot resolve server '%s'\n", srvc->host );
  1210. goto bail;
  1211. }
  1212. info( "ok\n" );
  1213. addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
  1214. s = socket( PF_INET, SOCK_STREAM, 0 );
  1215. if (s < 0) {
  1216. perror( "socket" );
  1217. exit( 1 );
  1218. }
  1219. infon( "Connecting to %s:%hu... ", inet_ntoa( addr.sin_addr ), ntohs( addr.sin_port ) );
  1220. if (connect( s, (struct sockaddr *)&addr, sizeof(addr) )) {
  1221. close( s );
  1222. perror( "connect" );
  1223. goto bail;
  1224. }
  1225. info( "ok\n" );
  1226. ctx->buf.sock.fd = s;
  1227. }
  1228. #ifdef HAVE_LIBSSL
  1229. if (srvc->use_imaps) {
  1230. if (start_tls( ctx ))
  1231. goto ssl_bail;
  1232. use_ssl = 1;
  1233. }
  1234. #endif
  1235. /* read the greeting string */
  1236. if (buffer_gets( &ctx->buf, &rsp ))
  1237. goto bail;
  1238. arg = next_arg( &rsp );
  1239. if (!arg || *arg != '*' || (arg = next_arg( &rsp )) == NULL) {
  1240. error( "IMAP error: invalid greeting response\n" );
  1241. goto bail;
  1242. }
  1243. preauth = 0;
  1244. if (!strcmp( "PREAUTH", arg ))
  1245. preauth = 1;
  1246. else if (strcmp( "OK", arg ) != 0) {
  1247. error( "IMAP error: unknown greeting response\n" );
  1248. goto bail;
  1249. }
  1250. parse_response_code( ctx, 0, rsp );
  1251. if (!ctx->caps && imap_exec( ctx, 0, "CAPABILITY" ) != RESP_OK)
  1252. goto bail;
  1253. if (!preauth) {
  1254. #ifdef HAVE_LIBSSL
  1255. if (!srvc->use_imaps && (srvc->use_sslv2 || srvc->use_sslv3 || srvc->use_tlsv1)) {
  1256. /* always try to select SSL support if available */
  1257. if (CAP(STARTTLS)) {
  1258. if (imap_exec( ctx, 0, "STARTTLS" ) != RESP_OK)
  1259. goto bail;
  1260. if (start_tls( ctx ))
  1261. goto ssl_bail;
  1262. use_ssl = 1;
  1263. if (imap_exec( ctx, 0, "CAPABILITY" ) != RESP_OK)
  1264. goto bail;
  1265. } else {
  1266. if (srvc->require_ssl) {
  1267. error( "IMAP error: SSL support not available\n" );
  1268. goto bail;
  1269. } else
  1270. warn( "IMAP warning: SSL support not available\n" );
  1271. }
  1272. }
  1273. #endif
  1274. info ("Logging in...\n");
  1275. if (!srvc->user) {
  1276. error( "Skipping account %s, no user\n", srvc->name );
  1277. goto bail;
  1278. }
  1279. if (!srvc->pass) {
  1280. char prompt[80];
  1281. sprintf( prompt, "Password (%s): ", srvc->name );
  1282. arg = getpass( prompt );
  1283. if (!arg) {
  1284. perror( "getpass" );
  1285. exit( 1 );
  1286. }
  1287. if (!*arg) {
  1288. error( "Skipping account %s, no password\n", srvc->name );
  1289. goto bail;
  1290. }
  1291. /*
  1292. * getpass() returns a pointer to a static buffer. make a copy
  1293. * for long term storage.
  1294. */
  1295. srvc->pass = nfstrdup( arg );
  1296. }
  1297. #ifdef HAVE_LIBSSL
  1298. if (CAP(CRAM)) {
  1299. struct imap_cmd *cmd = new_imap_cmd();
  1300. info( "Authenticating with CRAM-MD5\n" );
  1301. cmd->param.cont = do_cram_auth;
  1302. if (imap_exec( ctx, cmd, "AUTHENTICATE CRAM-MD5" ) != RESP_OK)
  1303. goto bail;
  1304. } else if (srvc->require_cram) {
  1305. error( "IMAP error: CRAM-MD5 authentication is not supported by server\n" );
  1306. goto bail;
  1307. } else
  1308. #endif
  1309. {
  1310. if (CAP(NOLOGIN)) {
  1311. error( "Skipping account %s, server forbids LOGIN\n", srvc->name );
  1312. goto bail;
  1313. }
  1314. #ifdef HAVE_LIBSSL
  1315. if (!use_ssl)
  1316. #endif
  1317. warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
  1318. if (imap_exec( ctx, 0, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass ) != RESP_OK) {
  1319. error( "IMAP error: LOGIN failed\n" );
  1320. goto bail;
  1321. }
  1322. }
  1323. } /* !preauth */
  1324. final:
  1325. ctx->prefix = "";
  1326. if (*conf->path)
  1327. ctx->prefix = conf->path;
  1328. else if (cfg->use_namespace && CAP(NAMESPACE)) {
  1329. /* get NAMESPACE info */
  1330. if (!ctx->got_namespace) {
  1331. if (imap_exec( ctx, 0, "NAMESPACE" ) != RESP_OK) {
  1332. cb( 0, aux );
  1333. return;
  1334. }
  1335. ctx->got_namespace = 1;
  1336. }
  1337. /* XXX for now assume personal namespace */
  1338. if (is_list( ctx->ns_personal ) &&
  1339. is_list( ctx->ns_personal->child ) &&
  1340. is_atom( ctx->ns_personal->child->child ))
  1341. ctx->prefix = ctx->ns_personal->child->child->val;
  1342. }
  1343. ctx->trashnc = 1;
  1344. cb( &ctx->gen, aux );
  1345. return;
  1346. #ifdef HAVE_LIBSSL
  1347. ssl_bail:
  1348. /* This avoids that we try to send LOGOUT to an unusable socket. */
  1349. close( ctx->buf.sock.fd );
  1350. ctx->buf.sock.fd = -1;
  1351. #endif
  1352. bail:
  1353. imap_cancel_store( &ctx->gen );
  1354. cb( 0, aux );
  1355. }
  1356. static void
  1357. imap_prepare_paths( store_t *gctx )
  1358. {
  1359. free_generic_messages( gctx->msgs );
  1360. gctx->msgs = 0;
  1361. }
  1362. static void
  1363. imap_prepare_opts( store_t *gctx, int opts )
  1364. {
  1365. gctx->opts = opts;
  1366. }
  1367. static int
  1368. imap_select( store_t *gctx, int minuid, int maxuid, int *excs, int nexcs,
  1369. int (*cb)( int sts, void *aux ), void *aux )
  1370. {
  1371. imap_store_t *ctx = (imap_store_t *)gctx;
  1372. struct imap_cmd *cmd = new_imap_cmd();
  1373. const char *prefix;
  1374. int ret, i, j, bl;
  1375. char buf[1000];
  1376. if (!strcmp( gctx->name, "INBOX" )) {
  1377. // ctx->currentnc = 0;
  1378. prefix = "";
  1379. } else {
  1380. // ctx->currentnc = 1; /* could use LIST results for that */
  1381. prefix = ctx->prefix;
  1382. }
  1383. ctx->uidnext = -1;
  1384. cmd->param.create = (gctx->opts & OPEN_CREATE) != 0;
  1385. cmd->param.trycreate = 1;
  1386. if ((ret = imap_exec_b( ctx, cmd, "SELECT \"%s%s\"", prefix, gctx->name )) != DRV_OK)
  1387. goto bail;
  1388. if (gctx->count) {
  1389. ctx->msgapp = &gctx->msgs;
  1390. sort_ints( excs, nexcs );
  1391. for (i = 0; i < nexcs; ) {
  1392. for (bl = 0; i < nexcs && bl < 960; i++) {
  1393. if (bl)
  1394. buf[bl++] = ',';
  1395. bl += sprintf( buf + bl, "%d", excs[i] );
  1396. j = i;
  1397. for (; i + 1 < nexcs && excs[i + 1] == excs[i] + 1; i++);
  1398. if (i != j)
  1399. bl += sprintf( buf + bl, ":%d", excs[i] );
  1400. }
  1401. if ((ret = imap_exec_b( ctx, 0, "UID FETCH %s (UID%s%s)", buf,
  1402. (gctx->opts & OPEN_FLAGS) ? " FLAGS" : "",
  1403. (gctx->opts & OPEN_SIZE) ? " RFC822.SIZE" : "" )) != DRV_OK)
  1404. goto bail;
  1405. }
  1406. if (maxuid == INT_MAX)
  1407. maxuid = ctx->uidnext >= 0 ? ctx->uidnext - 1 : 1000000000;
  1408. if (maxuid >= minuid &&
  1409. (ret = imap_exec_b( ctx, 0, "UID FETCH %d:%d (UID%s%s)", minuid, maxuid,
  1410. (gctx->opts & OPEN_FLAGS) ? " FLAGS" : "",
  1411. (gctx->opts & OPEN_SIZE) ? " RFC822.SIZE" : "" )) != DRV_OK)
  1412. goto bail;
  1413. }
  1414. ret = DRV_OK;
  1415. bail:
  1416. free( excs );
  1417. return cb( ret, aux );
  1418. }
  1419. static int
  1420. imap_fetch_msg( store_t *ctx, message_t *msg, msg_data_t *data,
  1421. int (*cb)( int sts, void *aux ), void *aux )
  1422. {
  1423. struct imap_cmd *cmd = new_imap_cmd();
  1424. cmd->param.uid = msg->uid;
  1425. cmd->param.aux = data;
  1426. return cb( imap_exec_m( (imap_store_t *)ctx, cmd, "UID FETCH %d (%sBODY.PEEK[])",
  1427. msg->uid, (msg->status & M_FLAGS) ? "" : "FLAGS " ), aux );
  1428. }
  1429. static int
  1430. imap_make_flags( int flags, char *buf )
  1431. {
  1432. const char *s;
  1433. unsigned i, d;
  1434. for (i = d = 0; i < as(Flags); i++)
  1435. if (flags & (1 << i)) {
  1436. buf[d++] = ' ';
  1437. buf[d++] = '\\';
  1438. for (s = Flags[i]; *s; s++)
  1439. buf[d++] = *s;
  1440. }
  1441. buf[0] = '(';
  1442. buf[d++] = ')';
  1443. return d;
  1444. }
  1445. static int
  1446. imap_flags_helper( imap_store_t *ctx, int uid, char what, int flags)
  1447. {
  1448. char buf[256];
  1449. buf[imap_make_flags( flags, buf )] = 0;
  1450. if (!submit_imap_cmd( ctx, 0, "UID STORE %d %cFLAGS.SILENT %s", uid, what, buf ))
  1451. return DRV_STORE_BAD;
  1452. process_imap_replies( ctx );
  1453. return DRV_OK;
  1454. }
  1455. static int
  1456. imap_set_flags( store_t *gctx, message_t *msg, int uid, int add, int del,
  1457. int (*cb)( int sts, void *aux ), void *aux )
  1458. {
  1459. imap_store_t *ctx = (imap_store_t *)gctx;
  1460. int ret;
  1461. if (msg) {
  1462. uid = msg->uid;
  1463. add &= ~msg->flags;
  1464. del &= msg->flags;
  1465. msg->flags |= add;
  1466. msg->flags &= ~del;
  1467. }
  1468. if ((!add || (ret = imap_flags_helper( ctx, uid, '+', add )) == DRV_OK) &&
  1469. (!del || (ret = imap_flags_helper( ctx, uid, '-', del )) == DRV_OK))
  1470. ret = DRV_OK;
  1471. return cb( ret, aux );
  1472. }
  1473. static int
  1474. imap_close( store_t *ctx,
  1475. int (*cb)( int sts, void *aux ), void *aux )
  1476. {
  1477. return cb( imap_exec_b( (imap_store_t *)ctx, 0, "CLOSE" ), aux );
  1478. }
  1479. static int
  1480. imap_trash_msg( store_t *gctx, message_t *msg,
  1481. int (*cb)( int sts, void *aux ), void *aux )
  1482. {
  1483. imap_store_t *ctx = (imap_store_t *)gctx;
  1484. struct imap_cmd *cmd = new_imap_cmd();
  1485. cmd->param.create = 1;
  1486. return cb( imap_exec_m( ctx, cmd, "UID COPY %d \"%s%s\"",
  1487. msg->uid, ctx->prefix, gctx->conf->trash ), aux );
  1488. }
  1489. static int
  1490. imap_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1491. int (*cb)( int sts, int uid, void *aux ), void *aux )
  1492. {
  1493. imap_store_t *ctx = (imap_store_t *)gctx;
  1494. struct imap_cmd *cmd = new_imap_cmd();
  1495. const char *prefix, *box;
  1496. int ret, d, uid;
  1497. char flagstr[128];
  1498. d = 0;
  1499. if (data->flags) {
  1500. d = imap_make_flags( data->flags, flagstr );
  1501. flagstr[d++] = ' ';
  1502. }
  1503. flagstr[d] = 0;
  1504. cmd->param.data_len = data->len;
  1505. cmd->param.data = data->data;
  1506. cmd->param.aux = &uid;
  1507. uid = -2;
  1508. if (to_trash) {
  1509. box = gctx->conf->trash;
  1510. prefix = ctx->prefix;
  1511. cmd->param.create = 1;
  1512. if (ctx->trashnc)
  1513. ctx->caps = ctx->rcaps & ~(1 << LITERALPLUS);
  1514. } else {
  1515. box = gctx->name;
  1516. prefix = !strcmp( box, "INBOX" ) ? "" : ctx->prefix;
  1517. cmd->param.create = (gctx->opts & OPEN_CREATE) != 0;
  1518. /*if (ctx->currentnc)
  1519. ctx->caps = ctx->rcaps & ~(1 << LITERALPLUS);*/
  1520. }
  1521. ret = imap_exec_m( ctx, cmd, "APPEND \"%s%s\" %s", prefix, box, flagstr );
  1522. ctx->caps = ctx->rcaps;
  1523. if (ret != DRV_OK)
  1524. return cb( ret, -1, aux );
  1525. if (to_trash)
  1526. ctx->trashnc = 0;
  1527. else {
  1528. /*ctx->currentnc = 0;*/
  1529. }
  1530. return cb( DRV_OK, uid, aux );
  1531. }
  1532. static int
  1533. imap_find_msg( store_t *gctx, const char *tuid,
  1534. int (*cb)( int sts, int uid, void *aux ), void *aux )
  1535. {
  1536. imap_store_t *ctx = (imap_store_t *)gctx;
  1537. struct imap_cmd *cmd = new_imap_cmd();
  1538. int ret, uid;
  1539. cmd->param.uid = -1; /* we're looking for a UID */
  1540. cmd->param.aux = &uid;
  1541. uid = -1; /* in case we get no SEARCH response at all */
  1542. if ((ret = imap_exec_m( ctx, cmd, "UID SEARCH HEADER X-TUID %." stringify(TUIDL) "s", tuid )) != DRV_OK)
  1543. return cb( ret, -1, aux );
  1544. else
  1545. return cb( uid <= 0 ? DRV_MSG_BAD : DRV_OK, uid, aux );
  1546. }
  1547. static void
  1548. imap_list( store_t *gctx,
  1549. void (*cb)( int sts, void *aux ), void *aux )
  1550. {
  1551. imap_store_t *ctx = (imap_store_t *)gctx;
  1552. int ret;
  1553. if ((ret = imap_exec_b( ctx, 0, "LIST \"\" \"%s%%\"", ctx->prefix )) == DRV_OK)
  1554. gctx->listed = 1;
  1555. cb( ret, aux );
  1556. }
  1557. static void
  1558. imap_cancel( store_t *gctx,
  1559. void (*cb)( int sts, void *aux ), void *aux )
  1560. {
  1561. (void)gctx;
  1562. cb( DRV_OK, aux );
  1563. }
  1564. static void
  1565. imap_commit( store_t *gctx )
  1566. {
  1567. (void)gctx;
  1568. }
  1569. imap_server_conf_t *servers, **serverapp = &servers;
  1570. static int
  1571. imap_parse_store( conffile_t *cfg, store_conf_t **storep, int *err )
  1572. {
  1573. imap_store_conf_t *store;
  1574. imap_server_conf_t *server, *srv, sserver;
  1575. int acc_opt = 0;
  1576. if (!strcasecmp( "IMAPAccount", cfg->cmd )) {
  1577. server = nfcalloc( sizeof(*server) );
  1578. server->name = nfstrdup( cfg->val );
  1579. *serverapp = server;
  1580. serverapp = &server->next;
  1581. store = 0;
  1582. *storep = 0;
  1583. } else if (!strcasecmp( "IMAPStore", cfg->cmd )) {
  1584. store = nfcalloc( sizeof(*store) );
  1585. store->gen.driver = &imap_driver;
  1586. store->gen.name = nfstrdup( cfg->val );
  1587. store->use_namespace = 1;
  1588. *storep = &store->gen;
  1589. memset( &sserver, 0, sizeof(sserver) );
  1590. server = &sserver;
  1591. } else
  1592. return 0;
  1593. #ifdef HAVE_LIBSSL
  1594. /* this will probably annoy people, but its the best default just in
  1595. * case people forget to turn it on
  1596. */
  1597. server->require_ssl = 1;
  1598. server->use_tlsv1 = 1;
  1599. #endif
  1600. while (getcline( cfg ) && cfg->cmd) {
  1601. if (!strcasecmp( "Host", cfg->cmd )) {
  1602. /* The imap[s]: syntax is just a backwards compat hack. */
  1603. #ifdef HAVE_LIBSSL
  1604. if (!memcmp( "imaps:", cfg->val, 6 )) {
  1605. cfg->val += 6;
  1606. server->use_imaps = 1;
  1607. server->use_sslv2 = 1;
  1608. server->use_sslv3 = 1;
  1609. } else
  1610. #endif
  1611. {
  1612. if (!memcmp( "imap:", cfg->val, 5 ))
  1613. cfg->val += 5;
  1614. }
  1615. if (!memcmp( "//", cfg->val, 2 ))
  1616. cfg->val += 2;
  1617. server->host = nfstrdup( cfg->val );
  1618. }
  1619. else if (!strcasecmp( "User", cfg->cmd ))
  1620. server->user = nfstrdup( cfg->val );
  1621. else if (!strcasecmp( "Pass", cfg->cmd ))
  1622. server->pass = nfstrdup( cfg->val );
  1623. else if (!strcasecmp( "Port", cfg->cmd ))
  1624. server->port = parse_int( cfg );
  1625. #ifdef HAVE_LIBSSL
  1626. else if (!strcasecmp( "CertificateFile", cfg->cmd )) {
  1627. server->cert_file = expand_strdup( cfg->val );
  1628. if (access( server->cert_file, R_OK )) {
  1629. error( "%s:%d: CertificateFile '%s': %s\n",
  1630. cfg->file, cfg->line, server->cert_file, strerror( errno ) );
  1631. *err = 1;
  1632. }
  1633. } else if (!strcasecmp( "RequireSSL", cfg->cmd ))
  1634. server->require_ssl = parse_bool( cfg );
  1635. else if (!strcasecmp( "UseIMAPS", cfg->cmd ))
  1636. server->use_imaps = parse_bool( cfg );
  1637. else if (!strcasecmp( "UseSSLv2", cfg->cmd ))
  1638. server->use_sslv2 = parse_bool( cfg );
  1639. else if (!strcasecmp( "UseSSLv3", cfg->cmd ))
  1640. server->use_sslv3 = parse_bool( cfg );
  1641. else if (!strcasecmp( "UseTLSv1", cfg->cmd ))
  1642. server->use_tlsv1 = parse_bool( cfg );
  1643. else if (!strcasecmp( "RequireCRAM", cfg->cmd ))
  1644. server->require_cram = parse_bool( cfg );
  1645. #endif
  1646. else if (!strcasecmp( "Tunnel", cfg->cmd ))
  1647. server->tunnel = nfstrdup( cfg->val );
  1648. else if (store) {
  1649. if (!strcasecmp( "Account", cfg->cmd )) {
  1650. for (srv = servers; srv; srv = srv->next)
  1651. if (srv->name && !strcmp( srv->name, cfg->val ))
  1652. goto gotsrv;
  1653. error( "%s:%d: unknown IMAP account '%s'\n", cfg->file, cfg->line, cfg->val );
  1654. *err = 1;
  1655. continue;
  1656. gotsrv:
  1657. store->server = srv;
  1658. } else if (!strcasecmp( "UseNamespace", cfg->cmd ))
  1659. store->use_namespace = parse_bool( cfg );
  1660. else if (!strcasecmp( "Path", cfg->cmd ))
  1661. store->gen.path = nfstrdup( cfg->val );
  1662. else
  1663. parse_generic_store( &store->gen, cfg, err );
  1664. continue;
  1665. } else {
  1666. error( "%s:%d: unknown/misplaced keyword '%s'\n", cfg->file, cfg->line, cfg->cmd );
  1667. *err = 1;
  1668. continue;
  1669. }
  1670. acc_opt = 1;
  1671. }
  1672. if (!store || !store->server) {
  1673. if (!server->tunnel && !server->host) {
  1674. if (store)
  1675. error( "IMAP store '%s' has incomplete/missing connection details\n", store->gen.name );
  1676. else
  1677. error( "IMAP account '%s' has incomplete/missing connection details\n", server->name );
  1678. *err = 1;
  1679. return 1;
  1680. }
  1681. }
  1682. if (store) {
  1683. if (!store->server) {
  1684. store->server = nfmalloc( sizeof(sserver) );
  1685. memcpy( store->server, &sserver, sizeof(sserver) );
  1686. store->server->name = store->gen.name;
  1687. } else if (acc_opt) {
  1688. error( "IMAP store '%s' has both Account and account-specific options\n", store->gen.name );
  1689. *err = 1;
  1690. }
  1691. }
  1692. return 1;
  1693. }
  1694. struct driver imap_driver = {
  1695. DRV_CRLF,
  1696. imap_parse_store,
  1697. imap_cleanup,
  1698. imap_open_store,
  1699. imap_disown_store,
  1700. imap_own_store,
  1701. imap_cancel_store,
  1702. imap_list,
  1703. imap_prepare_paths,
  1704. imap_prepare_opts,
  1705. imap_select,
  1706. imap_fetch_msg,
  1707. imap_store_msg,
  1708. imap_find_msg,
  1709. imap_set_flags,
  1710. imap_trash_msg,
  1711. imap_close,
  1712. imap_cancel,
  1713. imap_commit,
  1714. };