config.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. static int
  87. is_true (const char *val)
  88. {
  89. return
  90. !strcasecmp (val, "yes") ||
  91. !strcasecmp (val, "true") ||
  92. !strcasecmp (val, "on") ||
  93. !strcmp (val, "1");
  94. }
  95. void
  96. load_config (const char *where, int *o2o)
  97. {
  98. char path[_POSIX_PATH_MAX];
  99. char buf[1024];
  100. struct passwd *pw;
  101. config_t **cur = &boxes, *cfg;
  102. int line = 0;
  103. FILE *fp;
  104. char *p, *cmd, *val;
  105. if (!where)
  106. {
  107. pw = getpwuid (getuid ());
  108. snprintf (path, sizeof (path), "%s/.isyncrc", pw->pw_dir);
  109. where = path;
  110. }
  111. info ("Reading configuration file %s\n", where);
  112. fp = fopen (where, "r");
  113. if (!fp)
  114. {
  115. if (errno != ENOENT)
  116. perror ("fopen");
  117. return;
  118. }
  119. buf[sizeof buf - 1] = 0;
  120. cfg = &global;
  121. while ((fgets (buf, sizeof (buf) - 1, fp)))
  122. {
  123. p = buf;
  124. cmd = next_arg (&p);
  125. val = next_arg (&p);
  126. line++;
  127. if (!cmd || *cmd == '#')
  128. continue;
  129. if (!strcasecmp ("mailbox", cmd))
  130. {
  131. if (*o2o)
  132. break;
  133. cur = &(*cur)->next;
  134. cfg = *cur = malloc (sizeof (config_t));
  135. config_defaults (cfg);
  136. /* not expanded at this point */
  137. cfg->path = strdup (val);
  138. }
  139. else if (!strcasecmp ("OneToOne", cmd))
  140. {
  141. if (*cur) {
  142. forbid:
  143. fprintf (stderr,
  144. "%s:%d: keyword '%s' allowed only in global section\n",
  145. path, line, cmd);
  146. continue;
  147. }
  148. *o2o = is_true (val);
  149. }
  150. else if (!strcasecmp ("maildir", cmd))
  151. {
  152. if (*cur)
  153. goto forbid;
  154. /* this only affects the global setting */
  155. free (global.maildir);
  156. global.maildir = expand_strdup (val);
  157. }
  158. else if (!strcasecmp ("folder", cmd))
  159. {
  160. if (*cur)
  161. goto forbid;
  162. /* this only affects the global setting */
  163. global.folder = strdup (val);
  164. }
  165. else if (!strcasecmp ("inbox", cmd))
  166. {
  167. if (*cur)
  168. goto forbid;
  169. /* this only affects the global setting */
  170. global.inbox = strdup (val);
  171. }
  172. else if (!strcasecmp ("host", cmd))
  173. {
  174. #if HAVE_LIBSSL
  175. if (!strncasecmp ("imaps:", val, 6))
  176. {
  177. val += 6;
  178. cfg->use_imaps = 1;
  179. cfg->port = 993;
  180. cfg->use_sslv2 = 1;
  181. cfg->use_sslv3 = 1;
  182. }
  183. #endif
  184. cfg->host = strdup (val);
  185. }
  186. else if (!strcasecmp ("user", cmd))
  187. {
  188. if (*cur)
  189. (*cur)->user = strdup (val);
  190. else {
  191. free (global.user);
  192. global.user = strdup (val);
  193. }
  194. }
  195. else if (!strcasecmp ("pass", cmd))
  196. cfg->pass = strdup (val);
  197. else if (!strcasecmp ("port", cmd))
  198. cfg->port = atoi (val);
  199. else if (!strcasecmp ("box", cmd))
  200. cfg->box = strdup (val);
  201. else if (!strcasecmp ("alias", cmd))
  202. {
  203. if (!*cur) {
  204. fprintf (stderr,
  205. "%s:%d: keyword 'alias' allowed only in mailbox specification\n",
  206. path, line);
  207. continue;
  208. }
  209. cfg->alias = strdup (val);
  210. }
  211. else if (!strcasecmp ("maxsize", cmd))
  212. cfg->max_size = atol (val);
  213. else if (!strcasecmp ("MaxMessages", cmd))
  214. cfg->max_messages = atol (val);
  215. else if (!strcasecmp ("UseNamespace", cmd))
  216. cfg->use_namespace = is_true (val);
  217. else if (!strcasecmp ("CopyDeletedTo", cmd))
  218. cfg->copy_deleted_to = strdup (val);
  219. else if (!strcasecmp ("Tunnel", cmd))
  220. cfg->tunnel = strdup (val);
  221. else if (!strcasecmp ("Expunge", cmd))
  222. cfg->expunge = is_true (val);
  223. else if (!strcasecmp ("Delete", cmd))
  224. cfg->delete = is_true (val);
  225. #if HAVE_LIBSSL
  226. else if (!strcasecmp ("CertificateFile", cmd))
  227. cfg->cert_file = expand_strdup (val);
  228. else if (!strcasecmp ("RequireSSL", cmd))
  229. cfg->require_ssl = is_true (val);
  230. else if (!strcasecmp ("UseSSLv2", cmd))
  231. cfg->use_sslv2 = is_true (val);
  232. else if (!strcasecmp ("UseSSLv3", cmd))
  233. cfg->use_sslv3 = is_true (val);
  234. else if (!strcasecmp ("UseTLSv1", cmd))
  235. cfg->use_tlsv1 = is_true (val);
  236. else if (!strcasecmp ("RequireCRAM", cmd))
  237. cfg->require_cram = is_true (val);
  238. #endif
  239. else if (buf[0])
  240. fprintf (stderr, "%s:%d: unknown keyword '%s'\n", path, line, cmd);
  241. }
  242. fclose (fp);
  243. }
  244. config_t *
  245. find_box (const char *s)
  246. {
  247. config_t *p = boxes;
  248. for (; p; p = p->next)
  249. {
  250. if (!strcmp (s, p->path) || (p->alias && !strcmp (s, p->alias)))
  251. return p;
  252. else
  253. {
  254. /* check to see if the full pathname was specified on the
  255. * command line.
  256. */
  257. char *t = expand_strdup (p->path);
  258. if (!strcmp (s, t))
  259. {
  260. free (t);
  261. return p;
  262. }
  263. free (t);
  264. }
  265. }
  266. return 0;
  267. }
  268. void
  269. free_config (void)
  270. {
  271. free (global.user);
  272. free (global.maildir);
  273. free (global.host);
  274. free (global.pass);
  275. }