config.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-2 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 <stdio.h>
  28. #include "isync.h"
  29. config_t *boxes = 0;
  30. /* set defaults from the global configuration section */
  31. static void
  32. config_defaults (config_t * conf)
  33. {
  34. memcpy (conf, &global, sizeof (config_t));
  35. }
  36. static char *
  37. my_strndup (const char *s, size_t nchars)
  38. {
  39. char *r = malloc (sizeof (char) * (nchars + 1));
  40. strncpy (r, s, nchars);
  41. r[nchars] = 0;
  42. return r;
  43. }
  44. char *
  45. expand_strdup (const char *s)
  46. {
  47. char path[_POSIX_PATH_MAX];
  48. struct passwd *pw;
  49. const char *p;
  50. if (*s == '~')
  51. {
  52. s++;
  53. if (*s == '/')
  54. {
  55. /* current user */
  56. pw = getpwuid (getuid ());
  57. p = s + 1;
  58. }
  59. else
  60. {
  61. char *user;
  62. p = strchr (s, '/');
  63. if (p)
  64. {
  65. user = my_strndup (s, (int)(p - s));
  66. p++;
  67. }
  68. else
  69. user = strdup (s);
  70. pw = getpwnam (user);
  71. free (user);
  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",
  81. global.maildir ? 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 = &boxes;
  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 (!strcasecmp ("mailbox", cmd))
  120. {
  121. if (*cur)
  122. cur = &(*cur)->next;
  123. *cur = calloc (1, sizeof (config_t));
  124. config_defaults (*cur);
  125. /* not expanded at this point */
  126. (*cur)->path = strdup (val);
  127. }
  128. else if (!strcasecmp ("maildir", cmd))
  129. {
  130. /* this only affects the global setting */
  131. free (global.maildir);
  132. global.maildir = expand_strdup (val);
  133. }
  134. else if (!strcasecmp ("host", cmd))
  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. (*cur)->use_sslv2 = 1;
  145. (*cur)->use_sslv3 = 1;
  146. }
  147. else
  148. {
  149. global.use_imaps = 1;
  150. global.port = 993;
  151. global.use_sslv2 = 1;
  152. global.use_sslv3 = 1;
  153. }
  154. }
  155. #endif
  156. if (*cur)
  157. (*cur)->host = strdup (val);
  158. else
  159. global.host = strdup (val);
  160. }
  161. else if (!strcasecmp ("user", cmd))
  162. {
  163. if (*cur)
  164. (*cur)->user = strdup (val);
  165. else
  166. global.user = strdup (val);
  167. }
  168. else if (!strcasecmp ("pass", cmd))
  169. {
  170. if (*cur)
  171. (*cur)->pass = strdup (val);
  172. else
  173. global.pass = strdup (val);
  174. }
  175. else if (!strcasecmp ("port", cmd))
  176. {
  177. if (*cur)
  178. (*cur)->port = atoi (val);
  179. else
  180. global.port = atoi (val);
  181. }
  182. else if (!strcasecmp ("box", cmd))
  183. {
  184. if (*cur)
  185. (*cur)->box = strdup (val);
  186. else
  187. global.box = strdup (val);
  188. }
  189. else if (!strcasecmp ("alias", cmd))
  190. {
  191. if (*cur)
  192. (*cur)->alias = strdup (val);
  193. }
  194. else if (!strcasecmp ("maxsize", cmd))
  195. {
  196. if (*cur)
  197. (*cur)->max_size = atol (val);
  198. else
  199. global.max_size = atol (val);
  200. }
  201. else if (!strcasecmp ("MaxMessages", cmd))
  202. {
  203. if (*cur)
  204. (*cur)->max_messages = atol (val);
  205. else
  206. global.max_messages = atol (val);
  207. }
  208. else if (!strcasecmp ("UseNamespace", cmd))
  209. {
  210. if (*cur)
  211. (*cur)->use_namespace = (strcasecmp (val, "yes") == 0);
  212. else
  213. global.use_namespace = (strcasecmp (val, "yes") == 0);
  214. }
  215. else if (!strcasecmp ("CopyDeletedTo", cmd))
  216. {
  217. if (*cur)
  218. (*cur)->copy_deleted_to = strdup (val);
  219. else
  220. global.copy_deleted_to = strdup (val);
  221. }
  222. else if (!strcasecmp ("Expunge", cmd))
  223. {
  224. if (*cur)
  225. (*cur)->expunge = (strcasecmp (val, "yes") == 0);
  226. else
  227. global.expunge = (strcasecmp (val, "yes") == 0);
  228. }
  229. else if (!strcasecmp ("Delete", cmd))
  230. {
  231. if (*cur)
  232. (*cur)->delete = (strcasecmp (val, "yes") == 0);
  233. else
  234. global.delete = (strcasecmp (val, "yes") == 0);
  235. }
  236. #if HAVE_LIBSSL
  237. else if (!strcasecmp ("CertificateFile", cmd))
  238. {
  239. if (*cur)
  240. (*cur)->cert_file = expand_strdup (val);
  241. else
  242. global.cert_file = expand_strdup (val);
  243. }
  244. else if (!strcasecmp ("RequireSSL", cmd))
  245. {
  246. if (*cur)
  247. (*cur)->require_ssl = (strcasecmp (val, "yes") == 0);
  248. else
  249. global.require_ssl = (strcasecmp (val, "yes") == 0);
  250. }
  251. else if (!strcasecmp ("UseSSLv2", cmd))
  252. {
  253. if (*cur)
  254. (*cur)->use_sslv2 = (strcasecmp (val, "yes") == 0);
  255. else
  256. global.use_sslv2 = (strcasecmp (val, "yes") == 0);
  257. }
  258. else if (!strcasecmp ("UseSSLv3", cmd))
  259. {
  260. if (*cur)
  261. (*cur)->use_sslv3 = (strcasecmp (val, "yes") == 0);
  262. else
  263. global.use_sslv3 = (strcasecmp (val, "yes") == 0);
  264. }
  265. else if (!strcasecmp ("UseTLSv1", cmd))
  266. {
  267. if (*cur)
  268. (*cur)->use_tlsv1 = (strcasecmp (val, "yes") == 0);
  269. else
  270. global.use_tlsv1 = (strcasecmp (val, "yes") == 0);
  271. }
  272. else if (!strcasecmp ("RequireCRAM", cmd))
  273. {
  274. if (*cur)
  275. (*cur)->require_cram = (strcasecmp (val, "yes") == 0);
  276. else
  277. global.require_cram = (strcasecmp (val, "yes") == 0);
  278. }
  279. #endif
  280. else if (buf[0])
  281. printf ("%s:%d:unknown keyword:%s\n", path, line, cmd);
  282. }
  283. fclose (fp);
  284. }
  285. config_t *
  286. find_box (const char *s)
  287. {
  288. config_t *p = boxes;
  289. for (; p; p = p->next)
  290. {
  291. if (!strcmp (s, p->path) || (p->alias && !strcmp (s, p->alias)))
  292. return p;
  293. else
  294. {
  295. /* check to see if the full pathname was specified on the
  296. * command line.
  297. */
  298. char *t = expand_strdup (p->path);
  299. if (!strcmp (s, t))
  300. {
  301. free (t);
  302. return p;
  303. }
  304. free (t);
  305. }
  306. }
  307. return 0;
  308. }
  309. void
  310. free_config (void)
  311. {
  312. free (global.user);
  313. free (global.maildir);
  314. free (global.host);
  315. free (global.pass);
  316. }