drv_maildir.c 52 KB

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