config.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * isync - mbsync wrapper: IMAP4 to maildir mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "isync.h"
  21. #include <unistd.h>
  22. #include <limits.h>
  23. #include <errno.h>
  24. #include <pwd.h>
  25. #include <sys/types.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. static int local_home, local_root;
  30. static char *
  31. my_strndup( const char *s, size_t nchars )
  32. {
  33. char *r = nfmalloc( sizeof(char) * (nchars + 1) );
  34. memcpy( r, s, nchars );
  35. r[nchars] = 0;
  36. return r;
  37. }
  38. char *
  39. expand_strdup( const char *s )
  40. {
  41. struct passwd *pw;
  42. const char *p, *q;
  43. char *r;
  44. if (*s == '~') {
  45. s++;
  46. if (!*s) {
  47. p = 0;
  48. q = Home;
  49. } else if (*s == '/') {
  50. p = s + 1;
  51. q = Home;
  52. } else {
  53. if ((p = strchr( s, '/' ))) {
  54. r = my_strndup( s, (int)(p - s) );
  55. pw = getpwnam( r );
  56. free( r );
  57. p++;
  58. } else
  59. pw = getpwnam( s );
  60. if (!pw)
  61. return 0;
  62. q = pw->pw_dir;
  63. }
  64. nfasprintf( &r, "%s/%s", q, p ? p : "" );
  65. return r;
  66. } else if (*s != '/' && xmaildir) {
  67. nfasprintf( &r, "%s/%s", xmaildir, s );
  68. return r;
  69. } else
  70. return nfstrdup( s );
  71. }
  72. static int
  73. is_true( const char *val )
  74. {
  75. return
  76. !strcasecmp( val, "yes" ) ||
  77. !strcasecmp( val, "true" ) ||
  78. !strcasecmp( val, "on" ) ||
  79. !strcmp( val, "1" );
  80. }
  81. void
  82. load_config( const char *path, config_t ***stor )
  83. {
  84. config_t **sstor, *cfg;
  85. FILE *fp;
  86. char *p, *cmd, *val;
  87. int line = 0;
  88. char buf[1024];
  89. if (!(fp = fopen( path, "r" ))) {
  90. if (errno != ENOENT)
  91. perror( "fopen" );
  92. return;
  93. }
  94. if (!Quiet && !Debug && !Verbose)
  95. printf( "Reading configuration file %s\n", path );
  96. buf[sizeof(buf) - 1] = 0;
  97. cfg = &global;
  98. while (fgets( buf, sizeof(buf) - 1, fp )) {
  99. p = buf;
  100. cmd = next_arg( &p );
  101. val = next_arg( &p );
  102. line++;
  103. if (!cmd || *cmd == '#')
  104. continue;
  105. if (!val) {
  106. fprintf( stderr, "%s:%d: parameter missing\n", path, line );
  107. continue;
  108. }
  109. if (!strcasecmp( "Mailbox", cmd )) {
  110. if (o2o)
  111. break;
  112. cfg = **stor = nfmalloc( sizeof(config_t) );
  113. *stor = &cfg->next;
  114. memcpy( cfg, &global, sizeof(config_t) );
  115. if (val[0] == '~' && val[1] == '/') {
  116. val += 2;
  117. local_home = 1;
  118. } else if (!memcmp( val, Home, HomeLen ) && val[HomeLen] == '/') {
  119. val += HomeLen + 1;
  120. local_home = 1;
  121. } else if (val[0] == '/')
  122. local_root = 1;
  123. else
  124. local_home = 1;
  125. /* not expanded at this point */
  126. cfg->path = nfstrdup( val );
  127. } else if (!strcasecmp( "OneToOne", cmd )) {
  128. if (boxes) {
  129. forbid:
  130. fprintf( stderr,
  131. "%s:%d: keyword '%s' allowed only in global section\n",
  132. path, line, cmd );
  133. continue;
  134. }
  135. o2o = is_true( val );
  136. } else if (!strcasecmp( "Maildir", cmd )) {
  137. if (boxes)
  138. goto forbid;
  139. maildir = nfstrdup( val );
  140. xmaildir = expand_strdup( val );
  141. } else if (!strcasecmp( "Folder", cmd )) {
  142. if (boxes)
  143. goto forbid;
  144. folder = nfstrdup( val );
  145. } else if (!strcasecmp( "Inbox", cmd )) {
  146. if (boxes)
  147. goto forbid;
  148. inbox = nfstrdup( val );
  149. } else if (!strcasecmp( "Host", cmd )) {
  150. if (!memcmp( "imaps:", val, 6 )) {
  151. val += 6;
  152. cfg->use_imaps = 1;
  153. cfg->port = 993;
  154. cfg->use_sslv2 = 1;
  155. cfg->use_sslv3 = 1;
  156. }
  157. cfg->host = nfstrdup( val );
  158. } else if (!strcasecmp( "User", cmd ))
  159. cfg->user = nfstrdup( val );
  160. else if (!strcasecmp( "Pass", cmd ))
  161. cfg->pass = nfstrdup( val );
  162. else if (!strcasecmp ( "Port", cmd ))
  163. cfg->port = atoi( val );
  164. else if (!strcasecmp ( "Box", cmd ))
  165. cfg->box = nfstrdup( val );
  166. else if (!strcasecmp ( "Alias", cmd )) {
  167. if (!boxes) {
  168. fprintf( stderr,
  169. "%s:%d: keyword 'Alias' allowed only in mailbox specification\n",
  170. path, line );
  171. continue;
  172. }
  173. cfg->alias = nfstrdup( val );
  174. } else if (!strcasecmp( "MaxSize", cmd ))
  175. cfg->max_size = atol( val );
  176. else if (!strcasecmp ( "MaxMessages", cmd ))
  177. cfg->max_messages = atol( val );
  178. else if (!strcasecmp ( "UseNamespace", cmd ))
  179. cfg->use_namespace = is_true( val );
  180. else if (!strcasecmp ( "CopyDeletedTo", cmd ))
  181. cfg->copy_deleted_to = nfstrdup( val );
  182. else if (!strcasecmp ( "Tunnel", cmd ))
  183. cfg->tunnel = nfstrdup( val );
  184. else if (!strcasecmp ( "Expunge", cmd ))
  185. cfg->expunge = is_true( val );
  186. else if (!strcasecmp( "Delete", cmd ))
  187. cfg->delete = is_true( val );
  188. else if (!strcasecmp( "CertificateFile", cmd ))
  189. cfg->cert_file = expand_strdup( val );
  190. else if (!strcasecmp( "RequireSSL", cmd ))
  191. cfg->require_ssl = is_true( val );
  192. else if (!strcasecmp( "UseSSLv2", cmd ))
  193. cfg->use_sslv2 = is_true( val );
  194. else if (!strcasecmp( "UseSSLv3", cmd ))
  195. cfg->use_sslv3 = is_true( val );
  196. else if (!strcasecmp( "UseTLSv1", cmd ))
  197. cfg->use_tlsv1 = is_true( val );
  198. else if (!strcasecmp( "RequireCRAM", cmd ))
  199. cfg->require_cram = is_true( val );
  200. else if (buf[0])
  201. fprintf( stderr, "%s:%d: unknown keyword '%s'\n", path, line, cmd );
  202. }
  203. fclose( fp );
  204. if (o2o) {
  205. if (!global.host && !global.tunnel) {
  206. fprintf( stderr, "Neither Host nor Tunnel given to OneToOne. Aborting.\n" );
  207. exit( 1 );
  208. }
  209. } else
  210. for (sstor = &boxes; (cfg = *sstor); ) {
  211. if (!cfg->host && !cfg->tunnel) {
  212. fprintf( stderr, "Mailbox '%s' has neither Host nor Tunnel. Skipping.\n",
  213. cfg->alias ? cfg->alias : cfg->path );
  214. if (&cfg->next == *stor)
  215. *stor = sstor;
  216. *sstor = cfg->next;
  217. continue;
  218. }
  219. sstor = &cfg->next;
  220. }
  221. }
  222. static const char *
  223. tb( int on )
  224. {
  225. return on ? "yes" : "no";
  226. }
  227. static void
  228. write_imap_server( FILE *fp, config_t *cfg )
  229. {
  230. config_t *pbox;
  231. char *p, *p2;
  232. int hl, a1, a2, a3, a4;
  233. char buf[128], ubuf[64];
  234. static int tunnels;
  235. if (cfg->tunnel)
  236. nfasprintf( (char **)&cfg->old_server_name, "tunnel%d", ++tunnels );
  237. else {
  238. if (sscanf( cfg->host, "%d.%d.%d.%d", &a1, &a2, &a3, &a4 ) == 4)
  239. hl = nfsnprintf( buf, sizeof(buf), "%s", cfg->host );
  240. else {
  241. /* XXX this does not avoid clashes. add port? */
  242. p = strrchr( cfg->host, '.' );
  243. if (!p)
  244. hl = nfsnprintf( buf, sizeof(buf), "%s", cfg->host );
  245. else {
  246. hl = nfsnprintf( buf, sizeof(buf), "%.*s", p - cfg->host, cfg->host );
  247. p2 = strrchr( buf, '.' );
  248. if (p2)
  249. hl = sprintf( buf, "%s", p2 + 1 );
  250. }
  251. }
  252. if (boxes) /* !o2o */
  253. for (pbox = boxes; pbox != cfg; pbox = pbox->next)
  254. if (!memcmp( pbox->server_name, buf, hl + 1 )) {
  255. nfasprintf( (char **)&cfg->old_server_name, "%s-%d", buf, ++pbox->old_servers );
  256. goto gotsrv;
  257. }
  258. cfg->old_server_name = nfstrdup( buf );
  259. cfg->old_servers = 1;
  260. gotsrv: ;
  261. }
  262. if (cfg->user)
  263. nfsnprintf( ubuf, sizeof(ubuf), "%s@", cfg->user );
  264. else
  265. ubuf[0] = 0;
  266. if (!cfg->host)
  267. hl = nfsnprintf( buf, sizeof(buf), "%stunnel", ubuf );
  268. else {
  269. if (cfg->port != (cfg->use_imaps ? 993 : 143))
  270. hl = nfsnprintf( buf, sizeof(buf), "%s%s_%d", ubuf, cfg->host, cfg->port );
  271. else
  272. hl = nfsnprintf( buf, sizeof(buf), "%s%s", ubuf, cfg->host );
  273. }
  274. if (boxes) /* !o2o */
  275. for (pbox = boxes; pbox != cfg; pbox = pbox->next)
  276. if (!memcmp( pbox->server_name, buf, hl + 1 )) {
  277. nfasprintf( (char **)&cfg->server_name, "%s-%d", buf, ++pbox->servers );
  278. goto ngotsrv;
  279. }
  280. cfg->server_name = nfstrdup( buf );
  281. cfg->servers = 1;
  282. ngotsrv: ;
  283. fprintf( fp, "IMAPAccount %s\n", cfg->server_name );
  284. if (cfg->tunnel)
  285. fprintf( fp, "Tunnel \"%s\"\n", cfg->tunnel );
  286. else {
  287. if (cfg->use_imaps)
  288. fprintf( fp, "Host imaps:%s\n", cfg->host );
  289. else
  290. fprintf( fp, "Host %s\n", cfg->host );
  291. fprintf( fp, "Port %d\n", cfg->port );
  292. }
  293. if (cfg->user)
  294. fprintf( fp, "User %s\n", cfg->user );
  295. if (cfg->pass)
  296. fprintf( fp, "Pass \"%s\"\n", cfg->pass );
  297. fprintf( fp, "RequireCRAM %s\nRequireSSL %s\n"
  298. "UseSSLv2 %s\nUseSSLv3 %s\nUseTLSv1 %s\n",
  299. tb(cfg->require_cram), tb(cfg->require_ssl),
  300. tb(cfg->use_sslv2), tb(cfg->use_sslv3), tb(cfg->use_tlsv1) );
  301. if ((cfg->use_imaps || cfg->use_sslv2 || cfg->use_sslv3 || cfg->use_tlsv1) &&
  302. cfg->cert_file)
  303. fprintf( fp, "CertificateFile %s\n", cfg->cert_file );
  304. fputc( '\n', fp );
  305. }
  306. static void
  307. write_imap_store( FILE *fp, config_t *cfg )
  308. {
  309. if (cfg->stores > 1)
  310. nfasprintf( (char **)&cfg->store_name, "%s-%d", cfg->old_server_name, cfg->stores );
  311. else
  312. cfg->store_name = cfg->old_server_name;
  313. fprintf( fp, "IMAPStore %s\nAccount %s\n",
  314. cfg->store_name, cfg->server_name );
  315. if (*folder)
  316. fprintf( fp, "Path \"%s\"\n", folder );
  317. else
  318. fprintf( fp, "UseNamespace %s\n", tb(cfg->use_namespace) );
  319. if (inbox)
  320. fprintf( fp, "MapInbox \"%s\"\n", inbox );
  321. if (cfg->copy_deleted_to)
  322. fprintf( fp, "Trash \"%s\"\n", cfg->copy_deleted_to );
  323. fputc( '\n', fp );
  324. }
  325. static void
  326. write_channel_parm( FILE *fp, config_t *cfg )
  327. {
  328. if (cfg->max_size)
  329. fprintf( fp, "MaxSize %d\n", cfg->max_size );
  330. if (cfg->max_messages)
  331. fprintf( fp, "MaxMessages %d\n", cfg->max_messages );
  332. if (!cfg->delete && !delete)
  333. fputs( "Sync New ReNew Flags\n", fp );
  334. if (cfg->expunge || expunge)
  335. fputs( "Expunge Both\n", fp );
  336. fputc( '\n', fp );
  337. }
  338. static int
  339. mstrcmp( const char *s1, const char *s2 )
  340. {
  341. if (s1 == s2)
  342. return 0;
  343. if (!s1 || !s2)
  344. return 1;
  345. return strcmp( s1, s2 );
  346. }
  347. void
  348. write_config( int fd )
  349. {
  350. FILE *fp;
  351. const char *cn, *scn;
  352. config_t *box, *sbox, *pbox;
  353. if (!(fp = fdopen( fd, "w" ))) {
  354. perror( "fdopen" );
  355. return;
  356. }
  357. fprintf( fp, "SyncState *\n\n" );
  358. if (local_home || o2o)
  359. fprintf( fp, "MaildirStore local\nPath \"%s/\"\nInbox \"%s/INBOX\"\nAltMap %s\n\n",
  360. maildir, maildir, tb( altmap > 0 ) );
  361. if (local_root)
  362. fprintf( fp, "MaildirStore local_root\nPath /\nAltMap %s\n\n", tb( altmap > 0 ) );
  363. if (o2o) {
  364. write_imap_server( fp, &global );
  365. write_imap_store( fp, &global );
  366. fprintf( fp, "Channel o2o\nMaster :%s:\nSlave :local:\nPattern %%\n", global.store_name );
  367. write_channel_parm( fp, &global );
  368. } else {
  369. for (box = boxes; box; box = box->next) {
  370. for (pbox = boxes; pbox != box; pbox = pbox->next) {
  371. if (box->tunnel) {
  372. if (mstrcmp( pbox->tunnel, box->tunnel ))
  373. continue;
  374. } else {
  375. if (mstrcmp( pbox->host, box->host ) ||
  376. pbox->use_imaps != box->use_imaps ||
  377. pbox->port != box->port)
  378. continue;
  379. }
  380. if (mstrcmp( pbox->user, box->user ) ||
  381. mstrcmp( pbox->pass, box->pass )) /* nonsense */
  382. continue;
  383. if ((box->use_imaps || box->use_sslv2 ||
  384. box->use_sslv3 || box->use_tlsv1) &&
  385. mstrcmp( pbox->cert_file, box->cert_file )) /* nonsense */
  386. continue;
  387. if (pbox->use_imaps != box->use_imaps ||
  388. pbox->use_sslv2 != box->use_sslv2 ||
  389. pbox->use_sslv3 != box->use_sslv3 ||
  390. pbox->use_tlsv1 != box->use_tlsv1)
  391. continue;
  392. box->server_name = pbox->server_name;
  393. for (sbox = boxes; sbox != box; sbox = sbox->next) {
  394. if (sbox->server_name != box->server_name ||
  395. mstrcmp( sbox->copy_deleted_to, box->copy_deleted_to ) ||
  396. (!*folder && sbox->use_namespace != box->use_namespace))
  397. continue;
  398. box->store_name = sbox->store_name;
  399. goto gotall;
  400. }
  401. box->stores = ++pbox->stores;
  402. goto gotsrv;
  403. }
  404. write_imap_server( fp, box );
  405. box->stores = 1;
  406. gotsrv:
  407. write_imap_store( fp, box );
  408. gotall:
  409. if (box->alias)
  410. cn = box->alias;
  411. else {
  412. cn = strrchr( box->path, '/' );
  413. if (cn)
  414. cn++;
  415. else
  416. cn = box->path;
  417. }
  418. for (sbox = boxes; sbox != box; sbox = sbox->next) {
  419. if (sbox->alias)
  420. scn = sbox->alias;
  421. else {
  422. scn = strrchr( sbox->path, '/' );
  423. if (scn)
  424. scn++;
  425. else
  426. scn = sbox->path;
  427. }
  428. if (mstrcmp( cn, scn ))
  429. continue;
  430. nfasprintf( (char **)&box->channel_name, "%s-%d", cn, ++sbox->channels );
  431. goto gotchan;
  432. }
  433. box->channels = 1;
  434. box->channel_name = cn;
  435. gotchan:
  436. if (box->path[0] == '/')
  437. fprintf( fp, "Channel %s\nMaster :%s:%s\nSlave :local_root:%s\n",
  438. box->channel_name, box->store_name, box->box, box->path + 1 );
  439. else
  440. fprintf( fp, "Channel %s\nMaster :%s:%s\nSlave :local:%s\n",
  441. box->channel_name, box->store_name, box->box, box->path );
  442. write_channel_parm( fp, box );
  443. }
  444. }
  445. fclose( fp );
  446. }
  447. config_t *
  448. find_box( const char *s )
  449. {
  450. config_t *p;
  451. char *t;
  452. if (!memcmp( s, Home, HomeLen ) && s[HomeLen] == '/')
  453. s += HomeLen + 1;
  454. for (p = boxes; p; p = p->next) {
  455. if (!strcmp( s, p->path ) || (p->alias && !strcmp( s, p->alias )))
  456. return p;
  457. /* check to see if the full pathname was specified on the
  458. * command line.
  459. */
  460. t = expand_strdup( p->path );
  461. if (!strcmp( s, t )) {
  462. free( t );
  463. return p;
  464. }
  465. free( t );
  466. }
  467. return 0;
  468. }