NetSurf
401login.c
Go to the documentation of this file.
1/*
2 * Copyright 2011 Daniel Silverstone <dsilvers@digital-scurf.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf 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; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <string.h>
23
24#include "utils/ring.h"
25#include "utils/nsurl.h"
26
27#include "monkey/output.h"
28#include "monkey/401login.h"
29
30struct monkey401 {
32 uint32_t num;
33 nserror (*cb)(struct nsurl*, const char *, const char *, const char *, void *);
34 void *cbpw;
35 char *username;
36 char *password;
37 char *realm;
38 struct nsurl *url;
39};
40
41static struct monkey401 *m401_ring = NULL;
42static uint32_t m401_ctr = 0;
43
44
47 const char *realm,
48 const char *username,
49 const char *password,
50 nserror (*cb)(struct nsurl *url,
51 const char *realm,
52 const char *username,
53 const char *password,
54 void *pw),
55 void *cbpw)
56{
57 struct monkey401 *m401_ctx;
58
59 m401_ctx = calloc(1, sizeof(*m401_ctx));
60 if (m401_ctx == NULL) {
61 return NSERROR_NOMEM;
62 }
63 m401_ctx->realm = strdup(realm);
64 if (m401_ctx->realm == NULL) {
65 free(m401_ctx);
66 return NSERROR_NOMEM;
67 }
68 m401_ctx->url = nsurl_ref(url);
69 m401_ctx->cb = cb;
70 m401_ctx->cbpw = cbpw;
71 m401_ctx->num = m401_ctr++;
72
73 RING_INSERT(m401_ring, m401_ctx);
74
75 if (username == NULL) {
76 username = "";
77 }
78
79 if (password == NULL) {
80 password = "";
81 }
82
83 moutf(MOUT_LOGIN, "OPEN LWIN %u URL %s", m401_ctx->num, nsurl_access(url));
84 moutf(MOUT_LOGIN, "USER LWIN %u STR %s", m401_ctx->num, username);
85 moutf(MOUT_LOGIN, "PASS LWIN %u STR %s", m401_ctx->num, password);
86 moutf(MOUT_LOGIN, "REALM LWIN %u STR %s", m401_ctx->num, realm);
87
88 return NSERROR_OK;
89}
90
91static struct monkey401 *
92monkey_find_login_by_num(uint32_t login_num)
93{
94 struct monkey401 *ret = NULL;
95
96 RING_ITERATE_START(struct monkey401, m401_ring, c_ring) {
97 if (c_ring->num == login_num) {
98 ret = c_ring;
100 }
101 } RING_ITERATE_END(m401_ring, c_ring);
102
103 return ret;
104}
105
106static void free_login_context(struct monkey401 *m401_ctx) {
107 moutf(MOUT_LOGIN, "DESTROY LWIN %u", m401_ctx->num);
108 RING_REMOVE(m401_ring, m401_ctx);
109 if (m401_ctx->username != NULL) {
110 free(m401_ctx->username);
111 }
112 if (m401_ctx->password != NULL) {
113 free(m401_ctx->password);
114 }
115 free(m401_ctx->realm);
116 nsurl_unref(m401_ctx->url);
117 free(m401_ctx);
118}
119
120static void
121monkey_login_handle_go(int argc, char **argv)
122{
123 struct monkey401 *m401_ctx;
124
125 if (argc != 3) {
126 moutf(MOUT_ERROR, "LOGIN GO ARGS BAD");
127 return;
128 }
129
130 m401_ctx = monkey_find_login_by_num(atoi(argv[2]));
131 if (m401_ctx == NULL) {
132 moutf(MOUT_ERROR, "LOGIN NUM BAD");
133 return;
134 }
135
136 m401_ctx->cb(m401_ctx->url, m401_ctx->realm, m401_ctx->username, m401_ctx->password, m401_ctx->cbpw);
137
138 free_login_context(m401_ctx);
139}
140
141static void
142monkey_login_handle_destroy(int argc, char **argv)
143{
144 struct monkey401 *m401_ctx;
145
146 if (argc != 3) {
147 moutf(MOUT_ERROR, "LOGIN DESTROY ARGS BAD");
148 return;
149 }
150
151 m401_ctx = monkey_find_login_by_num(atoi(argv[2]));
152 if (m401_ctx == NULL) {
153 moutf(MOUT_ERROR, "LOGIN NUM BAD");
154 return;
155 }
156
157 free_login_context(m401_ctx);
158}
159
160static void
161monkey_login_handle_username(int argc, char **argv)
162{
163 struct monkey401 *m401_ctx;
164
165 if (argc != 4) {
166 moutf(MOUT_ERROR, "LOGIN USERNAME ARGS BAD");
167 return;
168 }
169
170 m401_ctx = monkey_find_login_by_num(atoi(argv[2]));
171 if (m401_ctx == NULL) {
172 moutf(MOUT_ERROR, "LOGIN NUM BAD");
173 return;
174 }
175
176 if (m401_ctx->username != NULL) {
177 free(m401_ctx->username);
178 }
179
180 m401_ctx->username = strdup(argv[3]);
181}
182
183static void
184monkey_login_handle_password(int argc, char **argv)
185{
186 struct monkey401 *m401_ctx;
187
188 if (argc != 4) {
189 moutf(MOUT_ERROR, "LOGIN PASSWORD ARGS BAD");
190 return;
191 }
192
193 m401_ctx = monkey_find_login_by_num(atoi(argv[2]));
194 if (m401_ctx == NULL) {
195 moutf(MOUT_ERROR, "LOGIN NUM BAD");
196 return;
197 }
198
199 if (m401_ctx->password != NULL) {
200 free(m401_ctx->password);
201 }
202
203 m401_ctx->password = strdup(argv[3]);
204}
205
206void
207monkey_login_handle_command(int argc, char **argv)
208{
209 if (argc == 1)
210 return;
211
212 if (strcmp(argv[1], "USERNAME") == 0) {
214 } else if (strcmp(argv[1], "PASSWORD") == 0) {
216 } else if (strcmp(argv[1], "DESTROY") == 0) {
217 monkey_login_handle_destroy(argc, argv);
218 } else if (strcmp(argv[1], "GO") == 0) {
219 monkey_login_handle_go(argc, argv);
220 } else {
221 moutf(MOUT_ERROR, "LOGIN COMMAND UNKNOWN %s\n", argv[1]);
222 }
223}
static struct monkey401 * m401_ring
Definition: 401login.c:41
static void monkey_login_handle_password(int argc, char **argv)
Definition: 401login.c:184
static void monkey_login_handle_username(int argc, char **argv)
Definition: 401login.c:161
static uint32_t m401_ctr
Definition: 401login.c:42
void monkey_login_handle_command(int argc, char **argv)
Definition: 401login.c:207
static void free_login_context(struct monkey401 *m401_ctx)
Definition: 401login.c:106
static struct monkey401 * monkey_find_login_by_num(uint32_t login_num)
Definition: 401login.c:92
nserror gui_401login_open(struct nsurl *url, const char *realm, const char *username, const char *password, nserror(*cb)(struct nsurl *url, const char *realm, const char *username, const char *password, void *pw), void *cbpw)
Definition: 401login.c:46
static void monkey_login_handle_go(int argc, char **argv)
Definition: 401login.c:121
static void monkey_login_handle_destroy(int argc, char **argv)
Definition: 401login.c:142
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
NetSurf URL handling (interface).
void nsurl_unref(nsurl *url)
Drop a reference to a NetSurf URL object.
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
nsurl * nsurl_ref(nsurl *url)
Increment the reference count to a NetSurf URL object.
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
int moutf(enum monkey_output_type mout_type, const char *fmt,...)
Definition: output.c:39
@ MOUT_LOGIN
Definition: output.h:28
@ MOUT_ERROR
Definition: output.h:24
Ring list structure.
#define RING_ITERATE_STOP(ring, iteratorptr)
Definition: ring.h:133
#define RING_REMOVE(ring, element)
Remove the given element from the specified ring.
Definition: ring.h:53
#define RING_INSERT(ring, element)
Insert the given item into the specified ring.
Definition: ring.h:40
#define RING_ITERATE_END(ring, iteratorptr)
Definition: ring.h:136
#define RING_ITERATE_START(ringtype, ring, iteratorptr)
Definition: ring.h:127
Interface to utility string handling.
void * cbpw
Definition: 401login.c:34
char * password
Definition: 401login.c:36
struct monkey401 * r_next
Definition: 401login.c:31
char * username
Definition: 401login.c:35
nserror(* cb)(struct nsurl *, const char *, const char *, const char *, void *)
Definition: 401login.c:33
uint32_t num
Definition: 401login.c:32
char * realm
Definition: 401login.c:37
struct monkey401 * r_prev
Definition: 401login.c:31
struct nsurl * url
Definition: 401login.c:38