summaryrefslogtreecommitdiff
path: root/libmailutils/wicket/file.c
blob: 3eb4477303cbf5c571fc5dc06d28efe23a7fecb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999-2021 Free Software Foundation, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */

/* A "file wicket" implementation */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/auth.h>
#include <mailutils/url.h>
#include <mailutils/stream.h>
#include <mailutils/cstr.h>
#include <mailutils/nls.h>

struct file_wicket
{
  char *filename;
};

static void
_file_wicket_destroy (mu_wicket_t wicket)
{
  struct file_wicket *fw = mu_wicket_get_data (wicket);
  free (fw->filename);
  free (fw);
}

struct file_ticket
{
  char *filename;
  char *user;
  mu_url_t tickurl;
};

static void
file_ticket_destroy (mu_ticket_t ticket)
{
  struct file_ticket *ft = mu_ticket_get_data (ticket);
  if (ft)
    {
      free (ft->filename);
      free (ft->user);
      mu_url_destroy (&ft->tickurl);
      free (ft);
    }
}

int
file_ticket_get_cred (mu_ticket_t ticket, mu_url_t url, const char *challenge,
		      char **pplain, mu_secret_t *psec)
{
  struct file_ticket *ft = mu_ticket_get_data (ticket);
  int rc = 0;

  if (!ft->tickurl)
    {
      rc = mu_wicket_file_match_url (ft->filename, url,
				     MU_URL_PARSE_ALL,
				     &ft->tickurl);
      if (rc)
	return rc;
     }
  if (pplain)
    {
      if (ft->user)
	{
	  *pplain = strdup (ft->user);
	  if (!*pplain)
	    rc = ENOMEM;
	}
      else
	rc = mu_url_aget_user (ft->tickurl, pplain);
    }
  else
    rc = mu_url_get_secret (ft->tickurl, psec);
  return rc;
}

static int
_file_wicket_get_ticket (mu_wicket_t wicket, void *data,
			 const char *user, mu_ticket_t *pticket)
{
  int rc;
  mu_ticket_t ticket;
  struct file_wicket *fw = data;
  struct file_ticket *ft = calloc (1, sizeof (*ft));
  ft->filename = strdup (fw->filename);
  if (!ft->filename)
    {
      free (ft);
      return ENOMEM;
    }
  if (user)
    {
      ft->user = strdup (user);
      if (!ft->user)
	{
	  free (ft->filename);
	  free (ft);
	  return ENOMEM;
	}
    }
  else
    ft->user = NULL;

  rc = mu_ticket_create (&ticket, NULL);
  if (rc)
    {
      free (ft->filename);
      free (ft->user);
      free (ft);
      return rc;
    }

  mu_ticket_set_destroy (ticket, file_ticket_destroy, NULL);
  mu_ticket_set_data (ticket, ft, NULL);
  mu_ticket_set_get_cred (ticket, file_ticket_get_cred, NULL);

  *pticket = ticket;
  return 0;
}

int
mu_wicket_stream_match_url (mu_stream_t stream, struct mu_locus_point *loc,
			    mu_url_t url, int parse_flags,
			    mu_url_t *pticket_url)
{
  int rc;
  mu_url_t u = NULL;
  char *buf = NULL;
  size_t bufsize = 0;
  size_t len;
  mu_url_t pret = NULL;
  int weight = 0;
  int line = loc->mu_line;

  while ((rc = mu_stream_getline (stream, &buf, &bufsize, &len)) == 0
	 && len > 0)
    {
      char *p;
      int err;
      int n;

      loc->mu_line++;
      p = mu_str_stripws (buf);

      /* Skip empty lines and comments. */
      if (*p == 0 || *p == '#')
	continue;

      if ((err = mu_url_create_hint (&u, p, parse_flags, NULL)) != 0)
	{
	  /* Skip erroneous entry */
	  mu_error (_("%s:%u: cannot create URL: %s"),
		    loc->mu_file, loc->mu_line, mu_strerror (err));
	  continue;
	}

      if (!mu_url_has_flag (u, MU_URL_USER|MU_URL_SECRET))
	{
	  mu_error (_("%s:%u: URL is missing required parts"),
		    loc->mu_file, loc->mu_line);
	  mu_url_destroy (&u);
	  continue;
	}

      if (!mu_url_matches_ticket (u, url, &n))
	{
	  mu_url_destroy (&u);
	  continue;
	}

      if (!pret || n < weight)
	{
	  pret = u;
	  weight = n;
	  line = loc->mu_line;
	  if (weight == 0)
	    break;
	}
    }
  free (buf);

  if (rc == 0)
    {
      if (pret)
	{
	  *pticket_url = pret;
	  loc->mu_line = line;
	}
      else
	rc = MU_ERR_NOENT;
    }

  return rc;
}

int
mu_wicket_file_match_url (const char *name, mu_url_t url,
			  int parse_flags,
			  mu_url_t *pticket_url)
{
  mu_stream_t stream;
  int rc;
  struct mu_locus_point loc;

  rc = mu_file_stream_create (&stream, name, MU_STREAM_READ);
  if (rc)
    return rc;
  loc.mu_file = (char*) name;
  loc.mu_line = 0;
  loc.mu_col = 0;
  rc = mu_wicket_stream_match_url (stream, &loc, url, parse_flags,
				   pticket_url);
  mu_stream_close (stream);
  mu_stream_destroy (&stream);
  return rc;
}

int
mu_file_wicket_create (mu_wicket_t *pwicket, const char *filename)
{
  mu_wicket_t wicket;
  int rc;
  struct file_wicket *fw = calloc (1, sizeof (*fw));

  if (!fw)
    return ENOMEM;
  fw->filename = strdup (filename);
  if (!fw->filename)
    {
      free (fw);
      return ENOMEM;
    }

  rc = mu_wicket_create (&wicket);
  if (rc)
    {
      free (fw->filename);
      free (fw);
      return rc;
    }
  mu_wicket_set_data (wicket, fw);
  mu_wicket_set_destroy (wicket, _file_wicket_destroy);
  mu_wicket_set_get_ticket (wicket, _file_wicket_get_ticket);
  *pwicket = wicket;
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.