config.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-1 Michael R. Elkins <me@mutt.org>
  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
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #define _GNU_SOURCE 1
  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. #include "isync.h"
  30. config_t *boxes = 0;
  31. /* set defaults from the global configuration section */
  32. void
  33. config_defaults (config_t * conf)
  34. {
  35. conf->user = global.user;
  36. conf->pass = global.pass;
  37. conf->port = global.port;
  38. conf->box = global.box;
  39. conf->host = global.host;
  40. conf->max_size = global.max_size;
  41. conf->copy_deleted_to = global.copy_deleted_to;
  42. conf->use_namespace = global.use_namespace;
  43. conf->expunge = global.expunge;
  44. conf->delete = global.delete;
  45. conf->poll = global.poll;
  46. #if HAVE_LIBSSL
  47. conf->require_ssl = global.require_ssl;
  48. conf->use_imaps = global.use_imaps;
  49. conf->cert_file = global.cert_file;
  50. conf->use_sslv2 = global.use_sslv2;
  51. conf->use_sslv3 = global.use_sslv3;
  52. conf->use_tlsv1 = global.use_tlsv1;
  53. #endif
  54. }
  55. char *
  56. expand_strdup (const char *s)
  57. {
  58. char path[_POSIX_PATH_MAX];
  59. struct passwd *pw;
  60. const char *p;
  61. if (*s == '~')
  62. {
  63. s++;
  64. if (*s == '/')
  65. {
  66. /* current user */
  67. pw = getpwuid (getuid ());
  68. p = s + 1;
  69. }
  70. else
  71. {
  72. char *user;
  73. p = strchr (s, '/');
  74. if (p)
  75. {
  76. user = strndup (s, (int)(p - s));
  77. p++;
  78. }
  79. else
  80. user = strdup (s);
  81. pw = getpwnam (user);
  82. free (user);
  83. }
  84. if (!pw)
  85. return 0;
  86. snprintf (path, sizeof (path), "%s/%s", pw->pw_dir, p ? p : "");
  87. s = path;
  88. }
  89. else if (*s != '/')
  90. {
  91. snprintf (path, sizeof (path), "%s/%s",
  92. global.maildir ? global.maildir : "", s);
  93. s = path;
  94. }
  95. return strdup (s);
  96. }
  97. void
  98. load_config (const char *where)
  99. {
  100. char path[_POSIX_PATH_MAX];
  101. char buf[1024];
  102. struct passwd *pw;
  103. config_t **cur = &boxes;
  104. int line = 0;
  105. FILE *fp;
  106. char *p, *cmd, *val;
  107. if (!where)
  108. {
  109. pw = getpwuid (getuid ());
  110. snprintf (path, sizeof (path), "%s/.isyncrc", pw->pw_dir);
  111. where = path;
  112. }
  113. printf ("Reading %s\n", where);
  114. fp = fopen (where, "r");
  115. if (!fp)
  116. {
  117. if (errno != ENOENT)
  118. perror ("fopen");
  119. return;
  120. }
  121. buf[sizeof buf - 1] = 0;
  122. while ((fgets (buf, sizeof (buf) - 1, fp)))
  123. {
  124. p = buf;
  125. cmd = next_arg (&p);
  126. val = next_arg (&p);
  127. line++;
  128. if (!cmd || *cmd == '#')
  129. continue;
  130. if (!strcasecmp ("mailbox", cmd))
  131. {
  132. if (*cur)
  133. cur = &(*cur)->next;
  134. *cur = calloc (1, sizeof (config_t));
  135. config_defaults (*cur);
  136. /* not expanded at this point */
  137. (*cur)->path = strdup (val);
  138. }
  139. else if (!strcasecmp ("maildir", cmd))
  140. {
  141. /* this only affects the global setting */
  142. free (global.maildir);
  143. global.maildir = expand_strdup (val);
  144. }
  145. else if (!strcasecmp ("host", cmd))
  146. {
  147. #if HAVE_LIBSSL
  148. if (!strncasecmp ("imaps:", val, 6))
  149. {
  150. val += 6;
  151. if (*cur)
  152. {
  153. (*cur)->use_imaps = 1;
  154. (*cur)->port = 993;
  155. (*cur)->use_sslv2 = 1;
  156. (*cur)->use_sslv3 = 1;
  157. }
  158. else
  159. {
  160. global.use_imaps = 1;
  161. global.port = 993;
  162. global.use_sslv2 = 1;
  163. global.use_sslv3 = 1;
  164. }
  165. }
  166. #endif
  167. if (*cur)
  168. (*cur)->host = strdup (val);
  169. else
  170. global.host = strdup (val);
  171. }
  172. else if (!strcasecmp ("user", cmd))
  173. {
  174. if (*cur)
  175. (*cur)->user = strdup (val);
  176. else
  177. global.user = strdup (val);
  178. }
  179. else if (!strcasecmp ("pass", cmd))
  180. {
  181. if (*cur)
  182. (*cur)->pass = strdup (val);
  183. else
  184. global.pass = strdup (val);
  185. }
  186. else if (!strcasecmp ("port", cmd))
  187. {
  188. if (*cur)
  189. (*cur)->port = atoi (val);
  190. else
  191. global.port = atoi (val);
  192. }
  193. else if (!strcasecmp ("box", cmd))
  194. {
  195. if (*cur)
  196. (*cur)->box = strdup (val);
  197. else
  198. global.box = strdup (val);
  199. }
  200. else if (!strcasecmp ("alias", cmd))
  201. {
  202. if (*cur)
  203. (*cur)->alias = strdup (val);
  204. }
  205. else if (!strcasecmp ("maxsize", cmd))
  206. {
  207. if (*cur)
  208. (*cur)->max_size = atol (val);
  209. else
  210. global.max_size = atol (val);
  211. }
  212. else if (!strcasecmp ("UseNamespace", cmd))
  213. {
  214. if (*cur)
  215. (*cur)->use_namespace = (strcasecmp (val, "yes") == 0);
  216. else
  217. global.use_namespace = (strcasecmp (val, "yes") == 0);
  218. }
  219. else if (!strcasecmp ("CopyDeletedTo", cmd))
  220. {
  221. if (*cur)
  222. (*cur)->copy_deleted_to = strdup (val);
  223. else
  224. global.copy_deleted_to = strdup (val);
  225. }
  226. else if (!strcasecmp ("Expunge", cmd))
  227. {
  228. if (*cur)
  229. (*cur)->expunge = (strcasecmp (val, "yes") == 0);
  230. else
  231. global.expunge = (strcasecmp (val, "yes") == 0);
  232. }
  233. else if (!strcasecmp ("Delete", cmd))
  234. {
  235. if (*cur)
  236. (*cur)->delete = (strcasecmp (val, "yes") == 0);
  237. else
  238. global.delete = (strcasecmp (val, "yes") == 0);
  239. }
  240. #if 0
  241. else if (!strcasecmp ("Poll", cmd))
  242. {
  243. if (*cur)
  244. (*cur)->poll = atoi (val);
  245. else
  246. global.poll = atoi (val);
  247. }
  248. #endif
  249. #if HAVE_LIBSSL
  250. else if (!strcasecmp ("CertificateFile", cmd))
  251. {
  252. if (*cur)
  253. (*cur)->cert_file = expand_strdup (val);
  254. else
  255. global.cert_file = expand_strdup (val);
  256. }
  257. else if (!strcasecmp ("RequireSSL", cmd))
  258. {
  259. if (*cur)
  260. (*cur)->require_ssl = (strcasecmp (val, "yes") == 0);
  261. else
  262. global.require_ssl = (strcasecmp (val, "yes") == 0);
  263. }
  264. else if (!strcasecmp ("UseSSLv2", cmd))
  265. {
  266. if (*cur)
  267. (*cur)->use_sslv2 = (strcasecmp (val, "yes") == 0);
  268. else
  269. global.use_sslv2 = (strcasecmp (val, "yes") == 0);
  270. }
  271. else if (!strcasecmp ("UseSSLv3", cmd))
  272. {
  273. if (*cur)
  274. (*cur)->use_sslv3 = (strcasecmp (val, "yes") == 0);
  275. else
  276. global.use_sslv3 = (strcasecmp (val, "yes") == 0);
  277. }
  278. else if (!strcasecmp ("UseTLSv1", cmd))
  279. {
  280. if (*cur)
  281. (*cur)->use_tlsv1 = (strcasecmp (val, "yes") == 0);
  282. else
  283. global.use_tlsv1 = (strcasecmp (val, "yes") == 0);
  284. }
  285. else if (!strcasecmp ("RequireCRAM", cmd))
  286. {
  287. if (*cur)
  288. (*cur)->require_cram = (strcasecmp (val, "yes") == 0);
  289. else
  290. global.require_cram = (strcasecmp (val, "yes") == 0);
  291. }
  292. #endif
  293. else if (buf[0])
  294. printf ("%s:%d:unknown command:%s\n", path, line, cmd);
  295. }
  296. fclose (fp);
  297. }
  298. config_t *
  299. find_box (const char *s)
  300. {
  301. config_t *p = boxes;
  302. for (; p; p = p->next)
  303. {
  304. if (!strcmp (s, p->path) || (p->alias && !strcmp (s, p->alias)))
  305. return p;
  306. else
  307. {
  308. /* check to see if the full pathname was specified on the
  309. * command line.
  310. */
  311. char *t = expand_strdup (p->path);
  312. if (!strcmp (s, t))
  313. {
  314. free (t);
  315. return p;
  316. }
  317. free (t);
  318. }
  319. }
  320. return 0;
  321. }