drv_maildir.c 39 KB

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