config.c 7.3 KB

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