drv_maildir.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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 <sys/file.h>
  35. #include <errno.h>
  36. #include <time.h>
  37. #include <utime.h>
  38. #define USE_DB 1
  39. #ifdef __linux__
  40. # define LEGACY_FLOCK 1
  41. #endif
  42. #if !defined(_POSIX_SYNCHRONIZED_IO) || _POSIX_SYNCHRONIZED_IO <= 0
  43. # define fdatasync fsync
  44. #endif
  45. #ifdef USE_DB
  46. #include <db.h>
  47. #endif /* USE_DB */
  48. typedef struct maildir_store_conf {
  49. store_conf_t gen;
  50. char *inbox;
  51. #ifdef USE_DB
  52. int alt_map;
  53. #endif /* USE_DB */
  54. char info_delimiter;
  55. char *info_prefix, *info_stop; /* precalculated from info_delimiter */
  56. } maildir_store_conf_t;
  57. typedef struct maildir_message {
  58. message_t gen;
  59. char *base;
  60. } maildir_message_t;
  61. typedef struct maildir_store {
  62. store_t gen;
  63. int uvfd, uvok, nuid, fresh;
  64. int minuid, maxuid, newuid, nexcs, *excs;
  65. char *trash;
  66. #ifdef USE_DB
  67. DB *db;
  68. #endif /* USE_DB */
  69. } maildir_store_t;
  70. #ifdef USE_DB
  71. static DBT key, value; /* no need to be reentrant, and this saves lots of memset()s */
  72. #endif /* USE_DB */
  73. static struct flock lck;
  74. static int MaildirCount;
  75. static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
  76. static uchar
  77. maildir_parse_flags( const char *info_prefix, const char *base )
  78. {
  79. const char *s;
  80. uint i;
  81. uchar flags;
  82. flags = 0;
  83. if ((s = strstr( base, info_prefix )))
  84. for (s += 3, i = 0; i < as(Flags); i++)
  85. if (strchr( s, Flags[i] ))
  86. flags |= (1 << i);
  87. return flags;
  88. }
  89. static char *
  90. maildir_join_path( const char *prefix, const char *box )
  91. {
  92. char *out, *p;
  93. int pl, bl, n;
  94. char c;
  95. pl = strlen( prefix );
  96. for (bl = 0, n = 0; (c = box[bl]); bl++)
  97. if (c == '/')
  98. n++;
  99. out = nfmalloc( pl + bl + n + 1 );
  100. memcpy( out, prefix, pl );
  101. p = out + pl;
  102. while ((c = *box++)) {
  103. *p++ = c;
  104. if (c == '/')
  105. *p++ = '.';
  106. }
  107. *p = 0;
  108. return out;
  109. }
  110. static int
  111. maildir_validate_path( const store_conf_t *conf )
  112. {
  113. struct stat st;
  114. if (!conf->path) {
  115. error( "Maildir error: store '%s' has no Path\n", conf->name );
  116. return -1;
  117. }
  118. if (stat( conf->path, &st ) || !S_ISDIR(st.st_mode)) {
  119. error( "Maildir error: cannot open store '%s'\n", conf->path );
  120. return -1;
  121. }
  122. return 0;
  123. }
  124. static void
  125. maildir_open_store( store_conf_t *conf, const char *label ATTR_UNUSED,
  126. void (*cb)( store_t *ctx, void *aux ), void *aux )
  127. {
  128. maildir_store_t *ctx;
  129. ctx = nfcalloc( sizeof(*ctx) );
  130. ctx->gen.conf = conf;
  131. ctx->uvfd = -1;
  132. if (conf->trash) {
  133. if (maildir_validate_path( conf ) < 0) {
  134. free( ctx );
  135. cb( 0, aux );
  136. return;
  137. }
  138. ctx->trash = maildir_join_path( conf->path, conf->trash );
  139. }
  140. cb( &ctx->gen, aux );
  141. }
  142. static void
  143. free_maildir_messages( message_t *msg )
  144. {
  145. message_t *tmsg;
  146. for (; (tmsg = msg); msg = tmsg) {
  147. tmsg = msg->next;
  148. free( ((maildir_message_t *)msg)->base );
  149. free( msg );
  150. }
  151. }
  152. static void
  153. maildir_cleanup( store_t *gctx )
  154. {
  155. maildir_store_t *ctx = (maildir_store_t *)gctx;
  156. free_maildir_messages( gctx->msgs );
  157. #ifdef USE_DB
  158. if (ctx->db)
  159. ctx->db->close( ctx->db, 0 );
  160. #endif /* USE_DB */
  161. free( gctx->path );
  162. free( ctx->excs );
  163. if (ctx->uvfd >= 0)
  164. close( ctx->uvfd );
  165. }
  166. static void
  167. maildir_disown_store( store_t *gctx )
  168. {
  169. maildir_cleanup( gctx );
  170. free( ((maildir_store_t *)gctx)->trash );
  171. free_string_list( gctx->boxes );
  172. free( gctx );
  173. }
  174. static void
  175. maildir_cleanup_drv( void )
  176. {
  177. }
  178. static void
  179. maildir_invoke_bad_callback( store_t *ctx )
  180. {
  181. ctx->bad_callback( ctx->bad_callback_aux );
  182. }
  183. static int maildir_list_inbox( store_t *gctx, int *flags );
  184. static int
  185. maildir_list_recurse( store_t *gctx, int isBox, int *flags, const char *inbox, int inboxLen,
  186. char *path, int pathLen, char *name, int nameLen )
  187. {
  188. DIR *dir;
  189. int pl, nl, missing;
  190. struct dirent *de;
  191. struct stat st;
  192. if (isBox) {
  193. path[pathLen++] = '/';
  194. nfsnprintf( path + pathLen, _POSIX_PATH_MAX - pathLen, "cur" );
  195. missing = stat( path, &st ) || !S_ISDIR(st.st_mode);
  196. if (!missing || isBox > 1)
  197. add_string_list( &gctx->boxes, name );
  198. if (missing)
  199. return 0;
  200. path[pathLen] = 0;
  201. name[nameLen++] = '/';
  202. }
  203. if (!(dir = opendir( path ))) {
  204. sys_error( "Maildir error: cannot list %s", path );
  205. return -1;
  206. }
  207. while ((de = readdir( dir ))) {
  208. const char *ent = de->d_name;
  209. pl = pathLen + nfsnprintf( path + pathLen, _POSIX_PATH_MAX - pathLen, "%s", ent );
  210. if (inbox && equals( path, pl, inbox, inboxLen )) {
  211. if (maildir_list_inbox( gctx, flags ) < 0) {
  212. closedir( dir );
  213. return -1;
  214. }
  215. } else {
  216. if (*ent == '.') {
  217. if (!isBox)
  218. continue;
  219. if (!ent[1] || ent[1] == '.')
  220. continue;
  221. ent++;
  222. } else {
  223. if (isBox)
  224. continue;
  225. if (!nameLen && equals( ent, -1, "INBOX", 5 )) {
  226. path[pathLen] = 0;
  227. warn( "Maildir warning: ignoring INBOX in %s\n", path );
  228. continue;
  229. }
  230. }
  231. nl = nameLen + nfsnprintf( name + nameLen, _POSIX_PATH_MAX - nameLen, "%s", ent );
  232. if (maildir_list_recurse( gctx, 1, flags, inbox, inboxLen, path, pl, name, nl ) < 0) {
  233. closedir( dir );
  234. return -1;
  235. }
  236. }
  237. }
  238. closedir (dir);
  239. return 0;
  240. }
  241. static int
  242. maildir_list_inbox( store_t *gctx, int *flags )
  243. {
  244. char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
  245. *flags &= ~LIST_INBOX;
  246. return maildir_list_recurse(
  247. gctx, 2, flags, 0, 0,
  248. path, nfsnprintf( path, _POSIX_PATH_MAX, "%s", ((maildir_store_conf_t *)gctx->conf)->inbox ),
  249. name, nfsnprintf( name, _POSIX_PATH_MAX, "INBOX" ) );
  250. }
  251. static int
  252. maildir_list_path( store_t *gctx, int *flags )
  253. {
  254. const char *inbox = ((maildir_store_conf_t *)gctx->conf)->inbox;
  255. char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
  256. if (maildir_validate_path( gctx->conf ) < 0)
  257. return -1;
  258. return maildir_list_recurse(
  259. gctx, 0, flags, inbox, strlen( inbox ),
  260. path, nfsnprintf( path, _POSIX_PATH_MAX, "%s", gctx->conf->path ),
  261. name, 0 );
  262. }
  263. static void
  264. maildir_list_store( store_t *gctx, int flags,
  265. void (*cb)( int sts, void *aux ), void *aux )
  266. {
  267. if (((flags & LIST_PATH) && maildir_list_path( gctx, &flags ) < 0) ||
  268. ((flags & LIST_INBOX) && maildir_list_inbox( gctx, &flags ) < 0)) {
  269. maildir_invoke_bad_callback( gctx );
  270. cb( DRV_CANCELED, aux );
  271. } else {
  272. cb( DRV_OK, aux );
  273. }
  274. }
  275. static const char *subdirs[] = { "cur", "new", "tmp" };
  276. typedef struct {
  277. char *base;
  278. int size;
  279. uint uid:31, recent:1;
  280. char tuid[TUIDL];
  281. } msg_t;
  282. typedef struct {
  283. msg_t *ents;
  284. int nents, nalloc;
  285. } msglist_t;
  286. static void
  287. maildir_free_scan( msglist_t *msglist )
  288. {
  289. int i;
  290. if (msglist->ents) {
  291. for (i = 0; i < msglist->nents; i++)
  292. free( msglist->ents[i].base );
  293. free( msglist->ents );
  294. }
  295. }
  296. #define _24_HOURS (3600 * 24)
  297. static int
  298. maildir_validate( const char *box, int create, maildir_store_t *ctx )
  299. {
  300. DIR *dirp;
  301. struct dirent *entry;
  302. char *p;
  303. time_t now;
  304. int i, bl, ret;
  305. struct stat st;
  306. char buf[_POSIX_PATH_MAX];
  307. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", box );
  308. if (stat( buf, &st )) {
  309. if (errno == ENOENT) {
  310. if (create) {
  311. p = memrchr( buf, '/', bl - 1 );
  312. if (*(p + 1) == '.') {
  313. *p = 0;
  314. if ((ret = maildir_validate( buf, 1, ctx )) != DRV_OK)
  315. return ret;
  316. *p = '/';
  317. }
  318. if (mkdir( buf, 0700 )) {
  319. sys_error( "Maildir error: cannot create mailbox '%s'", buf );
  320. maildir_invoke_bad_callback( &ctx->gen );
  321. return DRV_CANCELED;
  322. }
  323. for (i = 0; i < 3; i++) {
  324. memcpy( buf + bl, subdirs[i], 4 );
  325. if (mkdir( buf, 0700 )) {
  326. sys_error( "Maildir error: cannot create directory %s", buf );
  327. return DRV_BOX_BAD;
  328. }
  329. }
  330. } else {
  331. error( "Maildir error: mailbox '%s' does not exist\n", buf );
  332. return DRV_BOX_BAD;
  333. }
  334. } else {
  335. sys_error( "Maildir error: cannot access mailbox '%s'", buf );
  336. return DRV_BOX_BAD;
  337. }
  338. ctx->fresh = 1;
  339. } else {
  340. for (i = 0; i < 3; i++) {
  341. memcpy( buf + bl, subdirs[i], 4 );
  342. if (stat( buf, &st ) || !S_ISDIR(st.st_mode)) {
  343. error( "Maildir error: '%.*s' is no valid mailbox\n", bl, buf );
  344. return DRV_BOX_BAD;
  345. }
  346. }
  347. memcpy( buf + bl, "tmp/", 5 );
  348. bl += 4;
  349. if (!(dirp = opendir( buf ))) {
  350. sys_error( "Maildir error: cannot list %s", buf );
  351. return DRV_BOX_BAD;
  352. }
  353. time( &now );
  354. while ((entry = readdir( dirp ))) {
  355. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s", entry->d_name );
  356. if (stat( buf, &st )) {
  357. if (errno != ENOENT)
  358. sys_error( "Maildir error: cannot access %s", buf );
  359. } else if (S_ISREG(st.st_mode) && now - st.st_ctime >= _24_HOURS) {
  360. /* this should happen infrequently enough that it won't be
  361. * bothersome to the user to display when it occurs.
  362. */
  363. info( "Maildir notice: removing stale file %s\n", buf );
  364. if (unlink( buf ) && errno != ENOENT)
  365. sys_error( "Maildir error: cannot remove %s", buf );
  366. }
  367. }
  368. closedir( dirp );
  369. ctx->fresh = 0;
  370. }
  371. return DRV_OK;
  372. }
  373. #ifdef USE_DB
  374. static void
  375. make_key( const char *info_stop, DBT *tkey, char *name )
  376. {
  377. char *u = strpbrk( name, info_stop );
  378. tkey->data = name;
  379. tkey->size = u ? (size_t)(u - name) : strlen( name );
  380. }
  381. static int
  382. maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid )
  383. {
  384. int ret, uv[2];
  385. if (uid)
  386. *uid = ++ctx->nuid;
  387. key.data = (void *)"UIDVALIDITY";
  388. key.size = 11;
  389. uv[0] = ctx->gen.uidvalidity;
  390. uv[1] = ctx->nuid;
  391. value.data = uv;
  392. value.size = sizeof(uv);
  393. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
  394. tbork:
  395. ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
  396. return DRV_BOX_BAD;
  397. }
  398. if (uid) {
  399. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  400. value.data = uid;
  401. value.size = sizeof(*uid);
  402. if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 )))
  403. goto tbork;
  404. }
  405. if ((ret = ctx->db->sync( ctx->db, 0 ))) {
  406. ctx->db->err( ctx->db, ret, "Maildir error: db->sync()" );
  407. return DRV_BOX_BAD;
  408. }
  409. return DRV_OK;
  410. }
  411. #endif /* USE_DB */
  412. static int
  413. maildir_store_uid( maildir_store_t *ctx )
  414. {
  415. int n;
  416. char buf[128];
  417. n = sprintf( buf, "%d\n%d\n", ctx->gen.uidvalidity, ctx->nuid );
  418. lseek( ctx->uvfd, 0, SEEK_SET );
  419. if (write( ctx->uvfd, buf, n ) != n || ftruncate( ctx->uvfd, n ) || (UseFSync && fdatasync( ctx->uvfd ))) {
  420. error( "Maildir error: cannot write UIDVALIDITY.\n" );
  421. return DRV_BOX_BAD;
  422. }
  423. return DRV_OK;
  424. }
  425. static int
  426. maildir_init_uid( maildir_store_t *ctx )
  427. {
  428. ctx->gen.uidvalidity = time( 0 );
  429. ctx->nuid = 0;
  430. ctx->uvok = 0;
  431. #ifdef USE_DB
  432. if (ctx->db) {
  433. u_int32_t count;
  434. ctx->db->truncate( ctx->db, 0, &count, 0 );
  435. return maildir_set_uid( ctx, 0, 0 );
  436. }
  437. #endif /* USE_DB */
  438. return maildir_store_uid( ctx );
  439. }
  440. static int
  441. maildir_init_uid_new( maildir_store_t *ctx )
  442. {
  443. info( "Maildir notice: no UIDVALIDITY, creating new.\n" );
  444. return maildir_init_uid( ctx );
  445. }
  446. static int
  447. maildir_uidval_lock( maildir_store_t *ctx )
  448. {
  449. int n;
  450. char buf[128];
  451. #ifdef LEGACY_FLOCK
  452. /* This is legacy only */
  453. if (flock( ctx->uvfd, LOCK_EX ) < 0) {
  454. error( "Maildir error: cannot flock UIDVALIDITY.\n" );
  455. return DRV_BOX_BAD;
  456. }
  457. #endif
  458. /* This (theoretically) works over NFS. Let's hope nobody else did
  459. the same in the opposite order, as we'd deadlock then. */
  460. #if SEEK_SET != 0
  461. lck.l_whence = SEEK_SET;
  462. #endif
  463. lck.l_type = F_WRLCK;
  464. if (fcntl( ctx->uvfd, F_SETLKW, &lck )) {
  465. error( "Maildir error: cannot fcntl lock UIDVALIDITY.\n" );
  466. return DRV_BOX_BAD;
  467. }
  468. lseek( ctx->uvfd, 0, SEEK_SET );
  469. if ((n = read( ctx->uvfd, buf, sizeof(buf) - 1 )) <= 0 ||
  470. (buf[n] = 0, sscanf( buf, "%d\n%d", &ctx->gen.uidvalidity, &ctx->nuid ) != 2)) {
  471. #if 1
  472. /* In a generic driver, resetting the UID validity would be the right thing.
  473. * But this would mess up the sync state completely. So better bail out and
  474. * give the user a chance to fix the mailbox. */
  475. if (n) {
  476. error( "Maildir error: cannot read UIDVALIDITY.\n" );
  477. return DRV_BOX_BAD;
  478. }
  479. #endif
  480. return maildir_init_uid_new( ctx );
  481. } else
  482. ctx->uvok = 1;
  483. return DRV_OK;
  484. }
  485. static void
  486. maildir_uidval_unlock( maildir_store_t *ctx )
  487. {
  488. lck.l_type = F_UNLCK;
  489. fcntl( ctx->uvfd, F_SETLK, &lck );
  490. #ifdef LEGACY_FLOCK
  491. /* This is legacy only */
  492. flock( ctx->uvfd, LOCK_UN );
  493. #endif
  494. }
  495. static int
  496. maildir_obtain_uid( maildir_store_t *ctx, int *uid )
  497. {
  498. *uid = ++ctx->nuid;
  499. return maildir_store_uid( ctx );
  500. }
  501. static int
  502. maildir_compare( const void *l, const void *r )
  503. {
  504. msg_t *lm = (msg_t *)l, *rm = (msg_t *)r;
  505. char *ldot, *rdot, *ldot2, *rdot2, *lseq, *rseq;
  506. int ret, llen, rlen;
  507. if ((ret = lm->uid - rm->uid))
  508. return ret;
  509. /* No UID, so sort by arrival date. We should not do this, but we rely
  510. on the suggested unique file name scheme - we have no choice. */
  511. /* The first field are always the seconds. Alphabetical sort should be
  512. faster than numeric. */
  513. if (!(ldot = strchr( lm->base, '.' )) || !(rdot = strchr( rm->base, '.' )))
  514. goto stronly; /* Should never happen ... */
  515. llen = ldot - lm->base, rlen = rdot - rm->base;
  516. /* The shorter number is smaller. Really. This won't trigger with any
  517. mail created after Sep 9 2001 anyway. */
  518. if ((ret = llen - rlen))
  519. return ret;
  520. if ((ret = memcmp( lm->base, rm->base, llen )))
  521. return ret;
  522. ldot++, rdot++;
  523. if ((llen = strtol( ldot, &ldot2, 10 ))) {
  524. if (!(rlen = strtol( rdot, &rdot2, 10 )))
  525. goto stronly; /* Comparing apples to oranges ... */
  526. /* Classical PID specs */
  527. if ((ret = llen - rlen)) {
  528. retpid:
  529. /* Handle PID wraparound. This works only on systems
  530. where PIDs are not reused too fast */
  531. if (ret > 20000 || ret < -20000)
  532. ret = -ret;
  533. return ret;
  534. }
  535. return (*ldot2 != '_' ? 0 : atoi( ldot2 + 1 )) -
  536. (*rdot2 != '_' ? 0 : atoi( rdot2 + 1 ));
  537. }
  538. if (!(ldot2 = strchr( ldot, '.' )) || !(rdot2 = strchr( rdot, '.' )))
  539. goto stronly; /* Should never happen ... */
  540. llen = ldot2 - ldot, rlen = rdot2 - rdot;
  541. if (((lseq = memchr( ldot, '#', llen )) && (rseq = memchr( rdot, '#', rlen ))) ||
  542. ((lseq = memchr( ldot, 'M', llen )) && (rseq = memchr( rdot, 'M', rlen ))))
  543. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  544. if ((lseq = memchr( ldot, 'P', llen )) && (rseq = memchr( rdot, 'P', rlen ))) {
  545. if ((ret = atoi( lseq + 1 ) - atoi( rseq + 1 )))
  546. goto retpid;
  547. if ((lseq = memchr( ldot, 'Q', llen )) && (rseq = memchr( rdot, 'Q', rlen )))
  548. return atoi( lseq + 1 ) - atoi( rseq + 1 );
  549. }
  550. stronly:
  551. /* Fall-back, so the sort order is defined at all */
  552. return strcmp( lm->base, rm->base );
  553. }
  554. static int
  555. maildir_scan( maildir_store_t *ctx, msglist_t *msglist )
  556. {
  557. maildir_store_conf_t *conf = (maildir_store_conf_t *)ctx->gen.conf;
  558. DIR *d;
  559. FILE *f;
  560. struct dirent *e;
  561. const char *u, *ru;
  562. #ifdef USE_DB
  563. DB *tdb;
  564. DBC *dbc;
  565. #endif /* USE_DB */
  566. msg_t *entry;
  567. int i, j, uid, bl, fnl, ret;
  568. time_t now, stamps[2];
  569. struct stat st;
  570. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  571. #ifdef USE_DB
  572. if (!ctx->db)
  573. #endif /* USE_DB */
  574. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
  575. return ret;
  576. again:
  577. msglist->ents = 0;
  578. msglist->nents = msglist->nalloc = 0;
  579. ctx->gen.count = ctx->gen.recent = 0;
  580. if (ctx->uvok || ctx->maxuid == INT_MAX) {
  581. #ifdef USE_DB
  582. if (ctx->db) {
  583. if (db_create( &tdb, 0, 0 )) {
  584. fputs( "Maildir error: db_create() failed\n", stderr );
  585. return DRV_BOX_BAD;
  586. }
  587. if ((tdb->open)( tdb, 0, 0, 0, DB_HASH, DB_CREATE, 0 )) {
  588. fputs( "Maildir error: tdb->open() failed\n", stderr );
  589. bork:
  590. tdb->close( tdb, 0 );
  591. return DRV_BOX_BAD;
  592. }
  593. }
  594. #endif /* USE_DB */
  595. bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", ctx->gen.path );
  596. restat:
  597. now = time( 0 );
  598. for (i = 0; i < 2; i++) {
  599. memcpy( buf + bl, subdirs[i], 4 );
  600. if (stat( buf, &st )) {
  601. sys_error( "Maildir error: cannot stat %s", buf );
  602. goto dfail;
  603. }
  604. if (st.st_mtime == now && !(DFlags & ZERODELAY)) {
  605. /* If the modification happened during this second, we wouldn't be able to
  606. * tell if there were further modifications during this second. So wait.
  607. * This has the nice side effect that we wait for "batches" of changes to
  608. * complete. On the downside, it can potentially block indefinitely. */
  609. info( "Maildir notice: sleeping due to recent directory modification.\n" );
  610. sleep( 1 ); /* FIXME: should make this async */
  611. goto restat;
  612. }
  613. stamps[i] = st.st_mtime;
  614. }
  615. for (i = 0; i < 2; i++) {
  616. memcpy( buf + bl, subdirs[i], 4 );
  617. if (!(d = opendir( buf ))) {
  618. sys_error( "Maildir error: cannot list %s", buf );
  619. rfail:
  620. maildir_free_scan( msglist );
  621. dfail:
  622. #ifdef USE_DB
  623. if (ctx->db)
  624. tdb->close( tdb, 0 );
  625. #endif /* USE_DB */
  626. return DRV_BOX_BAD;
  627. }
  628. while ((e = readdir( d ))) {
  629. if (*e->d_name == '.')
  630. continue;
  631. ctx->gen.count++;
  632. ctx->gen.recent += i;
  633. #ifdef USE_DB
  634. if (ctx->db) {
  635. make_key( conf->info_stop, &key, e->d_name );
  636. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  637. if (ret != DB_NOTFOUND) {
  638. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  639. mbork:
  640. maildir_free_scan( msglist );
  641. closedir( d );
  642. goto bork;
  643. }
  644. uid = INT_MAX;
  645. } else {
  646. value.size = 0;
  647. if ((ret = tdb->put( tdb, 0, &key, &value, 0 ))) {
  648. tdb->err( tdb, ret, "Maildir error: tdb->put()" );
  649. goto mbork;
  650. }
  651. uid = *(int *)value.data;
  652. }
  653. } else
  654. #endif /* USE_DB */
  655. {
  656. uid = (ctx->uvok && (u = strstr( e->d_name, ",U=" ))) ? atoi( u + 3 ) : 0;
  657. if (!uid)
  658. uid = INT_MAX;
  659. }
  660. if (uid <= ctx->maxuid) {
  661. if (uid < ctx->minuid) {
  662. for (j = 0; j < ctx->nexcs; j++)
  663. if (ctx->excs[j] == uid)
  664. goto oke;
  665. continue;
  666. oke: ;
  667. }
  668. if (msglist->nalloc == msglist->nents) {
  669. msglist->nalloc = msglist->nalloc * 2 + 100;
  670. msglist->ents = nfrealloc( msglist->ents, msglist->nalloc * sizeof(msg_t) );
  671. }
  672. entry = &msglist->ents[msglist->nents++];
  673. entry->base = nfstrdup( e->d_name );
  674. entry->uid = uid;
  675. entry->recent = i;
  676. entry->size = 0;
  677. entry->tuid[0] = 0;
  678. }
  679. }
  680. closedir( d );
  681. }
  682. for (i = 0; i < 2; i++) {
  683. memcpy( buf + bl, subdirs[i], 4 );
  684. if (stat( buf, &st )) {
  685. sys_error( "Maildir error: cannot re-stat %s", buf );
  686. goto rfail;
  687. }
  688. if (st.st_mtime != stamps[i]) {
  689. /* Somebody messed with the mailbox since we started listing it. */
  690. #ifdef USE_DB
  691. if (ctx->db)
  692. tdb->close( tdb, 0 );
  693. #endif /* USE_DB */
  694. maildir_free_scan( msglist );
  695. goto again;
  696. }
  697. }
  698. #ifdef USE_DB
  699. if (ctx->db) {
  700. if ((ret = ctx->db->cursor( ctx->db, 0, &dbc, 0 )))
  701. ctx->db->err( ctx->db, ret, "Maildir error: db->cursor()" );
  702. else {
  703. for (;;) {
  704. if ((ret = dbc->c_get( dbc, &key, &value, DB_NEXT ))) {
  705. if (ret != DB_NOTFOUND)
  706. ctx->db->err( ctx->db, ret, "Maildir error: db->c_get()" );
  707. break;
  708. }
  709. if (!equals( key.data, key.size, "UIDVALIDITY", 11 ) &&
  710. (ret = tdb->get( tdb, 0, &key, &value, 0 ))) {
  711. if (ret != DB_NOTFOUND) {
  712. tdb->err( tdb, ret, "Maildir error: tdb->get()" );
  713. break;
  714. }
  715. if ((ret = dbc->c_del( dbc, 0 ))) {
  716. ctx->db->err( ctx->db, ret, "Maildir error: db->c_del()" );
  717. break;
  718. }
  719. }
  720. }
  721. dbc->c_close( dbc );
  722. }
  723. tdb->close( tdb, 0 );
  724. }
  725. #endif /* USE_DB */
  726. qsort( msglist->ents, msglist->nents, sizeof(msg_t), maildir_compare );
  727. for (uid = i = 0; i < msglist->nents; i++) {
  728. entry = &msglist->ents[i];
  729. if (entry->uid != INT_MAX) {
  730. if (uid == entry->uid) {
  731. #if 1
  732. /* See comment in maildir_uidval_lock() why this is fatal. */
  733. error( "Maildir error: duplicate UID %d.\n", uid );
  734. maildir_free_scan( msglist );
  735. return DRV_BOX_BAD;
  736. #else
  737. info( "Maildir notice: duplicate UID; changing UIDVALIDITY.\n");
  738. if ((ret = maildir_init_uid( ctx )) != DRV_OK) {
  739. maildir_free_scan( msglist );
  740. return ret;
  741. }
  742. maildir_free_scan( msglist );
  743. goto again;
  744. #endif
  745. }
  746. uid = entry->uid;
  747. if (uid > ctx->nuid) {
  748. /* In principle, we could just warn and top up nuid. However, getting into this
  749. * situation might indicate some serious trouble, so let's not make it worse. */
  750. error( "Maildir error: UID %d is beyond highest assigned UID %d.\n", uid, ctx->nuid );
  751. maildir_free_scan( msglist );
  752. return DRV_BOX_BAD;
  753. }
  754. if ((ctx->gen.opts & OPEN_SIZE) || ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid))
  755. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  756. #ifdef USE_DB
  757. } else if (ctx->db) {
  758. if ((ret = maildir_set_uid( ctx, entry->base, &uid )) != DRV_OK) {
  759. maildir_free_scan( msglist );
  760. return ret;
  761. }
  762. entry->uid = uid;
  763. if ((ctx->gen.opts & OPEN_SIZE) || ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid))
  764. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
  765. #endif /* USE_DB */
  766. } else {
  767. if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  768. maildir_free_scan( msglist );
  769. return ret;
  770. }
  771. entry->uid = uid;
  772. if ((u = strstr( entry->base, ",U=" )))
  773. for (ru = u + 3; isdigit( (uchar)*ru ); ru++);
  774. else
  775. u = ru = strchr( entry->base, conf->info_delimiter );
  776. fnl = (u ?
  777. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%.*s,U=%d%s", subdirs[entry->recent], (int)(u - entry->base), entry->base, uid, ru ) :
  778. nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s,U=%d", subdirs[entry->recent], entry->base, uid ))
  779. + 1 - 4;
  780. memcpy( nbuf, buf, bl + 4 );
  781. nfsnprintf( nbuf + bl + 4, sizeof(nbuf) - bl - 4, "%s", entry->base );
  782. if (rename( nbuf, buf )) {
  783. if (errno != ENOENT) {
  784. sys_error( "Maildir error: cannot rename %s to %s", nbuf, buf );
  785. fail:
  786. maildir_free_scan( msglist );
  787. return DRV_BOX_BAD;
  788. }
  789. retry:
  790. maildir_free_scan( msglist );
  791. goto again;
  792. }
  793. free( entry->base );
  794. entry->base = nfmalloc( fnl );
  795. memcpy( entry->base, buf + bl + 4, fnl );
  796. }
  797. if (ctx->gen.opts & OPEN_SIZE) {
  798. if (stat( buf, &st )) {
  799. if (errno != ENOENT) {
  800. sys_error( "Maildir error: cannot stat %s", buf );
  801. goto fail;
  802. }
  803. goto retry;
  804. }
  805. entry->size = st.st_size;
  806. }
  807. if ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid) {
  808. if (!(f = fopen( buf, "r" ))) {
  809. if (errno != ENOENT) {
  810. sys_error( "Maildir error: cannot open %s", buf );
  811. goto fail;
  812. }
  813. goto retry;
  814. }
  815. while (fgets( nbuf, sizeof(nbuf), f )) {
  816. if (!nbuf[0] || nbuf[0] == '\n')
  817. break;
  818. if (starts_with( nbuf, -1, "X-TUID: ", 8 ) && nbuf[8 + TUIDL] == '\n') {
  819. memcpy( entry->tuid, nbuf + 8, TUIDL );
  820. break;
  821. }
  822. }
  823. fclose( f );
  824. }
  825. }
  826. ctx->uvok = 1;
  827. }
  828. #ifdef USE_DB
  829. if (!ctx->db)
  830. #endif /* ! USE_DB */
  831. maildir_uidval_unlock( ctx );
  832. return DRV_OK;
  833. }
  834. static void
  835. maildir_init_msg( maildir_store_t *ctx, maildir_message_t *msg, msg_t *entry )
  836. {
  837. msg->base = entry->base;
  838. entry->base = 0; /* prevent deletion */
  839. msg->gen.size = entry->size;
  840. msg->gen.srec = 0;
  841. strncpy( msg->gen.tuid, entry->tuid, TUIDL );
  842. if (entry->recent)
  843. msg->gen.status |= M_RECENT;
  844. if (ctx->gen.opts & OPEN_FLAGS) {
  845. msg->gen.status |= M_FLAGS;
  846. msg->gen.flags = maildir_parse_flags( ((maildir_store_conf_t *)ctx->gen.conf)->info_prefix, msg->base );
  847. } else
  848. msg->gen.flags = 0;
  849. }
  850. static void
  851. maildir_app_msg( maildir_store_t *ctx, message_t ***msgapp, msg_t *entry )
  852. {
  853. maildir_message_t *msg = nfmalloc( sizeof(*msg) );
  854. msg->gen.next = **msgapp;
  855. **msgapp = &msg->gen;
  856. *msgapp = &msg->gen.next;
  857. msg->gen.uid = entry->uid;
  858. msg->gen.status = 0;
  859. maildir_init_msg( ctx, msg, entry );
  860. }
  861. static int
  862. maildir_select_box( store_t *gctx, const char *name )
  863. {
  864. maildir_store_t *ctx = (maildir_store_t *)gctx;
  865. maildir_cleanup( gctx );
  866. gctx->msgs = 0;
  867. ctx->excs = 0;
  868. ctx->uvfd = -1;
  869. #ifdef USE_DB
  870. ctx->db = 0;
  871. #endif /* USE_DB */
  872. if (starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) {
  873. gctx->path = maildir_join_path( ((maildir_store_conf_t *)gctx->conf)->inbox, name + 5 );
  874. } else {
  875. if (maildir_validate_path( gctx->conf ) < 0) {
  876. gctx->path = 0;
  877. return DRV_CANCELED;
  878. }
  879. gctx->path = maildir_join_path( gctx->conf->path, name );
  880. }
  881. return DRV_OK;
  882. }
  883. static void
  884. maildir_open_box( store_t *gctx, int create,
  885. void (*cb)( int sts, void *aux ), void *aux )
  886. {
  887. maildir_store_t *ctx = (maildir_store_t *)gctx;
  888. int ret;
  889. #ifdef USE_DB
  890. struct stat st;
  891. #endif /* USE_DB */
  892. char uvpath[_POSIX_PATH_MAX];
  893. if ((ret = maildir_validate( gctx->path, create, ctx )) != DRV_OK) {
  894. cb( ret, aux );
  895. return;
  896. }
  897. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  898. #ifndef USE_DB
  899. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) < 0) {
  900. sys_error( "Maildir error: cannot write %s", uvpath );
  901. cb( DRV_BOX_BAD, aux );
  902. return;
  903. }
  904. #else
  905. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  906. nfsnprintf( uvpath, sizeof(uvpath), "%s/.isyncuidmap.db", gctx->path );
  907. if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
  908. if (((maildir_store_conf_t *)gctx->conf)->alt_map) {
  909. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  910. goto dbok;
  911. } else {
  912. nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
  913. if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
  914. goto fnok;
  915. }
  916. sys_error( "Maildir error: cannot write %s", uvpath );
  917. cb( DRV_BOX_BAD, aux );
  918. return;
  919. }
  920. dbok:
  921. #if SEEK_SET != 0
  922. lck.l_whence = SEEK_SET;
  923. #endif
  924. lck.l_type = F_WRLCK;
  925. if (fcntl( ctx->uvfd, F_SETLKW, &lck )) {
  926. sys_error( "Maildir error: cannot lock %s", uvpath );
  927. cb( DRV_BOX_BAD, aux );
  928. return;
  929. }
  930. if (fstat( ctx->uvfd, &st )) {
  931. sys_error( "Maildir error: cannot stat %s", uvpath );
  932. cb( DRV_BOX_BAD, aux );
  933. return;
  934. }
  935. if (db_create( &ctx->db, 0, 0 )) {
  936. fputs( "Maildir error: db_create() failed\n", stderr );
  937. cb( DRV_BOX_BAD, aux );
  938. return;
  939. }
  940. if ((ret = (ctx->db->open)( ctx->db, 0, uvpath, 0, DB_HASH,
  941. st.st_size ? 0 : DB_CREATE | DB_TRUNCATE, 0 ))) {
  942. ctx->db->err( ctx->db, ret, "Maildir error: db->open(%s)", uvpath );
  943. cb( DRV_BOX_BAD, aux );
  944. return;
  945. }
  946. key.data = (void *)"UIDVALIDITY";
  947. key.size = 11;
  948. if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
  949. if (ret != DB_NOTFOUND) {
  950. ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
  951. cb( DRV_BOX_BAD, aux );
  952. return;
  953. }
  954. if (maildir_init_uid_new( ctx ) != DRV_OK) {
  955. cb( DRV_BOX_BAD, aux );
  956. return;
  957. }
  958. } else {
  959. ctx->gen.uidvalidity = ((int *)value.data)[0];
  960. ctx->nuid = ((int *)value.data)[1];
  961. ctx->uvok = 1;
  962. }
  963. cb( DRV_OK, aux );
  964. return;
  965. }
  966. fnok:
  967. #endif /* USE_DB */
  968. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK) {
  969. cb( ret, aux );
  970. return;
  971. }
  972. maildir_uidval_unlock( ctx );
  973. cb( DRV_OK, aux );
  974. }
  975. static void
  976. maildir_prepare_load_box( store_t *gctx, int opts )
  977. {
  978. if (opts & OPEN_SETFLAGS)
  979. opts |= OPEN_OLD;
  980. if (opts & OPEN_EXPUNGE)
  981. opts |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
  982. gctx->opts = opts;
  983. }
  984. static void
  985. maildir_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int *excs, int nexcs,
  986. void (*cb)( int sts, void *aux ), void *aux )
  987. {
  988. maildir_store_t *ctx = (maildir_store_t *)gctx;
  989. message_t **msgapp;
  990. msglist_t msglist;
  991. int i;
  992. ctx->minuid = minuid;
  993. ctx->maxuid = maxuid;
  994. ctx->newuid = newuid;
  995. ctx->excs = nfrealloc( excs, nexcs * sizeof(int) );
  996. ctx->nexcs = nexcs;
  997. if (ctx->fresh) {
  998. ctx->gen.count = ctx->gen.recent = 0;
  999. goto dontscan;
  1000. }
  1001. if (maildir_scan( ctx, &msglist ) != DRV_OK) {
  1002. cb( DRV_BOX_BAD, aux );
  1003. return;
  1004. }
  1005. msgapp = &ctx->gen.msgs;
  1006. for (i = 0; i < msglist.nents; i++)
  1007. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1008. maildir_free_scan( &msglist );
  1009. dontscan:
  1010. cb( DRV_OK, aux );
  1011. }
  1012. static int
  1013. maildir_rescan( maildir_store_t *ctx )
  1014. {
  1015. message_t **msgapp;
  1016. maildir_message_t *msg;
  1017. msglist_t msglist;
  1018. int i;
  1019. if (maildir_scan( ctx, &msglist ) != DRV_OK)
  1020. return DRV_BOX_BAD;
  1021. for (msgapp = &ctx->gen.msgs, i = 0;
  1022. (msg = (maildir_message_t *)*msgapp) || i < msglist.nents; )
  1023. {
  1024. if (!msg) {
  1025. #if 0
  1026. debug( "adding new message %d\n", msglist.ents[i].uid );
  1027. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1028. #else
  1029. debug( "ignoring new message %d\n", msglist.ents[i].uid );
  1030. #endif
  1031. i++;
  1032. } else if (i >= msglist.nents) {
  1033. debug( "purging deleted message %d\n", msg->gen.uid );
  1034. msg->gen.status = M_DEAD;
  1035. msgapp = &msg->gen.next;
  1036. } else if (msglist.ents[i].uid < msg->gen.uid) {
  1037. /* this should not happen, actually */
  1038. #if 0
  1039. debug( "adding new message %d\n", msglist.ents[i].uid );
  1040. maildir_app_msg( ctx, &msgapp, msglist.ents + i );
  1041. #else
  1042. debug( "ignoring new message %d\n", msglist.ents[i].uid );
  1043. #endif
  1044. i++;
  1045. } else if (msglist.ents[i].uid > msg->gen.uid) {
  1046. debug( "purging deleted message %d\n", msg->gen.uid );
  1047. msg->gen.status = M_DEAD;
  1048. msgapp = &msg->gen.next;
  1049. } else {
  1050. debug( "updating message %d\n", msg->gen.uid );
  1051. msg->gen.status &= ~(M_FLAGS|M_RECENT);
  1052. free( msg->base );
  1053. maildir_init_msg( ctx, msg, msglist.ents + i );
  1054. i++, msgapp = &msg->gen.next;
  1055. }
  1056. }
  1057. maildir_free_scan( &msglist );
  1058. return DRV_OK;
  1059. }
  1060. static int
  1061. maildir_again( maildir_store_t *ctx, maildir_message_t *msg,
  1062. const char *err, const char *fn, const char *fn2 )
  1063. {
  1064. int ret;
  1065. if (errno != ENOENT) {
  1066. sys_error( err, fn, fn2 );
  1067. return DRV_BOX_BAD;
  1068. }
  1069. if ((ret = maildir_rescan( ctx )) != DRV_OK)
  1070. return ret;
  1071. return (msg->gen.status & M_DEAD) ? DRV_MSG_BAD : DRV_OK;
  1072. }
  1073. static void
  1074. maildir_fetch_msg( store_t *gctx, message_t *gmsg, msg_data_t *data,
  1075. void (*cb)( int sts, void *aux ), void *aux )
  1076. {
  1077. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1078. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1079. int fd, ret;
  1080. struct stat st;
  1081. char buf[_POSIX_PATH_MAX];
  1082. for (;;) {
  1083. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1084. if ((fd = open( buf, O_RDONLY )) >= 0)
  1085. break;
  1086. if ((ret = maildir_again( ctx, msg, "Cannot open %s", buf, 0 )) != DRV_OK) {
  1087. cb( ret, aux );
  1088. return;
  1089. }
  1090. }
  1091. fstat( fd, &st );
  1092. data->len = st.st_size;
  1093. if (data->date == -1)
  1094. data->date = st.st_mtime;
  1095. data->data = nfmalloc( data->len );
  1096. if (read( fd, data->data, data->len ) != data->len) {
  1097. sys_error( "Maildir error: cannot read %s", buf );
  1098. close( fd );
  1099. cb( DRV_MSG_BAD, aux );
  1100. return;
  1101. }
  1102. close( fd );
  1103. if (!(gmsg->status & M_FLAGS))
  1104. data->flags = maildir_parse_flags( ((maildir_store_conf_t *)gctx->conf)->info_prefix, msg->base );
  1105. cb( DRV_OK, aux );
  1106. }
  1107. static int
  1108. maildir_make_flags( char info_delimiter, int flags, char *buf )
  1109. {
  1110. uint i, d;
  1111. buf[0] = info_delimiter;
  1112. buf[1] = '2';
  1113. buf[2] = ',';
  1114. for (d = 3, i = 0; i < as(Flags); i++)
  1115. if (flags & (1 << i))
  1116. buf[d++] = Flags[i];
  1117. buf[d] = 0;
  1118. return d;
  1119. }
  1120. static void
  1121. maildir_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1122. void (*cb)( int sts, int uid, void *aux ), void *aux )
  1123. {
  1124. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1125. const char *box;
  1126. int ret, fd, bl, uid;
  1127. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX], fbuf[NUM_FLAGS + 3], base[128];
  1128. bl = nfsnprintf( base, sizeof(base), "%ld.%d_%d.%s", (long)time( 0 ), Pid, ++MaildirCount, Hostname );
  1129. if (!to_trash) {
  1130. #ifdef USE_DB
  1131. if (ctx->db) {
  1132. if ((ret = maildir_set_uid( ctx, base, &uid )) != DRV_OK) {
  1133. free( data->data );
  1134. cb( ret, 0, aux );
  1135. return;
  1136. }
  1137. } else
  1138. #endif /* USE_DB */
  1139. {
  1140. if ((ret = maildir_uidval_lock( ctx )) != DRV_OK ||
  1141. (ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
  1142. free( data->data );
  1143. cb( ret, 0, aux );
  1144. return;
  1145. }
  1146. maildir_uidval_unlock( ctx );
  1147. nfsnprintf( base + bl, sizeof(base) - bl, ",U=%d", uid );
  1148. }
  1149. box = gctx->path;
  1150. } else {
  1151. box = ctx->trash;
  1152. }
  1153. maildir_make_flags( ((maildir_store_conf_t *)gctx->conf)->info_delimiter, data->flags, fbuf );
  1154. nfsnprintf( buf, sizeof(buf), "%s/tmp/%s%s", box, base, fbuf );
  1155. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1156. if (errno != ENOENT || !to_trash) {
  1157. sys_error( "Maildir error: cannot create %s", buf );
  1158. free( data->data );
  1159. cb( DRV_BOX_BAD, 0, aux );
  1160. return;
  1161. }
  1162. if ((ret = maildir_validate( box, 1, ctx )) != DRV_OK) {
  1163. free( data->data );
  1164. cb( ret, 0, aux );
  1165. return;
  1166. }
  1167. if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
  1168. sys_error( "Maildir error: cannot create %s", buf );
  1169. free( data->data );
  1170. cb( DRV_BOX_BAD, 0, aux );
  1171. return;
  1172. }
  1173. }
  1174. ret = write( fd, data->data, data->len );
  1175. free( data->data );
  1176. if (ret != data->len || (UseFSync && (ret = fsync( fd )))) {
  1177. if (ret < 0)
  1178. sys_error( "Maildir error: cannot write %s", buf );
  1179. else
  1180. error( "Maildir error: cannot write %s. Disk full?\n", buf );
  1181. close( fd );
  1182. cb( DRV_BOX_BAD, 0, aux );
  1183. return;
  1184. }
  1185. if (close( fd ) < 0) {
  1186. /* Quota exceeded may cause this. */
  1187. sys_error( "Maildir error: cannot write %s", buf );
  1188. cb( DRV_BOX_BAD, 0, aux );
  1189. return;
  1190. }
  1191. if (data->date) {
  1192. /* Set atime and mtime according to INTERNALDATE or mtime of source message */
  1193. struct utimbuf utimebuf;
  1194. utimebuf.actime = utimebuf.modtime = data->date;
  1195. if (utime( buf, &utimebuf ) < 0) {
  1196. sys_error( "Maildir error: cannot set times for %s", buf );
  1197. cb( DRV_BOX_BAD, 0, aux );
  1198. return;
  1199. }
  1200. }
  1201. /* Moving seen messages to cur/ is strictly speaking incorrect, but makes mutt happy. */
  1202. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%s%s", box, subdirs[!(data->flags & F_SEEN)], base, fbuf );
  1203. if (rename( buf, nbuf )) {
  1204. sys_error( "Maildir error: cannot rename %s to %s", buf, nbuf );
  1205. cb( DRV_BOX_BAD, 0, aux );
  1206. return;
  1207. }
  1208. cb( DRV_OK, uid, aux );
  1209. }
  1210. static void
  1211. maildir_find_new_msgs( store_t *gctx ATTR_UNUSED, int newuid ATTR_UNUSED,
  1212. void (*cb)( int sts, void *aux ) ATTR_UNUSED, void *aux ATTR_UNUSED )
  1213. {
  1214. assert( !"maildir_find_new_msgs is not supposed to be called" );
  1215. }
  1216. static void
  1217. maildir_set_msg_flags( store_t *gctx, message_t *gmsg, int uid ATTR_UNUSED, int add, int del,
  1218. void (*cb)( int sts, void *aux ), void *aux )
  1219. {
  1220. maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
  1221. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1222. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1223. char *s, *p;
  1224. uint i;
  1225. int j, ret, ol, fl, bbl, bl, tl;
  1226. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1227. bbl = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1228. memcpy( nbuf, gctx->path, bbl - 1 );
  1229. memcpy( nbuf + bbl - 1, "/cur/", 5 );
  1230. for (;;) {
  1231. bl = bbl + nfsnprintf( buf + bbl, sizeof(buf) - bbl, "%s/", subdirs[gmsg->status & M_RECENT] );
  1232. ol = strlen( msg->base );
  1233. if ((int)sizeof(buf) - bl < ol + 3 + NUM_FLAGS)
  1234. oob();
  1235. memcpy( buf + bl, msg->base, ol + 1 );
  1236. memcpy( nbuf + bl, msg->base, ol + 1 );
  1237. if ((s = strstr( nbuf + bl, conf->info_prefix ))) {
  1238. s += 3;
  1239. fl = ol - (s - (nbuf + bl));
  1240. for (i = 0; i < as(Flags); i++) {
  1241. if ((p = strchr( s, Flags[i] ))) {
  1242. if (del & (1 << i)) {
  1243. memcpy( p, p + 1, fl - (p - s) );
  1244. fl--;
  1245. }
  1246. } else if (add & (1 << i)) {
  1247. for (j = 0; j < fl && Flags[i] > s[j]; j++);
  1248. fl++;
  1249. memmove( s + j + 1, s + j, fl - j );
  1250. s[j] = Flags[i];
  1251. }
  1252. }
  1253. tl = ol + 3 + fl;
  1254. } else {
  1255. tl = ol + maildir_make_flags( conf->info_delimiter, msg->gen.flags, nbuf + bl + ol );
  1256. }
  1257. if (!rename( buf, nbuf ))
  1258. break;
  1259. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot rename %s to %s", buf, nbuf )) != DRV_OK) {
  1260. cb( ret, aux );
  1261. return;
  1262. }
  1263. }
  1264. free( msg->base );
  1265. msg->base = nfmalloc( tl + 1 );
  1266. memcpy( msg->base, nbuf + bl, tl + 1 );
  1267. msg->gen.flags |= add;
  1268. msg->gen.flags &= ~del;
  1269. gmsg->status &= ~M_RECENT;
  1270. cb( DRV_OK, aux );
  1271. }
  1272. #ifdef USE_DB
  1273. static int
  1274. maildir_purge_msg( maildir_store_t *ctx, const char *name )
  1275. {
  1276. int ret;
  1277. make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
  1278. if ((ret = ctx->db->del( ctx->db, 0, &key, 0 ))) {
  1279. ctx->db->err( ctx->db, ret, "Maildir error: db->del()" );
  1280. return DRV_BOX_BAD;
  1281. }
  1282. return DRV_OK;
  1283. }
  1284. #endif /* USE_DB */
  1285. static void
  1286. maildir_trash_msg( store_t *gctx, message_t *gmsg,
  1287. void (*cb)( int sts, void *aux ), void *aux )
  1288. {
  1289. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1290. maildir_message_t *msg = (maildir_message_t *)gmsg;
  1291. char *s;
  1292. int ret;
  1293. struct stat st;
  1294. char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
  1295. for (;;) {
  1296. nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
  1297. s = strstr( msg->base, ((maildir_store_conf_t *)gctx->conf)->info_prefix );
  1298. nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%ld.%d_%d.%s%s", ctx->trash,
  1299. subdirs[gmsg->status & M_RECENT], (long)time( 0 ), Pid, ++MaildirCount, Hostname, s ? s : "" );
  1300. if (!rename( buf, nbuf ))
  1301. break;
  1302. if (!stat( buf, &st )) {
  1303. if ((ret = maildir_validate( ctx->trash, 1, ctx )) != DRV_OK) {
  1304. cb( ret, aux );
  1305. return;
  1306. }
  1307. if (!rename( buf, nbuf ))
  1308. break;
  1309. if (errno != ENOENT) {
  1310. sys_error( "Maildir error: cannot move %s to %s", buf, nbuf );
  1311. cb( DRV_BOX_BAD, aux );
  1312. return;
  1313. }
  1314. }
  1315. if ((ret = maildir_again( ctx, msg, "Maildir error: cannot move %s to %s", buf, nbuf )) != DRV_OK) {
  1316. cb( ret, aux );
  1317. return;
  1318. }
  1319. }
  1320. gmsg->status |= M_DEAD;
  1321. gctx->count--;
  1322. #ifdef USE_DB
  1323. if (ctx->db) {
  1324. cb( maildir_purge_msg( ctx, msg->base ), aux );
  1325. return;
  1326. }
  1327. #endif /* USE_DB */
  1328. cb( DRV_OK, aux );
  1329. }
  1330. static void
  1331. maildir_close_box( store_t *gctx,
  1332. void (*cb)( int sts, void *aux ), void *aux )
  1333. {
  1334. #ifdef USE_DB
  1335. maildir_store_t *ctx = (maildir_store_t *)gctx;
  1336. #endif /* USE_DB */
  1337. message_t *msg;
  1338. int basel, retry, ret;
  1339. char buf[_POSIX_PATH_MAX];
  1340. for (;;) {
  1341. retry = 0;
  1342. basel = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
  1343. for (msg = gctx->msgs; msg; msg = msg->next)
  1344. if (!(msg->status & M_DEAD) && (msg->flags & F_DELETED)) {
  1345. nfsnprintf( buf + basel, sizeof(buf) - basel, "%s/%s", subdirs[msg->status & M_RECENT], ((maildir_message_t *)msg)->base );
  1346. if (unlink( buf )) {
  1347. if (errno == ENOENT)
  1348. retry = 1;
  1349. else
  1350. sys_error( "Maildir error: cannot remove %s", buf );
  1351. } else {
  1352. msg->status |= M_DEAD;
  1353. gctx->count--;
  1354. #ifdef USE_DB
  1355. if (ctx->db && (ret = maildir_purge_msg( ctx, ((maildir_message_t *)msg)->base )) != DRV_OK) {
  1356. cb( ret, aux );
  1357. return;
  1358. }
  1359. #endif /* USE_DB */
  1360. }
  1361. }
  1362. if (!retry) {
  1363. cb( DRV_OK, aux );
  1364. return;
  1365. }
  1366. if ((ret = maildir_rescan( (maildir_store_t *)gctx )) != DRV_OK) {
  1367. cb( ret, aux );
  1368. return;
  1369. }
  1370. }
  1371. }
  1372. static void
  1373. maildir_cancel_cmds( store_t *gctx ATTR_UNUSED,
  1374. void (*cb)( void *aux ), void *aux )
  1375. {
  1376. cb( aux );
  1377. }
  1378. static void
  1379. maildir_commit_cmds( store_t *gctx )
  1380. {
  1381. (void) gctx;
  1382. }
  1383. static int
  1384. maildir_parse_store( conffile_t *cfg, store_conf_t **storep )
  1385. {
  1386. maildir_store_conf_t *store;
  1387. if (strcasecmp( "MaildirStore", cfg->cmd ))
  1388. return 0;
  1389. store = nfcalloc( sizeof(*store) );
  1390. store->info_delimiter = FieldDelimiter;
  1391. store->gen.driver = &maildir_driver;
  1392. store->gen.name = nfstrdup( cfg->val );
  1393. while (getcline( cfg ) && cfg->cmd)
  1394. if (!strcasecmp( "Inbox", cfg->cmd ))
  1395. store->inbox = expand_strdup( cfg->val );
  1396. else if (!strcasecmp( "Path", cfg->cmd ))
  1397. store->gen.path = expand_strdup( cfg->val );
  1398. #ifdef USE_DB
  1399. else if (!strcasecmp( "AltMap", cfg->cmd ))
  1400. store->alt_map = parse_bool( cfg );
  1401. #endif /* USE_DB */
  1402. else if (!strcasecmp( "InfoDelimiter", cfg->cmd )) {
  1403. if (strlen( cfg->val ) != 1) {
  1404. error( "%s:%d: Info delimiter must be exactly one character long\n", cfg->file, cfg->line );
  1405. cfg->err = 1;
  1406. continue;
  1407. }
  1408. store->info_delimiter = cfg->val[0];
  1409. if (!ispunct( store->info_delimiter )) {
  1410. error( "%s:%d: Info delimiter must be a punctuation character\n", cfg->file, cfg->line );
  1411. cfg->err = 1;
  1412. continue;
  1413. }
  1414. } else
  1415. parse_generic_store( &store->gen, cfg );
  1416. if (!store->inbox)
  1417. store->inbox = expand_strdup( "~/Maildir" );
  1418. nfasprintf( &store->info_prefix, "%c2,", store->info_delimiter );
  1419. nfasprintf( &store->info_stop, "%c,", store->info_delimiter );
  1420. *storep = &store->gen;
  1421. return 1;
  1422. }
  1423. struct driver maildir_driver = {
  1424. 0, /* XXX DRV_CRLF? */
  1425. maildir_parse_store,
  1426. maildir_cleanup_drv,
  1427. maildir_open_store,
  1428. maildir_disown_store,
  1429. maildir_disown_store, /* _cancel_, but it's the same */
  1430. maildir_list_store,
  1431. maildir_select_box,
  1432. maildir_open_box,
  1433. maildir_prepare_load_box,
  1434. maildir_load_box,
  1435. maildir_fetch_msg,
  1436. maildir_store_msg,
  1437. maildir_find_new_msgs,
  1438. maildir_set_msg_flags,
  1439. maildir_trash_msg,
  1440. maildir_close_box,
  1441. maildir_cancel_cmds,
  1442. maildir_commit_cmds,
  1443. };