drv_maildir.c 52 KB

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