config.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. * As a special exception, isync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #include <unistd.h>
  24. #include <limits.h>
  25. #include <errno.h>
  26. #include <pwd.h>
  27. #include <sys/types.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include "isync.h"
  32. config_t *boxes = 0;
  33. /* set defaults from the global configuration section */
  34. static void
  35. config_defaults (config_t * conf)
  36. {
  37. memcpy (conf, &global, sizeof (config_t));
  38. }
  39. static char *
  40. my_strndup (const char *s, size_t nchars)
  41. {
  42. char *r = malloc (sizeof (char) * (nchars + 1));
  43. strncpy (r, s, nchars);
  44. r[nchars] = 0;
  45. return r;
  46. }
  47. char *
  48. expand_strdup (const char *s)
  49. {
  50. char path[_POSIX_PATH_MAX];
  51. struct passwd *pw;
  52. const char *p;
  53. if (*s == '~')
  54. {
  55. s++;
  56. if (*s == '/')
  57. {
  58. /* current user */
  59. pw = getpwuid (getuid ());
  60. p = s + 1;
  61. }
  62. else
  63. {
  64. char *user;
  65. p = strchr (s, '/');
  66. if (p)
  67. {
  68. user = my_strndup (s, (int)(p - s));
  69. p++;
  70. }
  71. else
  72. user = strdup (s);
  73. pw = getpwnam (user);
  74. free (user);
  75. }
  76. if (!pw)
  77. return 0;
  78. snprintf (path, sizeof (path), "%s/%s", pw->pw_dir, p ? p : "");
  79. s = path;
  80. }
  81. else if (*s != '/')
  82. {
  83. snprintf (path, sizeof (path), "%s/%s",
  84. global.maildir ? global.maildir : "", s);
  85. s = path;
  86. }
  87. return strdup (s);
  88. }
  89. static int
  90. is_true (const char *val)
  91. {
  92. return
  93. !strcasecmp (val, "yes") ||
  94. !strcasecmp (val, "true") ||
  95. !strcasecmp (val, "on") ||
  96. !strcmp (val, "1");
  97. }
  98. void
  99. load_config (const char *where, int *o2o)
  100. {
  101. char path[_POSIX_PATH_MAX];
  102. char buf[1024];
  103. struct passwd *pw;
  104. config_t **cur = &boxes, *cfg;
  105. int line = 0;
  106. FILE *fp;
  107. char *p, *cmd, *val;
  108. if (!where)
  109. {
  110. pw = getpwuid (getuid ());
  111. snprintf (path, sizeof (path), "%s/.isyncrc", pw->pw_dir);
  112. where = path;
  113. }
  114. info ("Reading configuration file %s\n", where);
  115. fp = fopen (where, "r");
  116. if (!fp)
  117. {
  118. if (errno != ENOENT)
  119. perror ("fopen");
  120. return;
  121. }
  122. buf[sizeof buf - 1] = 0;
  123. cfg = &global;
  124. while ((fgets (buf, sizeof (buf) - 1, fp)))
  125. {
  126. p = buf;
  127. cmd = next_arg (&p);
  128. val = next_arg (&p);
  129. line++;
  130. if (!cmd || *cmd == '#')
  131. continue;
  132. if (!strcasecmp ("mailbox", cmd))
  133. {
  134. if (*o2o)
  135. break;
  136. cur = &(*cur)->next;
  137. cfg = *cur = malloc (sizeof (config_t));
  138. config_defaults (cfg);
  139. /* not expanded at this point */
  140. cfg->path = strdup (val);
  141. }
  142. else if (!strcasecmp ("OneToOne", cmd))
  143. {
  144. if (*cur) {
  145. forbid:
  146. fprintf (stderr,
  147. "%s:%d: keyword '%s' allowed only in global section\n",
  148. path, line, cmd);
  149. continue;
  150. }
  151. *o2o = is_true (val);
  152. }
  153. else if (!strcasecmp ("maildir", cmd))
  154. {
  155. if (*cur)
  156. goto forbid;
  157. /* this only affects the global setting */
  158. free (global.maildir);
  159. global.maildir = expand_strdup (val);
  160. }
  161. else if (!strcasecmp ("folder", cmd))
  162. {
  163. if (*cur)
  164. goto forbid;
  165. /* this only affects the global setting */
  166. global.folder = strdup (val);
  167. }
  168. else if (!strcasecmp ("inbox", cmd))
  169. {
  170. if (*cur)
  171. goto forbid;
  172. /* this only affects the global setting */
  173. global.inbox = strdup (val);
  174. }
  175. else if (!strcasecmp ("host", cmd))
  176. {
  177. #if HAVE_LIBSSL
  178. if (!strncasecmp ("imaps:", val, 6))
  179. {
  180. val += 6;
  181. cfg->use_imaps = 1;
  182. cfg->port = 993;
  183. cfg->use_sslv2 = 1;
  184. cfg->use_sslv3 = 1;
  185. }
  186. #endif
  187. cfg->host = strdup (val);
  188. }
  189. else if (!strcasecmp ("user", cmd))
  190. {
  191. if (*cur)
  192. (*cur)->user = strdup (val);
  193. else {
  194. free (global.user);
  195. global.user = strdup (val);
  196. }
  197. }
  198. else if (!strcasecmp ("pass", cmd))
  199. cfg->pass = strdup (val);
  200. else if (!strcasecmp ("port", cmd))
  201. cfg->port = atoi (val);
  202. else if (!strcasecmp ("box", cmd))
  203. cfg->box = strdup (val);
  204. else if (!strcasecmp ("alias", cmd))
  205. {
  206. if (!*cur) {
  207. fprintf (stderr,
  208. "%s:%d: keyword 'alias' allowed only in mailbox specification\n",
  209. path, line);
  210. continue;
  211. }
  212. cfg->alias = strdup (val);
  213. }
  214. else if (!strcasecmp ("maxsize", cmd))
  215. cfg->max_size = atol (val);
  216. else if (!strcasecmp ("MaxMessages", cmd))
  217. cfg->max_messages = atol (val);
  218. else if (!strcasecmp ("UseNamespace", cmd))
  219. cfg->use_namespace = is_true (val);
  220. else if (!strcasecmp ("CopyDeletedTo", cmd))
  221. cfg->copy_deleted_to = strdup (val);
  222. else if (!strcasecmp ("Tunnel", cmd))
  223. cfg->tunnel = strdup (val);
  224. else if (!strcasecmp ("Expunge", cmd))
  225. cfg->expunge = is_true (val);
  226. else if (!strcasecmp ("Delete", cmd))
  227. cfg->delete = is_true (val);
  228. #if HAVE_LIBSSL
  229. else if (!strcasecmp ("CertificateFile", cmd))
  230. cfg->cert_file = expand_strdup (val);
  231. else if (!strcasecmp ("RequireSSL", cmd))
  232. cfg->require_ssl = is_true (val);
  233. else if (!strcasecmp ("UseSSLv2", cmd))
  234. cfg->use_sslv2 = is_true (val);
  235. else if (!strcasecmp ("UseSSLv3", cmd))
  236. cfg->use_sslv3 = is_true (val);
  237. else if (!strcasecmp ("UseTLSv1", cmd))
  238. cfg->use_tlsv1 = is_true (val);
  239. else if (!strcasecmp ("RequireCRAM", cmd))
  240. cfg->require_cram = is_true (val);
  241. #endif
  242. else if (buf[0])
  243. fprintf (stderr, "%s:%d: unknown keyword '%s'\n", path, line, cmd);
  244. }
  245. fclose (fp);
  246. }
  247. config_t *
  248. find_box (const char *s)
  249. {
  250. config_t *p = boxes;
  251. for (; p; p = p->next)
  252. {
  253. if (!strcmp (s, p->path) || (p->alias && !strcmp (s, p->alias)))
  254. return p;
  255. else
  256. {
  257. /* check to see if the full pathname was specified on the
  258. * command line.
  259. */
  260. char *t = expand_strdup (p->path);
  261. if (!strcmp (s, t))
  262. {
  263. free (t);
  264. return p;
  265. }
  266. free (t);
  267. }
  268. }
  269. return 0;
  270. }
  271. void
  272. free_config (void)
  273. {
  274. free (global.user);
  275. free (global.maildir);
  276. free (global.host);
  277. free (global.pass);
  278. }