config.c 7.1 KB

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