summaryrefslogtreecommitdiff
path: root/src/ostree/ot-remote-cookie-util.c
blob: 8cf1eb07d23b4811eb6f032150f4f7f24fc505a2 (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
/*
 * Copyright (C) 2015 Red Hat, Inc.
 * Copyright (C) 2016 Sjoerd Simons <sjoerd@luon.net>
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * 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 2 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 <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "ot-remote-cookie-util.h"

#include "otutil.h"
#include "ot-main.h"
#include "ot-remote-builtins.h"
#include "ostree-repo-private.h"

typedef struct OtCookieParser OtCookieParser;
struct OtCookieParser {
  char *buf;
  char *iter;

  char *line;
  char *domain;
  char *flag;
  char *path;
  char *secure;
  long long unsigned int expiration;
  char *name;
  char *value;
};
void ot_cookie_parser_free (OtCookieParser *parser);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(OtCookieParser, ot_cookie_parser_free)

gboolean
ot_parse_cookies_at (int dfd, const char *path,
                     OtCookieParser **out_parser,
                     GCancellable *cancellable,
                     GError **error);
gboolean
ot_parse_cookies_next (OtCookieParser *parser);

static void
ot_cookie_parser_clear (OtCookieParser *parser)
{
  g_clear_pointer (&parser->domain, (GDestroyNotify)g_free);
  g_clear_pointer (&parser->flag, (GDestroyNotify)g_free);
  g_clear_pointer (&parser->path, (GDestroyNotify)g_free);
  g_clear_pointer (&parser->secure, (GDestroyNotify)g_free);
  g_clear_pointer (&parser->name, (GDestroyNotify)g_free);
  g_clear_pointer (&parser->value, (GDestroyNotify)g_free);
}

void
ot_cookie_parser_free (OtCookieParser *parser)
{
  ot_cookie_parser_clear (parser);
  g_free (parser->buf);
  g_free (parser);
}

gboolean
ot_parse_cookies_at (int dfd, const char *path,
                     OtCookieParser **out_parser,
                     GCancellable *cancellable,
                     GError **error)
{
  OtCookieParser *parser;
  g_autofree char *cookies_content = NULL;
  glnx_autofd int infd = -1;

  infd = openat (dfd, path, O_RDONLY | O_CLOEXEC);
  if (infd < 0)
    {
      if (errno != ENOENT)
        {
          glnx_set_error_from_errno (error);
          return FALSE;
        }
    }
  else
    {
      cookies_content = glnx_fd_readall_utf8 (infd, NULL, cancellable, error);
      if (!cookies_content)
        return FALSE;
    }

  parser = *out_parser = g_new0 (OtCookieParser, 1);
  parser->buf = g_steal_pointer (&cookies_content);
  parser->iter = parser->buf;
  return TRUE;
}

gboolean
ot_parse_cookies_next (OtCookieParser *parser)
{
  while (parser->iter)
    {
      char *iter = parser->iter;
      char *next = strchr (iter, '\n');

      if (next)
        {
          *next = '\0';
          parser->iter = next + 1;
        }
      else
        parser->iter = NULL;

      ot_cookie_parser_clear (parser);
      if (sscanf (iter, "%ms\t%ms\t%ms\t%ms\t%llu\t%ms\t%ms",
                  &parser->domain,
                  &parser->flag,
                  &parser->path,
                  &parser->secure,
                  &parser->expiration,
                  &parser->name,
                  &parser->value) != 7)
        continue;

      parser->line = iter;
      return TRUE;
    }

  return FALSE;
}

gboolean
ot_add_cookie_at (int dfd, const char *jar_path,
                  const char *domain, const char *path,
                  const char *name, const char *value,
                  GError **error)
{
  glnx_autofd int fd = openat (dfd, jar_path, O_WRONLY | O_APPEND | O_CREAT, 0644);
  if (fd < 0)
    return glnx_throw_errno_prefix (error, "open(%s)", jar_path);

  g_autoptr(GDateTime) now = g_date_time_new_now_utc ();
  g_autoptr(GDateTime) expires = g_date_time_add_years (now, 25);

  /* Adapted from soup-cookie-jar-text.c:write_cookie() */
  g_autofree char *buf = g_strdup_printf ("%s\t%s\t%s\t%s\t%llu\t%s\t%s\n",
                                          domain,
                                          *domain == '.' ? "TRUE" : "FALSE",
                                          path,
                                          "FALSE",
                                          (long long unsigned)g_date_time_to_unix (expires),
                                          name,
                                          value);
  if (glnx_loop_write (fd, buf, strlen (buf)) < 0)
    return glnx_throw_errno_prefix (error, "write");
  return TRUE;
}

gboolean
ot_delete_cookie_at (int dfd, const char *jar_path,
                     const char *domain, const char *path,
                     const char *name,
                     GError **error)
{
  gboolean found = FALSE;
  g_auto(GLnxTmpfile) tmpf = { 0, };
  g_autoptr(OtCookieParser) parser = NULL;

  if (!ot_parse_cookies_at (dfd, jar_path, &parser, NULL, error))
    return FALSE;

  g_assert (!strchr (jar_path, '/'));
  if (!glnx_open_tmpfile_linkable_at (dfd, ".", O_WRONLY | O_CLOEXEC,
                                      &tmpf, error))
    return FALSE;

  while (ot_parse_cookies_next (parser))
    {
      if (strcmp (domain, parser->domain) == 0 &&
          strcmp (path, parser->path) == 0 &&
          strcmp (name, parser->name) == 0)
        {
          found = TRUE;
          /* Match, skip writing this one */
          continue;
        }

      if (glnx_loop_write (tmpf.fd, parser->line, strlen (parser->line)) < 0 ||
          glnx_loop_write (tmpf.fd, "\n", 1) < 0)
        return glnx_throw_errno_prefix (error, "write");
    }

  if (!glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_REPLACE,
                             dfd, jar_path,
                             error))
    return FALSE;

  if (!found)
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Cookie not found in jar");

  return TRUE;
}


gboolean
ot_list_cookies_at (int dfd, const char *jar_path, GError **error)
{
  g_autoptr(OtCookieParser) parser = NULL;

  if (!ot_parse_cookies_at (AT_FDCWD, jar_path, &parser, NULL, error))
    return FALSE;

  while (ot_parse_cookies_next (parser))
    {
      g_autoptr(GDateTime) expires = g_date_time_new_from_unix_utc (parser->expiration);
      g_autofree char *expires_str = NULL;

      if (expires != NULL)
        expires_str = g_date_time_format (expires, "%Y-%m-%d %H:%M:%S +0000");

      g_print ("--\n");
      g_print ("Domain: %s\n", parser->domain);
      g_print ("Path: %s\n", parser->path);
      g_print ("Name: %s\n", parser->name);
      g_print ("Secure: %s\n", parser->secure);
      if (expires_str != NULL)
        g_print ("Expires: %s\n", expires_str);
      g_print ("Value: %s\n", parser->value);
    }

  return TRUE;
}