drv_maildir.c 50 KB

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