drv_maildir.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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. while ((want_tuid || want_msgid) && fgets( nbuf, sizeof(nbuf), f )) {
  1067. int bufl = strlen( nbuf );
  1068. if (bufl && nbuf[bufl - 1] == '\n')
  1069. --bufl;
  1070. if (bufl && nbuf[bufl - 1] == '\r')
  1071. --bufl;
  1072. if (!bufl)
  1073. break;
  1074. if (want_tuid && starts_with( nbuf, bufl, "X-TUID: ", 8 )) {
  1075. if (bufl < 8 + TUIDL) {
  1076. error( "Maildir error: malformed X-TUID header (UID %u)\n", uid );
  1077. continue;
  1078. }
  1079. memcpy( entry->tuid, nbuf + 8, TUIDL );
  1080. want_tuid = 0;
  1081. in_msgid = 0;
  1082. continue;
  1083. }
  1084. if (want_msgid && starts_with_upper( nbuf, bufl, "MESSAGE-ID:", 11 )) {
  1085. off = 11;
  1086. } else if (in_msgid) {
  1087. if (!isspace( nbuf[0] )) {
  1088. in_msgid = 0;
  1089. continue;
  1090. }
  1091. off = 1;
  1092. } else {
  1093. continue;
  1094. }
  1095. while (off < bufl && isspace( nbuf[off] ))
  1096. off++;
  1097. if (off == bufl) {
  1098. in_msgid = 1;
  1099. continue;
  1100. }
  1101. entry->msgid = nfstrndup( nbuf + off, bufl - off );
  1102. want_msgid = 0;
  1103. in_msgid = 0;
  1104. }
  1105. fclose( f );
  1106. }
  1107. }
  1108. ctx->uvok = 1;
  1109. }
  1110. return DRV_OK;
  1111. }
  1112. static void
  1113. maildir_init_msg( maildir_store_t *ctx, maildir_message_t *msg, msg_t *entry )
  1114. {
  1115. msg->base = entry->base;
  1116. entry->base = 0; /* prevent deletion */
  1117. msg->gen.msgid = entry->msgid;
  1118. entry->msgid = 0; /* prevent deletion */
  1119. msg->gen.size = entry->size;
  1120. msg->gen.srec = 0;
  1121. memcpy( msg->gen.tuid, entry->tuid, TUIDL );
  1122. if (entry->recent)
  1123. msg->gen.status |= M_RECENT;
  1124. if (ctx->opts & OPEN_FLAGS) {
  1125. msg->gen.status |= M_FLAGS;
  1126. msg->gen.flags = maildir_parse_flags( ((maildir_store_conf_t *)ctx->gen.conf)->info_prefix, msg->base );
  1127. } else
  1128. msg->gen.flags = 0;
  1129. }
  1130. static void
  1131. maildir_app_msg( maildir_store_t *ctx, message_t ***msgapp, msg_t *entry )
  1132. {
  1133. maildir_message_t *msg = nfmalloc( sizeof(*msg) );
  1134. msg->gen.next = **msgapp;
  1135. **msgapp = &msg->gen;
  1136. *msgapp = &msg->gen.next;
  1137. msg->gen.uid = entry->uid;
  1138. msg->gen.status = 0;
  1139. maildir_init_msg( ctx, msg, entry );
  1140. }
  1141. static int
  1142. maildir_select_box( store_t *gctx, const char *name )
  1143. {
  1144. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  1145. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1146. maildir_cleanup( gctx );
  1147. ctx->msgs = 0;
  1148. ctx->excs.data = 0;
  1149. ctx->uvfd = -1;
  1150. #ifdef USE_DB
  1151. ctx->db = 0;
  1152. ctx->usedb = 0;
  1153. #endif /* USE_DB */
  1154. ctx->fresh[0] = ctx->fresh[1] = 0;
  1155. if (starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) {
  1156. if (!name[5]) {
  1157. ctx->path = nfstrdup( conf->inbox );
  1158. ctx->is_inbox = 1;
  1159. } else {
  1160. ctx->path = maildir_join_path( conf, 1, name + 5 );
  1161. ctx->is_inbox = 0;
  1162. }
  1163. } else {
  1164. if (!(ctx->path = maildir_join_path( conf, 0, name )))
  1165. return DRV_CANCELED;
  1166. ctx->is_inbox = 0;
  1167. }
  1168. return ctx->path ? DRV_OK : DRV_BOX_BAD;
  1169. }
  1170. static const char *
  1171. maildir_get_box_path( store_t *gctx )
  1172. {
  1173. return ((maildir_store_t *)gctx)->path;
  1174. }
  1175. static void
  1176. maildir_open_box( store_t *gctx,
  1177. void (*cb)( int sts, int uidvalidity, void *aux ), void *aux )
  1178. {
  1179. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1180. int ret;
  1181. char uvpath[_POSIX_PATH_MAX];
  1182. if ((ret = maildir_validate( ctx->path, ctx->is_inbox, ctx )) != DRV_OK)
  1183. goto bail;
  1184. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", ctx->path );
  1185. #ifndef USE_DB
  1186. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) < 0) {
  1187. sys_error( "Maildir error: cannot write %s", uvpath );
  1188. cb( DRV_BOX_BAD, UIDVAL_BAD, aux );
  1189. return;
  1190. }
  1191. #else
  1192. ctx->usedb = 0;
  1193. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  1194. nfsnprintf( uvpath, sizeof(uvpath), "%s/.isyncuidmap.db", ctx->path );
  1195. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  1196. if (((maildir_store_conf_t *)gctx->conf)->alt_map) {
  1197. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  1198. goto dbok;
  1199. } else {
  1200. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", ctx->path );
  1201. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  1202. goto fnok;
  1203. }
  1204. sys_error( "Maildir error: cannot write %s", uvpath );
  1205. cb( DRV_BOX_BAD, UIDVAL_BAD, aux );
  1206. return;
  1207. } else {
  1208. dbok:
  1209. ctx->usedb = nfstrdup( uvpath );
  1210. }
  1211. }
  1212. fnok:
  1213. #endif /* USE_DB */
  1214. ret = maildir_uidval_lock( ctx );
  1215. bail:
  1216. cb( ret, ctx->uidvalidity, aux );
  1217. }
  1218. static int
  1219. maildir_get_uidnext( store_t *gctx ATTR_UNUSED )
  1220. {
  1221. return 0;
  1222. }
  1223. static void
  1224. maildir_create_box( store_t *gctx,
  1225. void (*cb)( int sts, void *aux ), void *aux )
  1226. {
  1227. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1228. cb( maildir_validate( ctx->path, 1, ctx ), aux );
  1229. }
  1230. static int
  1231. maildir_confirm_box_empty( store_t *gctx )
  1232. {
  1233. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1234. msg_t_array_alloc_t msglist;
  1235. ctx->excs.size = ctx->minuid = ctx->maxuid = ctx->newuid = 0;
  1236. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1237. return DRV_BOX_BAD;
  1238. maildir_free_scan( &msglist );
  1239. return ctx->total_msgs ? DRV_BOX_BAD : DRV_OK;
  1240. }
  1241. static void
  1242. maildir_delete_box( store_t *gctx,
  1243. void (*cb)( int sts, void *aux ), void *aux )
  1244. {
  1245. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1246. int i, bl, ret = DRV_OK;
  1247. struct stat st;
  1248. char buf[_POSIX_PATH_MAX];
  1249. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", ctx->path );
  1250. if (stat( buf, &st )) {
  1251. if (errno != ENOENT) {
  1252. sys_error( "Maildir error: cannot access mailbox '%s'", ctx->path );
  1253. ret = DRV_BOX_BAD;
  1254. }
  1255. } else if (!S_ISDIR(st.st_mode)) {
  1256. error( "Maildir error: '%s' is no valid mailbox\n", ctx->path );
  1257. ret = DRV_BOX_BAD;
  1258. } else if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) == DRV_OK) {
  1259. nfsnprintf( buf + bl, sizeof(buf) - bl, ".uidvalidity" );
  1260. if (unlink( buf ) && errno != ENOENT)
  1261. goto badrm;
  1262. #ifdef USE_DB
  1263. nfsnprintf( buf + bl, sizeof(buf) - bl, ".isyncuidmap.db" );
  1264. if (unlink( buf ) && errno != ENOENT)
  1265. goto badrm;
  1266. #endif
  1267. /* We delete cur/ last, as it is the indicator for a present mailbox.
  1268. * That way an interrupted operation can be resumed. */
  1269. for (i = 3; --i >= 0; ) {
  1270. memcpy( buf + bl, subdirs[i], 4 );
  1271. if (rmdir( buf ) && errno != ENOENT) {
  1272. badrm:
  1273. sys_error( "Maildir error: cannot remove '%s'", buf );
  1274. ret = DRV_BOX_BAD;
  1275. break;
  1276. }
  1277. }
  1278. }
  1279. cb( ret, aux );
  1280. }
  1281. static int
  1282. maildir_finish_delete_box( store_t *gctx )
  1283. {
  1284. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1285. /* Subfolders are not deleted; the deleted folder is only "stripped of its mailboxness".
  1286. * Consequently, the rmdir may legitimately fail. This behavior follows the IMAP spec. */
  1287. if (rmdir( ctx->path ) && errno != ENOENT && errno != ENOTEMPTY) {
  1288. sys_error( "Maildir warning: cannot remove '%s'", ctx->path );
  1289. return DRV_BOX_BAD;
  1290. }
  1291. return DRV_OK;
  1292. }
  1293. static int
  1294. maildir_prepare_load_box( store_t *gctx, int opts )
  1295. {
  1296. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1297. if (opts & OPEN_SETFLAGS)
  1298. opts |= OPEN_OLD;
  1299. if (opts & OPEN_EXPUNGE)
  1300. opts |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
  1301. ctx->opts = opts;
  1302. return opts;
  1303. }
  1304. static void
  1305. maildir_load_box( store_t *gctx, uint minuid, uint maxuid, uint newuid, uint seenuid, uint_array_t excs,
  1306. void (*cb)( int sts, message_t *msgs, int total_msgs, int recent_msgs, void *aux ), void *aux )
  1307. {
  1308. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1309. message_t **msgapp;
  1310. msg_t_array_alloc_t msglist;
  1311. int i;
  1312. ctx->minuid = minuid;
  1313. ctx->maxuid = maxuid;
  1314. ctx->newuid = newuid;
  1315. ctx->seenuid = seenuid;
  1316. ARRAY_SQUEEZE( &excs );
  1317. ctx->excs = excs;
  1318. if (maildir_scan( ctx, &msglist ) != DRV_OK) {
  1319. cb( DRV_BOX_BAD, 0, 0, 0, aux );
  1320. return;
  1321. }
  1322. msgapp = &ctx->msgs;
  1323. for (i = 0; i < msglist.array.size; i++)
  1324. maildir_app_msg( ctx, &msgapp, msglist.array.data + i );
  1325. maildir_free_scan( &msglist );
  1326. cb( DRV_OK, ctx->msgs, ctx->total_msgs, ctx->recent_msgs, aux );
  1327. }
  1328. static int
  1329. maildir_rescan( maildir_store_t *ctx )
  1330. {
  1331. message_t **msgapp;
  1332. maildir_message_t *msg;
  1333. msg_t_array_alloc_t msglist;
  1334. int i;
  1335. ctx->fresh[0] = ctx->fresh[1] = 0;
  1336. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1337. return DRV_BOX_BAD;
  1338. for (msgapp = &ctx->msgs, i = 0;
  1339. (msg = (maildir_message_t *)*msgapp) || i < msglist.array.size; )
  1340. {
  1341. if (!msg) {
  1342. #if 0
  1343. debug( "adding new message %u\n", msglist.array.data[i].uid );
  1344. maildir_app_msg( ctx, &msgapp, msglist.array.data + i );
  1345. #else
  1346. debug( "ignoring new message %u\n", msglist.array.data[i].uid );
  1347. #endif
  1348. i++;
  1349. } else if (i >= msglist.array.size) {
  1350. debug( "purging deleted message %u\n", msg->gen.uid );
  1351. msg->gen.status = M_DEAD;
  1352. msgapp = &msg->gen.next;
  1353. } else if (msglist.array.data[i].uid < msg->gen.uid) {
  1354. /* this should not happen, actually */
  1355. #if 0
  1356. debug( "adding new message %u\n", msglist.array.data[i].uid );
  1357. maildir_app_msg( ctx, &msgapp, msglist.array.data + i );
  1358. #else
  1359. debug( "ignoring new message %u\n", msglist.array.data[i].uid );
  1360. #endif
  1361. i++;
  1362. } else if (msglist.array.data[i].uid > msg->gen.uid) {
  1363. debug( "purging deleted message %u\n", msg->gen.uid );
  1364. msg->gen.status = M_DEAD;
  1365. msgapp = &msg->gen.next;
  1366. } else {
  1367. debug( "updating message %u\n", msg->gen.uid );
  1368. msg->gen.status &= ~(M_FLAGS|M_RECENT);
  1369. free( msg->base );
  1370. free( msg->gen.msgid );
  1371. maildir_init_msg( ctx, msg, msglist.array.data + i );
  1372. i++, msgapp = &msg->gen.next;
  1373. }
  1374. }
  1375. maildir_free_scan( &msglist );
  1376. return DRV_OK;
  1377. }
  1378. static int
  1379. maildir_again( maildir_store_t *ctx, maildir_message_t *msg,
  1380. const char *err, const char *fn, const char *fn2 )
  1381. {
  1382. int ret;
  1383. if (errno != ENOENT) {
  1384. sys_error( err, fn, fn2 );
  1385. return DRV_BOX_BAD;
  1386. }
  1387. if ((ret = maildir_rescan( ctx )) != DRV_OK)
  1388. return ret;
  1389. return (msg->gen.status & M_DEAD) ? DRV_MSG_BAD : DRV_OK;
  1390. }
  1391. static void
  1392. maildir_fetch_msg( store_t *gctx, message_t *gmsg, msg_data_t *data,
  1393. void (*cb)( int sts, void *aux ), void *aux )
  1394. {
  1395. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1396. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1397. int fd, ret;
  1398. struct stat st;
  1399. char buf[_POSIX_PATH_MAX];
  1400. for (;;) {
  1401. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", ctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1402. if ((fd = open( buf, O_RDONLY )) >= 0)
  1403. break;
  1404. if ((ret = maildir_again( ctx, msg, "Cannot open %s", buf, 0 )) != DRV_OK) {
  1405. cb( ret, aux );
  1406. return;
  1407. }
  1408. }
  1409. fstat( fd, &st );
  1410. data->len = st.st_size;
  1411. if (data->date == -1)
  1412. data->date = st.st_mtime;
  1413. data->data = nfmalloc( data->len );
  1414. if (read( fd, data->data, data->len ) != data->len) {
  1415. sys_error( "Maildir error: cannot read %s", buf );
  1416. close( fd );
  1417. cb( DRV_MSG_BAD, aux );
  1418. return;
  1419. }
  1420. close( fd );
  1421. if (!(gmsg->status & M_FLAGS))
  1422. data->flags = maildir_parse_flags( ((maildir_store_conf_t *)gctx->conf)->info_prefix, msg->base );
  1423. cb( DRV_OK, aux );
  1424. }
  1425. static int
  1426. maildir_make_flags( char info_delimiter, int flags, char *buf )
  1427. {
  1428. uint i, d;
  1429. buf[0] = info_delimiter;
  1430. buf[1] = '2';
  1431. buf[2] = ',';
  1432. for (d = 3, i = 0; i < as(Flags); i++)
  1433. if (flags & (1 << i))
  1434. buf[d++] = Flags[i];
  1435. buf[d] = 0;
  1436. return d;
  1437. }
  1438. static void
  1439. maildir_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1440. void (*cb)( int sts, uint uid, void *aux ), void *aux )
  1441. {
  1442. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1443. const char *box;
  1444. int ret, fd, bl;
  1445. uint uid;
  1446. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX], fbuf[NUM_FLAGS + 3], base[128];
  1447. bl = nfsnprintf( base, sizeof(base), "%ld.%d_%d.%s", (long)time( 0 ), Pid, ++MaildirCount, Hostname );
  1448. if (!to_trash) {
  1449. #ifdef USE_DB
  1450. if (ctx->usedb) {
  1451. if ((ret = maildir_set_uid( ctx, base, &uid )) != DRV_OK) {
  1452. free( data->data );
  1453. cb( ret, 0, aux );
  1454. return;
  1455. }
  1456. } else
  1457. #endif /* USE_DB */
  1458. {
  1459. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  1460. free( data->data );
  1461. cb( ret, 0, aux );
  1462. return;
  1463. }
  1464. nfsnprintf( base + bl, sizeof(base) - bl, ",U=%u", uid );
  1465. }
  1466. box = ctx->path;
  1467. } else {
  1468. uid = 0;
  1469. box = ctx->trash;
  1470. }
  1471. maildir_make_flags( ((maildir_store_conf_t *)gctx->conf)->info_delimiter, data->flags, fbuf );
  1472. nfsnprintf( buf, sizeof(buf), "%s/tmp/%s%s", box, base, fbuf );
  1473. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1474. if (errno != ENOENT || !to_trash) {
  1475. sys_error( "Maildir error: cannot create %s", buf );
  1476. free( data->data );
  1477. cb( DRV_BOX_BAD, 0, aux );
  1478. return;
  1479. }
  1480. if ((ret = maildir_validate( box, 1, ctx )) != DRV_OK) {
  1481. free( data->data );
  1482. cb( ret, 0, aux );
  1483. return;
  1484. }
  1485. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1486. sys_error( "Maildir error: cannot create %s", buf );
  1487. free( data->data );
  1488. cb( DRV_BOX_BAD, 0, aux );
  1489. return;
  1490. }
  1491. }
  1492. ret = write( fd, data->data, data->len );
  1493. free( data->data );
  1494. if (ret != data->len || (UseFSync && (ret = fsync( fd )))) {
  1495. if (ret < 0)
  1496. sys_error( "Maildir error: cannot write %s", buf );
  1497. else
  1498. error( "Maildir error: cannot write %s. Disk full?\n", buf );
  1499. close( fd );
  1500. cb( DRV_BOX_BAD, 0, aux );
  1501. return;
  1502. }
  1503. if (close( fd ) < 0) {
  1504. /* Quota exceeded may cause this. */
  1505. sys_error( "Maildir error: cannot write %s", buf );
  1506. cb( DRV_BOX_BAD, 0, aux );
  1507. return;
  1508. }
  1509. if (data->date) {
  1510. /* Set atime and mtime according to INTERNALDATE or mtime of source message */
  1511. struct utimbuf utimebuf;
  1512. utimebuf.actime = utimebuf.modtime = data->date;
  1513. if (utime( buf, &utimebuf ) < 0) {
  1514. sys_error( "Maildir error: cannot set times for %s", buf );
  1515. cb( DRV_BOX_BAD, 0, aux );
  1516. return;
  1517. }
  1518. }
  1519. /* Moving seen messages to cur/ is strictly speaking incorrect, but makes mutt happy. */
  1520. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%s%s", box, subdirs[!(data->flags & F_SEEN)], base, fbuf );
  1521. if (rename( buf, nbuf )) {
  1522. sys_error( "Maildir error: cannot rename %s to %s", buf, nbuf );
  1523. cb( DRV_BOX_BAD, 0, aux );
  1524. return;
  1525. }
  1526. cb( DRV_OK, uid, aux );
  1527. }
  1528. static void
  1529. maildir_find_new_msgs( store_t *gctx ATTR_UNUSED, uint newuid ATTR_UNUSED,
  1530. void (*cb)( int sts, message_t *msgs, void *aux ) ATTR_UNUSED, void *aux ATTR_UNUSED )
  1531. {
  1532. assert( !"maildir_find_new_msgs is not supposed to be called" );
  1533. }
  1534. static void
  1535. maildir_set_msg_flags( store_t *gctx, message_t *gmsg, uint uid ATTR_UNUSED, int add, int del,
  1536. void (*cb)( int sts, void *aux ), void *aux )
  1537. {
  1538. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  1539. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1540. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1541. char *s, *p;
  1542. uint i;
  1543. int j, ret, ol, fl, bbl, bl, tl;
  1544. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1545. bbl = nfsnprintf( buf, sizeof(buf), "%s/", ctx->path );
  1546. memcpy( nbuf, ctx->path, bbl - 1 );
  1547. memcpy( nbuf + bbl - 1, "/cur/", 5 );
  1548. for (;;) {
  1549. bl = bbl + nfsnprintf( buf + bbl, sizeof(buf) - bbl, "%s/", subdirs[gmsg->status & M_RECENT] );
  1550. ol = strlen( msg->base );
  1551. if ((int)sizeof(buf) - bl < ol + 3 + NUM_FLAGS)
  1552. oob();
  1553. memcpy( buf + bl, msg->base, ol + 1 );
  1554. memcpy( nbuf + bl, msg->base, ol + 1 );
  1555. if ((s = strstr( nbuf + bl, conf->info_prefix ))) {
  1556. s += 3;
  1557. fl = ol - (s - (nbuf + bl));
  1558. for (i = 0; i < as(Flags); i++) {
  1559. if ((p = strchr( s, Flags[i] ))) {
  1560. if (del & (1 << i)) {
  1561. memmove( p, p + 1, fl - (p - s) );
  1562. fl--;
  1563. }
  1564. } else if (add & (1 << i)) {
  1565. for (j = 0; j < fl && Flags[i] > s[j]; j++);
  1566. fl++;
  1567. memmove( s + j + 1, s + j, fl - j );
  1568. s[j] = Flags[i];
  1569. }
  1570. }
  1571. tl = ol + 3 + fl;
  1572. } else {
  1573. tl = ol + maildir_make_flags( conf->info_delimiter, msg->gen.flags, nbuf + bl + ol );
  1574. }
  1575. if (!rename( buf, nbuf ))
  1576. break;
  1577. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot rename %s to %s", buf, nbuf )) != DRV_OK) {
  1578. cb( ret, aux );
  1579. return;
  1580. }
  1581. }
  1582. free( msg->base );
  1583. msg->base = nfstrndup( nbuf + bl, tl );
  1584. msg->gen.flags |= add;
  1585. msg->gen.flags &= ~del;
  1586. gmsg->status &= ~M_RECENT;
  1587. cb( DRV_OK, aux );
  1588. }
  1589. #ifdef USE_DB
  1590. static int
  1591. maildir_purge_msg( maildir_store_t *ctx, const char *name )
  1592. {
  1593. int ret;
  1594. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  1595. return ret;
  1596. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  1597. if ((ret = ctx->db->del( ctx->db, 0, &key, 0 ))) {
  1598. ctx->db->err( ctx->db, ret, "Maildir error: db->del()" );
  1599. return DRV_BOX_BAD;
  1600. }
  1601. return DRV_OK;
  1602. }
  1603. #endif /* USE_DB */
  1604. static void
  1605. maildir_trash_msg( store_t *gctx, message_t *gmsg,
  1606. void (*cb)( int sts, void *aux ), void *aux )
  1607. {
  1608. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1609. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1610. char *s;
  1611. int ret;
  1612. struct stat st;
  1613. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1614. for (;;) {
  1615. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", ctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1616. s = strstr( msg->base, ((maildir_store_conf_t *)gctx->conf)->info_prefix );
  1617. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%ld.%d_%d.%s%s", ctx->trash,
  1618. subdirs[gmsg->status & M_RECENT], (long)time( 0 ), Pid, ++MaildirCount, Hostname, s ? s : "" );
  1619. if (!rename( buf, nbuf ))
  1620. break;
  1621. if (!stat( buf, &st )) {
  1622. if ((ret = maildir_validate( ctx->trash, 1, ctx )) != DRV_OK) {
  1623. cb( ret, aux );
  1624. return;
  1625. }
  1626. if (!rename( buf, nbuf ))
  1627. break;
  1628. if (errno != ENOENT) {
  1629. sys_error( "Maildir error: cannot move %s to %s", buf, nbuf );
  1630. cb( DRV_BOX_BAD, aux );
  1631. return;
  1632. }
  1633. }
  1634. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot move %s to %s", buf, nbuf )) != DRV_OK) {
  1635. cb( ret, aux );
  1636. return;
  1637. }
  1638. }
  1639. gmsg->status |= M_DEAD;
  1640. ctx->total_msgs--;
  1641. #ifdef USE_DB
  1642. if (ctx->usedb) {
  1643. cb( maildir_purge_msg( ctx, msg->base ), aux );
  1644. return;
  1645. }
  1646. #endif /* USE_DB */
  1647. cb( DRV_OK, aux );
  1648. }
  1649. static void
  1650. maildir_close_box( store_t *gctx,
  1651. void (*cb)( int sts, void *aux ), void *aux )
  1652. {
  1653. #ifdef USE_DB
  1654. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1655. #endif /* USE_DB */
  1656. message_t *msg;
  1657. int basel, retry, ret;
  1658. char buf[_POSIX_PATH_MAX];
  1659. for (;;) {
  1660. retry = 0;
  1661. basel = nfsnprintf( buf, sizeof(buf), "%s/", ctx->path );
  1662. for (msg = ctx->msgs; msg; msg = msg->next)
  1663. if (!(msg->status & M_DEAD) && (msg->flags & F_DELETED)) {
  1664. nfsnprintf( buf + basel, sizeof(buf) - basel, "%s/%s", subdirs[msg->status & M_RECENT], ((maildir_message_t *)msg)->base );
  1665. if (unlink( buf )) {
  1666. if (errno == ENOENT)
  1667. retry = 1;
  1668. else
  1669. sys_error( "Maildir error: cannot remove %s", buf );
  1670. } else {
  1671. msg->status |= M_DEAD;
  1672. ctx->total_msgs--;
  1673. #ifdef USE_DB
  1674. if (ctx->db && (ret = maildir_purge_msg( ctx, ((maildir_message_t *)msg)->base )) != DRV_OK) {
  1675. cb( ret, aux );
  1676. return;
  1677. }
  1678. #endif /* USE_DB */
  1679. }
  1680. }
  1681. if (!retry) {
  1682. cb( DRV_OK, aux );
  1683. return;
  1684. }
  1685. if ((ret = maildir_rescan( (maildir_store_t *)gctx )) != DRV_OK) {
  1686. cb( ret, aux );
  1687. return;
  1688. }
  1689. }
  1690. }
  1691. static void
  1692. maildir_cancel_cmds( store_t *gctx ATTR_UNUSED,
  1693. void (*cb)( void *aux ), void *aux )
  1694. {
  1695. cb( aux );
  1696. }
  1697. static void
  1698. maildir_commit_cmds( store_t *gctx )
  1699. {
  1700. (void) gctx;
  1701. }
  1702. static int
  1703. maildir_get_memory_usage( store_t *gctx ATTR_UNUSED )
  1704. {
  1705. return 0;
  1706. }
  1707. static int
  1708. maildir_get_fail_state( store_conf_t *gconf )
  1709. {
  1710. return ((maildir_store_conf_t *)gconf)->failed;
  1711. }
  1712. static int
  1713. maildir_parse_store( conffile_t *cfg, store_conf_t **storep )
  1714. {
  1715. maildir_store_conf_t *store;
  1716. if (strcasecmp( "MaildirStore", cfg->cmd ))
  1717. return 0;
  1718. store = nfcalloc( sizeof(*store) );
  1719. store->info_delimiter = FieldDelimiter;
  1720. store->gen.driver = &maildir_driver;
  1721. store->gen.name = nfstrdup( cfg->val );
  1722. while (getcline( cfg ) && cfg->cmd)
  1723. if (!strcasecmp( "Inbox", cfg->cmd ))
  1724. store->inbox = expand_strdup( cfg->val );
  1725. else if (!strcasecmp( "Path", cfg->cmd ))
  1726. store->gen.path = expand_strdup( cfg->val );
  1727. #ifdef USE_DB
  1728. else if (!strcasecmp( "AltMap", cfg->cmd ))
  1729. store->alt_map = parse_bool( cfg );
  1730. #endif /* USE_DB */
  1731. else if (!strcasecmp( "InfoDelimiter", cfg->cmd )) {
  1732. if (strlen( cfg->val ) != 1) {
  1733. error( "%s:%d: Info delimiter must be exactly one character long\n", cfg->file, cfg->line );
  1734. cfg->err = 1;
  1735. continue;
  1736. }
  1737. store->info_delimiter = cfg->val[0];
  1738. if (!ispunct( store->info_delimiter )) {
  1739. error( "%s:%d: Info delimiter must be a punctuation character\n", cfg->file, cfg->line );
  1740. cfg->err = 1;
  1741. continue;
  1742. }
  1743. } else if (!strcasecmp( "SubFolders", cfg->cmd )) {
  1744. if (!strcasecmp( "Verbatim", cfg->val )) {
  1745. store->sub_style = SUB_VERBATIM;
  1746. } else if (!strcasecmp( "Maildir++", cfg->val )) {
  1747. store->sub_style = SUB_MAILDIRPP;
  1748. } else if (!strcasecmp( "Legacy", cfg->val )) {
  1749. store->sub_style = SUB_LEGACY;
  1750. } else {
  1751. error( "%s:%d: Unrecognized SubFolders style\n", cfg->file, cfg->line );
  1752. cfg->err = 1;
  1753. }
  1754. } else
  1755. parse_generic_store( &store->gen, cfg );
  1756. if (!store->inbox)
  1757. store->inbox = expand_strdup( "~/Maildir" );
  1758. if (store->sub_style == SUB_MAILDIRPP && store->gen.path) {
  1759. error( "Maildir store '%s': Setting Path is incompatible with 'SubFolders Maildir++'\n", store->gen.name );
  1760. cfg->err = 1;
  1761. }
  1762. nfasprintf( &store->info_prefix, "%c2,", store->info_delimiter );
  1763. nfasprintf( &store->info_stop, "%c,", store->info_delimiter );
  1764. *storep = &store->gen;
  1765. return 1;
  1766. }
  1767. static int
  1768. maildir_get_caps( store_t *gctx ATTR_UNUSED )
  1769. {
  1770. return 0; /* XXX DRV_CRLF? */
  1771. }
  1772. struct driver maildir_driver = {
  1773. maildir_get_caps,
  1774. maildir_parse_store,
  1775. maildir_cleanup_drv,
  1776. maildir_alloc_store,
  1777. maildir_set_bad_callback,
  1778. maildir_connect_store,
  1779. maildir_free_store,
  1780. maildir_free_store, /* _cancel_, but it's the same */
  1781. maildir_list_store,
  1782. maildir_select_box,
  1783. maildir_get_box_path,
  1784. maildir_create_box,
  1785. maildir_open_box,
  1786. maildir_get_uidnext,
  1787. maildir_confirm_box_empty,
  1788. maildir_delete_box,
  1789. maildir_finish_delete_box,
  1790. maildir_prepare_load_box,
  1791. maildir_load_box,
  1792. maildir_fetch_msg,
  1793. maildir_store_msg,
  1794. maildir_find_new_msgs,
  1795. maildir_set_msg_flags,
  1796. maildir_trash_msg,
  1797. maildir_close_box,
  1798. maildir_cancel_cmds,
  1799. maildir_commit_cmds,
  1800. maildir_get_memory_usage,
  1801. maildir_get_fail_state,
  1802. };