config.c 6.9 KB

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