config.c 6.5 KB

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