drv_maildir.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2010-2013 Oswald Buddenhagen <ossi@users.sf.net>
  5. * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * As a special exception, mbsync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #include "driver.h"
  24. #include <assert.h>
  25. #include <limits.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <dirent.h>
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include <sys/stat.h>
  34. #include <sys/file.h>
  35. #include <errno.h>
  36. #include <time.h>
  37. #include <utime.h>
  38. #define USE_DB 1
  39. #ifdef __linux__
  40. # define LEGACY_FLOCK 1
  41. #endif
  42. #if !defined(_POSIX_SYNCHRONIZED_IO) || _POSIX_SYNCHRONIZED_IO <= 0
  43. # define fdatasync fsync
  44. #endif
  45. #ifdef USE_DB
  46. #include <db.h>
  47. #endif /* USE_DB */
  48. typedef struct maildir_store_conf {
  49. store_conf_t gen;
  50. char *inbox;
  51. #ifdef USE_DB
  52. int alt_map;
  53. #endif /* USE_DB */
  54. char info_delimiter;
  55. char *info_prefix, *info_stop; /* precalculated from info_delimiter */
  56. } maildir_store_conf_t;
  57. typedef struct maildir_message {
  58. message_t gen;
  59. char *base;
  60. } maildir_message_t;
  61. typedef struct maildir_store {
  62. store_t gen;
  63. int uvfd, uvok, nuid, is_inbox, fresh[3];
  64. int minuid, maxuid, newuid, nexcs, *excs;
  65. char *trash;
  66. #ifdef USE_DB
  67. DB *db;
  68. char *usedb;
  69. #endif /* USE_DB */
  70. wakeup_t lcktmr;
  71. } maildir_store_t;
  72. #ifdef USE_DB
  73. static DBT key, value; /* no need to be reentrant, and this saves lots of memset()s */
  74. #endif /* USE_DB */
  75. static struct flock lck;
  76. static int MaildirCount;
  77. static void ATTR_PRINTFLIKE(1, 2)
  78. debug( const char *msg, ... )
  79. {
  80. va_list va;
  81. va_start( va, msg );
  82. vdebug( DEBUG_SYNC, msg, va );
  83. va_end( va );
  84. }
  85. static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
  86. static uchar
  87. maildir_parse_flags( const char *info_prefix, const char *base )
  88. {
  89. const char *s;
  90. uint i;
  91. uchar flags;
  92. flags = 0;
  93. if ((s = strstr( base, info_prefix )))
  94. for (s += 3, i = 0; i < as(Flags); i++)
  95. if (strchr( s, Flags[i] ))
  96. flags |= (1 << i);
  97. return flags;
  98. }
  99. static char *
  100. maildir_join_path( const char *prefix, const char *box )
  101. {
  102. char *out, *p;
  103. int pl, bl, n;
  104. char c;
  105. pl = strlen( prefix );
  106. for (bl = 0, n = 0; (c = box[bl]); bl++)
  107. if (c == '/')
  108. n++;
  109. out = nfmalloc( pl + bl + n + 1 );
  110. memcpy( out, prefix, pl );
  111. p = out + pl;
  112. while ((c = *box++)) {
  113. *p++ = c;
  114. if (c == '/')
  115. *p++ = '.';
  116. }
  117. *p = 0;
  118. return out;
  119. }
  120. static int
  121. maildir_validate_path( store_conf_t *conf )
  122. {
  123. struct stat st;
  124. if (!conf->path) {
  125. error( "Maildir error: store '%s' has no Path\n", conf->name );
  126. conf->failed = FAIL_FINAL;
  127. return -1;
  128. }
  129. if (stat( conf->path, &st ) || !S_ISDIR(st.st_mode)) {
  130. error( "Maildir error: cannot open store '%s'\n", conf->path );
  131. conf->failed = FAIL_FINAL;
  132. return -1;
  133. }
  134. return 0;
  135. }
  136. static void lcktmr_timeout( void *aux );
  137. static void
  138. maildir_open_store( store_conf_t *conf, const char *label ATTR_UNUSED,
  139. void (*cb)( store_t *ctx, void *aux ), void *aux )
  140. {
  141. maildir_store_t *ctx;
  142. ctx = nfcalloc( sizeof(*ctx) );
  143. ctx->gen.conf = conf;
  144. ctx->uvfd = -1;
  145. init_wakeup( &ctx->lcktmr, lcktmr_timeout, ctx );
  146. if (conf->trash) {
  147. if (maildir_validate_path( conf ) < 0) {
  148. free( ctx );
  149. cb( 0, aux );
  150. return;
  151. }
  152. ctx->trash = maildir_join_path( conf->path, conf->trash );
  153. }
  154. cb( &ctx->gen, aux );
  155. }
  156. static void
  157. free_maildir_messages( message_t *msg )
  158. {
  159. message_t *tmsg;
  160. for (; (tmsg = msg); msg = tmsg) {
  161. tmsg = msg->next;
  162. free( ((maildir_message_t *)msg)->base );
  163. free( msg );
  164. }
  165. }
  166. static void
  167. maildir_cleanup( store_t *gctx )
  168. {
  169. maildir_store_t *ctx = (maildir_store_t *)gctx;
  170. free_maildir_messages( gctx->msgs );
  171. #ifdef USE_DB
  172. if (ctx->db)
  173. ctx->db->close( ctx->db, 0 );
  174. free( ctx->usedb );
  175. #endif /* USE_DB */
  176. free( gctx->path );
  177. free( ctx->excs );
  178. if (ctx->uvfd >= 0)
  179. close( ctx->uvfd );
  180. conf_wakeup( &ctx->lcktmr, -1 );
  181. }
  182. static void
  183. maildir_disown_store( store_t *gctx )
  184. {
  185. maildir_store_t *ctx = (maildir_store_t *)gctx;
  186. maildir_cleanup( gctx );
  187. wipe_wakeup( &ctx->lcktmr );
  188. free( ctx->trash );
  189. free_string_list( gctx->boxes );
  190. free( gctx );
  191. }
  192. static void
  193. maildir_cleanup_drv( void )
  194. {
  195. }
  196. static void
  197. maildir_invoke_bad_callback( store_t *ctx )
  198. {
  199. ctx->bad_callback( ctx->bad_callback_aux );
  200. }
  201. static int maildir_list_inbox( store_t *gctx, int *flags );
  202. static int
  203. maildir_list_recurse( store_t *gctx, int isBox, int *flags, const char *inbox, int inboxLen,
  204. char *path, int pathLen, char *name, int nameLen )
  205. {
  206. DIR *dir;
  207. int pl, nl;
  208. struct dirent *de;
  209. struct stat st;
  210. if (isBox) {
  211. path[pathLen++] = '/';
  212. if (isBox > 1 ||
  213. (nfsnprintf( path + pathLen, _POSIX_PATH_MAX - pathLen, "cur" ),
  214. !stat( path, &st ) && S_ISDIR(st.st_mode)))
  215. add_string_list( &gctx->boxes, name );
  216. path[pathLen] = 0;
  217. name[nameLen++] = '/';
  218. }
  219. if (!(dir = opendir( path ))) {
  220. if (isBox && (errno == ENOENT || errno == ENOTDIR))
  221. return 0;
  222. sys_error( "Maildir error: cannot list %s", path );
  223. return -1;
  224. }
  225. while ((de = readdir( dir ))) {
  226. const char *ent = de->d_name;
  227. pl = pathLen + nfsnprintf( path + pathLen, _POSIX_PATH_MAX - pathLen, "%s", ent );
  228. if (inbox && equals( path, pl, inbox, inboxLen )) {
  229. if (maildir_list_inbox( gctx, flags ) < 0) {
  230. closedir( dir );
  231. return -1;
  232. }
  233. } else {
  234. if (*ent == '.') {
  235. if (!isBox)
  236. continue;
  237. if (!ent[1] || ent[1] == '.')
  238. continue;
  239. ent++;
  240. } else {
  241. if (isBox)
  242. continue;
  243. if (!nameLen && equals( ent, -1, "INBOX", 5 )) {
  244. path[pathLen] = 0;
  245. warn( "Maildir warning: ignoring INBOX in %s\n", path );
  246. continue;
  247. }
  248. }
  249. nl = nameLen + nfsnprintf( name + nameLen, _POSIX_PATH_MAX - nameLen, "%s", ent );
  250. if (maildir_list_recurse( gctx, 1, flags, inbox, inboxLen, path, pl, name, nl ) < 0) {
  251. closedir( dir );
  252. return -1;
  253. }
  254. }
  255. }
  256. closedir (dir);
  257. return 0;
  258. }
  259. static int
  260. maildir_list_inbox( store_t *gctx, int *flags )
  261. {
  262. char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
  263. *flags &= ~LIST_INBOX;
  264. return maildir_list_recurse(
  265. gctx, 2, flags, 0, 0,
  266. path, nfsnprintf( path, _POSIX_PATH_MAX, "%s", ((maildir_store_conf_t *)gctx->conf)->inbox ),
  267. name, nfsnprintf( name, _POSIX_PATH_MAX, "INBOX" ) );
  268. }
  269. static int
  270. maildir_list_path( store_t *gctx, int *flags )
  271. {
  272. const char *inbox = ((maildir_store_conf_t *)gctx->conf)->inbox;
  273. char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
  274. if (maildir_validate_path( gctx->conf ) < 0)
  275. return -1;
  276. return maildir_list_recurse(
  277. gctx, 0, flags, inbox, strlen( inbox ),
  278. path, nfsnprintf( path, _POSIX_PATH_MAX, "%s", gctx->conf->path ),
  279. name, 0 );
  280. }
  281. static void
  282. maildir_list_store( store_t *gctx, int flags,
  283. void (*cb)( int sts, void *aux ), void *aux )
  284. {
  285. if (((flags & LIST_PATH) && maildir_list_path( gctx, &flags ) < 0) ||
  286. ((flags & LIST_INBOX) && maildir_list_inbox( gctx, &flags ) < 0)) {
  287. maildir_invoke_bad_callback( gctx );
  288. cb( DRV_CANCELED, aux );
  289. } else {
  290. cb( DRV_OK, aux );
  291. }
  292. }
  293. static const char *subdirs[] = { "cur", "new", "tmp" };
  294. typedef struct {
  295. char *base;
  296. int size;
  297. uint uid:31, recent:1;
  298. char tuid[TUIDL];
  299. } msg_t;
  300. typedef struct {
  301. msg_t *ents;
  302. int nents, nalloc;
  303. } msglist_t;
  304. static void
  305. maildir_free_scan( msglist_t *msglist )
  306. {
  307. int i;
  308. if (msglist->ents) {
  309. for (i = 0; i < msglist->nents; i++)
  310. free( msglist->ents[i].base );
  311. free( msglist->ents );
  312. }
  313. }
  314. #define _24_HOURS (3600 * 24)
  315. static int
  316. maildir_clear_tmp( char *buf, int bufsz, int bl )
  317. {
  318. DIR *dirp;
  319. struct dirent *entry;
  320. time_t now;
  321. struct stat st;
  322. memcpy( buf + bl, "tmp/", 5 );
  323. bl += 4;
  324. if (!(dirp = opendir( buf ))) {
  325. sys_error( "Maildir error: cannot list %s", buf );
  326. return DRV_BOX_BAD;
  327. }
  328. time( &now );
  329. while ((entry = readdir( dirp ))) {
  330. nfsnprintf( buf + bl, bufsz - bl, "%s", entry->d_name );
  331. if (stat( buf, &st )) {
  332. if (errno != ENOENT)
  333. sys_error( "Maildir error: cannot access %s", buf );
  334. } else if (S_ISREG(st.st_mode) && now - st.st_ctime >= _24_HOURS) {
  335. /* This should happen infrequently enough that it won't be
  336. * bothersome to the user to display when it occurs.
  337. */
  338. notice( "Maildir notice: removing stale file %s\n", buf );
  339. if (unlink( buf ) && errno != ENOENT)
  340. sys_error( "Maildir error: cannot remove %s", buf );
  341. }
  342. }
  343. closedir( dirp );
  344. return DRV_OK;
  345. }
  346. static int
  347. make_box_dir( char *buf, int bl )
  348. {
  349. char *p;
  350. if (!mkdir( buf, 0700 ) || errno == EEXIST)
  351. return 0;
  352. p = memrchr( buf, '/', bl - 1 );
  353. if (*(p + 1) != '.') {
  354. errno = ENOENT;
  355. return -1;
  356. }
  357. *p = 0;
  358. if (make_box_dir( buf, (int)(p - buf) ))
  359. return -1;
  360. *p = '/';
  361. return mkdir( buf, 0700 );
  362. }
  363. static int
  364. maildir_validate( const char *box, int create, maildir_store_t *ctx )
  365. {
  366. int i, bl, ret;
  367. struct stat st;
  368. char buf[_POSIX_PATH_MAX];
  369. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", box );
  370. if (stat( buf, &st )) {
  371. if (errno != ENOENT) {
  372. sys_error( "Maildir error: cannot access mailbox '%s'", box );
  373. return DRV_BOX_BAD;
  374. }
  375. if (!create)
  376. return DRV_BOX_BAD;
  377. if (make_box_dir( buf, bl )) {
  378. sys_error( "Maildir error: cannot create mailbox '%s'", box );
  379. ctx->gen.conf->failed = FAIL_FINAL;
  380. maildir_invoke_bad_callback( &ctx->gen );
  381. return DRV_CANCELED;
  382. }
  383. } else if (!S_ISDIR(st.st_mode)) {
  384. notdir:
  385. error( "Maildir error: '%s' is no valid mailbox\n", box );
  386. return DRV_BOX_BAD;
  387. }
  388. for (i = 0; i < 3; i++) {
  389. memcpy( buf + bl, subdirs[i], 4 );
  390. if (stat( buf, &st )) {
  391. /* We always create new/ and tmp/ if they are missing. cur/ is the presence indicator. */
  392. if (!i && !create)
  393. return DRV_BOX_BAD;
  394. if (mkdir( buf, 0700 )) {
  395. sys_error( "Maildir error: cannot create directory %s", buf );
  396. return DRV_BOX_BAD;
  397. }
  398. ctx->fresh[i] = 1;
  399. } else if (!S_ISDIR(st.st_mode)) {
  400. goto notdir;
  401. } else {
  402. if (i == 2) {
  403. if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) != DRV_OK)
  404. return ret;
  405. }
  406. }
  407. }
  408. return DRV_OK;
  409. }
  410. #ifdef USE_DB
  411. static void
  412. make_key( const char *info_stop, DBT *tkey, char *name )
  413. {
  414. char *u = strpbrk( name, info_stop );
  415. tkey->data = name;
  416. tkey->size = u ? (size_t)(u - name) : strlen( name );
  417. }
  418. #endif /* USE_DB */
  419. static int
  420. maildir_store_uidval( maildir_store_t *ctx )
  421. {
  422. int n;
  423. #ifdef USE_DB
  424. int ret, uv[2];
  425. #endif
  426. char buf[128];
  427. #ifdef USE_DB
  428. if (ctx->db) {
  429. key.data = (void *)"UIDVALIDITY";
  430. key.size = 11;
  431. uv[0] = ctx->gen.uidvalidity;
  432. uv[1] = ctx->nuid;
  433. value.data = uv;
  434. value.size = sizeof(uv);
  435. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
  436. ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
  437. return DRV_BOX_BAD;
  438. }
  439. if ((ret = ctx->db->sync( ctx->db, 0 ))) {
  440. ctx->db->err( ctx->db, ret, "Maildir error: db->sync()" );
  441. return DRV_BOX_BAD;
  442. }
  443. } else
  444. #endif /* USE_DB */
  445. {
  446. n = sprintf( buf, "%d\n%d\n", ctx->gen.uidvalidity, ctx->nuid );
  447. lseek( ctx->uvfd, 0, SEEK_SET );
  448. if (write( ctx->uvfd, buf, n ) != n || ftruncate( ctx->uvfd, n ) || (UseFSync && fdatasync( ctx->uvfd ))) {
  449. error( "Maildir error: cannot write UIDVALIDITY.\n" );
  450. return DRV_BOX_BAD;
  451. }
  452. }
  453. conf_wakeup( &ctx->lcktmr, 2 );
  454. return DRV_OK;
  455. }
  456. static int
  457. maildir_init_uidval( maildir_store_t *ctx )
  458. {
  459. ctx->gen.uidvalidity = time( 0 );
  460. ctx->nuid = 0;
  461. ctx->uvok = 0;
  462. #ifdef USE_DB
  463. if (ctx->db) {
  464. u_int32_t count;
  465. ctx->db->truncate( ctx->db, 0, &count, 0 );
  466. }
  467. #endif /* USE_DB */
  468. return maildir_store_uidval( ctx );
  469. }
  470. static int
  471. maildir_init_uidval_new( maildir_store_t *ctx )
  472. {
  473. notice( "Maildir notice: no UIDVALIDITY, creating new.\n" );
  474. return maildir_init_uidval( ctx );
  475. }
  476. static int
  477. maildir_uidval_lock( maildir_store_t *ctx )
  478. {
  479. int n;
  480. #ifdef USE_DB
  481. int ret;
  482. struct stat st;
  483. #endif
  484. char buf[128];
  485. if (pending_wakeup( &ctx->lcktmr )) {
  486. /* The unlock timer is active, so we are obviously already locked. */
  487. return DRV_OK;
  488. }
  489. #ifdef LEGACY_FLOCK
  490. /* This is legacy only */
  491. if (flock( ctx->uvfd, LOCK_EX ) < 0) {
  492. error( "Maildir error: cannot flock UIDVALIDITY.\n" );
  493. return DRV_BOX_BAD;
  494. }
  495. #endif
  496. /* This (theoretically) works over NFS. Let's hope nobody else did
  497. the same in the opposite order, as we'd deadlock then. */
  498. #if SEEK_SET != 0
  499. lck.l_whence = SEEK_SET;
  500. #endif
  501. lck.l_type = F_WRLCK;
  502. if (fcntl( ctx->uvfd, F_SETLKW, &lck )) {
  503. error( "Maildir error: cannot fcntl lock UIDVALIDITY.\n" );
  504. return DRV_BOX_BAD;
  505. }
  506. #ifdef USE_DB
  507. if (ctx->usedb) {
  508. if (fstat( ctx->uvfd, &st )) {
  509. sys_error( "Maildir error: cannot fstat UID database" );
  510. return DRV_BOX_BAD;
  511. }
  512. if (db_create( &ctx->db, 0, 0 )) {
  513. fputs( "Maildir error: db_create() failed\n", stderr );
  514. return DRV_BOX_BAD;
  515. }
  516. if ((ret = (ctx->db->open)( ctx->db, 0, ctx->usedb, 0, DB_HASH,
  517. st.st_size ? 0 : DB_CREATE | DB_TRUNCATE, 0 ))) {
  518. ctx->db->err( ctx->db, ret, "Maildir error: db->open(%s)", ctx->usedb );
  519. return DRV_BOX_BAD;
  520. }
  521. key.data = (void *)"UIDVALIDITY";
  522. key.size = 11;
  523. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  524. if (ret != DB_NOTFOUND) {
  525. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  526. return DRV_BOX_BAD;
  527. }
  528. return maildir_init_uidval_new( ctx );
  529. }
  530. ctx->gen.uidvalidity = ((int *)value.data)[0];
  531. ctx->nuid = ((int *)value.data)[1];
  532. } else
  533. #endif
  534. {
  535. lseek( ctx->uvfd, 0, SEEK_SET );
  536. if ((n = read( ctx->uvfd, buf, sizeof(buf) - 1 )) <= 0 ||
  537. (buf[n] = 0, sscanf( buf, "%d\n%d", &ctx->gen.uidvalidity, &ctx->nuid ) != 2)) {
  538. #if 1
  539. /* In a generic driver, resetting the UID validity would be the right thing.
  540. * But this would mess up the sync state completely. So better bail out and
  541. * give the user a chance to fix the mailbox. */
  542. if (n) {
  543. error( "Maildir error: cannot read UIDVALIDITY.\n" );
  544. return DRV_BOX_BAD;
  545. }
  546. #endif
  547. return maildir_init_uidval_new( ctx );
  548. }
  549. }
  550. ctx->uvok = 1;
  551. conf_wakeup( &ctx->lcktmr, 2 );
  552. return DRV_OK;
  553. }
  554. static void
  555. maildir_uidval_unlock( maildir_store_t *ctx )
  556. {
  557. #ifdef USE_DB
  558. if (ctx->db) {
  559. ctx->db->close( ctx->db, 0 );
  560. ctx->db = 0;
  561. }
  562. #endif /* USE_DB */
  563. lck.l_type = F_UNLCK;
  564. fcntl( ctx->uvfd, F_SETLK, &lck );
  565. #ifdef LEGACY_FLOCK
  566. /* This is legacy only */
  567. flock( ctx->uvfd, LOCK_UN );
  568. #endif
  569. }
  570. static void
  571. lcktmr_timeout( void *aux )
  572. {
  573. maildir_uidval_unlock( (maildir_store_t *)aux );
  574. }
  575. static int
  576. maildir_obtain_uid( maildir_store_t *ctx, int *uid )
  577. {
  578. int ret;
  579. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  580. return ret;
  581. *uid = ++ctx->nuid;
  582. return maildir_store_uidval( ctx );
  583. }
  584. #ifdef USE_DB
  585. static int
  586. maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid )
  587. {
  588. int ret;
  589. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  590. return ret;
  591. *uid = ++ctx->nuid;
  592. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  593. value.data = uid;
  594. value.size = sizeof(*uid);
  595. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
  596. ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
  597. return DRV_BOX_BAD;
  598. }
  599. return maildir_store_uidval( ctx );
  600. }
  601. #endif
  602. static int
  603. maildir_compare( const void *l, const void *r )
  604. {
  605. msg_t *lm = (msg_t *)l, *rm = (msg_t *)r;
  606. char *ldot, *rdot, *ldot2, *rdot2, *lseq, *rseq;
  607. int ret, llen, rlen;
  608. if ((ret = lm->uid - rm->uid))
  609. return ret;
  610. /* No UID, so sort by arrival date. We should not do this, but we rely
  611. on the suggested unique file name scheme - we have no choice. */
  612. /* The first field are always the seconds. Alphabetical sort should be
  613. faster than numeric. */
  614. if (!(ldot = strchr( lm->base, '.' )) || !(rdot = strchr( rm->base, '.' )))
  615. goto stronly; /* Should never happen ... */
  616. llen = ldot - lm->base, rlen = rdot - rm->base;
  617. /* The shorter number is smaller. Really. This won't trigger with any
  618. mail created after Sep 9 2001 anyway. */
  619. if ((ret = llen - rlen))
  620. return ret;
  621. if ((ret = memcmp( lm->base, rm->base, llen )))
  622. return ret;
  623. ldot++, rdot++;
  624. if ((llen = strtol( ldot, &ldot2, 10 ))) {
  625. if (!(rlen = strtol( rdot, &rdot2, 10 )))
  626. goto stronly; /* Comparing apples to oranges ... */
  627. /* Classical PID specs */
  628. if ((ret = llen - rlen)) {
  629. retpid:
  630. /* Handle PID wraparound. This works only on systems
  631. where PIDs are not reused too fast */
  632. if (ret > 20000 || ret < -20000)
  633. ret = -ret;
  634. return ret;
  635. }
  636. return (*ldot2 != '_' ? 0 : atoi( ldot2 + 1 )) -
  637. (*rdot2 != '_' ? 0 : atoi( rdot2 + 1 ));
  638. }
  639. if (!(ldot2 = strchr( ldot, '.' )) || !(rdot2 = strchr( rdot, '.' )))
  640. goto stronly; /* Should never happen ... */
  641. llen = ldot2 - ldot, rlen = rdot2 - rdot;
  642. if (((lseq = memchr( ldot, '#', llen )) && (rseq = memchr( rdot, '#', rlen ))) ||
  643. ((lseq = memchr( ldot, 'M', llen )) && (rseq = memchr( rdot, 'M', rlen ))))
  644. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  645. if ((lseq = memchr( ldot, 'P', llen )) && (rseq = memchr( rdot, 'P', rlen ))) {
  646. if ((ret = atoi( lseq + 1 ) - atoi( rseq + 1 )))
  647. goto retpid;
  648. if ((lseq = memchr( ldot, 'Q', llen )) && (rseq = memchr( rdot, 'Q', rlen )))
  649. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  650. }
  651. stronly:
  652. /* Fall-back, so the sort order is defined at all */
  653. return strcmp( lm->base, rm->base );
  654. }
  655. static int
  656. maildir_scan( maildir_store_t *ctx, msglist_t *msglist )
  657. {
  658. maildir_store_conf_t *conf = (maildir_store_conf_t *)ctx->gen.conf;
  659. DIR *d;
  660. FILE *f;
  661. struct dirent *e;
  662. const char *u, *ru;
  663. #ifdef USE_DB
  664. DB *tdb;
  665. DBC *dbc;
  666. #endif /* USE_DB */
  667. msg_t *entry;
  668. int i, j, uid, bl, fnl, ret;
  669. time_t now, stamps[2];
  670. struct stat st;
  671. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  672. again:
  673. msglist->ents = 0;
  674. msglist->nents = msglist->nalloc = 0;
  675. ctx->gen.count = ctx->gen.recent = 0;
  676. if (ctx->uvok || ctx->maxuid == INT_MAX) {
  677. #ifdef USE_DB
  678. if (ctx->usedb) {
  679. if (db_create( &tdb, 0, 0 )) {
  680. fputs( "Maildir error: db_create() failed\n", stderr );
  681. return DRV_BOX_BAD;
  682. }
  683. if ((tdb->open)( tdb, 0, 0, 0, DB_HASH, DB_CREATE, 0 )) {
  684. fputs( "Maildir error: tdb->open() failed\n", stderr );
  685. bork:
  686. tdb->close( tdb, 0 );
  687. return DRV_BOX_BAD;
  688. }
  689. }
  690. #endif /* USE_DB */
  691. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", ctx->gen.path );
  692. restat:
  693. now = time( 0 );
  694. for (i = 0; i < 2; i++) {
  695. memcpy( buf + bl, subdirs[i], 4 );
  696. if (stat( buf, &st )) {
  697. sys_error( "Maildir error: cannot stat %s", buf );
  698. goto dfail;
  699. }
  700. if (st.st_mtime == now && !(DFlags & ZERODELAY) && !ctx->fresh[i]) {
  701. /* If the modification happened during this second, we wouldn't be able to
  702. * tell if there were further modifications during this second. So wait.
  703. * This has the nice side effect that we wait for "batches" of changes to
  704. * complete. On the downside, it can potentially block indefinitely. */
  705. notice( "Maildir notice: sleeping due to recent directory modification.\n" );
  706. sleep( 1 ); /* FIXME: should make this async */
  707. goto restat;
  708. }
  709. stamps[i] = st.st_mtime;
  710. }
  711. for (i = 0; i < 2; i++) {
  712. memcpy( buf + bl, subdirs[i], 4 );
  713. if (!(d = opendir( buf ))) {
  714. sys_error( "Maildir error: cannot list %s", buf );
  715. rfail:
  716. maildir_free_scan( msglist );
  717. dfail:
  718. #ifdef USE_DB
  719. if (ctx->usedb)
  720. tdb->close( tdb, 0 );
  721. #endif /* USE_DB */
  722. return DRV_BOX_BAD;
  723. }
  724. while ((e = readdir( d ))) {
  725. if (*e->d_name == '.')
  726. continue;
  727. ctx->gen.count++;
  728. ctx->gen.recent += i;
  729. #ifdef USE_DB
  730. if (ctx->usedb) {
  731. if (maildir_uidval_lock( ctx ) != DRV_OK)
  732. goto mbork;
  733. make_key( conf->info_stop, &key, e->d_name );
  734. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  735. if (ret != DB_NOTFOUND) {
  736. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  737. mbork:
  738. maildir_free_scan( msglist );
  739. closedir( d );
  740. goto bork;
  741. }
  742. uid = INT_MAX;
  743. } else {
  744. value.size = 0;
  745. if ((ret = tdb->put( tdb, 0, &key, &value, 0 ))) {
  746. tdb->err( tdb, ret, "Maildir error: tdb->put()" );
  747. goto mbork;
  748. }
  749. uid = *(int *)value.data;
  750. }
  751. } else
  752. #endif /* USE_DB */
  753. {
  754. uid = (ctx->uvok && (u = strstr( e->d_name, ",U=" ))) ? atoi( u + 3 ) : 0;
  755. if (!uid)
  756. uid = INT_MAX;
  757. }
  758. if (uid <= ctx->maxuid) {
  759. if (uid < ctx->minuid) {
  760. for (j = 0; j < ctx->nexcs; j++)
  761. if (ctx->excs[j] == uid)
  762. goto oke;
  763. continue;
  764. oke: ;
  765. }
  766. if (msglist->nalloc == msglist->nents) {
  767. msglist->nalloc = msglist->nalloc * 2 + 100;
  768. msglist->ents = nfrealloc( msglist->ents, msglist->nalloc * sizeof(msg_t) );
  769. }
  770. entry = &msglist->ents[msglist->nents++];
  771. entry->base = nfstrdup( e->d_name );
  772. entry->uid = uid;
  773. entry->recent = i;
  774. entry->size = 0;
  775. entry->tuid[0] = 0;
  776. }
  777. }
  778. closedir( d );
  779. }
  780. for (i = 0; i < 2; i++) {
  781. memcpy( buf + bl, subdirs[i], 4 );
  782. if (stat( buf, &st )) {
  783. sys_error( "Maildir error: cannot re-stat %s", buf );
  784. goto rfail;
  785. }
  786. if (st.st_mtime != stamps[i]) {
  787. /* Somebody messed with the mailbox since we started listing it. */
  788. #ifdef USE_DB
  789. if (ctx->usedb)
  790. tdb->close( tdb, 0 );
  791. #endif /* USE_DB */
  792. maildir_free_scan( msglist );
  793. goto again;
  794. }
  795. }
  796. #ifdef USE_DB
  797. if (ctx->usedb) {
  798. if (maildir_uidval_lock( ctx ) != DRV_OK)
  799. ;
  800. else if ((ret = ctx->db->cursor( ctx->db, 0, &dbc, 0 )))
  801. ctx->db->err( ctx->db, ret, "Maildir error: db->cursor()" );
  802. else {
  803. for (;;) {
  804. if ((ret = dbc->c_get( dbc, &key, &value, DB_NEXT ))) {
  805. if (ret != DB_NOTFOUND)
  806. ctx->db->err( ctx->db, ret, "Maildir error: db->c_get()" );
  807. break;
  808. }
  809. if (!equals( key.data, key.size, "UIDVALIDITY", 11 ) &&
  810. (ret = tdb->get( tdb, 0, &key, &value, 0 ))) {
  811. if (ret != DB_NOTFOUND) {
  812. tdb->err( tdb, ret, "Maildir error: tdb->get()" );
  813. break;
  814. }
  815. if ((ret = dbc->c_del( dbc, 0 ))) {
  816. ctx->db->err( ctx->db, ret, "Maildir error: db->c_del()" );
  817. break;
  818. }
  819. }
  820. }
  821. dbc->c_close( dbc );
  822. }
  823. tdb->close( tdb, 0 );
  824. }
  825. #endif /* USE_DB */
  826. qsort( msglist->ents, msglist->nents, sizeof(msg_t), maildir_compare );
  827. for (uid = i = 0; i < msglist->nents; i++) {
  828. entry = &msglist->ents[i];
  829. if (entry->uid != INT_MAX) {
  830. if (uid == entry->uid) {
  831. #if 1
  832. /* See comment in maildir_uidval_lock() why this is fatal. */
  833. error( "Maildir error: duplicate UID %d.\n", uid );
  834. maildir_free_scan( msglist );
  835. return DRV_BOX_BAD;
  836. #else
  837. notice( "Maildir notice: duplicate UID; changing UIDVALIDITY.\n");
  838. if ((ret = maildir_init_uid( ctx )) != DRV_OK) {
  839. maildir_free_scan( msglist );
  840. return ret;
  841. }
  842. maildir_free_scan( msglist );
  843. goto again;
  844. #endif
  845. }
  846. uid = entry->uid;
  847. if (uid > ctx->nuid) {
  848. /* In principle, we could just warn and top up nuid. However, getting into this
  849. * situation might indicate some serious trouble, so let's not make it worse. */
  850. error( "Maildir error: UID %d is beyond highest assigned UID %d.\n", uid, ctx->nuid );
  851. maildir_free_scan( msglist );
  852. return DRV_BOX_BAD;
  853. }
  854. if ((ctx->gen.opts & OPEN_SIZE) || ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid))
  855. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  856. #ifdef USE_DB
  857. } else if (ctx->usedb) {
  858. if ((ret = maildir_set_uid( ctx, entry->base, &uid )) != DRV_OK) {
  859. maildir_free_scan( msglist );
  860. return ret;
  861. }
  862. entry->uid = uid;
  863. if ((ctx->gen.opts & OPEN_SIZE) || ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid))
  864. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  865. #endif /* USE_DB */
  866. } else {
  867. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  868. maildir_free_scan( msglist );
  869. return ret;
  870. }
  871. entry->uid = uid;
  872. if ((u = strstr( entry->base, ",U=" )))
  873. for (ru = u + 3; isdigit( (uchar)*ru ); ru++);
  874. else
  875. u = ru = strchr( entry->base, conf->info_delimiter );
  876. fnl = (u ?
  877. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%.*s,U=%d%s", subdirs[entry->recent], (int)(u - entry->base), entry->base, uid, ru ) :
  878. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s,U=%d", subdirs[entry->recent], entry->base, uid ))
  879. + 1 - 4;
  880. memcpy( nbuf, buf, bl + 4 );
  881. nfsnprintf( nbuf + bl + 4, sizeof(nbuf) - bl - 4, "%s", entry->base );
  882. if (rename( nbuf, buf )) {
  883. if (errno != ENOENT) {
  884. sys_error( "Maildir error: cannot rename %s to %s", nbuf, buf );
  885. fail:
  886. maildir_free_scan( msglist );
  887. return DRV_BOX_BAD;
  888. }
  889. retry:
  890. maildir_free_scan( msglist );
  891. goto again;
  892. }
  893. free( entry->base );
  894. entry->base = nfmalloc( fnl );
  895. memcpy( entry->base, buf + bl + 4, fnl );
  896. }
  897. if (ctx->gen.opts & OPEN_SIZE) {
  898. if (stat( buf, &st )) {
  899. if (errno != ENOENT) {
  900. sys_error( "Maildir error: cannot stat %s", buf );
  901. goto fail;
  902. }
  903. goto retry;
  904. }
  905. entry->size = st.st_size;
  906. }
  907. if ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid) {
  908. if (!(f = fopen( buf, "r" ))) {
  909. if (errno != ENOENT) {
  910. sys_error( "Maildir error: cannot open %s", buf );
  911. goto fail;
  912. }
  913. goto retry;
  914. }
  915. while (fgets( nbuf, sizeof(nbuf), f )) {
  916. if (!nbuf[0] || nbuf[0] == '\n')
  917. break;
  918. if (starts_with( nbuf, -1, "X-TUID: ", 8 ) && nbuf[8 + TUIDL] == '\n') {
  919. memcpy( entry->tuid, nbuf + 8, TUIDL );
  920. break;
  921. }
  922. }
  923. fclose( f );
  924. }
  925. }
  926. ctx->uvok = 1;
  927. }
  928. return DRV_OK;
  929. }
  930. static void
  931. maildir_init_msg( maildir_store_t *ctx, maildir_message_t *msg, msg_t *entry )
  932. {
  933. msg->base = entry->base;
  934. entry->base = 0; /* prevent deletion */
  935. msg->gen.size = entry->size;
  936. msg->gen.srec = 0;
  937. strncpy( msg->gen.tuid, entry->tuid, TUIDL );
  938. if (entry->recent)
  939. msg->gen.status |= M_RECENT;
  940. if (ctx->gen.opts & OPEN_FLAGS) {
  941. msg->gen.status |= M_FLAGS;
  942. msg->gen.flags = maildir_parse_flags( ((maildir_store_conf_t *)ctx->gen.conf)->info_prefix, msg->base );
  943. } else
  944. msg->gen.flags = 0;
  945. }
  946. static void
  947. maildir_app_msg( maildir_store_t *ctx, message_t ***msgapp, msg_t *entry )
  948. {
  949. maildir_message_t *msg = nfmalloc( sizeof(*msg) );
  950. msg->gen.next = **msgapp;
  951. **msgapp = &msg->gen;
  952. *msgapp = &msg->gen.next;
  953. msg->gen.uid = entry->uid;
  954. msg->gen.status = 0;
  955. maildir_init_msg( ctx, msg, entry );
  956. }
  957. static int
  958. maildir_select_box( store_t *gctx, const char *name )
  959. {
  960. maildir_store_t *ctx = (maildir_store_t *)gctx;
  961. maildir_cleanup( gctx );
  962. gctx->msgs = 0;
  963. ctx->excs = 0;
  964. ctx->uvfd = -1;
  965. #ifdef USE_DB
  966. ctx->db = 0;
  967. ctx->usedb = 0;
  968. #endif /* USE_DB */
  969. ctx->fresh[0] = ctx->fresh[1] = 0;
  970. if (starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) {
  971. gctx->path = maildir_join_path( ((maildir_store_conf_t *)gctx->conf)->inbox, name + 5 );
  972. ctx->is_inbox = !name[5];
  973. } else {
  974. if (maildir_validate_path( gctx->conf ) < 0) {
  975. gctx->path = 0;
  976. return DRV_CANCELED;
  977. }
  978. gctx->path = maildir_join_path( gctx->conf->path, name );
  979. ctx->is_inbox = 0;
  980. }
  981. return DRV_OK;
  982. }
  983. static void
  984. maildir_open_box( store_t *gctx,
  985. void (*cb)( int sts, void *aux ), void *aux )
  986. {
  987. maildir_store_t *ctx = (maildir_store_t *)gctx;
  988. int ret;
  989. char uvpath[_POSIX_PATH_MAX];
  990. if ((ret = maildir_validate( gctx->path, ctx->is_inbox, ctx )) != DRV_OK)
  991. goto bail;
  992. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  993. #ifndef USE_DB
  994. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) < 0) {
  995. sys_error( "Maildir error: cannot write %s", uvpath );
  996. cb( DRV_BOX_BAD, aux );
  997. return;
  998. }
  999. #else
  1000. ctx->usedb = 0;
  1001. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  1002. nfsnprintf( uvpath, sizeof(uvpath), "%s/.isyncuidmap.db", gctx->path );
  1003. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  1004. if (((maildir_store_conf_t *)gctx->conf)->alt_map) {
  1005. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  1006. goto dbok;
  1007. } else {
  1008. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  1009. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  1010. goto fnok;
  1011. }
  1012. sys_error( "Maildir error: cannot write %s", uvpath );
  1013. cb( DRV_BOX_BAD, aux );
  1014. return;
  1015. } else {
  1016. dbok:
  1017. ctx->usedb = nfstrdup( uvpath );
  1018. }
  1019. }
  1020. fnok:
  1021. #endif /* USE_DB */
  1022. ret = maildir_uidval_lock( ctx );
  1023. bail:
  1024. cb( ret, aux );
  1025. }
  1026. static void
  1027. maildir_create_box( store_t *gctx,
  1028. void (*cb)( int sts, void *aux ), void *aux )
  1029. {
  1030. cb( maildir_validate( gctx->path, 1, (maildir_store_t *)gctx ), aux );
  1031. }
  1032. static int
  1033. maildir_confirm_box_empty( store_t *gctx )
  1034. {
  1035. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1036. msglist_t msglist;
  1037. ctx->nexcs = ctx->minuid = ctx->maxuid = ctx->newuid = 0;
  1038. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1039. return DRV_BOX_BAD;
  1040. maildir_free_scan( &msglist );
  1041. return gctx->count ? DRV_BOX_BAD : DRV_OK;
  1042. }
  1043. static void
  1044. maildir_delete_box( store_t *gctx,
  1045. void (*cb)( int sts, void *aux ), void *aux )
  1046. {
  1047. int i, bl, ret = DRV_OK;
  1048. struct stat st;
  1049. char buf[_POSIX_PATH_MAX];
  1050. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", gctx->path );
  1051. if (stat( buf, &st )) {
  1052. if (errno != ENOENT) {
  1053. sys_error( "Maildir error: cannot access mailbox '%s'", gctx->path );
  1054. ret = DRV_BOX_BAD;
  1055. }
  1056. } else if (!S_ISDIR(st.st_mode)) {
  1057. error( "Maildir error: '%s' is no valid mailbox\n", gctx->path );
  1058. ret = DRV_BOX_BAD;
  1059. } else if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) == DRV_OK) {
  1060. nfsnprintf( buf + bl, sizeof(buf) - bl, ".uidvalidity" );
  1061. if (unlink( buf ) && errno != ENOENT)
  1062. goto badrm;
  1063. #ifdef USE_DB
  1064. nfsnprintf( buf + bl, sizeof(buf) - bl, ".isyncuidmap.db" );
  1065. if (unlink( buf ) && errno != ENOENT)
  1066. goto badrm;
  1067. #endif
  1068. /* We delete cur/ last, as it is the indicator for a present mailbox.
  1069. * That way an interrupted operation can be resumed. */
  1070. for (i = 3; --i >= 0; ) {
  1071. memcpy( buf + bl, subdirs[i], 4 );
  1072. if (rmdir( buf ) && errno != ENOENT) {
  1073. badrm:
  1074. sys_error( "Maildir error: cannot remove '%s'", buf );
  1075. ret = DRV_BOX_BAD;
  1076. break;
  1077. }
  1078. }
  1079. }
  1080. cb( ret, aux );
  1081. }
  1082. static int
  1083. maildir_finish_delete_box( store_t *gctx )
  1084. {
  1085. /* Subfolders are not deleted; the deleted folder is only "stripped of its mailboxness".
  1086. * Consequently, the rmdir may legitimately fail. This behavior follows the IMAP spec. */
  1087. if (rmdir( gctx->path ) && errno != ENOENT && errno != ENOTEMPTY) {
  1088. sys_error( "Maildir warning: cannot remove '%s'", gctx->path );
  1089. return DRV_BOX_BAD;
  1090. }
  1091. return DRV_OK;
  1092. }
  1093. static void
  1094. maildir_prepare_load_box( store_t *gctx, int opts )
  1095. {
  1096. if (opts & OPEN_SETFLAGS)
  1097. opts |= OPEN_OLD;
  1098. if (opts & OPEN_EXPUNGE)
  1099. opts |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
  1100. gctx->opts = opts;
  1101. }
  1102. static void
  1103. maildir_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int *excs, int nexcs,
  1104. void (*cb)( int sts, void *aux ), void *aux )
  1105. {
  1106. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1107. message_t **msgapp;
  1108. msglist_t msglist;
  1109. int i;
  1110. ctx->minuid = minuid;
  1111. ctx->maxuid = maxuid;
  1112. ctx->newuid = newuid;
  1113. ctx->excs = nfrealloc( excs, nexcs * sizeof(int) );
  1114. ctx->nexcs = nexcs;
  1115. if (maildir_scan( ctx, &msglist ) != DRV_OK) {
  1116. cb( DRV_BOX_BAD, aux );
  1117. return;
  1118. }
  1119. msgapp = &ctx->gen.msgs;
  1120. for (i = 0; i < msglist.nents; i++)
  1121. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1122. maildir_free_scan( &msglist );
  1123. cb( DRV_OK, aux );
  1124. }
  1125. static int
  1126. maildir_rescan( maildir_store_t *ctx )
  1127. {
  1128. message_t **msgapp;
  1129. maildir_message_t *msg;
  1130. msglist_t msglist;
  1131. int i;
  1132. ctx->fresh[0] = ctx->fresh[1] = 0;
  1133. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1134. return DRV_BOX_BAD;
  1135. for (msgapp = &ctx->gen.msgs, i = 0;
  1136. (msg = (maildir_message_t *)*msgapp) || i < msglist.nents; )
  1137. {
  1138. if (!msg) {
  1139. #if 0
  1140. debug( "adding new message %d\n", msglist.ents[i].uid );
  1141. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1142. #else
  1143. debug( "ignoring new message %d\n", msglist.ents[i].uid );
  1144. #endif
  1145. i++;
  1146. } else if (i >= msglist.nents) {
  1147. debug( "purging deleted message %d\n", msg->gen.uid );
  1148. msg->gen.status = M_DEAD;
  1149. msgapp = &msg->gen.next;
  1150. } else if (msglist.ents[i].uid < msg->gen.uid) {
  1151. /* this should not happen, actually */
  1152. #if 0
  1153. debug( "adding new message %d\n", msglist.ents[i].uid );
  1154. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1155. #else
  1156. debug( "ignoring new message %d\n", msglist.ents[i].uid );
  1157. #endif
  1158. i++;
  1159. } else if (msglist.ents[i].uid > msg->gen.uid) {
  1160. debug( "purging deleted message %d\n", msg->gen.uid );
  1161. msg->gen.status = M_DEAD;
  1162. msgapp = &msg->gen.next;
  1163. } else {
  1164. debug( "updating message %d\n", msg->gen.uid );
  1165. msg->gen.status &= ~(M_FLAGS|M_RECENT);
  1166. free( msg->base );
  1167. maildir_init_msg( ctx, msg, msglist.ents + i );
  1168. i++, msgapp = &msg->gen.next;
  1169. }
  1170. }
  1171. maildir_free_scan( &msglist );
  1172. return DRV_OK;
  1173. }
  1174. static int
  1175. maildir_again( maildir_store_t *ctx, maildir_message_t *msg,
  1176. const char *err, const char *fn, const char *fn2 )
  1177. {
  1178. int ret;
  1179. if (errno != ENOENT) {
  1180. sys_error( err, fn, fn2 );
  1181. return DRV_BOX_BAD;
  1182. }
  1183. if ((ret = maildir_rescan( ctx )) != DRV_OK)
  1184. return ret;
  1185. return (msg->gen.status & M_DEAD) ? DRV_MSG_BAD : DRV_OK;
  1186. }
  1187. static void
  1188. maildir_fetch_msg( store_t *gctx, message_t *gmsg, msg_data_t *data,
  1189. void (*cb)( int sts, void *aux ), void *aux )
  1190. {
  1191. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1192. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1193. int fd, ret;
  1194. struct stat st;
  1195. char buf[_POSIX_PATH_MAX];
  1196. for (;;) {
  1197. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1198. if ((fd = open( buf, O_RDONLY )) >= 0)
  1199. break;
  1200. if ((ret = maildir_again( ctx, msg, "Cannot open %s", buf, 0 )) != DRV_OK) {
  1201. cb( ret, aux );
  1202. return;
  1203. }
  1204. }
  1205. fstat( fd, &st );
  1206. data->len = st.st_size;
  1207. if (data->date == -1)
  1208. data->date = st.st_mtime;
  1209. data->data = nfmalloc( data->len );
  1210. if (read( fd, data->data, data->len ) != data->len) {
  1211. sys_error( "Maildir error: cannot read %s", buf );
  1212. close( fd );
  1213. cb( DRV_MSG_BAD, aux );
  1214. return;
  1215. }
  1216. close( fd );
  1217. if (!(gmsg->status & M_FLAGS))
  1218. data->flags = maildir_parse_flags( ((maildir_store_conf_t *)gctx->conf)->info_prefix, msg->base );
  1219. cb( DRV_OK, aux );
  1220. }
  1221. static int
  1222. maildir_make_flags( char info_delimiter, int flags, char *buf )
  1223. {
  1224. uint i, d;
  1225. buf[0] = info_delimiter;
  1226. buf[1] = '2';
  1227. buf[2] = ',';
  1228. for (d = 3, i = 0; i < as(Flags); i++)
  1229. if (flags & (1 << i))
  1230. buf[d++] = Flags[i];
  1231. buf[d] = 0;
  1232. return d;
  1233. }
  1234. static void
  1235. maildir_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1236. void (*cb)( int sts, int uid, void *aux ), void *aux )
  1237. {
  1238. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1239. const char *box;
  1240. int ret, fd, bl, uid;
  1241. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX], fbuf[NUM_FLAGS + 3], base[128];
  1242. bl = nfsnprintf( base, sizeof(base), "%ld.%d_%d.%s", (long)time( 0 ), Pid, ++MaildirCount, Hostname );
  1243. if (!to_trash) {
  1244. #ifdef USE_DB
  1245. if (ctx->usedb) {
  1246. if ((ret = maildir_set_uid( ctx, base, &uid )) != DRV_OK) {
  1247. free( data->data );
  1248. cb( ret, 0, aux );
  1249. return;
  1250. }
  1251. } else
  1252. #endif /* USE_DB */
  1253. {
  1254. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  1255. free( data->data );
  1256. cb( ret, 0, aux );
  1257. return;
  1258. }
  1259. nfsnprintf( base + bl, sizeof(base) - bl, ",U=%d", uid );
  1260. }
  1261. box = gctx->path;
  1262. } else {
  1263. box = ctx->trash;
  1264. }
  1265. maildir_make_flags( ((maildir_store_conf_t *)gctx->conf)->info_delimiter, data->flags, fbuf );
  1266. nfsnprintf( buf, sizeof(buf), "%s/tmp/%s%s", box, base, fbuf );
  1267. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1268. if (errno != ENOENT || !to_trash) {
  1269. sys_error( "Maildir error: cannot create %s", buf );
  1270. free( data->data );
  1271. cb( DRV_BOX_BAD, 0, aux );
  1272. return;
  1273. }
  1274. if ((ret = maildir_validate( box, 1, ctx )) != DRV_OK) {
  1275. free( data->data );
  1276. cb( ret, 0, aux );
  1277. return;
  1278. }
  1279. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1280. sys_error( "Maildir error: cannot create %s", buf );
  1281. free( data->data );
  1282. cb( DRV_BOX_BAD, 0, aux );
  1283. return;
  1284. }
  1285. }
  1286. ret = write( fd, data->data, data->len );
  1287. free( data->data );
  1288. if (ret != data->len || (UseFSync && (ret = fsync( fd )))) {
  1289. if (ret < 0)
  1290. sys_error( "Maildir error: cannot write %s", buf );
  1291. else
  1292. error( "Maildir error: cannot write %s. Disk full?\n", buf );
  1293. close( fd );
  1294. cb( DRV_BOX_BAD, 0, aux );
  1295. return;
  1296. }
  1297. if (close( fd ) < 0) {
  1298. /* Quota exceeded may cause this. */
  1299. sys_error( "Maildir error: cannot write %s", buf );
  1300. cb( DRV_BOX_BAD, 0, aux );
  1301. return;
  1302. }
  1303. if (data->date) {
  1304. /* Set atime and mtime according to INTERNALDATE or mtime of source message */
  1305. struct utimbuf utimebuf;
  1306. utimebuf.actime = utimebuf.modtime = data->date;
  1307. if (utime( buf, &utimebuf ) < 0) {
  1308. sys_error( "Maildir error: cannot set times for %s", buf );
  1309. cb( DRV_BOX_BAD, 0, aux );
  1310. return;
  1311. }
  1312. }
  1313. /* Moving seen messages to cur/ is strictly speaking incorrect, but makes mutt happy. */
  1314. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%s%s", box, subdirs[!(data->flags & F_SEEN)], base, fbuf );
  1315. if (rename( buf, nbuf )) {
  1316. sys_error( "Maildir error: cannot rename %s to %s", buf, nbuf );
  1317. cb( DRV_BOX_BAD, 0, aux );
  1318. return;
  1319. }
  1320. cb( DRV_OK, uid, aux );
  1321. }
  1322. static void
  1323. maildir_find_new_msgs( store_t *gctx ATTR_UNUSED, int newuid ATTR_UNUSED,
  1324. void (*cb)( int sts, void *aux ) ATTR_UNUSED, void *aux ATTR_UNUSED )
  1325. {
  1326. assert( !"maildir_find_new_msgs is not supposed to be called" );
  1327. }
  1328. static void
  1329. maildir_set_msg_flags( store_t *gctx, message_t *gmsg, int uid ATTR_UNUSED, int add, int del,
  1330. void (*cb)( int sts, void *aux ), void *aux )
  1331. {
  1332. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  1333. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1334. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1335. char *s, *p;
  1336. uint i;
  1337. int j, ret, ol, fl, bbl, bl, tl;
  1338. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1339. bbl = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1340. memcpy( nbuf, gctx->path, bbl - 1 );
  1341. memcpy( nbuf + bbl - 1, "/cur/", 5 );
  1342. for (;;) {
  1343. bl = bbl + nfsnprintf( buf + bbl, sizeof(buf) - bbl, "%s/", subdirs[gmsg->status & M_RECENT] );
  1344. ol = strlen( msg->base );
  1345. if ((int)sizeof(buf) - bl < ol + 3 + NUM_FLAGS)
  1346. oob();
  1347. memcpy( buf + bl, msg->base, ol + 1 );
  1348. memcpy( nbuf + bl, msg->base, ol + 1 );
  1349. if ((s = strstr( nbuf + bl, conf->info_prefix ))) {
  1350. s += 3;
  1351. fl = ol - (s - (nbuf + bl));
  1352. for (i = 0; i < as(Flags); i++) {
  1353. if ((p = strchr( s, Flags[i] ))) {
  1354. if (del & (1 << i)) {
  1355. memcpy( p, p + 1, fl - (p - s) );
  1356. fl--;
  1357. }
  1358. } else if (add & (1 << i)) {
  1359. for (j = 0; j < fl && Flags[i] > s[j]; j++);
  1360. fl++;
  1361. memmove( s + j + 1, s + j, fl - j );
  1362. s[j] = Flags[i];
  1363. }
  1364. }
  1365. tl = ol + 3 + fl;
  1366. } else {
  1367. tl = ol + maildir_make_flags( conf->info_delimiter, msg->gen.flags, nbuf + bl + ol );
  1368. }
  1369. if (!rename( buf, nbuf ))
  1370. break;
  1371. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot rename %s to %s", buf, nbuf )) != DRV_OK) {
  1372. cb( ret, aux );
  1373. return;
  1374. }
  1375. }
  1376. free( msg->base );
  1377. msg->base = nfmalloc( tl + 1 );
  1378. memcpy( msg->base, nbuf + bl, tl + 1 );
  1379. msg->gen.flags |= add;
  1380. msg->gen.flags &= ~del;
  1381. gmsg->status &= ~M_RECENT;
  1382. cb( DRV_OK, aux );
  1383. }
  1384. #ifdef USE_DB
  1385. static int
  1386. maildir_purge_msg( maildir_store_t *ctx, const char *name )
  1387. {
  1388. int ret;
  1389. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  1390. return ret;
  1391. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  1392. if ((ret = ctx->db->del( ctx->db, 0, &key, 0 ))) {
  1393. ctx->db->err( ctx->db, ret, "Maildir error: db->del()" );
  1394. return DRV_BOX_BAD;
  1395. }
  1396. return DRV_OK;
  1397. }
  1398. #endif /* USE_DB */
  1399. static void
  1400. maildir_trash_msg( store_t *gctx, message_t *gmsg,
  1401. void (*cb)( int sts, void *aux ), void *aux )
  1402. {
  1403. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1404. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1405. char *s;
  1406. int ret;
  1407. struct stat st;
  1408. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1409. for (;;) {
  1410. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1411. s = strstr( msg->base, ((maildir_store_conf_t *)gctx->conf)->info_prefix );
  1412. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%ld.%d_%d.%s%s", ctx->trash,
  1413. subdirs[gmsg->status & M_RECENT], (long)time( 0 ), Pid, ++MaildirCount, Hostname, s ? s : "" );
  1414. if (!rename( buf, nbuf ))
  1415. break;
  1416. if (!stat( buf, &st )) {
  1417. if ((ret = maildir_validate( ctx->trash, 1, ctx )) != DRV_OK) {
  1418. cb( ret, aux );
  1419. return;
  1420. }
  1421. if (!rename( buf, nbuf ))
  1422. break;
  1423. if (errno != ENOENT) {
  1424. sys_error( "Maildir error: cannot move %s to %s", buf, nbuf );
  1425. cb( DRV_BOX_BAD, aux );
  1426. return;
  1427. }
  1428. }
  1429. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot move %s to %s", buf, nbuf )) != DRV_OK) {
  1430. cb( ret, aux );
  1431. return;
  1432. }
  1433. }
  1434. gmsg->status |= M_DEAD;
  1435. gctx->count--;
  1436. #ifdef USE_DB
  1437. if (ctx->usedb) {
  1438. cb( maildir_purge_msg( ctx, msg->base ), aux );
  1439. return;
  1440. }
  1441. #endif /* USE_DB */
  1442. cb( DRV_OK, aux );
  1443. }
  1444. static void
  1445. maildir_close_box( store_t *gctx,
  1446. void (*cb)( int sts, void *aux ), void *aux )
  1447. {
  1448. #ifdef USE_DB
  1449. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1450. #endif /* USE_DB */
  1451. message_t *msg;
  1452. int basel, retry, ret;
  1453. char buf[_POSIX_PATH_MAX];
  1454. for (;;) {
  1455. retry = 0;
  1456. basel = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1457. for (msg = gctx->msgs; msg; msg = msg->next)
  1458. if (!(msg->status & M_DEAD) && (msg->flags & F_DELETED)) {
  1459. nfsnprintf( buf + basel, sizeof(buf) - basel, "%s/%s", subdirs[msg->status & M_RECENT], ((maildir_message_t *)msg)->base );
  1460. if (unlink( buf )) {
  1461. if (errno == ENOENT)
  1462. retry = 1;
  1463. else
  1464. sys_error( "Maildir error: cannot remove %s", buf );
  1465. } else {
  1466. msg->status |= M_DEAD;
  1467. gctx->count--;
  1468. #ifdef USE_DB
  1469. if (ctx->db && (ret = maildir_purge_msg( ctx, ((maildir_message_t *)msg)->base )) != DRV_OK) {
  1470. cb( ret, aux );
  1471. return;
  1472. }
  1473. #endif /* USE_DB */
  1474. }
  1475. }
  1476. if (!retry) {
  1477. cb( DRV_OK, aux );
  1478. return;
  1479. }
  1480. if ((ret = maildir_rescan( (maildir_store_t *)gctx )) != DRV_OK) {
  1481. cb( ret, aux );
  1482. return;
  1483. }
  1484. }
  1485. }
  1486. static void
  1487. maildir_cancel_cmds( store_t *gctx ATTR_UNUSED,
  1488. void (*cb)( void *aux ), void *aux )
  1489. {
  1490. cb( aux );
  1491. }
  1492. static void
  1493. maildir_commit_cmds( store_t *gctx )
  1494. {
  1495. (void) gctx;
  1496. }
  1497. static int
  1498. maildir_memory_usage( store_t *gctx ATTR_UNUSED )
  1499. {
  1500. return 0;
  1501. }
  1502. static int
  1503. maildir_parse_store( conffile_t *cfg, store_conf_t **storep )
  1504. {
  1505. maildir_store_conf_t *store;
  1506. if (strcasecmp( "MaildirStore", cfg->cmd ))
  1507. return 0;
  1508. store = nfcalloc( sizeof(*store) );
  1509. store->info_delimiter = FieldDelimiter;
  1510. store->gen.driver = &maildir_driver;
  1511. store->gen.name = nfstrdup( cfg->val );
  1512. while (getcline( cfg ) && cfg->cmd)
  1513. if (!strcasecmp( "Inbox", cfg->cmd ))
  1514. store->inbox = expand_strdup( cfg->val );
  1515. else if (!strcasecmp( "Path", cfg->cmd ))
  1516. store->gen.path = expand_strdup( cfg->val );
  1517. #ifdef USE_DB
  1518. else if (!strcasecmp( "AltMap", cfg->cmd ))
  1519. store->alt_map = parse_bool( cfg );
  1520. #endif /* USE_DB */
  1521. else if (!strcasecmp( "InfoDelimiter", cfg->cmd )) {
  1522. if (strlen( cfg->val ) != 1) {
  1523. error( "%s:%d: Info delimiter must be exactly one character long\n", cfg->file, cfg->line );
  1524. cfg->err = 1;
  1525. continue;
  1526. }
  1527. store->info_delimiter = cfg->val[0];
  1528. if (!ispunct( store->info_delimiter )) {
  1529. error( "%s:%d: Info delimiter must be a punctuation character\n", cfg->file, cfg->line );
  1530. cfg->err = 1;
  1531. continue;
  1532. }
  1533. } else
  1534. parse_generic_store( &store->gen, cfg );
  1535. if (!store->inbox)
  1536. store->inbox = expand_strdup( "~/Maildir" );
  1537. nfasprintf( &store->info_prefix, "%c2,", store->info_delimiter );
  1538. nfasprintf( &store->info_stop, "%c,", store->info_delimiter );
  1539. *storep = &store->gen;
  1540. return 1;
  1541. }
  1542. struct driver maildir_driver = {
  1543. 0, /* XXX DRV_CRLF? */
  1544. maildir_parse_store,
  1545. maildir_cleanup_drv,
  1546. maildir_open_store,
  1547. maildir_disown_store,
  1548. maildir_disown_store, /* _cancel_, but it's the same */
  1549. maildir_list_store,
  1550. maildir_select_box,
  1551. maildir_create_box,
  1552. maildir_open_box,
  1553. maildir_confirm_box_empty,
  1554. maildir_delete_box,
  1555. maildir_finish_delete_box,
  1556. maildir_prepare_load_box,
  1557. maildir_load_box,
  1558. maildir_fetch_msg,
  1559. maildir_store_msg,
  1560. maildir_find_new_msgs,
  1561. maildir_set_msg_flags,
  1562. maildir_trash_msg,
  1563. maildir_close_box,
  1564. maildir_cancel_cmds,
  1565. maildir_commit_cmds,
  1566. maildir_memory_usage,
  1567. };