drv_imap.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  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. #if 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. #if 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. #if 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. #if 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. #if HAVE_LIBSSL
  143. CRAM,
  144. STARTTLS,
  145. #endif
  146. };
  147. static const char *cap_list[] = {
  148. "LOGINDISABLED",
  149. "UIDPLUS",
  150. "LITERAL+",
  151. "NAMESPACE",
  152. #if 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. #if 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. 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. #if 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. #if 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. #if 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. #if 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 = (struct imap_cmd *)((char *)ctx->in_progress_append -
  933. offsetof(struct imap_cmd, next));
  934. if (cmdp->param.data) {
  935. n = socket_write( &ctx->buf.sock, cmdp->param.data, cmdp->param.data_len );
  936. free( cmdp->param.data );
  937. cmdp->param.data = 0;
  938. if (n != (int)cmdp->param.data_len)
  939. return RESP_BAD;
  940. } else if (cmdp->param.cont) {
  941. if (cmdp->param.cont( ctx, cmdp, cmd ))
  942. return RESP_BAD;
  943. } else {
  944. error( "IMAP error: unexpected command continuation request\n" );
  945. return RESP_BAD;
  946. }
  947. if (socket_write( &ctx->buf.sock, "\r\n", 2 ) != 2)
  948. return RESP_BAD;
  949. if (!cmdp->param.cont)
  950. ctx->literal_pending = 0;
  951. if (!tcmd)
  952. return DRV_OK;
  953. } else {
  954. tag = atoi( arg );
  955. for (pcmdp = &ctx->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
  956. if (cmdp->tag == tag)
  957. goto gottag;
  958. error( "IMAP error: unexpected tag %s\n", arg );
  959. return RESP_BAD;
  960. gottag:
  961. if (!(*pcmdp = cmdp->next))
  962. ctx->in_progress_append = pcmdp;
  963. ctx->num_in_progress--;
  964. if (cmdp->param.cont || cmdp->param.data)
  965. ctx->literal_pending = 0;
  966. arg = next_arg( &cmd );
  967. if (!strcmp( "OK", arg ))
  968. resp = DRV_OK;
  969. else {
  970. if (!strcmp( "NO", arg )) {
  971. if (cmdp->param.create && cmd && (cmdp->param.trycreate || !memcmp( cmd, "[TRYCREATE]", 11 ))) { /* SELECT, APPEND or UID COPY */
  972. p = strchr( cmdp->cmd, '"' );
  973. if (!submit_imap_cmd( ctx, 0, "CREATE %.*s", strchr( p + 1, '"' ) - p + 1, p )) {
  974. resp = RESP_BAD;
  975. goto normal;
  976. }
  977. /* not waiting here violates the spec, but a server that does not
  978. grok this nonetheless violates it too. */
  979. ncmdp = nfmalloc( sizeof(*ncmdp) );
  980. memcpy( &ncmdp->param, &cmdp->param, sizeof(cmdp->param) );
  981. ncmdp->param.create = 0;
  982. if (!submit_imap_cmd( ctx, ncmdp, "%s", cmdp->cmd )) {
  983. resp = RESP_BAD;
  984. goto normal;
  985. }
  986. free( cmdp->cmd );
  987. free( cmdp );
  988. if (!tcmd)
  989. return 0; /* ignored */
  990. if (cmdp == tcmd)
  991. tcmd = ncmdp;
  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. if (!tcmd || tcmd == cmdp)
  1010. return resp;
  1011. }
  1012. }
  1013. /* not reached */
  1014. }
  1015. static void
  1016. imap_cancel_store( store_t *gctx )
  1017. {
  1018. imap_store_t *ctx = (imap_store_t *)gctx;
  1019. free_generic_messages( gctx->msgs );
  1020. free_string_list( ctx->gen.boxes );
  1021. if (ctx->buf.sock.fd >= 0)
  1022. close( ctx->buf.sock.fd );
  1023. #ifdef HAVE_LIBSSL
  1024. if (ctx->buf.sock.ssl)
  1025. SSL_free( ctx->buf.sock.ssl );
  1026. if (ctx->SSLContext)
  1027. SSL_CTX_free( ctx->SSLContext );
  1028. #endif
  1029. free_list( ctx->ns_personal );
  1030. free_list( ctx->ns_other );
  1031. free_list( ctx->ns_shared );
  1032. free( ctx );
  1033. }
  1034. static store_t *unowned;
  1035. static void
  1036. imap_disown_store( store_t *gctx )
  1037. {
  1038. free_generic_messages( gctx->msgs );
  1039. gctx->msgs = 0;
  1040. gctx->next = unowned;
  1041. unowned = gctx;
  1042. }
  1043. static store_t *
  1044. imap_own_store( store_conf_t *conf )
  1045. {
  1046. store_t *store, **storep;
  1047. for (storep = &unowned; (store = *storep); storep = &store->next)
  1048. if (store->conf == conf) {
  1049. *storep = store->next;
  1050. return store;
  1051. }
  1052. return 0;
  1053. }
  1054. static void
  1055. imap_cleanup( void )
  1056. {
  1057. store_t *ctx, *nctx;
  1058. for (ctx = unowned; ctx; ctx = nctx) {
  1059. nctx = ctx->next;
  1060. imap_exec( (imap_store_t *)ctx, 0, "LOGOUT" );
  1061. imap_cancel_store( ctx );
  1062. }
  1063. }
  1064. #ifdef HAVE_LIBSSL
  1065. static int
  1066. start_tls( imap_store_t *ctx )
  1067. {
  1068. int ret;
  1069. static int ssl_inited;
  1070. if (!ssl_inited) {
  1071. SSL_library_init();
  1072. SSL_load_error_strings();
  1073. ssl_inited = 1;
  1074. }
  1075. if (init_ssl_ctx( ctx ))
  1076. return 1;
  1077. ctx->buf.sock.ssl = SSL_new( ctx->SSLContext );
  1078. SSL_set_fd( ctx->buf.sock.ssl, ctx->buf.sock.fd );
  1079. if ((ret = SSL_connect( ctx->buf.sock.ssl )) <= 0) {
  1080. socket_perror( "connect", &ctx->buf.sock, ret );
  1081. return 1;
  1082. }
  1083. /* verify the server certificate */
  1084. if (verify_cert( ctx ))
  1085. return 1;
  1086. ctx->buf.sock.use_ssl = 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. /* XXX merge into do_cram_auth? */
  1099. static char *
  1100. cram( const char *challenge, const char *user, const char *pass )
  1101. {
  1102. HMAC_CTX hmac;
  1103. char hash[16];
  1104. char hex[33];
  1105. int i;
  1106. unsigned int hashlen = sizeof(hash);
  1107. char buf[256];
  1108. int len = strlen( challenge );
  1109. char *response = nfcalloc( 1 + len );
  1110. char *final;
  1111. /* response will always be smaller than challenge because we are
  1112. * decoding.
  1113. */
  1114. len = EVP_DecodeBlock( (unsigned char *)response, (unsigned char *)challenge, strlen( challenge ) );
  1115. HMAC_Init( &hmac, (unsigned char *) pass, strlen( pass ), EVP_md5() );
  1116. HMAC_Update( &hmac, (unsigned char *)response, strlen( response ) );
  1117. HMAC_Final( &hmac, (unsigned char *)hash, &hashlen );
  1118. assert( hashlen == sizeof(hash) );
  1119. free( response );
  1120. hex[32] = 0;
  1121. for (i = 0; i < 16; i++) {
  1122. hex[2 * i] = hexchar( (hash[i] >> 4) & 0xf );
  1123. hex[2 * i + 1] = hexchar( hash[i] & 0xf );
  1124. }
  1125. nfsnprintf( buf, sizeof(buf), "%s %s", user, hex );
  1126. len = strlen( buf );
  1127. len = ENCODED_SIZE( len ) + 1;
  1128. final = nfmalloc( len );
  1129. final[len - 1] = 0;
  1130. assert( EVP_EncodeBlock( (unsigned char *)final, (unsigned char *)buf, strlen( buf ) ) == len - 1 );
  1131. return final;
  1132. }
  1133. static int
  1134. do_cram_auth( imap_store_t *ctx, struct imap_cmd *cmdp, const char *prompt )
  1135. {
  1136. imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server;
  1137. char *resp;
  1138. int n, l;
  1139. resp = cram( prompt, srvc->user, srvc->pass );
  1140. if (DFlags & VERBOSE)
  1141. printf( ">+> %s\n", resp );
  1142. l = strlen( resp );
  1143. n = socket_write( &ctx->buf.sock, resp, l );
  1144. free( resp );
  1145. if (n != l)
  1146. return -1;
  1147. cmdp->param.cont = 0;
  1148. return 0;
  1149. }
  1150. #endif
  1151. static void
  1152. imap_open_store( store_conf_t *conf,
  1153. void (*cb)( store_t *srv, void *aux ), void *aux )
  1154. {
  1155. imap_store_conf_t *cfg = (imap_store_conf_t *)conf;
  1156. imap_server_conf_t *srvc = cfg->server;
  1157. imap_store_t *ctx;
  1158. store_t **ctxp;
  1159. char *arg, *rsp;
  1160. struct hostent *he;
  1161. struct sockaddr_in addr;
  1162. int s, a[2], preauth;
  1163. #if HAVE_LIBSSL
  1164. int use_ssl;
  1165. #endif
  1166. for (ctxp = &unowned; (ctx = (imap_store_t *)*ctxp); ctxp = &ctx->gen.next)
  1167. if (((imap_store_conf_t *)ctx->gen.conf)->server == srvc) {
  1168. *ctxp = ctx->gen.next;
  1169. /* One could ping the server here, but given that the idle timeout
  1170. * is at least 30 minutes, this sounds pretty pointless. */
  1171. free_string_list( ctx->gen.boxes );
  1172. ctx->gen.boxes = 0;
  1173. ctx->gen.listed = 0;
  1174. ctx->gen.conf = conf;
  1175. goto final;
  1176. }
  1177. ctx = nfcalloc( sizeof(*ctx) );
  1178. ctx->gen.conf = conf;
  1179. ctx->buf.sock.fd = -1;
  1180. ctx->in_progress_append = &ctx->in_progress;
  1181. /* open connection to IMAP server */
  1182. #if HAVE_LIBSSL
  1183. use_ssl = 0;
  1184. #endif
  1185. if (srvc->tunnel) {
  1186. infon( "Starting tunnel '%s'... ", srvc->tunnel );
  1187. if (socketpair( PF_UNIX, SOCK_STREAM, 0, a )) {
  1188. perror( "socketpair" );
  1189. exit( 1 );
  1190. }
  1191. if (fork() == 0) {
  1192. if (dup2( a[0], 0 ) == -1 || dup2( a[0], 1 ) == -1)
  1193. _exit( 127 );
  1194. close( a[0] );
  1195. close( a[1] );
  1196. execl( "/bin/sh", "sh", "-c", srvc->tunnel, (char *)0 );
  1197. _exit( 127 );
  1198. }
  1199. close (a[0]);
  1200. ctx->buf.sock.fd = a[1];
  1201. info( "ok\n" );
  1202. } else {
  1203. memset( &addr, 0, sizeof(addr) );
  1204. addr.sin_port = srvc->port ? htons( srvc->port ) :
  1205. #ifdef HAVE_LIBSSL
  1206. srvc->use_imaps ? htons( 993 ) :
  1207. #endif
  1208. htons( 143 );
  1209. addr.sin_family = AF_INET;
  1210. infon( "Resolving %s... ", srvc->host );
  1211. he = gethostbyname( srvc->host );
  1212. if (!he) {
  1213. error( "IMAP error: Cannot resolve server '%s'\n", srvc->host );
  1214. goto bail;
  1215. }
  1216. info( "ok\n" );
  1217. addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
  1218. s = socket( PF_INET, SOCK_STREAM, 0 );
  1219. if (s < 0) {
  1220. perror( "socket" );
  1221. exit( 1 );
  1222. }
  1223. infon( "Connecting to %s:%hu... ", inet_ntoa( addr.sin_addr ), ntohs( addr.sin_port ) );
  1224. if (connect( s, (struct sockaddr *)&addr, sizeof(addr) )) {
  1225. close( s );
  1226. perror( "connect" );
  1227. goto bail;
  1228. }
  1229. info( "ok\n" );
  1230. ctx->buf.sock.fd = s;
  1231. }
  1232. #if HAVE_LIBSSL
  1233. if (srvc->use_imaps) {
  1234. if (start_tls( ctx ))
  1235. goto ssl_bail;
  1236. use_ssl = 1;
  1237. }
  1238. #endif
  1239. /* read the greeting string */
  1240. if (buffer_gets( &ctx->buf, &rsp ))
  1241. goto bail;
  1242. arg = next_arg( &rsp );
  1243. if (!arg || *arg != '*' || (arg = next_arg( &rsp )) == NULL) {
  1244. error( "IMAP error: invalid greeting response\n" );
  1245. goto bail;
  1246. }
  1247. preauth = 0;
  1248. if (!strcmp( "PREAUTH", arg ))
  1249. preauth = 1;
  1250. else if (strcmp( "OK", arg ) != 0) {
  1251. error( "IMAP error: unknown greeting response\n" );
  1252. goto bail;
  1253. }
  1254. parse_response_code( ctx, 0, rsp );
  1255. if (!ctx->caps && imap_exec( ctx, 0, "CAPABILITY" ) != RESP_OK)
  1256. goto bail;
  1257. if (!preauth) {
  1258. #if HAVE_LIBSSL
  1259. if (!srvc->use_imaps && (srvc->use_sslv2 || srvc->use_sslv3 || srvc->use_tlsv1)) {
  1260. /* always try to select SSL support if available */
  1261. if (CAP(STARTTLS)) {
  1262. if (imap_exec( ctx, 0, "STARTTLS" ) != RESP_OK)
  1263. goto bail;
  1264. if (start_tls( ctx ))
  1265. goto ssl_bail;
  1266. use_ssl = 1;
  1267. if (imap_exec( ctx, 0, "CAPABILITY" ) != RESP_OK)
  1268. goto bail;
  1269. } else {
  1270. if (srvc->require_ssl) {
  1271. error( "IMAP error: SSL support not available\n" );
  1272. goto bail;
  1273. } else
  1274. warn( "IMAP warning: SSL support not available\n" );
  1275. }
  1276. }
  1277. #endif
  1278. info ("Logging in...\n");
  1279. if (!srvc->user) {
  1280. error( "Skipping account %s, no user\n", srvc->name );
  1281. goto bail;
  1282. }
  1283. if (!srvc->pass) {
  1284. char prompt[80];
  1285. sprintf( prompt, "Password (%s): ", srvc->name );
  1286. arg = getpass( prompt );
  1287. if (!arg) {
  1288. perror( "getpass" );
  1289. exit( 1 );
  1290. }
  1291. if (!*arg) {
  1292. error( "Skipping account %s, no password\n", srvc->name );
  1293. goto bail;
  1294. }
  1295. /*
  1296. * getpass() returns a pointer to a static buffer. make a copy
  1297. * for long term storage.
  1298. */
  1299. srvc->pass = nfstrdup( arg );
  1300. }
  1301. #if HAVE_LIBSSL
  1302. if (CAP(CRAM)) {
  1303. struct imap_cmd *cmd = new_imap_cmd();
  1304. info( "Authenticating with CRAM-MD5\n" );
  1305. cmd->param.cont = do_cram_auth;
  1306. if (imap_exec( ctx, cmd, "AUTHENTICATE CRAM-MD5" ) != RESP_OK)
  1307. goto bail;
  1308. } else if (srvc->require_cram) {
  1309. error( "IMAP error: CRAM-MD5 authentication is not supported by server\n" );
  1310. goto bail;
  1311. } else
  1312. #endif
  1313. {
  1314. if (CAP(NOLOGIN)) {
  1315. error( "Skipping account %s, server forbids LOGIN\n", srvc->name );
  1316. goto bail;
  1317. }
  1318. #if HAVE_LIBSSL
  1319. if (!use_ssl)
  1320. #endif
  1321. warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
  1322. if (imap_exec( ctx, 0, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass ) != RESP_OK) {
  1323. error( "IMAP error: LOGIN failed\n" );
  1324. goto bail;
  1325. }
  1326. }
  1327. } /* !preauth */
  1328. final:
  1329. ctx->prefix = "";
  1330. if (*conf->path)
  1331. ctx->prefix = conf->path;
  1332. else if (cfg->use_namespace && CAP(NAMESPACE)) {
  1333. /* get NAMESPACE info */
  1334. if (!ctx->got_namespace) {
  1335. if (imap_exec( ctx, 0, "NAMESPACE" ) != RESP_OK) {
  1336. cb( 0, aux );
  1337. return;
  1338. }
  1339. ctx->got_namespace = 1;
  1340. }
  1341. /* XXX for now assume personal namespace */
  1342. if (is_list( ctx->ns_personal ) &&
  1343. is_list( ctx->ns_personal->child ) &&
  1344. is_atom( ctx->ns_personal->child->child ))
  1345. ctx->prefix = ctx->ns_personal->child->child->val;
  1346. }
  1347. ctx->trashnc = 1;
  1348. cb( &ctx->gen, aux );
  1349. return;
  1350. #if HAVE_LIBSSL
  1351. ssl_bail:
  1352. /* This avoids that we try to send LOGOUT to an unusable socket. */
  1353. close( ctx->buf.sock.fd );
  1354. ctx->buf.sock.fd = -1;
  1355. #endif
  1356. bail:
  1357. imap_cancel_store( &ctx->gen );
  1358. cb( 0, aux );
  1359. }
  1360. static void
  1361. imap_prepare_paths( store_t *gctx )
  1362. {
  1363. free_generic_messages( gctx->msgs );
  1364. gctx->msgs = 0;
  1365. }
  1366. static void
  1367. imap_prepare_opts( store_t *gctx, int opts )
  1368. {
  1369. gctx->opts = opts;
  1370. }
  1371. static int
  1372. imap_select( store_t *gctx, int minuid, int maxuid, int *excs, int nexcs,
  1373. int (*cb)( int sts, void *aux ), void *aux )
  1374. {
  1375. imap_store_t *ctx = (imap_store_t *)gctx;
  1376. struct imap_cmd *cmd = new_imap_cmd();
  1377. const char *prefix;
  1378. int ret, i, j, bl;
  1379. char buf[1000];
  1380. if (!strcmp( gctx->name, "INBOX" )) {
  1381. // ctx->currentnc = 0;
  1382. prefix = "";
  1383. } else {
  1384. // ctx->currentnc = 1; /* could use LIST results for that */
  1385. prefix = ctx->prefix;
  1386. }
  1387. ctx->uidnext = -1;
  1388. cmd->param.create = (gctx->opts & OPEN_CREATE) != 0;
  1389. cmd->param.trycreate = 1;
  1390. if ((ret = imap_exec_b( ctx, cmd, "SELECT \"%s%s\"", prefix, gctx->name )) != DRV_OK)
  1391. goto bail;
  1392. if (gctx->count) {
  1393. ctx->msgapp = &gctx->msgs;
  1394. sort_ints( excs, nexcs );
  1395. for (i = 0; i < nexcs; ) {
  1396. for (bl = 0; i < nexcs && bl < 960; i++) {
  1397. if (bl)
  1398. buf[bl++] = ',';
  1399. bl += sprintf( buf + bl, "%d", excs[i] );
  1400. j = i;
  1401. for (; i + 1 < nexcs && excs[i + 1] == excs[i] + 1; i++);
  1402. if (i != j)
  1403. bl += sprintf( buf + bl, ":%d", excs[i] );
  1404. }
  1405. if ((ret = imap_exec_b( ctx, 0, "UID FETCH %s (UID%s%s)", buf,
  1406. (gctx->opts & OPEN_FLAGS) ? " FLAGS" : "",
  1407. (gctx->opts & OPEN_SIZE) ? " RFC822.SIZE" : "" )) != DRV_OK)
  1408. goto bail;
  1409. }
  1410. if (maxuid == INT_MAX)
  1411. maxuid = ctx->uidnext >= 0 ? ctx->uidnext - 1 : 1000000000;
  1412. if (maxuid >= minuid &&
  1413. (ret = imap_exec_b( ctx, 0, "UID FETCH %d:%d (UID%s%s)", minuid, maxuid,
  1414. (gctx->opts & OPEN_FLAGS) ? " FLAGS" : "",
  1415. (gctx->opts & OPEN_SIZE) ? " RFC822.SIZE" : "" )) != DRV_OK)
  1416. goto bail;
  1417. }
  1418. ret = DRV_OK;
  1419. bail:
  1420. free( excs );
  1421. return cb( ret, aux );
  1422. }
  1423. static int
  1424. imap_fetch_msg( store_t *ctx, message_t *msg, msg_data_t *data,
  1425. int (*cb)( int sts, void *aux ), void *aux )
  1426. {
  1427. struct imap_cmd *cmd = new_imap_cmd();
  1428. cmd->param.uid = msg->uid;
  1429. cmd->param.aux = data;
  1430. return cb( imap_exec_m( (imap_store_t *)ctx, cmd, "UID FETCH %d (%sBODY.PEEK[])",
  1431. msg->uid, (msg->status & M_FLAGS) ? "" : "FLAGS " ), aux );
  1432. }
  1433. static int
  1434. imap_make_flags( int flags, char *buf )
  1435. {
  1436. const char *s;
  1437. unsigned i, d;
  1438. for (i = d = 0; i < as(Flags); i++)
  1439. if (flags & (1 << i)) {
  1440. buf[d++] = ' ';
  1441. buf[d++] = '\\';
  1442. for (s = Flags[i]; *s; s++)
  1443. buf[d++] = *s;
  1444. }
  1445. buf[0] = '(';
  1446. buf[d++] = ')';
  1447. return d;
  1448. }
  1449. static int
  1450. imap_flags_helper( imap_store_t *ctx, int uid, char what, int flags)
  1451. {
  1452. char buf[256];
  1453. buf[imap_make_flags( flags, buf )] = 0;
  1454. if (!submit_imap_cmd( ctx, 0, "UID STORE %d %cFLAGS.SILENT %s", uid, what, buf ))
  1455. return DRV_STORE_BAD;
  1456. process_imap_replies( ctx );
  1457. return DRV_OK;
  1458. }
  1459. static int
  1460. imap_set_flags( store_t *gctx, message_t *msg, int uid, int add, int del,
  1461. int (*cb)( int sts, void *aux ), void *aux )
  1462. {
  1463. imap_store_t *ctx = (imap_store_t *)gctx;
  1464. int ret;
  1465. if (msg) {
  1466. uid = msg->uid;
  1467. add &= ~msg->flags;
  1468. del &= msg->flags;
  1469. msg->flags |= add;
  1470. msg->flags &= ~del;
  1471. }
  1472. if ((!add || (ret = imap_flags_helper( ctx, uid, '+', add )) == DRV_OK) &&
  1473. (!del || (ret = imap_flags_helper( ctx, uid, '-', del )) == DRV_OK))
  1474. ret = DRV_OK;
  1475. return cb( ret, aux );
  1476. }
  1477. static int
  1478. imap_close( store_t *ctx,
  1479. int (*cb)( int sts, void *aux ), void *aux )
  1480. {
  1481. return cb( imap_exec_b( (imap_store_t *)ctx, 0, "CLOSE" ), aux );
  1482. }
  1483. static int
  1484. imap_trash_msg( store_t *gctx, message_t *msg,
  1485. int (*cb)( int sts, void *aux ), void *aux )
  1486. {
  1487. imap_store_t *ctx = (imap_store_t *)gctx;
  1488. struct imap_cmd *cmd = new_imap_cmd();
  1489. cmd->param.create = 1;
  1490. return cb( imap_exec_m( ctx, cmd, "UID COPY %d \"%s%s\"",
  1491. msg->uid, ctx->prefix, gctx->conf->trash ), aux );
  1492. }
  1493. static int
  1494. imap_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1495. int (*cb)( int sts, int uid, void *aux ), void *aux )
  1496. {
  1497. imap_store_t *ctx = (imap_store_t *)gctx;
  1498. struct imap_cmd *cmd = new_imap_cmd();
  1499. const char *prefix, *box;
  1500. int ret, d, uid;
  1501. char flagstr[128];
  1502. d = 0;
  1503. if (data->flags) {
  1504. d = imap_make_flags( data->flags, flagstr );
  1505. flagstr[d++] = ' ';
  1506. }
  1507. flagstr[d] = 0;
  1508. cmd->param.data_len = data->len;
  1509. cmd->param.data = data->data;
  1510. cmd->param.aux = &uid;
  1511. uid = -2;
  1512. if (to_trash) {
  1513. box = gctx->conf->trash;
  1514. prefix = ctx->prefix;
  1515. cmd->param.create = 1;
  1516. if (ctx->trashnc)
  1517. ctx->caps = ctx->rcaps & ~(1 << LITERALPLUS);
  1518. } else {
  1519. box = gctx->name;
  1520. prefix = !strcmp( box, "INBOX" ) ? "" : ctx->prefix;
  1521. cmd->param.create = (gctx->opts & OPEN_CREATE) != 0;
  1522. /*if (ctx->currentnc)
  1523. ctx->caps = ctx->rcaps & ~(1 << LITERALPLUS);*/
  1524. }
  1525. ret = imap_exec_m( ctx, cmd, "APPEND \"%s%s\" %s", prefix, box, flagstr );
  1526. ctx->caps = ctx->rcaps;
  1527. if (ret != DRV_OK)
  1528. return cb( ret, -1, aux );
  1529. if (to_trash)
  1530. ctx->trashnc = 0;
  1531. else {
  1532. /*ctx->currentnc = 0;*/
  1533. }
  1534. return cb( DRV_OK, uid, aux );
  1535. }
  1536. static int
  1537. imap_find_msg( store_t *gctx, const char *tuid,
  1538. int (*cb)( int sts, int uid, void *aux ), void *aux )
  1539. {
  1540. imap_store_t *ctx = (imap_store_t *)gctx;
  1541. struct imap_cmd *cmd = new_imap_cmd();
  1542. int ret, uid;
  1543. cmd->param.uid = -1; /* we're looking for a UID */
  1544. cmd->param.aux = &uid;
  1545. uid = -1; /* in case we get no SEARCH response at all */
  1546. if ((ret = imap_exec_m( ctx, cmd, "UID SEARCH HEADER X-TUID %." stringify(TUIDL) "s", tuid )) != DRV_OK)
  1547. return cb( ret, -1, aux );
  1548. else
  1549. return cb( uid <= 0 ? DRV_MSG_BAD : DRV_OK, uid, aux );
  1550. }
  1551. static void
  1552. imap_list( store_t *gctx,
  1553. void (*cb)( int sts, void *aux ), void *aux )
  1554. {
  1555. imap_store_t *ctx = (imap_store_t *)gctx;
  1556. int ret;
  1557. if ((ret = imap_exec_b( ctx, 0, "LIST \"\" \"%s%%\"", ctx->prefix )) == DRV_OK)
  1558. gctx->listed = 1;
  1559. cb( ret, aux );
  1560. }
  1561. static void
  1562. imap_cancel( store_t *gctx,
  1563. void (*cb)( int sts, void *aux ), void *aux )
  1564. {
  1565. (void)gctx;
  1566. cb( DRV_OK, aux );
  1567. }
  1568. static void
  1569. imap_commit( store_t *gctx )
  1570. {
  1571. (void)gctx;
  1572. }
  1573. imap_server_conf_t *servers, **serverapp = &servers;
  1574. static int
  1575. imap_parse_store( conffile_t *cfg, store_conf_t **storep, int *err )
  1576. {
  1577. imap_store_conf_t *store;
  1578. imap_server_conf_t *server, *srv, sserver;
  1579. int acc_opt = 0;
  1580. if (!strcasecmp( "IMAPAccount", cfg->cmd )) {
  1581. server = nfcalloc( sizeof(*server) );
  1582. server->name = nfstrdup( cfg->val );
  1583. *serverapp = server;
  1584. serverapp = &server->next;
  1585. store = 0;
  1586. *storep = 0;
  1587. } else if (!strcasecmp( "IMAPStore", cfg->cmd )) {
  1588. store = nfcalloc( sizeof(*store) );
  1589. store->gen.driver = &imap_driver;
  1590. store->gen.name = nfstrdup( cfg->val );
  1591. store->use_namespace = 1;
  1592. *storep = &store->gen;
  1593. memset( &sserver, 0, sizeof(sserver) );
  1594. server = &sserver;
  1595. } else
  1596. return 0;
  1597. #if HAVE_LIBSSL
  1598. /* this will probably annoy people, but its the best default just in
  1599. * case people forget to turn it on
  1600. */
  1601. server->require_ssl = 1;
  1602. server->use_tlsv1 = 1;
  1603. #endif
  1604. while (getcline( cfg ) && cfg->cmd) {
  1605. if (!strcasecmp( "Host", cfg->cmd )) {
  1606. /* The imap[s]: syntax is just a backwards compat hack. */
  1607. #if HAVE_LIBSSL
  1608. if (!memcmp( "imaps:", cfg->val, 6 )) {
  1609. cfg->val += 6;
  1610. server->use_imaps = 1;
  1611. server->use_sslv2 = 1;
  1612. server->use_sslv3 = 1;
  1613. } else
  1614. #endif
  1615. {
  1616. if (!memcmp( "imap:", cfg->val, 5 ))
  1617. cfg->val += 5;
  1618. }
  1619. if (!memcmp( "//", cfg->val, 2 ))
  1620. cfg->val += 2;
  1621. server->host = nfstrdup( cfg->val );
  1622. }
  1623. else if (!strcasecmp( "User", cfg->cmd ))
  1624. server->user = nfstrdup( cfg->val );
  1625. else if (!strcasecmp( "Pass", cfg->cmd ))
  1626. server->pass = nfstrdup( cfg->val );
  1627. else if (!strcasecmp( "Port", cfg->cmd ))
  1628. server->port = parse_int( cfg );
  1629. #if HAVE_LIBSSL
  1630. else if (!strcasecmp( "CertificateFile", cfg->cmd )) {
  1631. server->cert_file = expand_strdup( cfg->val );
  1632. if (access( server->cert_file, R_OK )) {
  1633. error( "%s:%d: CertificateFile '%s': %s\n",
  1634. cfg->file, cfg->line, server->cert_file, strerror( errno ) );
  1635. *err = 1;
  1636. }
  1637. } else if (!strcasecmp( "RequireSSL", cfg->cmd ))
  1638. server->require_ssl = parse_bool( cfg );
  1639. else if (!strcasecmp( "UseIMAPS", cfg->cmd ))
  1640. server->use_imaps = parse_bool( cfg );
  1641. else if (!strcasecmp( "UseSSLv2", cfg->cmd ))
  1642. server->use_sslv2 = parse_bool( cfg );
  1643. else if (!strcasecmp( "UseSSLv3", cfg->cmd ))
  1644. server->use_sslv3 = parse_bool( cfg );
  1645. else if (!strcasecmp( "UseTLSv1", cfg->cmd ))
  1646. server->use_tlsv1 = parse_bool( cfg );
  1647. else if (!strcasecmp( "RequireCRAM", cfg->cmd ))
  1648. server->require_cram = parse_bool( cfg );
  1649. #endif
  1650. else if (!strcasecmp( "Tunnel", cfg->cmd ))
  1651. server->tunnel = nfstrdup( cfg->val );
  1652. else if (store) {
  1653. if (!strcasecmp( "Account", cfg->cmd )) {
  1654. for (srv = servers; srv; srv = srv->next)
  1655. if (srv->name && !strcmp( srv->name, cfg->val ))
  1656. goto gotsrv;
  1657. error( "%s:%d: unknown IMAP account '%s'\n", cfg->file, cfg->line, cfg->val );
  1658. *err = 1;
  1659. continue;
  1660. gotsrv:
  1661. store->server = srv;
  1662. } else if (!strcasecmp( "UseNamespace", cfg->cmd ))
  1663. store->use_namespace = parse_bool( cfg );
  1664. else if (!strcasecmp( "Path", cfg->cmd ))
  1665. store->gen.path = nfstrdup( cfg->val );
  1666. else
  1667. parse_generic_store( &store->gen, cfg, err );
  1668. continue;
  1669. } else {
  1670. error( "%s:%d: unknown/misplaced keyword '%s'\n", cfg->file, cfg->line, cfg->cmd );
  1671. *err = 1;
  1672. continue;
  1673. }
  1674. acc_opt = 1;
  1675. }
  1676. if (!store || !store->server) {
  1677. if (!server->tunnel && !server->host) {
  1678. if (store)
  1679. error( "IMAP store '%s' has incomplete/missing connection details\n", store->gen.name );
  1680. else
  1681. error( "IMAP account '%s' has incomplete/missing connection details\n", server->name );
  1682. *err = 1;
  1683. return 1;
  1684. }
  1685. }
  1686. if (store) {
  1687. if (!store->server) {
  1688. store->server = nfmalloc( sizeof(sserver) );
  1689. memcpy( store->server, &sserver, sizeof(sserver) );
  1690. store->server->name = store->gen.name;
  1691. } else if (acc_opt) {
  1692. error( "IMAP store '%s' has both Account and account-specific options\n", store->gen.name );
  1693. *err = 1;
  1694. }
  1695. }
  1696. return 1;
  1697. }
  1698. struct driver imap_driver = {
  1699. DRV_CRLF,
  1700. imap_parse_store,
  1701. imap_cleanup,
  1702. imap_open_store,
  1703. imap_disown_store,
  1704. imap_own_store,
  1705. imap_cancel_store,
  1706. imap_list,
  1707. imap_prepare_paths,
  1708. imap_prepare_opts,
  1709. imap_select,
  1710. imap_fetch_msg,
  1711. imap_store_msg,
  1712. imap_find_msg,
  1713. imap_set_flags,
  1714. imap_trash_msg,
  1715. imap_close,
  1716. imap_cancel,
  1717. imap_commit,
  1718. };