config.c 6.4 KB

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