drv_maildir.c 52 KB

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