config.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. {
  107. perror ("fopen");
  108. return;
  109. }
  110. }
  111. buf[sizeof buf - 1] = 0;
  112. while ((fgets (buf, sizeof (buf) - 1, fp)))
  113. {
  114. p = buf;
  115. cmd = next_arg (&p);
  116. val = next_arg (&p);
  117. line++;
  118. if (!cmd || *cmd == '#')
  119. continue;
  120. if (!strncasecmp ("mailbox", cmd, 7))
  121. {
  122. if (*cur)
  123. cur = &(*cur)->next;
  124. *cur = calloc (1, sizeof (config_t));
  125. config_defaults (*cur);
  126. (*cur)->path = expand_strdup (val);
  127. }
  128. else if (!strncasecmp ("maildir", cmd, 7))
  129. {
  130. /* this only affects the global setting */
  131. free (global.maildir);
  132. global.maildir = expand_strdup (val);
  133. }
  134. else if (!strncasecmp ("host", cmd, 4))
  135. {
  136. #if HAVE_LIBSSL
  137. if (!strncasecmp ("imaps:", val, 6))
  138. {
  139. val += 6;
  140. if (*cur)
  141. {
  142. (*cur)->use_imaps = 1;
  143. (*cur)->port = 993;
  144. }
  145. else
  146. {
  147. global.use_imaps = 1;
  148. global.port = 993;
  149. }
  150. }
  151. #endif
  152. if (*cur)
  153. (*cur)->host = strdup (val);
  154. else
  155. global.host = strdup (val);
  156. }
  157. else if (!strncasecmp ("user", cmd, 4))
  158. {
  159. if (*cur)
  160. (*cur)->user = strdup (val);
  161. else
  162. global.user = strdup (val);
  163. }
  164. else if (!strncasecmp ("pass", cmd, 4))
  165. {
  166. if (*cur)
  167. (*cur)->pass = strdup (val);
  168. else
  169. global.pass = strdup (val);
  170. }
  171. else if (!strncasecmp ("port", cmd, 4))
  172. {
  173. if (*cur)
  174. (*cur)->port = atoi (val);
  175. else
  176. global.port = atoi (val);
  177. }
  178. else if (!strncasecmp ("box", cmd, 3))
  179. {
  180. if (*cur)
  181. (*cur)->box = strdup (val);
  182. else
  183. global.box = strdup (val);
  184. }
  185. else if (!strncasecmp ("alias", cmd, 5))
  186. {
  187. if (*cur)
  188. (*cur)->alias = strdup (val);
  189. }
  190. else if (!strncasecmp ("maxsize", cmd, 7))
  191. {
  192. if (*cur)
  193. (*cur)->max_size = atol (val);
  194. else
  195. global.max_size = atol (val);
  196. }
  197. else if (!strncasecmp ("UseNamespace", cmd, 12))
  198. {
  199. if (*cur)
  200. (*cur)->use_namespace = (strcasecmp (val, "yes") == 0);
  201. else
  202. global.use_namespace = (strcasecmp (val, "yes") == 0);
  203. }
  204. else if (!strncasecmp ("CopyDeletedTo", cmd, 13))
  205. {
  206. if (*cur)
  207. (*cur)->copy_deleted_to = strdup (val);
  208. else
  209. global.copy_deleted_to = strdup (val);
  210. }
  211. else if (!strncasecmp ("Expunge", cmd, 7))
  212. {
  213. if (*cur)
  214. (*cur)->expunge = (strcasecmp (val, "yes") == 0);
  215. else
  216. global.expunge = (strcasecmp (val, "yes") == 0);
  217. }
  218. #if HAVE_LIBSSL
  219. else if (!strncasecmp ("CertificateFile", cmd, 15))
  220. {
  221. if (*cur)
  222. (*cur)->cert_file = expand_strdup (val);
  223. else
  224. global.cert_file = expand_strdup (val);
  225. }
  226. else if (!strncasecmp ("RequireSSL", cmd, 10))
  227. {
  228. if (*cur)
  229. (*cur)->require_ssl = (strcasecmp (val, "yes") == 0);
  230. else
  231. global.require_ssl = (strcasecmp (val, "yes") == 0);
  232. }
  233. else if (!strncasecmp ("UseSSLv2", cmd, 8))
  234. {
  235. if (*cur)
  236. (*cur)->use_sslv2 = (strcasecmp (val, "yes") == 0);
  237. else
  238. global.use_sslv2 = (strcasecmp (val, "yes") == 0);
  239. }
  240. else if (!strncasecmp ("UseSSLv3", cmd, 8))
  241. {
  242. if (*cur)
  243. (*cur)->use_sslv3 = (strcasecmp (val, "yes") == 0);
  244. else
  245. global.use_sslv3 = (strcasecmp (val, "yes") == 0);
  246. }
  247. else if (!strncasecmp ("UseTLSv1", cmd, 8))
  248. {
  249. if (*cur)
  250. (*cur)->use_tlsv1 = (strcasecmp (val, "yes") == 0);
  251. else
  252. global.use_tlsv1 = (strcasecmp (val, "yes") == 0);
  253. }
  254. else if (!strncasecmp ("RequireCRAM", cmd, 11))
  255. {
  256. if (*cur)
  257. (*cur)->require_cram = (strcasecmp (val, "yes") == 0);
  258. else
  259. global.require_cram = (strcasecmp (val, "yes") == 0);
  260. }
  261. #endif
  262. else if (buf[0])
  263. printf ("%s:%d:unknown command:%s", path, line, cmd);
  264. }
  265. fclose (fp);
  266. }
  267. config_t *
  268. find_box (const char *s)
  269. {
  270. config_t *p = box;
  271. for (; p; p = p->next)
  272. if (!strcmp (s, p->path) || (p->alias && !strcmp (s, p->alias)))
  273. return p;
  274. return 0;
  275. }