drv_imap.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  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. #endif
  93. } Socket_t;
  94. typedef struct {
  95. Socket_t sock;
  96. int bytes;
  97. int offset;
  98. char buf[1024];
  99. } buffer_t;
  100. struct imap_cmd;
  101. #define max_in_progress 50 /* make this configurable? */
  102. typedef struct imap_store {
  103. store_t gen;
  104. const char *prefix;
  105. int uidnext; /* from SELECT responses */
  106. unsigned trashnc:1; /* trash folder's existence is not confirmed yet */
  107. unsigned got_namespace:1;
  108. list_t *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
  109. message_t **msgapp; /* FETCH results */
  110. unsigned caps; /* CAPABILITY results */
  111. /* command queue */
  112. int nexttag, num_in_progress, literal_pending;
  113. struct imap_cmd *in_progress, **in_progress_append;
  114. #ifdef HAVE_LIBSSL
  115. SSL_CTX *SSLContext;
  116. #endif
  117. buffer_t buf; /* this is BIG, so put it last */
  118. } imap_store_t;
  119. struct imap_cmd {
  120. struct imap_cmd *next;
  121. char *cmd;
  122. int tag;
  123. struct {
  124. int (*cont)( imap_store_t *ctx, struct imap_cmd *cmd, const char *prompt );
  125. void (*done)( imap_store_t *ctx, struct imap_cmd *cmd, int response );
  126. void *aux;
  127. char *data;
  128. int data_len;
  129. int uid; /* to identify fetch responses */
  130. unsigned
  131. to_trash:1, /* we are storing to trash, not current. */
  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->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->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->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->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 bufl, litplus;
  443. const char *buffmt;
  444. char buf[1024];
  445. while (ctx->literal_pending)
  446. get_cmd_result( ctx, 0 );
  447. if (!cmd)
  448. cmd = new_imap_cmd();
  449. cmd->tag = ++ctx->nexttag;
  450. if (fmt)
  451. nfvasprintf( &cmd->cmd, fmt, ap );
  452. if (!cmd->param.data) {
  453. buffmt = "%d %s\r\n";
  454. litplus = 0;
  455. } else if ((cmd->param.to_trash && ctx->trashnc) || !CAP(LITERALPLUS)) {
  456. buffmt = "%d %s{%d}\r\n";
  457. litplus = 0;
  458. } else {
  459. buffmt = "%d %s{%d+}\r\n";
  460. litplus = 1;
  461. }
  462. bufl = nfsnprintf( buf, sizeof(buf), buffmt,
  463. cmd->tag, cmd->cmd, cmd->param.data_len );
  464. if (DFlags & VERBOSE) {
  465. if (ctx->num_in_progress)
  466. printf( "(%d in progress) ", ctx->num_in_progress );
  467. if (memcmp( cmd->cmd, "LOGIN", 5 ))
  468. printf( ">>> %s", buf );
  469. else
  470. printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag );
  471. }
  472. if (socket_write( &ctx->buf.sock, buf, bufl ) != bufl)
  473. goto bail;
  474. if (litplus) {
  475. if (socket_write( &ctx->buf.sock, cmd->param.data, cmd->param.data_len ) != cmd->param.data_len ||
  476. socket_write( &ctx->buf.sock, "\r\n", 2 ) != 2)
  477. goto bail;
  478. free( cmd->param.data );
  479. cmd->param.data = 0;
  480. } else if (cmd->param.cont || cmd->param.data) {
  481. ctx->literal_pending = 1;
  482. }
  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. bail:
  489. free( cmd->param.data );
  490. free( cmd->cmd );
  491. free( cmd );
  492. return NULL;
  493. }
  494. static struct imap_cmd *
  495. submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd, const char *fmt, ... )
  496. {
  497. struct imap_cmd *ret;
  498. va_list ap;
  499. va_start( ap, fmt );
  500. ret = v_submit_imap_cmd( ctx, cmd, fmt, ap );
  501. va_end( ap );
  502. return ret;
  503. }
  504. static int
  505. imap_exec( imap_store_t *ctx, struct imap_cmd *cmdp, const char *fmt, ... )
  506. {
  507. va_list ap;
  508. va_start( ap, fmt );
  509. cmdp = v_submit_imap_cmd( ctx, cmdp, fmt, ap );
  510. va_end( ap );
  511. if (!cmdp)
  512. return RESP_BAD;
  513. return get_cmd_result( ctx, cmdp );
  514. }
  515. static int
  516. imap_exec_b( imap_store_t *ctx, struct imap_cmd *cmdp, const char *fmt, ... )
  517. {
  518. va_list ap;
  519. va_start( ap, fmt );
  520. cmdp = v_submit_imap_cmd( ctx, cmdp, fmt, ap );
  521. va_end( ap );
  522. if (!cmdp)
  523. return DRV_STORE_BAD;
  524. switch (get_cmd_result( ctx, cmdp )) {
  525. case RESP_BAD: return DRV_STORE_BAD;
  526. case RESP_NO: return DRV_BOX_BAD;
  527. default: return DRV_OK;
  528. }
  529. }
  530. static int
  531. imap_exec_m( imap_store_t *ctx, struct imap_cmd *cmdp, const char *fmt, ... )
  532. {
  533. va_list ap;
  534. va_start( ap, fmt );
  535. cmdp = v_submit_imap_cmd( ctx, cmdp, fmt, ap );
  536. va_end( ap );
  537. if (!cmdp)
  538. return DRV_STORE_BAD;
  539. switch (get_cmd_result( ctx, cmdp )) {
  540. case RESP_BAD: return DRV_STORE_BAD;
  541. case RESP_NO: return DRV_MSG_BAD;
  542. default: return DRV_OK;
  543. }
  544. }
  545. /*
  546. static void
  547. drain_imap_replies( imap_store_t *ctx )
  548. {
  549. while (ctx->num_in_progress)
  550. get_cmd_result( ctx, 0 );
  551. }
  552. */
  553. static void
  554. process_imap_replies( imap_store_t *ctx )
  555. {
  556. while (ctx->num_in_progress > max_in_progress ||
  557. socket_pending( &ctx->buf.sock ))
  558. get_cmd_result( ctx, 0 );
  559. }
  560. static int
  561. is_atom( list_t *list )
  562. {
  563. return list && list->val && list->val != NIL && list->val != LIST;
  564. }
  565. static int
  566. is_list( list_t *list )
  567. {
  568. return list && list->val == LIST;
  569. }
  570. static void
  571. free_list( list_t *list )
  572. {
  573. list_t *tmp;
  574. for (; list; list = tmp) {
  575. tmp = list->next;
  576. if (is_list( list ))
  577. free_list( list->child );
  578. else if (is_atom( list ))
  579. free( list->val );
  580. free( list );
  581. }
  582. }
  583. static int
  584. parse_imap_list_l( imap_store_t *ctx, char **sp, list_t **curp, int level )
  585. {
  586. list_t *cur;
  587. char *s = *sp, *p;
  588. int n, bytes;
  589. for (;;) {
  590. while (isspace( (unsigned char)*s ))
  591. s++;
  592. if (level && *s == ')') {
  593. s++;
  594. break;
  595. }
  596. *curp = cur = nfmalloc( sizeof(*cur) );
  597. curp = &cur->next;
  598. cur->val = 0; /* for clean bail */
  599. if (*s == '(') {
  600. /* sublist */
  601. s++;
  602. cur->val = LIST;
  603. if (parse_imap_list_l( ctx, &s, &cur->child, level + 1 ))
  604. goto bail;
  605. } else if (ctx && *s == '{') {
  606. /* literal */
  607. bytes = cur->len = strtol( s + 1, &s, 10 );
  608. if (*s != '}')
  609. goto bail;
  610. s = cur->val = nfmalloc( cur->len );
  611. /* dump whats left over in the input buffer */
  612. n = ctx->buf.bytes - ctx->buf.offset;
  613. if (n > bytes)
  614. /* the entire message fit in the buffer */
  615. n = bytes;
  616. memcpy( s, ctx->buf.buf + ctx->buf.offset, n );
  617. s += n;
  618. bytes -= n;
  619. /* mark that we used part of the buffer */
  620. ctx->buf.offset += n;
  621. /* now read the rest of the message */
  622. while (bytes > 0) {
  623. if ((n = socket_read( &ctx->buf.sock, s, bytes )) <= 0)
  624. goto bail;
  625. s += n;
  626. bytes -= n;
  627. }
  628. if (DFlags & XVERBOSE) {
  629. puts( "=========" );
  630. fwrite( cur->val, cur->len, 1, stdout );
  631. puts( "=========" );
  632. }
  633. if (buffer_gets( &ctx->buf, &s ))
  634. goto bail;
  635. } else if (*s == '"') {
  636. /* quoted string */
  637. s++;
  638. p = s;
  639. for (; *s != '"'; s++)
  640. if (!*s)
  641. goto bail;
  642. cur->len = s - p;
  643. s++;
  644. cur->val = nfmalloc( cur->len + 1 );
  645. memcpy( cur->val, p, cur->len );
  646. cur->val[cur->len] = 0;
  647. } else {
  648. /* atom */
  649. p = s;
  650. for (; *s && !isspace( (unsigned char)*s ); s++)
  651. if (level && *s == ')')
  652. break;
  653. cur->len = s - p;
  654. if (cur->len == 3 && !memcmp ("NIL", p, 3))
  655. cur->val = NIL;
  656. else {
  657. cur->val = nfmalloc( cur->len + 1 );
  658. memcpy( cur->val, p, cur->len );
  659. cur->val[cur->len] = 0;
  660. }
  661. }
  662. if (!level)
  663. break;
  664. if (!*s)
  665. goto bail;
  666. }
  667. *sp = s;
  668. *curp = 0;
  669. return 0;
  670. bail:
  671. *curp = 0;
  672. return -1;
  673. }
  674. static list_t *
  675. parse_imap_list( imap_store_t *ctx, char **sp )
  676. {
  677. list_t *head;
  678. if (!parse_imap_list_l( ctx, sp, &head, 0 ))
  679. return head;
  680. free_list( head );
  681. return NULL;
  682. }
  683. static list_t *
  684. parse_list( char **sp )
  685. {
  686. return parse_imap_list( 0, sp );
  687. }
  688. static int
  689. parse_fetch( imap_store_t *ctx, char *cmd ) /* move this down */
  690. {
  691. list_t *tmp, *list, *flags;
  692. char *body = 0;
  693. imap_message_t *cur;
  694. msg_data_t *msgdata;
  695. struct imap_cmd *cmdp;
  696. int uid = 0, mask = 0, status = 0, size = 0;
  697. unsigned i;
  698. list = parse_imap_list( ctx, &cmd );
  699. if (!is_list( list )) {
  700. error( "IMAP error: bogus FETCH response\n" );
  701. free_list( list );
  702. return -1;
  703. }
  704. for (tmp = list->child; tmp; tmp = tmp->next) {
  705. if (is_atom( tmp )) {
  706. if (!strcmp( "UID", tmp->val )) {
  707. tmp = tmp->next;
  708. if (is_atom( tmp ))
  709. uid = atoi( tmp->val );
  710. else
  711. error( "IMAP error: unable to parse UID\n" );
  712. } else if (!strcmp( "FLAGS", tmp->val )) {
  713. tmp = tmp->next;
  714. if (is_list( tmp )) {
  715. for (flags = tmp->child; flags; flags = flags->next) {
  716. if (is_atom( flags )) {
  717. if (flags->val[0] == '\\') { /* ignore user-defined flags for now */
  718. if (!strcmp( "Recent", flags->val + 1)) {
  719. status |= M_RECENT;
  720. goto flagok;
  721. }
  722. for (i = 0; i < as(Flags); i++)
  723. if (!strcmp( Flags[i], flags->val + 1 )) {
  724. mask |= 1 << i;
  725. goto flagok;
  726. }
  727. if (flags->val[1] == 'X' && flags->val[2] == '-')
  728. goto flagok; /* ignore system flag extensions */
  729. error( "IMAP warning: unknown system flag %s\n", flags->val );
  730. }
  731. flagok: ;
  732. } else
  733. error( "IMAP error: unable to parse FLAGS list\n" );
  734. }
  735. status |= M_FLAGS;
  736. } else
  737. error( "IMAP error: unable to parse FLAGS\n" );
  738. } else if (!strcmp( "RFC822.SIZE", tmp->val )) {
  739. tmp = tmp->next;
  740. if (is_atom( tmp ))
  741. size = atoi( tmp->val );
  742. else
  743. error( "IMAP error: unable to parse RFC822.SIZE\n" );
  744. } else if (!strcmp( "BODY[]", tmp->val )) {
  745. tmp = tmp->next;
  746. if (is_atom( tmp )) {
  747. body = tmp->val;
  748. tmp->val = 0; /* don't free together with list */
  749. size = tmp->len;
  750. } else
  751. error( "IMAP error: unable to parse BODY[]\n" );
  752. }
  753. }
  754. }
  755. if (body) {
  756. for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
  757. if (cmdp->param.uid == uid)
  758. goto gotuid;
  759. error( "IMAP error: unexpected FETCH response (UID %d)\n", uid );
  760. free_list( list );
  761. return -1;
  762. gotuid:
  763. msgdata = (msg_data_t *)cmdp->param.aux;
  764. msgdata->data = body;
  765. msgdata->len = size;
  766. if (status & M_FLAGS)
  767. msgdata->flags = mask;
  768. } else if (uid) { /* ignore async flag updates for now */
  769. /* XXX this will need sorting for out-of-order (multiple queries) */
  770. cur = nfcalloc( sizeof(*cur) );
  771. *ctx->msgapp = &cur->gen;
  772. ctx->msgapp = &cur->gen.next;
  773. cur->gen.next = 0;
  774. cur->gen.uid = uid;
  775. cur->gen.flags = mask;
  776. cur->gen.status = status;
  777. cur->gen.size = size;
  778. }
  779. free_list( list );
  780. return 0;
  781. }
  782. static void
  783. parse_capability( imap_store_t *ctx, char *cmd )
  784. {
  785. char *arg;
  786. unsigned i;
  787. ctx->caps = 0x80000000;
  788. while ((arg = next_arg( &cmd )))
  789. for (i = 0; i < as(cap_list); i++)
  790. if (!strcmp( cap_list[i], arg ))
  791. ctx->caps |= 1 << i;
  792. }
  793. static int
  794. parse_response_code( imap_store_t *ctx, struct imap_cmd *cmd, char *s )
  795. {
  796. char *arg, *earg, *p;
  797. if (*s != '[')
  798. return RESP_OK; /* no response code */
  799. s++;
  800. if (!(p = strchr( s, ']' ))) {
  801. error( "IMAP error: malformed response code\n" );
  802. return RESP_BAD;
  803. }
  804. *p++ = 0;
  805. arg = next_arg( &s );
  806. if (!strcmp( "UIDVALIDITY", arg )) {
  807. if (!(arg = next_arg( &s )) ||
  808. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg))
  809. {
  810. error( "IMAP error: malformed UIDVALIDITY status\n" );
  811. return RESP_BAD;
  812. }
  813. } else if (!strcmp( "UIDNEXT", arg )) {
  814. if (!(arg = next_arg( &s )) || (ctx->uidnext = strtol( arg, &p, 10 ), *p)) {
  815. error( "IMAP error: malformed NEXTUID status\n" );
  816. return RESP_BAD;
  817. }
  818. } else if (!strcmp( "CAPABILITY", arg )) {
  819. parse_capability( ctx, s );
  820. } else if (!strcmp( "ALERT", arg )) {
  821. /* RFC2060 says that these messages MUST be displayed
  822. * to the user
  823. */
  824. for (; isspace( (unsigned char)*p ); p++);
  825. error( "*** IMAP ALERT *** %s\n", p );
  826. } else if (cmd && cmd->param.aux && !strcmp( "APPENDUID", arg )) {
  827. if (!(arg = next_arg( &s )) ||
  828. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg) ||
  829. !(arg = next_arg( &s )) || !(*(int *)cmd->param.aux = atoi( arg )))
  830. {
  831. error( "IMAP error: malformed APPENDUID status\n" );
  832. return RESP_BAD;
  833. }
  834. }
  835. return RESP_OK;
  836. }
  837. static void
  838. parse_search( imap_store_t *ctx, char *cmd )
  839. {
  840. char *arg;
  841. struct imap_cmd *cmdp;
  842. int uid;
  843. if (!(arg = next_arg( &cmd )))
  844. uid = -1;
  845. else if (!(uid = atoi( arg ))) {
  846. error( "IMAP error: malformed SEARCH response\n" );
  847. return;
  848. } else if (next_arg( &cmd )) {
  849. warn( "IMAP warning: SEARCH returns multiple matches\n" );
  850. uid = -1; /* to avoid havoc */
  851. }
  852. /* Find the first command that expects a UID - this is guaranteed
  853. * to come in-order, as there are no other means to identify which
  854. * SEARCH response belongs to which request.
  855. */
  856. for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
  857. if (cmdp->param.uid == -1) {
  858. *(int *)cmdp->param.aux = uid;
  859. return;
  860. }
  861. error( "IMAP error: unexpected SEARCH response (UID %u)\n", uid );
  862. }
  863. static void
  864. parse_list_rsp( imap_store_t *ctx, char *cmd )
  865. {
  866. char *arg;
  867. list_t *list, *lp;
  868. int l;
  869. list = parse_list( &cmd );
  870. if (list->val == LIST)
  871. for (lp = list->child; lp; lp = lp->next)
  872. if (is_atom( lp ) && !strcasecmp( lp->val, "\\NoSelect" )) {
  873. free_list( list );
  874. return;
  875. }
  876. free_list( list );
  877. (void) next_arg( &cmd ); /* skip delimiter */
  878. arg = next_arg( &cmd );
  879. l = strlen( ctx->gen.conf->path );
  880. if (memcmp( arg, ctx->gen.conf->path, l ))
  881. return;
  882. arg += l;
  883. if (!memcmp( arg + strlen( arg ) - 5, ".lock", 5 )) /* workaround broken servers */
  884. return;
  885. add_string_list( &ctx->gen.boxes, arg );
  886. }
  887. static int
  888. get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
  889. {
  890. struct imap_cmd *cmdp, **pcmdp;
  891. char *cmd, *arg, *arg1, *p;
  892. int n, resp, resp2, tag;
  893. for (;;) {
  894. if (buffer_gets( &ctx->buf, &cmd ))
  895. return RESP_BAD;
  896. arg = next_arg( &cmd );
  897. if (*arg == '*') {
  898. arg = next_arg( &cmd );
  899. if (!arg) {
  900. error( "IMAP error: unable to parse untagged response\n" );
  901. return RESP_BAD;
  902. }
  903. if (!strcmp( "NAMESPACE", arg )) {
  904. ctx->ns_personal = parse_list( &cmd );
  905. ctx->ns_other = parse_list( &cmd );
  906. ctx->ns_shared = parse_list( &cmd );
  907. } else if (!strcmp( "OK", arg ) || !strcmp( "BAD", arg ) ||
  908. !strcmp( "NO", arg ) || !strcmp( "BYE", arg )) {
  909. parse_response_code( ctx, 0, cmd );
  910. } else if (!strcmp( "CAPABILITY", arg ))
  911. parse_capability( ctx, cmd );
  912. else if (!strcmp( "LIST", arg ))
  913. parse_list_rsp( ctx, cmd );
  914. else if (!strcmp( "SEARCH", arg ))
  915. parse_search( ctx, cmd );
  916. else if ((arg1 = next_arg( &cmd ))) {
  917. if (!strcmp( "EXISTS", arg1 ))
  918. ctx->gen.count = atoi( arg );
  919. else if (!strcmp( "RECENT", arg1 ))
  920. ctx->gen.recent = atoi( arg );
  921. else if(!strcmp ( "FETCH", arg1 )) {
  922. if (parse_fetch( ctx, cmd ))
  923. return RESP_BAD;
  924. }
  925. } else {
  926. error( "IMAP error: unable to parse untagged response\n" );
  927. return RESP_BAD;
  928. }
  929. } else if (!ctx->in_progress) {
  930. error( "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "" );
  931. return RESP_BAD;
  932. } else if (*arg == '+') {
  933. /* This can happen only with the last command underway, as
  934. it enforces a round-trip. */
  935. cmdp = ctx->in_progress;
  936. if (cmdp->param.data) {
  937. if (cmdp->param.to_trash)
  938. ctx->trashnc = 0; /* Can't get NO [TRYCREATE] any more. */
  939. n = socket_write( &ctx->buf.sock, cmdp->param.data, cmdp->param.data_len );
  940. free( cmdp->param.data );
  941. cmdp->param.data = 0;
  942. if (n != (int)cmdp->param.data_len)
  943. return RESP_BAD;
  944. } else if (cmdp->param.cont) {
  945. if (cmdp->param.cont( ctx, cmdp, cmd ))
  946. return RESP_BAD;
  947. } else {
  948. error( "IMAP error: unexpected command continuation request\n" );
  949. return RESP_BAD;
  950. }
  951. if (socket_write( &ctx->buf.sock, "\r\n", 2 ) != 2)
  952. return RESP_BAD;
  953. if (!cmdp->param.cont)
  954. ctx->literal_pending = 0;
  955. if (!tcmd)
  956. return RESP_OK;
  957. } else {
  958. tag = atoi( arg );
  959. for (pcmdp = &ctx->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
  960. if (cmdp->tag == tag)
  961. goto gottag;
  962. error( "IMAP error: unexpected tag %s\n", arg );
  963. return RESP_BAD;
  964. gottag:
  965. if (!(*pcmdp = cmdp->next))
  966. ctx->in_progress_append = pcmdp;
  967. ctx->num_in_progress--;
  968. if (cmdp->param.cont || cmdp->param.data)
  969. ctx->literal_pending = 0;
  970. arg = next_arg( &cmd );
  971. if (!strcmp( "OK", arg )) {
  972. if (cmdp->param.to_trash)
  973. ctx->trashnc = 0; /* Can't get NO [TRYCREATE] any more. */
  974. resp = RESP_OK;
  975. } else {
  976. if (!strcmp( "NO", arg )) {
  977. if (cmdp->param.create && cmd && (cmdp->param.trycreate || !memcmp( cmd, "[TRYCREATE]", 11 ))) { /* SELECT, APPEND or UID COPY */
  978. p = strchr( cmdp->cmd, '"' );
  979. if (!submit_imap_cmd( ctx, 0, "CREATE %.*s", strchr( p + 1, '"' ) - p + 1, p )) {
  980. resp = RESP_BAD;
  981. goto normal;
  982. }
  983. /* not waiting here violates the spec, but a server that does not
  984. grok this nonetheless violates it too. */
  985. cmdp->param.create = 0;
  986. if (!submit_imap_cmd( ctx, cmdp, 0 )) {
  987. resp = RESP_BAD;
  988. goto abnormal;
  989. }
  990. if (!tcmd)
  991. return 0; /* ignored */
  992. continue;
  993. }
  994. resp = RESP_NO;
  995. } else /*if (!strcmp( "BAD", arg ))*/
  996. resp = RESP_BAD;
  997. error( "IMAP command '%s' returned an error: %s %s\n",
  998. memcmp( cmdp->cmd, "LOGIN", 5 ) ? cmdp->cmd : "LOGIN <user> <pass>",
  999. arg, cmd ? cmd : "" );
  1000. }
  1001. if ((resp2 = parse_response_code( ctx, cmdp, cmd )) > resp)
  1002. resp = resp2;
  1003. normal:
  1004. if (cmdp->param.done)
  1005. cmdp->param.done( ctx, cmdp, resp );
  1006. free( cmdp->param.data );
  1007. free( cmdp->cmd );
  1008. free( cmdp );
  1009. abnormal:
  1010. if (!tcmd || tcmd == cmdp)
  1011. return resp;
  1012. }
  1013. }
  1014. /* not reached */
  1015. }
  1016. static void
  1017. imap_cancel_store( store_t *gctx )
  1018. {
  1019. imap_store_t *ctx = (imap_store_t *)gctx;
  1020. free_generic_messages( gctx->msgs );
  1021. free_string_list( ctx->gen.boxes );
  1022. if (ctx->buf.sock.fd >= 0)
  1023. close( ctx->buf.sock.fd );
  1024. #ifdef HAVE_LIBSSL
  1025. if (ctx->buf.sock.ssl)
  1026. SSL_free( ctx->buf.sock.ssl );
  1027. if (ctx->SSLContext)
  1028. SSL_CTX_free( ctx->SSLContext );
  1029. #endif
  1030. free_list( ctx->ns_personal );
  1031. free_list( ctx->ns_other );
  1032. free_list( ctx->ns_shared );
  1033. free( ctx );
  1034. }
  1035. static store_t *unowned;
  1036. static void
  1037. imap_disown_store( store_t *gctx )
  1038. {
  1039. free_generic_messages( gctx->msgs );
  1040. gctx->msgs = 0;
  1041. gctx->next = unowned;
  1042. unowned = gctx;
  1043. }
  1044. static store_t *
  1045. imap_own_store( store_conf_t *conf )
  1046. {
  1047. store_t *store, **storep;
  1048. for (storep = &unowned; (store = *storep); storep = &store->next)
  1049. if (store->conf == conf) {
  1050. *storep = store->next;
  1051. return store;
  1052. }
  1053. return 0;
  1054. }
  1055. static void
  1056. imap_cleanup( void )
  1057. {
  1058. store_t *ctx, *nctx;
  1059. for (ctx = unowned; ctx; ctx = nctx) {
  1060. nctx = ctx->next;
  1061. imap_exec( (imap_store_t *)ctx, 0, "LOGOUT" );
  1062. imap_cancel_store( ctx );
  1063. }
  1064. }
  1065. #ifdef HAVE_LIBSSL
  1066. static int
  1067. start_tls( imap_store_t *ctx )
  1068. {
  1069. int ret;
  1070. static int ssl_inited;
  1071. if (!ssl_inited) {
  1072. SSL_library_init();
  1073. SSL_load_error_strings();
  1074. ssl_inited = 1;
  1075. }
  1076. if (init_ssl_ctx( ctx ))
  1077. return 1;
  1078. ctx->buf.sock.ssl = SSL_new( ctx->SSLContext );
  1079. SSL_set_fd( ctx->buf.sock.ssl, ctx->buf.sock.fd );
  1080. if ((ret = SSL_connect( ctx->buf.sock.ssl )) <= 0) {
  1081. socket_perror( "connect", &ctx->buf.sock, ret );
  1082. return 1;
  1083. }
  1084. /* verify the server certificate */
  1085. if (verify_cert( ctx ))
  1086. return 1;
  1087. info( "Connection is now encrypted\n" );
  1088. return 0;
  1089. }
  1090. #define ENCODED_SIZE(n) (4*((n+2)/3))
  1091. static char
  1092. hexchar( unsigned int b )
  1093. {
  1094. if (b < 10)
  1095. return '0' + b;
  1096. return 'a' + (b - 10);
  1097. }
  1098. static void
  1099. cram( const char *challenge, const char *user, const char *pass, char **_final, int *_finallen )
  1100. {
  1101. unsigned char *response, *final;
  1102. unsigned hashlen;
  1103. int i, clen, rlen, blen, flen, olen;
  1104. unsigned char hash[16];
  1105. char buf[256], hex[33];
  1106. HMAC_CTX hmac;
  1107. HMAC_Init( &hmac, (unsigned char *)pass, strlen( pass ), EVP_md5() );
  1108. clen = strlen( challenge );
  1109. /* response will always be smaller than challenge because we are decoding. */
  1110. response = nfcalloc( 1 + clen );
  1111. rlen = EVP_DecodeBlock( response, (unsigned char *)challenge, clen );
  1112. HMAC_Update( &hmac, response, rlen );
  1113. free( response );
  1114. hashlen = sizeof(hash);
  1115. HMAC_Final( &hmac, hash, &hashlen );
  1116. assert( hashlen == sizeof(hash) );
  1117. hex[32] = 0;
  1118. for (i = 0; i < 16; i++) {
  1119. hex[2 * i] = hexchar( (hash[i] >> 4) & 0xf );
  1120. hex[2 * i + 1] = hexchar( hash[i] & 0xf );
  1121. }
  1122. blen = nfsnprintf( buf, sizeof(buf), "%s %s", user, hex );
  1123. flen = ENCODED_SIZE( blen );
  1124. final = nfmalloc( flen + 1 );
  1125. final[flen] = 0;
  1126. olen = EVP_EncodeBlock( (unsigned char *)final, (unsigned char *)buf, blen );
  1127. assert( olen == flen );
  1128. *_final = (char *)final;
  1129. *_finallen = flen;
  1130. }
  1131. static int
  1132. do_cram_auth( imap_store_t *ctx, struct imap_cmd *cmdp, const char *prompt )
  1133. {
  1134. imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server;
  1135. char *resp;
  1136. int n, l;
  1137. cram( prompt, srvc->user, srvc->pass, &resp, &l );
  1138. if (DFlags & VERBOSE)
  1139. printf( ">+> %s\n", resp );
  1140. n = socket_write( &ctx->buf.sock, resp, l );
  1141. free( resp );
  1142. if (n != l)
  1143. return -1;
  1144. cmdp->param.cont = 0;
  1145. return 0;
  1146. }
  1147. #endif
  1148. static void
  1149. imap_open_store( store_conf_t *conf,
  1150. void (*cb)( store_t *srv, void *aux ), void *aux )
  1151. {
  1152. imap_store_conf_t *cfg = (imap_store_conf_t *)conf;
  1153. imap_server_conf_t *srvc = cfg->server;
  1154. imap_store_t *ctx;
  1155. store_t **ctxp;
  1156. char *arg, *rsp;
  1157. struct hostent *he;
  1158. struct sockaddr_in addr;
  1159. int s, a[2], preauth;
  1160. for (ctxp = &unowned; (ctx = (imap_store_t *)*ctxp); ctxp = &ctx->gen.next)
  1161. if (((imap_store_conf_t *)ctx->gen.conf)->server == srvc) {
  1162. *ctxp = ctx->gen.next;
  1163. /* One could ping the server here, but given that the idle timeout
  1164. * is at least 30 minutes, this sounds pretty pointless. */
  1165. free_string_list( ctx->gen.boxes );
  1166. ctx->gen.boxes = 0;
  1167. ctx->gen.listed = 0;
  1168. ctx->gen.conf = conf;
  1169. goto final;
  1170. }
  1171. ctx = nfcalloc( sizeof(*ctx) );
  1172. ctx->gen.conf = conf;
  1173. ctx->buf.sock.fd = -1;
  1174. ctx->in_progress_append = &ctx->in_progress;
  1175. /* open connection to IMAP server */
  1176. if (srvc->tunnel) {
  1177. infon( "Starting tunnel '%s'... ", srvc->tunnel );
  1178. if (socketpair( PF_UNIX, SOCK_STREAM, 0, a )) {
  1179. perror( "socketpair" );
  1180. exit( 1 );
  1181. }
  1182. if (fork() == 0) {
  1183. if (dup2( a[0], 0 ) == -1 || dup2( a[0], 1 ) == -1)
  1184. _exit( 127 );
  1185. close( a[0] );
  1186. close( a[1] );
  1187. execl( "/bin/sh", "sh", "-c", srvc->tunnel, (char *)0 );
  1188. _exit( 127 );
  1189. }
  1190. close (a[0]);
  1191. ctx->buf.sock.fd = a[1];
  1192. info( "ok\n" );
  1193. } else {
  1194. memset( &addr, 0, sizeof(addr) );
  1195. addr.sin_port = srvc->port ? htons( srvc->port ) :
  1196. #ifdef HAVE_LIBSSL
  1197. srvc->use_imaps ? htons( 993 ) :
  1198. #endif
  1199. htons( 143 );
  1200. addr.sin_family = AF_INET;
  1201. infon( "Resolving %s... ", srvc->host );
  1202. he = gethostbyname( srvc->host );
  1203. if (!he) {
  1204. error( "IMAP error: Cannot resolve server '%s'\n", srvc->host );
  1205. goto bail;
  1206. }
  1207. info( "ok\n" );
  1208. addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
  1209. s = socket( PF_INET, SOCK_STREAM, 0 );
  1210. if (s < 0) {
  1211. perror( "socket" );
  1212. exit( 1 );
  1213. }
  1214. infon( "Connecting to %s:%hu... ", inet_ntoa( addr.sin_addr ), ntohs( addr.sin_port ) );
  1215. if (connect( s, (struct sockaddr *)&addr, sizeof(addr) )) {
  1216. close( s );
  1217. perror( "connect" );
  1218. goto bail;
  1219. }
  1220. info( "ok\n" );
  1221. ctx->buf.sock.fd = s;
  1222. }
  1223. #ifdef HAVE_LIBSSL
  1224. if (srvc->use_imaps) {
  1225. if (start_tls( ctx ))
  1226. goto ssl_bail;
  1227. }
  1228. #endif
  1229. /* read the greeting string */
  1230. if (buffer_gets( &ctx->buf, &rsp ))
  1231. goto bail;
  1232. arg = next_arg( &rsp );
  1233. if (!arg || *arg != '*' || (arg = next_arg( &rsp )) == NULL) {
  1234. error( "IMAP error: invalid greeting response\n" );
  1235. goto bail;
  1236. }
  1237. preauth = 0;
  1238. if (!strcmp( "PREAUTH", arg ))
  1239. preauth = 1;
  1240. else if (strcmp( "OK", arg ) != 0) {
  1241. error( "IMAP error: unknown greeting response\n" );
  1242. goto bail;
  1243. }
  1244. parse_response_code( ctx, 0, rsp );
  1245. if (!ctx->caps && imap_exec( ctx, 0, "CAPABILITY" ) != RESP_OK)
  1246. goto bail;
  1247. if (!preauth) {
  1248. #ifdef HAVE_LIBSSL
  1249. if (!srvc->use_imaps && (srvc->use_sslv2 || srvc->use_sslv3 || srvc->use_tlsv1)) {
  1250. /* always try to select SSL support if available */
  1251. if (CAP(STARTTLS)) {
  1252. if (imap_exec( ctx, 0, "STARTTLS" ) != RESP_OK)
  1253. goto bail;
  1254. if (start_tls( ctx ))
  1255. goto ssl_bail;
  1256. if (imap_exec( ctx, 0, "CAPABILITY" ) != RESP_OK)
  1257. goto bail;
  1258. } else {
  1259. if (srvc->require_ssl) {
  1260. error( "IMAP error: SSL support not available\n" );
  1261. goto bail;
  1262. } else
  1263. warn( "IMAP warning: SSL support not available\n" );
  1264. }
  1265. }
  1266. #endif
  1267. info ("Logging in...\n");
  1268. if (!srvc->user) {
  1269. error( "Skipping account %s, no user\n", srvc->name );
  1270. goto bail;
  1271. }
  1272. if (!srvc->pass) {
  1273. char prompt[80];
  1274. sprintf( prompt, "Password (%s): ", srvc->name );
  1275. arg = getpass( prompt );
  1276. if (!arg) {
  1277. perror( "getpass" );
  1278. exit( 1 );
  1279. }
  1280. if (!*arg) {
  1281. error( "Skipping account %s, no password\n", srvc->name );
  1282. goto bail;
  1283. }
  1284. /*
  1285. * getpass() returns a pointer to a static buffer. make a copy
  1286. * for long term storage.
  1287. */
  1288. srvc->pass = nfstrdup( arg );
  1289. }
  1290. #ifdef HAVE_LIBSSL
  1291. if (CAP(CRAM)) {
  1292. struct imap_cmd *cmd = new_imap_cmd();
  1293. info( "Authenticating with CRAM-MD5\n" );
  1294. cmd->param.cont = do_cram_auth;
  1295. if (imap_exec( ctx, cmd, "AUTHENTICATE CRAM-MD5" ) != RESP_OK)
  1296. goto bail;
  1297. } else if (srvc->require_cram) {
  1298. error( "IMAP error: CRAM-MD5 authentication is not supported by server\n" );
  1299. goto bail;
  1300. } else
  1301. #endif
  1302. {
  1303. if (CAP(NOLOGIN)) {
  1304. error( "Skipping account %s, server forbids LOGIN\n", srvc->name );
  1305. goto bail;
  1306. }
  1307. #ifdef HAVE_LIBSSL
  1308. if (!ctx->buf.sock.ssl)
  1309. #endif
  1310. warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
  1311. if (imap_exec( ctx, 0, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass ) != RESP_OK) {
  1312. error( "IMAP error: LOGIN failed\n" );
  1313. goto bail;
  1314. }
  1315. }
  1316. } /* !preauth */
  1317. final:
  1318. ctx->prefix = "";
  1319. if (*conf->path)
  1320. ctx->prefix = conf->path;
  1321. else if (cfg->use_namespace && CAP(NAMESPACE)) {
  1322. /* get NAMESPACE info */
  1323. if (!ctx->got_namespace) {
  1324. if (imap_exec( ctx, 0, "NAMESPACE" ) != RESP_OK) {
  1325. cb( 0, aux );
  1326. return;
  1327. }
  1328. ctx->got_namespace = 1;
  1329. }
  1330. /* XXX for now assume personal namespace */
  1331. if (is_list( ctx->ns_personal ) &&
  1332. is_list( ctx->ns_personal->child ) &&
  1333. is_atom( ctx->ns_personal->child->child ))
  1334. ctx->prefix = ctx->ns_personal->child->child->val;
  1335. }
  1336. ctx->trashnc = 1;
  1337. cb( &ctx->gen, aux );
  1338. return;
  1339. #ifdef HAVE_LIBSSL
  1340. ssl_bail:
  1341. /* This avoids that we try to send LOGOUT to an unusable socket. */
  1342. close( ctx->buf.sock.fd );
  1343. ctx->buf.sock.fd = -1;
  1344. #endif
  1345. bail:
  1346. imap_cancel_store( &ctx->gen );
  1347. cb( 0, aux );
  1348. }
  1349. static void
  1350. imap_prepare_paths( store_t *gctx )
  1351. {
  1352. free_generic_messages( gctx->msgs );
  1353. gctx->msgs = 0;
  1354. }
  1355. static void
  1356. imap_prepare_opts( store_t *gctx, int opts )
  1357. {
  1358. gctx->opts = opts;
  1359. }
  1360. static int
  1361. imap_select( store_t *gctx, int minuid, int maxuid, int *excs, int nexcs,
  1362. int (*cb)( int sts, void *aux ), void *aux )
  1363. {
  1364. imap_store_t *ctx = (imap_store_t *)gctx;
  1365. struct imap_cmd *cmd = new_imap_cmd();
  1366. const char *prefix;
  1367. int ret, i, j, bl;
  1368. char buf[1000];
  1369. if (!strcmp( gctx->name, "INBOX" )) {
  1370. prefix = "";
  1371. } else {
  1372. prefix = ctx->prefix;
  1373. }
  1374. ctx->uidnext = -1;
  1375. cmd->param.create = (gctx->opts & OPEN_CREATE) != 0;
  1376. cmd->param.trycreate = 1;
  1377. if ((ret = imap_exec_b( ctx, cmd, "SELECT \"%s%s\"", prefix, gctx->name )) != DRV_OK)
  1378. goto bail;
  1379. if (gctx->count) {
  1380. ctx->msgapp = &gctx->msgs;
  1381. sort_ints( excs, nexcs );
  1382. for (i = 0; i < nexcs; ) {
  1383. for (bl = 0; i < nexcs && bl < 960; i++) {
  1384. if (bl)
  1385. buf[bl++] = ',';
  1386. bl += sprintf( buf + bl, "%d", excs[i] );
  1387. j = i;
  1388. for (; i + 1 < nexcs && excs[i + 1] == excs[i] + 1; i++);
  1389. if (i != j)
  1390. bl += sprintf( buf + bl, ":%d", excs[i] );
  1391. }
  1392. if ((ret = imap_exec_b( ctx, 0, "UID FETCH %s (UID%s%s)", buf,
  1393. (gctx->opts & OPEN_FLAGS) ? " FLAGS" : "",
  1394. (gctx->opts & OPEN_SIZE) ? " RFC822.SIZE" : "" )) != DRV_OK)
  1395. goto bail;
  1396. }
  1397. if (maxuid == INT_MAX)
  1398. maxuid = ctx->uidnext >= 0 ? ctx->uidnext - 1 : 1000000000;
  1399. if (maxuid >= minuid &&
  1400. (ret = imap_exec_b( ctx, 0, "UID FETCH %d:%d (UID%s%s)", minuid, maxuid,
  1401. (gctx->opts & OPEN_FLAGS) ? " FLAGS" : "",
  1402. (gctx->opts & OPEN_SIZE) ? " RFC822.SIZE" : "" )) != DRV_OK)
  1403. goto bail;
  1404. }
  1405. ret = DRV_OK;
  1406. bail:
  1407. free( excs );
  1408. return cb( ret, aux );
  1409. }
  1410. static int
  1411. imap_fetch_msg( store_t *ctx, message_t *msg, msg_data_t *data,
  1412. int (*cb)( int sts, void *aux ), void *aux )
  1413. {
  1414. struct imap_cmd *cmd = new_imap_cmd();
  1415. cmd->param.uid = msg->uid;
  1416. cmd->param.aux = data;
  1417. return cb( imap_exec_m( (imap_store_t *)ctx, cmd, "UID FETCH %d (%sBODY.PEEK[])",
  1418. msg->uid, (msg->status & M_FLAGS) ? "" : "FLAGS " ), aux );
  1419. }
  1420. static int
  1421. imap_make_flags( int flags, char *buf )
  1422. {
  1423. const char *s;
  1424. unsigned i, d;
  1425. for (i = d = 0; i < as(Flags); i++)
  1426. if (flags & (1 << i)) {
  1427. buf[d++] = ' ';
  1428. buf[d++] = '\\';
  1429. for (s = Flags[i]; *s; s++)
  1430. buf[d++] = *s;
  1431. }
  1432. buf[0] = '(';
  1433. buf[d++] = ')';
  1434. return d;
  1435. }
  1436. static int
  1437. imap_flags_helper( imap_store_t *ctx, int uid, char what, int flags)
  1438. {
  1439. char buf[256];
  1440. buf[imap_make_flags( flags, buf )] = 0;
  1441. if (!submit_imap_cmd( ctx, 0, "UID STORE %d %cFLAGS.SILENT %s", uid, what, buf ))
  1442. return DRV_STORE_BAD;
  1443. process_imap_replies( ctx );
  1444. return DRV_OK;
  1445. }
  1446. static int
  1447. imap_set_flags( store_t *gctx, message_t *msg, int uid, int add, int del,
  1448. int (*cb)( int sts, void *aux ), void *aux )
  1449. {
  1450. imap_store_t *ctx = (imap_store_t *)gctx;
  1451. int ret;
  1452. if (msg) {
  1453. uid = msg->uid;
  1454. add &= ~msg->flags;
  1455. del &= msg->flags;
  1456. msg->flags |= add;
  1457. msg->flags &= ~del;
  1458. }
  1459. if ((!add || (ret = imap_flags_helper( ctx, uid, '+', add )) == DRV_OK) &&
  1460. (!del || (ret = imap_flags_helper( ctx, uid, '-', del )) == DRV_OK))
  1461. ret = DRV_OK;
  1462. return cb( ret, aux );
  1463. }
  1464. static int
  1465. imap_close( store_t *ctx,
  1466. int (*cb)( int sts, void *aux ), void *aux )
  1467. {
  1468. return cb( imap_exec_b( (imap_store_t *)ctx, 0, "CLOSE" ), aux );
  1469. }
  1470. static int
  1471. imap_trash_msg( store_t *gctx, message_t *msg,
  1472. int (*cb)( int sts, void *aux ), void *aux )
  1473. {
  1474. imap_store_t *ctx = (imap_store_t *)gctx;
  1475. struct imap_cmd *cmd = new_imap_cmd();
  1476. cmd->param.create = 1;
  1477. cmd->param.to_trash = 1;
  1478. return cb( imap_exec_m( ctx, cmd, "UID COPY %d \"%s%s\"",
  1479. msg->uid, ctx->prefix, gctx->conf->trash ), aux );
  1480. }
  1481. static int
  1482. imap_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1483. int (*cb)( int sts, int uid, void *aux ), void *aux )
  1484. {
  1485. imap_store_t *ctx = (imap_store_t *)gctx;
  1486. struct imap_cmd *cmd = new_imap_cmd();
  1487. const char *prefix, *box;
  1488. int ret, d, uid;
  1489. char flagstr[128];
  1490. d = 0;
  1491. if (data->flags) {
  1492. d = imap_make_flags( data->flags, flagstr );
  1493. flagstr[d++] = ' ';
  1494. }
  1495. flagstr[d] = 0;
  1496. cmd->param.data_len = data->len;
  1497. cmd->param.data = data->data;
  1498. cmd->param.aux = &uid;
  1499. uid = -2;
  1500. if (to_trash) {
  1501. box = gctx->conf->trash;
  1502. prefix = ctx->prefix;
  1503. cmd->param.create = 1;
  1504. cmd->param.to_trash = 1;
  1505. } else {
  1506. box = gctx->name;
  1507. prefix = !strcmp( box, "INBOX" ) ? "" : ctx->prefix;
  1508. }
  1509. ret = imap_exec_m( ctx, cmd, "APPEND \"%s%s\" %s", prefix, box, flagstr );
  1510. if (ret != DRV_OK)
  1511. return cb( ret, -1, aux );
  1512. return cb( DRV_OK, uid, aux );
  1513. }
  1514. static int
  1515. imap_find_msg( store_t *gctx, const char *tuid,
  1516. int (*cb)( int sts, int uid, void *aux ), void *aux )
  1517. {
  1518. imap_store_t *ctx = (imap_store_t *)gctx;
  1519. struct imap_cmd *cmd = new_imap_cmd();
  1520. int ret, uid;
  1521. cmd->param.uid = -1; /* we're looking for a UID */
  1522. cmd->param.aux = &uid;
  1523. uid = -1; /* in case we get no SEARCH response at all */
  1524. if ((ret = imap_exec_m( ctx, cmd, "UID SEARCH HEADER X-TUID %." stringify(TUIDL) "s", tuid )) != DRV_OK)
  1525. return cb( ret, -1, aux );
  1526. else
  1527. return cb( uid <= 0 ? DRV_MSG_BAD : DRV_OK, uid, aux );
  1528. }
  1529. static void
  1530. imap_list( store_t *gctx,
  1531. void (*cb)( int sts, void *aux ), void *aux )
  1532. {
  1533. imap_store_t *ctx = (imap_store_t *)gctx;
  1534. cb( imap_exec_b( ctx, 0, "LIST \"\" \"%s%%\"", ctx->prefix ), aux );
  1535. }
  1536. static void
  1537. imap_cancel( store_t *gctx,
  1538. void (*cb)( int sts, void *aux ), void *aux )
  1539. {
  1540. (void)gctx;
  1541. cb( DRV_OK, aux );
  1542. }
  1543. static void
  1544. imap_commit( store_t *gctx )
  1545. {
  1546. (void)gctx;
  1547. }
  1548. imap_server_conf_t *servers, **serverapp = &servers;
  1549. static int
  1550. imap_parse_store( conffile_t *cfg, store_conf_t **storep, int *err )
  1551. {
  1552. imap_store_conf_t *store;
  1553. imap_server_conf_t *server, *srv, sserver;
  1554. int acc_opt = 0;
  1555. if (!strcasecmp( "IMAPAccount", cfg->cmd )) {
  1556. server = nfcalloc( sizeof(*server) );
  1557. server->name = nfstrdup( cfg->val );
  1558. *serverapp = server;
  1559. serverapp = &server->next;
  1560. store = 0;
  1561. *storep = 0;
  1562. } else if (!strcasecmp( "IMAPStore", cfg->cmd )) {
  1563. store = nfcalloc( sizeof(*store) );
  1564. store->gen.driver = &imap_driver;
  1565. store->gen.name = nfstrdup( cfg->val );
  1566. store->use_namespace = 1;
  1567. *storep = &store->gen;
  1568. memset( &sserver, 0, sizeof(sserver) );
  1569. server = &sserver;
  1570. } else
  1571. return 0;
  1572. #ifdef HAVE_LIBSSL
  1573. /* this will probably annoy people, but its the best default just in
  1574. * case people forget to turn it on
  1575. */
  1576. server->require_ssl = 1;
  1577. server->use_tlsv1 = 1;
  1578. #endif
  1579. while (getcline( cfg ) && cfg->cmd) {
  1580. if (!strcasecmp( "Host", cfg->cmd )) {
  1581. /* The imap[s]: syntax is just a backwards compat hack. */
  1582. #ifdef HAVE_LIBSSL
  1583. if (!memcmp( "imaps:", cfg->val, 6 )) {
  1584. cfg->val += 6;
  1585. server->use_imaps = 1;
  1586. server->use_sslv2 = 1;
  1587. server->use_sslv3 = 1;
  1588. } else
  1589. #endif
  1590. {
  1591. if (!memcmp( "imap:", cfg->val, 5 ))
  1592. cfg->val += 5;
  1593. }
  1594. if (!memcmp( "//", cfg->val, 2 ))
  1595. cfg->val += 2;
  1596. server->host = nfstrdup( cfg->val );
  1597. }
  1598. else if (!strcasecmp( "User", cfg->cmd ))
  1599. server->user = nfstrdup( cfg->val );
  1600. else if (!strcasecmp( "Pass", cfg->cmd ))
  1601. server->pass = nfstrdup( cfg->val );
  1602. else if (!strcasecmp( "Port", cfg->cmd ))
  1603. server->port = parse_int( cfg );
  1604. #ifdef HAVE_LIBSSL
  1605. else if (!strcasecmp( "CertificateFile", cfg->cmd )) {
  1606. server->cert_file = expand_strdup( cfg->val );
  1607. if (access( server->cert_file, R_OK )) {
  1608. error( "%s:%d: CertificateFile '%s': %s\n",
  1609. cfg->file, cfg->line, server->cert_file, strerror( errno ) );
  1610. *err = 1;
  1611. }
  1612. } else if (!strcasecmp( "RequireSSL", cfg->cmd ))
  1613. server->require_ssl = parse_bool( cfg );
  1614. else if (!strcasecmp( "UseIMAPS", cfg->cmd ))
  1615. server->use_imaps = parse_bool( cfg );
  1616. else if (!strcasecmp( "UseSSLv2", cfg->cmd ))
  1617. server->use_sslv2 = parse_bool( cfg );
  1618. else if (!strcasecmp( "UseSSLv3", cfg->cmd ))
  1619. server->use_sslv3 = parse_bool( cfg );
  1620. else if (!strcasecmp( "UseTLSv1", cfg->cmd ))
  1621. server->use_tlsv1 = parse_bool( cfg );
  1622. else if (!strcasecmp( "RequireCRAM", cfg->cmd ))
  1623. server->require_cram = parse_bool( cfg );
  1624. #endif
  1625. else if (!strcasecmp( "Tunnel", cfg->cmd ))
  1626. server->tunnel = nfstrdup( cfg->val );
  1627. else if (store) {
  1628. if (!strcasecmp( "Account", cfg->cmd )) {
  1629. for (srv = servers; srv; srv = srv->next)
  1630. if (srv->name && !strcmp( srv->name, cfg->val ))
  1631. goto gotsrv;
  1632. error( "%s:%d: unknown IMAP account '%s'\n", cfg->file, cfg->line, cfg->val );
  1633. *err = 1;
  1634. continue;
  1635. gotsrv:
  1636. store->server = srv;
  1637. } else if (!strcasecmp( "UseNamespace", cfg->cmd ))
  1638. store->use_namespace = parse_bool( cfg );
  1639. else if (!strcasecmp( "Path", cfg->cmd ))
  1640. store->gen.path = nfstrdup( cfg->val );
  1641. else
  1642. parse_generic_store( &store->gen, cfg, err );
  1643. continue;
  1644. } else {
  1645. error( "%s:%d: unknown/misplaced keyword '%s'\n", cfg->file, cfg->line, cfg->cmd );
  1646. *err = 1;
  1647. continue;
  1648. }
  1649. acc_opt = 1;
  1650. }
  1651. if (!store || !store->server) {
  1652. if (!server->tunnel && !server->host) {
  1653. if (store)
  1654. error( "IMAP store '%s' has incomplete/missing connection details\n", store->gen.name );
  1655. else
  1656. error( "IMAP account '%s' has incomplete/missing connection details\n", server->name );
  1657. *err = 1;
  1658. return 1;
  1659. }
  1660. }
  1661. if (store) {
  1662. if (!store->server) {
  1663. store->server = nfmalloc( sizeof(sserver) );
  1664. memcpy( store->server, &sserver, sizeof(sserver) );
  1665. store->server->name = store->gen.name;
  1666. } else if (acc_opt) {
  1667. error( "IMAP store '%s' has both Account and account-specific options\n", store->gen.name );
  1668. *err = 1;
  1669. }
  1670. }
  1671. return 1;
  1672. }
  1673. struct driver imap_driver = {
  1674. DRV_CRLF,
  1675. imap_parse_store,
  1676. imap_cleanup,
  1677. imap_open_store,
  1678. imap_disown_store,
  1679. imap_own_store,
  1680. imap_cancel_store,
  1681. imap_list,
  1682. imap_prepare_paths,
  1683. imap_prepare_opts,
  1684. imap_select,
  1685. imap_fetch_msg,
  1686. imap_store_msg,
  1687. imap_find_msg,
  1688. imap_set_flags,
  1689. imap_trash_msg,
  1690. imap_close,
  1691. imap_cancel,
  1692. imap_commit,
  1693. };