drv_imap.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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. if (cmd->param.data)
  463. free( cmd->param.data );
  464. free( cmd->cmd );
  465. free( cmd );
  466. return NULL;
  467. }
  468. if (cmd->param.data) {
  469. if (CAP(LITERALPLUS)) {
  470. n = socket_write( &ctx->buf.sock, cmd->param.data, cmd->param.data_len );
  471. free( cmd->param.data );
  472. if (n != cmd->param.data_len ||
  473. (n = socket_write( &ctx->buf.sock, "\r\n", 2 )) != 2)
  474. {
  475. free( cmd->cmd );
  476. free( cmd );
  477. return NULL;
  478. }
  479. cmd->param.data = 0;
  480. } else
  481. ctx->literal_pending = 1;
  482. } else if (cmd->param.cont)
  483. ctx->literal_pending = 1;
  484. cmd->next = 0;
  485. *ctx->in_progress_append = cmd;
  486. ctx->in_progress_append = &cmd->next;
  487. ctx->num_in_progress++;
  488. return cmd;
  489. }
  490. static struct imap_cmd *
  491. submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd, const char *fmt, ... )
  492. {
  493. struct imap_cmd *ret;
  494. va_list ap;
  495. va_start( ap, fmt );
  496. ret = v_submit_imap_cmd( ctx, cmd, fmt, ap );
  497. va_end( ap );
  498. return ret;
  499. }
  500. static int
  501. imap_exec( imap_store_t *ctx, struct imap_cmd *cmdp, const char *fmt, ... )
  502. {
  503. va_list ap;
  504. va_start( ap, fmt );
  505. cmdp = v_submit_imap_cmd( ctx, cmdp, fmt, ap );
  506. va_end( ap );
  507. if (!cmdp)
  508. return RESP_BAD;
  509. return get_cmd_result( ctx, cmdp );
  510. }
  511. static int
  512. imap_exec_b( imap_store_t *ctx, struct imap_cmd *cmdp, const char *fmt, ... )
  513. {
  514. va_list ap;
  515. va_start( ap, fmt );
  516. cmdp = v_submit_imap_cmd( ctx, cmdp, fmt, ap );
  517. va_end( ap );
  518. if (!cmdp)
  519. return DRV_STORE_BAD;
  520. switch (get_cmd_result( ctx, cmdp )) {
  521. case RESP_BAD: return DRV_STORE_BAD;
  522. case RESP_NO: return DRV_BOX_BAD;
  523. default: return DRV_OK;
  524. }
  525. }
  526. static int
  527. imap_exec_m( imap_store_t *ctx, struct imap_cmd *cmdp, const char *fmt, ... )
  528. {
  529. va_list ap;
  530. va_start( ap, fmt );
  531. cmdp = v_submit_imap_cmd( ctx, cmdp, fmt, ap );
  532. va_end( ap );
  533. if (!cmdp)
  534. return DRV_STORE_BAD;
  535. switch (get_cmd_result( ctx, cmdp )) {
  536. case RESP_BAD: return DRV_STORE_BAD;
  537. case RESP_NO: return DRV_MSG_BAD;
  538. default: return DRV_OK;
  539. }
  540. }
  541. /*
  542. static void
  543. drain_imap_replies( imap_store_t *ctx )
  544. {
  545. while (ctx->num_in_progress)
  546. get_cmd_result( ctx, 0 );
  547. }
  548. */
  549. static void
  550. process_imap_replies( imap_store_t *ctx )
  551. {
  552. while (ctx->num_in_progress > max_in_progress ||
  553. socket_pending( &ctx->buf.sock ))
  554. get_cmd_result( ctx, 0 );
  555. }
  556. static int
  557. is_atom( list_t *list )
  558. {
  559. return list && list->val && list->val != NIL && list->val != LIST;
  560. }
  561. static int
  562. is_list( list_t *list )
  563. {
  564. return list && list->val == LIST;
  565. }
  566. static void
  567. free_list( list_t *list )
  568. {
  569. list_t *tmp;
  570. for (; list; list = tmp) {
  571. tmp = list->next;
  572. if (is_list( list ))
  573. free_list( list->child );
  574. else if (is_atom( list ))
  575. free( list->val );
  576. free( list );
  577. }
  578. }
  579. static int
  580. parse_imap_list_l( imap_store_t *ctx, char **sp, list_t **curp, int level )
  581. {
  582. list_t *cur;
  583. char *s = *sp, *p;
  584. int n, bytes;
  585. for (;;) {
  586. while (isspace( (unsigned char)*s ))
  587. s++;
  588. if (level && *s == ')') {
  589. s++;
  590. break;
  591. }
  592. *curp = cur = nfmalloc( sizeof(*cur) );
  593. curp = &cur->next;
  594. cur->val = 0; /* for clean bail */
  595. if (*s == '(') {
  596. /* sublist */
  597. s++;
  598. cur->val = LIST;
  599. if (parse_imap_list_l( ctx, &s, &cur->child, level + 1 ))
  600. goto bail;
  601. } else if (ctx && *s == '{') {
  602. /* literal */
  603. bytes = cur->len = strtol( s + 1, &s, 10 );
  604. if (*s != '}')
  605. goto bail;
  606. s = cur->val = nfmalloc( cur->len );
  607. /* dump whats left over in the input buffer */
  608. n = ctx->buf.bytes - ctx->buf.offset;
  609. if (n > bytes)
  610. /* the entire message fit in the buffer */
  611. n = bytes;
  612. memcpy( s, ctx->buf.buf + ctx->buf.offset, n );
  613. s += n;
  614. bytes -= n;
  615. /* mark that we used part of the buffer */
  616. ctx->buf.offset += n;
  617. /* now read the rest of the message */
  618. while (bytes > 0) {
  619. if ((n = socket_read( &ctx->buf.sock, s, bytes )) <= 0)
  620. goto bail;
  621. s += n;
  622. bytes -= n;
  623. }
  624. if (DFlags & XVERBOSE) {
  625. puts( "=========" );
  626. fwrite( cur->val, cur->len, 1, stdout );
  627. puts( "=========" );
  628. }
  629. if (buffer_gets( &ctx->buf, &s ))
  630. goto bail;
  631. } else if (*s == '"') {
  632. /* quoted string */
  633. s++;
  634. p = s;
  635. for (; *s != '"'; s++)
  636. if (!*s)
  637. goto bail;
  638. cur->len = s - p;
  639. s++;
  640. cur->val = nfmalloc( cur->len + 1 );
  641. memcpy( cur->val, p, cur->len );
  642. cur->val[cur->len] = 0;
  643. } else {
  644. /* atom */
  645. p = s;
  646. for (; *s && !isspace( (unsigned char)*s ); s++)
  647. if (level && *s == ')')
  648. break;
  649. cur->len = s - p;
  650. if (cur->len == 3 && !memcmp ("NIL", p, 3))
  651. cur->val = NIL;
  652. else {
  653. cur->val = nfmalloc( cur->len + 1 );
  654. memcpy( cur->val, p, cur->len );
  655. cur->val[cur->len] = 0;
  656. }
  657. }
  658. if (!level)
  659. break;
  660. if (!*s)
  661. goto bail;
  662. }
  663. *sp = s;
  664. *curp = 0;
  665. return 0;
  666. bail:
  667. *curp = 0;
  668. return -1;
  669. }
  670. static list_t *
  671. parse_imap_list( imap_store_t *ctx, char **sp )
  672. {
  673. list_t *head;
  674. if (!parse_imap_list_l( ctx, sp, &head, 0 ))
  675. return head;
  676. free_list( head );
  677. return NULL;
  678. }
  679. static list_t *
  680. parse_list( char **sp )
  681. {
  682. return parse_imap_list( 0, sp );
  683. }
  684. static int
  685. parse_fetch( imap_store_t *ctx, char *cmd ) /* move this down */
  686. {
  687. list_t *tmp, *list, *flags;
  688. char *body = 0;
  689. imap_message_t *cur;
  690. msg_data_t *msgdata;
  691. struct imap_cmd *cmdp;
  692. int uid = 0, mask = 0, status = 0, size = 0;
  693. unsigned i;
  694. list = parse_imap_list( ctx, &cmd );
  695. if (!is_list( list )) {
  696. error( "IMAP error: bogus FETCH response\n" );
  697. free_list( list );
  698. return -1;
  699. }
  700. for (tmp = list->child; tmp; tmp = tmp->next) {
  701. if (is_atom( tmp )) {
  702. if (!strcmp( "UID", tmp->val )) {
  703. tmp = tmp->next;
  704. if (is_atom( tmp ))
  705. uid = atoi( tmp->val );
  706. else
  707. error( "IMAP error: unable to parse UID\n" );
  708. } else if (!strcmp( "FLAGS", tmp->val )) {
  709. tmp = tmp->next;
  710. if (is_list( tmp )) {
  711. for (flags = tmp->child; flags; flags = flags->next) {
  712. if (is_atom( flags )) {
  713. if (flags->val[0] == '\\') { /* ignore user-defined flags for now */
  714. if (!strcmp( "Recent", flags->val + 1)) {
  715. status |= M_RECENT;
  716. goto flagok;
  717. }
  718. for (i = 0; i < as(Flags); i++)
  719. if (!strcmp( Flags[i], flags->val + 1 )) {
  720. mask |= 1 << i;
  721. goto flagok;
  722. }
  723. if (flags->val[1] == 'X' && flags->val[2] == '-')
  724. goto flagok; /* ignore system flag extensions */
  725. error( "IMAP warning: unknown system flag %s\n", flags->val );
  726. }
  727. flagok: ;
  728. } else
  729. error( "IMAP error: unable to parse FLAGS list\n" );
  730. }
  731. status |= M_FLAGS;
  732. } else
  733. error( "IMAP error: unable to parse FLAGS\n" );
  734. } else if (!strcmp( "RFC822.SIZE", tmp->val )) {
  735. tmp = tmp->next;
  736. if (is_atom( tmp ))
  737. size = atoi( tmp->val );
  738. else
  739. error( "IMAP error: unable to parse RFC822.SIZE\n" );
  740. } else if (!strcmp( "BODY[]", tmp->val )) {
  741. tmp = tmp->next;
  742. if (is_atom( tmp )) {
  743. body = tmp->val;
  744. tmp->val = 0; /* don't free together with list */
  745. size = tmp->len;
  746. } else
  747. error( "IMAP error: unable to parse BODY[]\n" );
  748. }
  749. }
  750. }
  751. if (body) {
  752. for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
  753. if (cmdp->param.uid == uid)
  754. goto gotuid;
  755. error( "IMAP error: unexpected FETCH response (UID %d)\n", uid );
  756. free_list( list );
  757. return -1;
  758. gotuid:
  759. msgdata = (msg_data_t *)cmdp->param.aux;
  760. msgdata->data = body;
  761. msgdata->len = size;
  762. if (status & M_FLAGS)
  763. msgdata->flags = mask;
  764. } else if (uid) { /* ignore async flag updates for now */
  765. /* XXX this will need sorting for out-of-order (multiple queries) */
  766. cur = nfcalloc( sizeof(*cur) );
  767. *ctx->msgapp = &cur->gen;
  768. ctx->msgapp = &cur->gen.next;
  769. cur->gen.next = 0;
  770. cur->gen.uid = uid;
  771. cur->gen.flags = mask;
  772. cur->gen.status = status;
  773. cur->gen.size = size;
  774. }
  775. free_list( list );
  776. return 0;
  777. }
  778. static void
  779. parse_capability( imap_store_t *ctx, char *cmd )
  780. {
  781. char *arg;
  782. unsigned i;
  783. ctx->caps = 0x80000000;
  784. while ((arg = next_arg( &cmd )))
  785. for (i = 0; i < as(cap_list); i++)
  786. if (!strcmp( cap_list[i], arg ))
  787. ctx->caps |= 1 << i;
  788. ctx->rcaps = ctx->caps;
  789. }
  790. static int
  791. parse_response_code( imap_store_t *ctx, struct imap_cmd *cmd, char *s )
  792. {
  793. char *arg, *earg, *p;
  794. if (*s != '[')
  795. return RESP_OK; /* no response code */
  796. s++;
  797. if (!(p = strchr( s, ']' ))) {
  798. error( "IMAP error: malformed response code\n" );
  799. return RESP_BAD;
  800. }
  801. *p++ = 0;
  802. arg = next_arg( &s );
  803. if (!strcmp( "UIDVALIDITY", arg )) {
  804. if (!(arg = next_arg( &s )) ||
  805. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg))
  806. {
  807. error( "IMAP error: malformed UIDVALIDITY status\n" );
  808. return RESP_BAD;
  809. }
  810. } else if (!strcmp( "UIDNEXT", arg )) {
  811. if (!(arg = next_arg( &s )) || !(ctx->uidnext = atoi( arg ))) {
  812. error( "IMAP error: malformed NEXTUID status\n" );
  813. return RESP_BAD;
  814. }
  815. } else if (!strcmp( "CAPABILITY", arg )) {
  816. parse_capability( ctx, s );
  817. } else if (!strcmp( "ALERT", arg )) {
  818. /* RFC2060 says that these messages MUST be displayed
  819. * to the user
  820. */
  821. for (; isspace( (unsigned char)*p ); p++);
  822. error( "*** IMAP ALERT *** %s\n", p );
  823. } else if (cmd && cmd->param.aux && !strcmp( "APPENDUID", arg )) {
  824. if (!(arg = next_arg( &s )) ||
  825. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg) ||
  826. !(arg = next_arg( &s )) || !(*(int *)cmd->param.aux = atoi( arg )))
  827. {
  828. error( "IMAP error: malformed APPENDUID status\n" );
  829. return RESP_BAD;
  830. }
  831. }
  832. return RESP_OK;
  833. }
  834. static void
  835. parse_search( imap_store_t *ctx, char *cmd )
  836. {
  837. char *arg;
  838. struct imap_cmd *cmdp;
  839. int uid;
  840. if (!(arg = next_arg( &cmd )))
  841. uid = -1;
  842. else if (!(uid = atoi( arg ))) {
  843. error( "IMAP error: malformed SEARCH response\n" );
  844. return;
  845. } else if (next_arg( &cmd )) {
  846. warn( "IMAP warning: SEARCH returns multiple matches\n" );
  847. uid = -1; /* to avoid havoc */
  848. }
  849. /* Find the first command that expects a UID - this is guaranteed
  850. * to come in-order, as there are no other means to identify which
  851. * SEARCH response belongs to which request.
  852. */
  853. for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
  854. if (cmdp->param.uid == -1) {
  855. *(int *)cmdp->param.aux = uid;
  856. return;
  857. }
  858. error( "IMAP error: unexpected SEARCH response (UID %u)\n", uid );
  859. }
  860. static void
  861. parse_list_rsp( imap_store_t *ctx, char *cmd )
  862. {
  863. char *arg;
  864. list_t *list, *lp;
  865. int l;
  866. list = parse_list( &cmd );
  867. if (list->val == LIST)
  868. for (lp = list->child; lp; lp = lp->next)
  869. if (is_atom( lp ) && !strcasecmp( lp->val, "\\NoSelect" )) {
  870. free_list( list );
  871. return;
  872. }
  873. free_list( list );
  874. (void) next_arg( &cmd ); /* skip delimiter */
  875. arg = next_arg( &cmd );
  876. l = strlen( ctx->gen.conf->path );
  877. if (memcmp( arg, ctx->gen.conf->path, l ))
  878. return;
  879. arg += l;
  880. if (!memcmp( arg + strlen( arg ) - 5, ".lock", 5 )) /* workaround broken servers */
  881. return;
  882. add_string_list( &ctx->gen.boxes, arg );
  883. }
  884. static int
  885. get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
  886. {
  887. struct imap_cmd *cmdp, **pcmdp, *ncmdp;
  888. char *cmd, *arg, *arg1, *p;
  889. int n, resp, resp2, tag;
  890. for (;;) {
  891. if (buffer_gets( &ctx->buf, &cmd ))
  892. return RESP_BAD;
  893. arg = next_arg( &cmd );
  894. if (*arg == '*') {
  895. arg = next_arg( &cmd );
  896. if (!arg) {
  897. error( "IMAP error: unable to parse untagged response\n" );
  898. return RESP_BAD;
  899. }
  900. if (!strcmp( "NAMESPACE", arg )) {
  901. ctx->ns_personal = parse_list( &cmd );
  902. ctx->ns_other = parse_list( &cmd );
  903. ctx->ns_shared = parse_list( &cmd );
  904. } else if (!strcmp( "OK", arg ) || !strcmp( "BAD", arg ) ||
  905. !strcmp( "NO", arg ) || !strcmp( "BYE", arg )) {
  906. if ((resp = parse_response_code( ctx, 0, cmd )) != RESP_OK)
  907. return resp;
  908. } else if (!strcmp( "CAPABILITY", arg ))
  909. parse_capability( ctx, cmd );
  910. else if (!strcmp( "LIST", arg ))
  911. parse_list_rsp( ctx, cmd );
  912. else if (!strcmp( "SEARCH", arg ))
  913. parse_search( ctx, cmd );
  914. else if ((arg1 = next_arg( &cmd ))) {
  915. if (!strcmp( "EXISTS", arg1 ))
  916. ctx->gen.count = atoi( arg );
  917. else if (!strcmp( "RECENT", arg1 ))
  918. ctx->gen.recent = atoi( arg );
  919. else if(!strcmp ( "FETCH", arg1 )) {
  920. if (parse_fetch( ctx, cmd ))
  921. return RESP_BAD;
  922. }
  923. } else {
  924. error( "IMAP error: unable to parse untagged response\n" );
  925. return RESP_BAD;
  926. }
  927. } else if (!ctx->in_progress) {
  928. error( "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "" );
  929. return RESP_BAD;
  930. } else if (*arg == '+') {
  931. /* This can happen only with the last command underway, as
  932. it enforces a round-trip. */
  933. cmdp = (struct imap_cmd *)((char *)ctx->in_progress_append -
  934. offsetof(struct imap_cmd, next));
  935. if (cmdp->param.data) {
  936. n = socket_write( &ctx->buf.sock, cmdp->param.data, cmdp->param.data_len );
  937. free( cmdp->param.data );
  938. cmdp->param.data = 0;
  939. if (n != (int)cmdp->param.data_len)
  940. return RESP_BAD;
  941. } else if (cmdp->param.cont) {
  942. if (cmdp->param.cont( ctx, cmdp, cmd ))
  943. return RESP_BAD;
  944. } else {
  945. error( "IMAP error: unexpected command continuation request\n" );
  946. return RESP_BAD;
  947. }
  948. if (socket_write( &ctx->buf.sock, "\r\n", 2 ) != 2)
  949. return RESP_BAD;
  950. if (!cmdp->param.cont)
  951. ctx->literal_pending = 0;
  952. if (!tcmd)
  953. return DRV_OK;
  954. } else {
  955. tag = atoi( arg );
  956. for (pcmdp = &ctx->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
  957. if (cmdp->tag == tag)
  958. goto gottag;
  959. error( "IMAP error: unexpected tag %s\n", arg );
  960. return RESP_BAD;
  961. gottag:
  962. if (!(*pcmdp = cmdp->next))
  963. ctx->in_progress_append = pcmdp;
  964. ctx->num_in_progress--;
  965. if (cmdp->param.cont || cmdp->param.data)
  966. ctx->literal_pending = 0;
  967. arg = next_arg( &cmd );
  968. if (!strcmp( "OK", arg ))
  969. resp = DRV_OK;
  970. else {
  971. if (!strcmp( "NO", arg )) {
  972. if (cmdp->param.create && cmd && (cmdp->param.trycreate || !memcmp( cmd, "[TRYCREATE]", 11 ))) { /* SELECT, APPEND or UID COPY */
  973. p = strchr( cmdp->cmd, '"' );
  974. if (!submit_imap_cmd( ctx, 0, "CREATE %.*s", strchr( p + 1, '"' ) - p + 1, p )) {
  975. resp = RESP_BAD;
  976. goto normal;
  977. }
  978. /* not waiting here violates the spec, but a server that does not
  979. grok this nonetheless violates it too. */
  980. ncmdp = nfmalloc( sizeof(*ncmdp) );
  981. memcpy( &ncmdp->param, &cmdp->param, sizeof(cmdp->param) );
  982. ncmdp->param.create = 0;
  983. if (!submit_imap_cmd( ctx, ncmdp, "%s", cmdp->cmd )) {
  984. resp = RESP_BAD;
  985. goto normal;
  986. }
  987. free( cmdp->cmd );
  988. free( cmdp );
  989. if (!tcmd)
  990. return 0; /* ignored */
  991. if (cmdp == tcmd)
  992. tcmd = ncmdp;
  993. continue;
  994. }
  995. resp = RESP_NO;
  996. } else /*if (!strcmp( "BAD", arg ))*/
  997. resp = RESP_BAD;
  998. error( "IMAP command '%s' returned an error: %s %s\n",
  999. memcmp( cmdp->cmd, "LOGIN", 5 ) ? cmdp->cmd : "LOGIN <user> <pass>",
  1000. arg, cmd ? cmd : "" );
  1001. }
  1002. if ((resp2 = parse_response_code( ctx, cmdp, cmd )) > resp)
  1003. resp = resp2;
  1004. normal:
  1005. if (cmdp->param.done)
  1006. cmdp->param.done( ctx, cmdp, resp );
  1007. if (cmdp->param.data)
  1008. free( cmdp->param.data );
  1009. free( cmdp->cmd );
  1010. free( cmdp );
  1011. if (!tcmd || tcmd == cmdp)
  1012. return resp;
  1013. }
  1014. }
  1015. /* not reached */
  1016. }
  1017. static void
  1018. imap_cancel_store( store_t *gctx )
  1019. {
  1020. imap_store_t *ctx = (imap_store_t *)gctx;
  1021. free_generic_messages( gctx->msgs );
  1022. free_string_list( ctx->gen.boxes );
  1023. if (ctx->buf.sock.fd >= 0)
  1024. close( ctx->buf.sock.fd );
  1025. #ifdef HAVE_LIBSSL
  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 bail;
  1236. use_ssl = 1;
  1237. }
  1238. #endif
  1239. /* read the greeting string */
  1240. if (buffer_gets( &ctx->buf, &rsp )) {
  1241. error( "IMAP error: no greeting response\n" );
  1242. goto bail;
  1243. }
  1244. arg = next_arg( &rsp );
  1245. if (!arg || *arg != '*' || (arg = next_arg( &rsp )) == NULL) {
  1246. error( "IMAP error: invalid greeting response\n" );
  1247. goto bail;
  1248. }
  1249. preauth = 0;
  1250. if (!strcmp( "PREAUTH", arg ))
  1251. preauth = 1;
  1252. else if (strcmp( "OK", arg ) != 0) {
  1253. error( "IMAP error: unknown greeting response\n" );
  1254. goto bail;
  1255. }
  1256. parse_response_code( ctx, 0, rsp );
  1257. if (!ctx->caps && imap_exec( ctx, 0, "CAPABILITY" ) != RESP_OK)
  1258. goto bail;
  1259. if (!preauth) {
  1260. #if HAVE_LIBSSL
  1261. if (!srvc->use_imaps && (srvc->use_sslv2 || srvc->use_sslv3 || srvc->use_tlsv1)) {
  1262. /* always try to select SSL support if available */
  1263. if (CAP(STARTTLS)) {
  1264. if (imap_exec( ctx, 0, "STARTTLS" ) != RESP_OK)
  1265. goto bail;
  1266. if (start_tls( ctx ))
  1267. goto bail;
  1268. use_ssl = 1;
  1269. if (imap_exec( ctx, 0, "CAPABILITY" ) != RESP_OK)
  1270. goto bail;
  1271. } else {
  1272. if (srvc->require_ssl) {
  1273. error( "IMAP error: SSL support not available\n" );
  1274. goto bail;
  1275. } else
  1276. warn( "IMAP warning: SSL support not available\n" );
  1277. }
  1278. }
  1279. #endif
  1280. info ("Logging in...\n");
  1281. if (!srvc->user) {
  1282. error( "Skipping account %s, no user\n", srvc->name );
  1283. goto bail;
  1284. }
  1285. if (!srvc->pass) {
  1286. char prompt[80];
  1287. sprintf( prompt, "Password (%s): ", srvc->name );
  1288. arg = getpass( prompt );
  1289. if (!arg) {
  1290. perror( "getpass" );
  1291. exit( 1 );
  1292. }
  1293. if (!*arg) {
  1294. error( "Skipping account %s, no password\n", srvc->name );
  1295. goto bail;
  1296. }
  1297. /*
  1298. * getpass() returns a pointer to a static buffer. make a copy
  1299. * for long term storage.
  1300. */
  1301. srvc->pass = nfstrdup( arg );
  1302. }
  1303. #if HAVE_LIBSSL
  1304. if (CAP(CRAM)) {
  1305. struct imap_cmd *cmd = new_imap_cmd();
  1306. info( "Authenticating with CRAM-MD5\n" );
  1307. cmd->param.cont = do_cram_auth;
  1308. if (imap_exec( ctx, cmd, "AUTHENTICATE CRAM-MD5" ) != RESP_OK)
  1309. goto bail;
  1310. } else if (srvc->require_cram) {
  1311. error( "IMAP error: CRAM-MD5 authentication is not supported by server\n" );
  1312. goto bail;
  1313. } else
  1314. #endif
  1315. {
  1316. if (CAP(NOLOGIN)) {
  1317. error( "Skipping account %s, server forbids LOGIN\n", srvc->name );
  1318. goto bail;
  1319. }
  1320. #if HAVE_LIBSSL
  1321. if (!use_ssl)
  1322. #endif
  1323. warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
  1324. if (imap_exec( ctx, 0, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass ) != RESP_OK) {
  1325. error( "IMAP error: LOGIN failed\n" );
  1326. goto bail;
  1327. }
  1328. }
  1329. } /* !preauth */
  1330. final:
  1331. ctx->prefix = "";
  1332. if (*conf->path)
  1333. ctx->prefix = conf->path;
  1334. else if (cfg->use_namespace && CAP(NAMESPACE)) {
  1335. /* get NAMESPACE info */
  1336. if (!ctx->got_namespace) {
  1337. if (imap_exec( ctx, 0, "NAMESPACE" ) != RESP_OK) {
  1338. cb( 0, aux );
  1339. return;
  1340. }
  1341. ctx->got_namespace = 1;
  1342. }
  1343. /* XXX for now assume personal namespace */
  1344. if (is_list( ctx->ns_personal ) &&
  1345. is_list( ctx->ns_personal->child ) &&
  1346. is_atom( ctx->ns_personal->child->child ))
  1347. ctx->prefix = ctx->ns_personal->child->child->val;
  1348. }
  1349. ctx->trashnc = 1;
  1350. cb( &ctx->gen, aux );
  1351. return;
  1352. bail:
  1353. imap_cancel_store( &ctx->gen );
  1354. cb( 0, aux );
  1355. }
  1356. static void
  1357. imap_prepare_paths( store_t *gctx )
  1358. {
  1359. free_generic_messages( gctx->msgs );
  1360. gctx->msgs = 0;
  1361. }
  1362. static void
  1363. imap_prepare_opts( store_t *gctx, int opts )
  1364. {
  1365. gctx->opts = opts;
  1366. }
  1367. static int
  1368. imap_select( store_t *gctx, int minuid, int maxuid, int *excs, int nexcs,
  1369. int (*cb)( int sts, void *aux ), void *aux )
  1370. {
  1371. imap_store_t *ctx = (imap_store_t *)gctx;
  1372. struct imap_cmd *cmd = new_imap_cmd();
  1373. const char *prefix;
  1374. int ret, i, j, bl;
  1375. char buf[1000];
  1376. if (!strcmp( gctx->name, "INBOX" )) {
  1377. // ctx->currentnc = 0;
  1378. prefix = "";
  1379. } else {
  1380. // ctx->currentnc = 1; /* could use LIST results for that */
  1381. prefix = ctx->prefix;
  1382. }
  1383. cmd->param.create = (gctx->opts & OPEN_CREATE) != 0;
  1384. cmd->param.trycreate = 1;
  1385. if ((ret = imap_exec_b( ctx, cmd, "SELECT \"%s%s\"", prefix, gctx->name )) != DRV_OK)
  1386. goto bail;
  1387. if (gctx->count) {
  1388. ctx->msgapp = &gctx->msgs;
  1389. sort_ints( excs, nexcs );
  1390. for (i = 0; i < nexcs; ) {
  1391. for (bl = 0; i < nexcs && bl < 960; i++) {
  1392. if (bl)
  1393. buf[bl++] = ',';
  1394. bl += sprintf( buf + bl, "%d", excs[i] );
  1395. j = i;
  1396. for (; i + 1 < nexcs && excs[i + 1] == excs[i] + 1; i++);
  1397. if (i != j)
  1398. bl += sprintf( buf + bl, ":%d", excs[i] );
  1399. }
  1400. if ((ret = imap_exec_b( ctx, 0, "UID FETCH %s (UID%s%s)", buf,
  1401. (gctx->opts & OPEN_FLAGS) ? " FLAGS" : "",
  1402. (gctx->opts & OPEN_SIZE) ? " RFC822.SIZE" : "" )) != DRV_OK)
  1403. goto bail;
  1404. }
  1405. if (maxuid == INT_MAX)
  1406. maxuid = ctx->uidnext ? ctx->uidnext - 1 : 1000000000;
  1407. if (maxuid >= minuid &&
  1408. (ret = imap_exec_b( ctx, 0, "UID FETCH %d:%d (UID%s%s)", minuid, maxuid,
  1409. (gctx->opts & OPEN_FLAGS) ? " FLAGS" : "",
  1410. (gctx->opts & OPEN_SIZE) ? " RFC822.SIZE" : "" )) != DRV_OK)
  1411. goto bail;
  1412. }
  1413. ret = DRV_OK;
  1414. bail:
  1415. if (excs)
  1416. free( excs );
  1417. return cb( ret, aux );
  1418. }
  1419. static int
  1420. imap_fetch_msg( store_t *ctx, message_t *msg, msg_data_t *data,
  1421. int (*cb)( int sts, void *aux ), void *aux )
  1422. {
  1423. struct imap_cmd *cmd = new_imap_cmd();
  1424. cmd->param.uid = msg->uid;
  1425. cmd->param.aux = data;
  1426. return cb( imap_exec_m( (imap_store_t *)ctx, cmd, "UID FETCH %d (%sBODY.PEEK[])",
  1427. msg->uid, (msg->status & M_FLAGS) ? "" : "FLAGS " ), aux );
  1428. }
  1429. static int
  1430. imap_make_flags( int flags, char *buf )
  1431. {
  1432. const char *s;
  1433. unsigned i, d;
  1434. for (i = d = 0; i < as(Flags); i++)
  1435. if (flags & (1 << i)) {
  1436. buf[d++] = ' ';
  1437. buf[d++] = '\\';
  1438. for (s = Flags[i]; *s; s++)
  1439. buf[d++] = *s;
  1440. }
  1441. buf[0] = '(';
  1442. buf[d++] = ')';
  1443. return d;
  1444. }
  1445. static int
  1446. imap_flags_helper( imap_store_t *ctx, int uid, char what, int flags)
  1447. {
  1448. char buf[256];
  1449. buf[imap_make_flags( flags, buf )] = 0;
  1450. if (!submit_imap_cmd( ctx, 0, "UID STORE %d %cFLAGS.SILENT %s", uid, what, buf ))
  1451. return DRV_STORE_BAD;
  1452. process_imap_replies( ctx );
  1453. return DRV_OK;
  1454. }
  1455. static int
  1456. imap_set_flags( store_t *gctx, message_t *msg, int uid, int add, int del,
  1457. int (*cb)( int sts, void *aux ), void *aux )
  1458. {
  1459. imap_store_t *ctx = (imap_store_t *)gctx;
  1460. int ret;
  1461. if (msg) {
  1462. uid = msg->uid;
  1463. add &= ~msg->flags;
  1464. del &= msg->flags;
  1465. msg->flags |= add;
  1466. msg->flags &= ~del;
  1467. }
  1468. if ((!add || (ret = imap_flags_helper( ctx, uid, '+', add )) == DRV_OK) &&
  1469. (!del || (ret = imap_flags_helper( ctx, uid, '-', del )) == DRV_OK))
  1470. ret = DRV_OK;
  1471. return cb( ret, aux );
  1472. }
  1473. static int
  1474. imap_close( store_t *ctx,
  1475. int (*cb)( int sts, void *aux ), void *aux )
  1476. {
  1477. return cb( imap_exec_b( (imap_store_t *)ctx, 0, "CLOSE" ), aux );
  1478. }
  1479. static int
  1480. imap_trash_msg( store_t *gctx, message_t *msg,
  1481. int (*cb)( int sts, void *aux ), void *aux )
  1482. {
  1483. imap_store_t *ctx = (imap_store_t *)gctx;
  1484. struct imap_cmd *cmd = new_imap_cmd();
  1485. cmd->param.create = 1;
  1486. return cb( imap_exec_m( ctx, cmd, "UID COPY %d \"%s%s\"",
  1487. msg->uid, ctx->prefix, gctx->conf->trash ), aux );
  1488. }
  1489. static int
  1490. imap_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1491. int (*cb)( int sts, int uid, void *aux ), void *aux )
  1492. {
  1493. imap_store_t *ctx = (imap_store_t *)gctx;
  1494. struct imap_cmd *cmd = new_imap_cmd();
  1495. const char *prefix, *box;
  1496. int ret, d, uid;
  1497. char flagstr[128];
  1498. d = 0;
  1499. if (data->flags) {
  1500. d = imap_make_flags( data->flags, flagstr );
  1501. flagstr[d++] = ' ';
  1502. }
  1503. flagstr[d] = 0;
  1504. cmd->param.data_len = data->len;
  1505. cmd->param.data = data->data;
  1506. cmd->param.aux = &uid;
  1507. uid = -2;
  1508. if (to_trash) {
  1509. box = gctx->conf->trash;
  1510. prefix = ctx->prefix;
  1511. cmd->param.create = 1;
  1512. if (ctx->trashnc)
  1513. ctx->caps = ctx->rcaps & ~(1 << LITERALPLUS);
  1514. } else {
  1515. box = gctx->name;
  1516. prefix = !strcmp( box, "INBOX" ) ? "" : ctx->prefix;
  1517. cmd->param.create = (gctx->opts & OPEN_CREATE) != 0;
  1518. /*if (ctx->currentnc)
  1519. ctx->caps = ctx->rcaps & ~(1 << LITERALPLUS);*/
  1520. }
  1521. ret = imap_exec_m( ctx, cmd, "APPEND \"%s%s\" %s", prefix, box, flagstr );
  1522. ctx->caps = ctx->rcaps;
  1523. if (ret != DRV_OK)
  1524. return cb( ret, -1, aux );
  1525. if (to_trash)
  1526. ctx->trashnc = 0;
  1527. else {
  1528. /*ctx->currentnc = 0;*/
  1529. }
  1530. return cb( DRV_OK, uid, aux );
  1531. }
  1532. static int
  1533. imap_find_msg( store_t *gctx, const char *tuid,
  1534. int (*cb)( int sts, int uid, void *aux ), void *aux )
  1535. {
  1536. imap_store_t *ctx = (imap_store_t *)gctx;
  1537. struct imap_cmd *cmd = new_imap_cmd();
  1538. int ret, uid;
  1539. cmd->param.uid = -1; /* we're looking for a UID */
  1540. cmd->param.aux = &uid;
  1541. uid = -1; /* in case we get no SEARCH response at all */
  1542. if ((ret = imap_exec_m( ctx, cmd, "UID SEARCH HEADER X-TUID %." stringify(TUIDL) "s", tuid )) != DRV_OK)
  1543. return cb( ret, -1, aux );
  1544. else
  1545. return cb( uid <= 0 ? DRV_MSG_BAD : DRV_OK, uid, aux );
  1546. }
  1547. static void
  1548. imap_list( store_t *gctx,
  1549. void (*cb)( int sts, void *aux ), void *aux )
  1550. {
  1551. imap_store_t *ctx = (imap_store_t *)gctx;
  1552. int ret;
  1553. if ((ret = imap_exec_b( ctx, 0, "LIST \"\" \"%s%%\"", ctx->prefix )) == DRV_OK)
  1554. gctx->listed = 1;
  1555. cb( ret, aux );
  1556. }
  1557. static void
  1558. imap_cancel( store_t *gctx,
  1559. void (*cb)( int sts, void *aux ), void *aux )
  1560. {
  1561. (void)gctx;
  1562. cb( DRV_OK, aux );
  1563. }
  1564. static void
  1565. imap_commit( store_t *gctx )
  1566. {
  1567. (void)gctx;
  1568. }
  1569. imap_server_conf_t *servers, **serverapp = &servers;
  1570. static int
  1571. imap_parse_store( conffile_t *cfg, store_conf_t **storep, int *err )
  1572. {
  1573. imap_store_conf_t *store;
  1574. imap_server_conf_t *server, *srv, sserver;
  1575. int acc_opt = 0;
  1576. if (!strcasecmp( "IMAPAccount", cfg->cmd )) {
  1577. server = nfcalloc( sizeof(*server) );
  1578. server->name = nfstrdup( cfg->val );
  1579. *serverapp = server;
  1580. serverapp = &server->next;
  1581. store = 0;
  1582. *storep = 0;
  1583. } else if (!strcasecmp( "IMAPStore", cfg->cmd )) {
  1584. store = nfcalloc( sizeof(*store) );
  1585. store->gen.driver = &imap_driver;
  1586. store->gen.name = nfstrdup( cfg->val );
  1587. store->use_namespace = 1;
  1588. *storep = &store->gen;
  1589. memset( &sserver, 0, sizeof(sserver) );
  1590. server = &sserver;
  1591. } else
  1592. return 0;
  1593. #if HAVE_LIBSSL
  1594. /* this will probably annoy people, but its the best default just in
  1595. * case people forget to turn it on
  1596. */
  1597. server->require_ssl = 1;
  1598. server->use_tlsv1 = 1;
  1599. #endif
  1600. while (getcline( cfg ) && cfg->cmd) {
  1601. if (!strcasecmp( "Host", cfg->cmd )) {
  1602. /* The imap[s]: syntax is just a backwards compat hack. */
  1603. #if HAVE_LIBSSL
  1604. if (!memcmp( "imaps:", cfg->val, 6 )) {
  1605. cfg->val += 6;
  1606. server->use_imaps = 1;
  1607. server->use_sslv2 = 1;
  1608. server->use_sslv3 = 1;
  1609. } else
  1610. #endif
  1611. {
  1612. if (!memcmp( "imap:", cfg->val, 5 ))
  1613. cfg->val += 5;
  1614. }
  1615. if (!memcmp( "//", cfg->val, 2 ))
  1616. cfg->val += 2;
  1617. server->host = nfstrdup( cfg->val );
  1618. }
  1619. else if (!strcasecmp( "User", cfg->cmd ))
  1620. server->user = nfstrdup( cfg->val );
  1621. else if (!strcasecmp( "Pass", cfg->cmd ))
  1622. server->pass = nfstrdup( cfg->val );
  1623. else if (!strcasecmp( "Port", cfg->cmd ))
  1624. server->port = parse_int( cfg );
  1625. #if HAVE_LIBSSL
  1626. else if (!strcasecmp( "CertificateFile", cfg->cmd )) {
  1627. server->cert_file = expand_strdup( cfg->val );
  1628. if (access( server->cert_file, R_OK )) {
  1629. error( "%s:%d: CertificateFile '%s': %s\n",
  1630. cfg->file, cfg->line, server->cert_file, strerror( errno ) );
  1631. *err = 1;
  1632. }
  1633. } else if (!strcasecmp( "RequireSSL", cfg->cmd ))
  1634. server->require_ssl = parse_bool( cfg );
  1635. else if (!strcasecmp( "UseIMAPS", cfg->cmd ))
  1636. server->use_imaps = parse_bool( cfg );
  1637. else if (!strcasecmp( "UseSSLv2", cfg->cmd ))
  1638. server->use_sslv2 = parse_bool( cfg );
  1639. else if (!strcasecmp( "UseSSLv3", cfg->cmd ))
  1640. server->use_sslv3 = parse_bool( cfg );
  1641. else if (!strcasecmp( "UseTLSv1", cfg->cmd ))
  1642. server->use_tlsv1 = parse_bool( cfg );
  1643. else if (!strcasecmp( "RequireCRAM", cfg->cmd ))
  1644. server->require_cram = parse_bool( cfg );
  1645. #endif
  1646. else if (!strcasecmp( "Tunnel", cfg->cmd ))
  1647. server->tunnel = nfstrdup( cfg->val );
  1648. else if (store) {
  1649. if (!strcasecmp( "Account", cfg->cmd )) {
  1650. for (srv = servers; srv; srv = srv->next)
  1651. if (srv->name && !strcmp( srv->name, cfg->val ))
  1652. goto gotsrv;
  1653. error( "%s:%d: unknown IMAP account '%s'\n", cfg->file, cfg->line, cfg->val );
  1654. *err = 1;
  1655. continue;
  1656. gotsrv:
  1657. store->server = srv;
  1658. } else if (!strcasecmp( "UseNamespace", cfg->cmd ))
  1659. store->use_namespace = parse_bool( cfg );
  1660. else if (!strcasecmp( "Path", cfg->cmd ))
  1661. store->gen.path = nfstrdup( cfg->val );
  1662. else
  1663. parse_generic_store( &store->gen, cfg, err );
  1664. continue;
  1665. } else {
  1666. error( "%s:%d: unknown/misplaced keyword '%s'\n", cfg->file, cfg->line, cfg->cmd );
  1667. *err = 1;
  1668. continue;
  1669. }
  1670. acc_opt = 1;
  1671. }
  1672. if (!store || !store->server) {
  1673. if (!server->tunnel && !server->host) {
  1674. if (store)
  1675. error( "IMAP store '%s' has incomplete/missing connection details\n", store->gen.name );
  1676. else
  1677. error( "IMAP account '%s' has incomplete/missing connection details\n", server->name );
  1678. *err = 1;
  1679. return 1;
  1680. }
  1681. }
  1682. if (store) {
  1683. if (!store->server) {
  1684. store->server = nfmalloc( sizeof(sserver) );
  1685. memcpy( store->server, &sserver, sizeof(sserver) );
  1686. store->server->name = store->gen.name;
  1687. } else if (acc_opt) {
  1688. error( "IMAP store '%s' has both Account and account-specific options\n", store->gen.name );
  1689. *err = 1;
  1690. }
  1691. }
  1692. return 1;
  1693. }
  1694. struct driver imap_driver = {
  1695. DRV_CRLF,
  1696. imap_parse_store,
  1697. imap_cleanup,
  1698. imap_open_store,
  1699. imap_disown_store,
  1700. imap_own_store,
  1701. imap_cancel_store,
  1702. imap_list,
  1703. imap_prepare_paths,
  1704. imap_prepare_opts,
  1705. imap_select,
  1706. imap_fetch_msg,
  1707. imap_store_msg,
  1708. imap_find_msg,
  1709. imap_set_flags,
  1710. imap_trash_msg,
  1711. imap_close,
  1712. imap_cancel,
  1713. imap_commit,
  1714. };