drv_maildir.c 52 KB

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