config.c 6.9 KB

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