config.c 6.4 KB

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