drv_maildir.c 52 KB

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