NetSurf
401login.c
Go to the documentation of this file.
1 /*
2  * Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk>
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 "utils/config.h"
20 
21 #include <assert.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include <oslib/wimp.h>
25 
26 #include "utils/log.h"
27 #include "utils/messages.h"
28 #include "utils/nsurl.h"
29 #include "netsurf/url_db.h"
30 
31 #include "riscos/gui.h"
32 #include "riscos/dialog.h"
33 #include "riscos/wimp_event.h"
34 
35 #define ICON_401LOGIN_LOGIN 0
36 #define ICON_401LOGIN_CANCEL 1
37 #define ICON_401LOGIN_HOST 2
38 #define ICON_401LOGIN_REALM 3
39 #define ICON_401LOGIN_USERNAME 4
40 #define ICON_401LOGIN_PASSWORD 5
41 
42 static void ro_gui_401login_close(wimp_w w);
43 static bool ro_gui_401login_apply(wimp_w w);
44 static nserror ro_gui_401login_open(nsurl *url, lwc_string *host,
45  const char *realm,
46  const char *username, const char *password,
47  nserror (*cb)(const char *username,
48  const char *password,
49  void *pw),
50  void *cbpw);
51 
52 static wimp_window *dialog_401_template;
53 
54 struct session_401 {
55  lwc_string *host; /**< Host for user display */
56  char *realm; /**< Authentication realm */
57  char uname[256]; /**< Buffer for username */
58  nsurl *url; /**< URL being fetched */
59  char pwd[256]; /**< Buffer for password */
60  nserror (*cb)(const char *username,
61  const char *password,
62  void *pw); /**< Continuation callback */
63  void *cbpw; /**< Continuation callback data */
64 };
65 
66 
67 /**
68  * Load the 401 login window template.
69  */
70 
72 {
73  dialog_401_template = ro_gui_dialog_load_template("login");
74 }
75 
76 
77 /**
78  * Open the login dialog
79  */
80 nserror gui_401login_open(nsurl *url, const char *realm,
81  const char *username, const char *password,
82  nserror (*cb)(const char *username,
83  const char *password,
84  void *pw),
85  void *cbpw)
86 {
87  nserror err;
88  lwc_string *host = nsurl_get_component(url, NSURL_HOST);
89  assert(host != NULL);
90 
91  err = ro_gui_401login_open(url, host, realm, username, password,
92  cb, cbpw);
93  lwc_string_unref(host);
94 
95  return err;
96 }
97 
98 
99 /**
100  * Open a 401 login window.
101  */
102 
103 nserror ro_gui_401login_open(nsurl *url, lwc_string *host, const char *realm,
104  const char *username, const char *password,
105  nserror (*cb)(const char *username,
106  const char *password,
107  void *pw),
108  void *cbpw)
109 {
110  struct session_401 *session;
111  size_t len;
112  wimp_w w;
113 
114  assert(host != NULL);
115  assert(username != NULL);
116  assert(password != NULL);
117 
118  session = calloc(1, sizeof(struct session_401));
119  if (!session) {
120  ro_warn_user("NoMemory", 0);
121  return NSERROR_NOMEM;
122  }
123 
124  if (realm == NULL)
125  realm = "Secure Area";
126 
127  session->url = nsurl_ref(url);
128  session->host = lwc_string_ref(host);
129  session->realm = strdup(realm);
130  session->cb = cb;
131  session->cbpw = cbpw;
132 
133  len = strlen(username);
134  assert(len < sizeof(session->uname));
135  memcpy(session->uname, username, len + 1);
136 
137  len = strlen(password);
138  assert(len < sizeof(session->pwd));
139  memcpy(session->pwd, password, len + 1);
140 
141  if (!session->realm) {
142  nsurl_unref(session->url);
143  lwc_string_unref(session->host);
144  free(session);
145  ro_warn_user("NoMemory", 0);
146  return NSERROR_NOMEM;
147  }
148 
149  /* fill in download window icons */
150  dialog_401_template->icons[ICON_401LOGIN_HOST].data.
151  indirected_text.text =
152  (char *)lwc_string_data(session->host);
153  dialog_401_template->icons[ICON_401LOGIN_HOST].data.
154  indirected_text.size =
155  lwc_string_length(session->host) + 1;
156  dialog_401_template->icons[ICON_401LOGIN_REALM].data.
157  indirected_text.text = session->realm;
158  dialog_401_template->icons[ICON_401LOGIN_REALM].data.
159  indirected_text.size = strlen(session->realm) + 1;
160  dialog_401_template->icons[ICON_401LOGIN_USERNAME].data.
161  indirected_text.text = session->uname;
162  dialog_401_template->icons[ICON_401LOGIN_USERNAME].data.
163  indirected_text.size = sizeof(session->uname);
164  dialog_401_template->icons[ICON_401LOGIN_PASSWORD].data.
165  indirected_text.text = session->pwd;
166  dialog_401_template->icons[ICON_401LOGIN_PASSWORD].data.
167  indirected_text.size = sizeof(session->pwd);
168 
169  /* create and open the window */
170  w = wimp_create_window(dialog_401_template);
171 
179 
180  ro_gui_dialog_open_persistent(NULL, w, false);
181 
182  return NSERROR_OK;
183 }
184 
185 /**
186  * Handle closing of login dialog
187  */
188 void ro_gui_401login_close(wimp_w w)
189 {
190  os_error *error;
191  struct session_401 *session;
192 
193  session = (struct session_401 *)ro_gui_wimp_event_get_user_data(w);
194 
195  assert(session);
196 
197  /* If ok didn't happen, send failure response */
198  if (session->cb != NULL)
199  session->cb(NULL, NULL, session->cbpw);
200 
201  nsurl_unref(session->url);
202  lwc_string_unref(session->host);
203  free(session->realm);
204  free(session);
205 
206  error = xwimp_delete_window(w);
207  if (error) {
208  NSLOG(netsurf, INFO, "xwimp_delete_window: 0x%x:%s",
209  error->errnum, error->errmess);
210  ro_warn_user("WimpError", error->errmess);
211  }
213 }
214 
215 
216 /* Login Clicked -> create a new fetch request, specifying uname & pwd
217  * CURLOPT_USERPWD takes a string "username:password"
218  */
219 bool ro_gui_401login_apply(wimp_w w)
220 {
221  struct session_401 *session;
222 
223  session = (struct session_401 *)ro_gui_wimp_event_get_user_data(w);
224 
225  assert(session);
226 
227  session->cb(session->uname, session->pwd, session->cbpw);
228 
229  /* Flag that we sent response by invalidating callback details */
230  session->cb = NULL;
231  session->cbpw = NULL;
232 
233  return true;
234 }
235 
static bool ro_gui_401login_apply(wimp_w w)
Definition: 401login.c:219
char * realm
Authentication realm.
Definition: 401login.c:56
Interface to utility string handling.
Localised message support (interface).
lwc_string * host
Host for user display.
Definition: 401login.c:55
void * cbpw
Continuation data.
Definition: login.c:36
Memory exhaustion.
Definition: errors.h:32
void ro_gui_401login_init(void)
Load the 401 login window template.
Definition: 401login.c:71
Automated RISC OS WIMP event handling (interface).
bool ro_gui_wimp_event_register_close_window(wimp_w w, void(*callback)(wimp_w w))
Register a function to be called after the window has been closed.
Definition: wimp_event.c:1492
char uname[256]
Buffer for username.
Definition: 401login.c:57
#define ICON_401LOGIN_LOGIN
Definition: 401login.c:35
nserror
Enumeration of error codes.
Definition: errors.h:29
#define ICON_401LOGIN_CANCEL
Definition: 401login.c:36
static nserror ro_gui_401login_open(nsurl *url, lwc_string *host, const char *realm, const char *username, const char *password, nserror(*cb)(const char *username, const char *password, void *pw), void *cbpw)
Open a 401 login window.
Definition: 401login.c:103
No error.
Definition: errors.h:30
wimp_window * ro_gui_dialog_load_template(const char *template_name)
Load a template without creating a window.
Definition: dialog.c:245
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:115
nsurl * url
URL being fetched.
Definition: 401login.c:58
bool ro_gui_wimp_event_register_text_field(wimp_w w, wimp_i i)
Register a text field to be automatically handled.
Definition: wimp_event.c:1318
char pwd[256]
Buffer for password.
Definition: 401login.c:59
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
bool ro_gui_wimp_event_register_ok(wimp_w w, wimp_i i, bool(*callback)(wimp_w w))
Register a function to be called for the OK action on a window.
Definition: wimp_event.c:1417
void * ro_gui_wimp_event_get_user_data(wimp_w w)
Gets the user data associated with a window.
Definition: wimp_event.c:486
lwc_string * nsurl_get_component(const nsurl *url, nsurl_component part)
Get part of a URL as a lwc_string, from a NetSurf URL object.
nserror(* cb)(struct nsurl *, const char *, const char *, const char *, void *)
Definition: 401login.c:33
bool ro_gui_wimp_event_set_user_data(wimp_w w, void *user)
Sets the user data associated with a window.
Definition: wimp_event.c:467
void ro_gui_dialog_open_persistent(wimp_w parent, wimp_w w, bool pointer)
Open a persistent dialog box relative to the pointer.
Definition: dialog.c:590
bool ro_gui_wimp_event_register_cancel(wimp_w w, wimp_i i)
Register a function to be called for the Cancel action on a window.
Definition: wimp_event.c:1403
Unified URL information database public interface.
void * cbpw
Definition: 401login.c:34
char * username
Definition: 401login.c:35
nserror(* cb)(const char *username, const char *password, void *pw)
Continuation callback.
Definition: login.c:33
#define ICON_401LOGIN_HOST
Definition: 401login.c:37
nsurl * nsurl_ref(nsurl *url)
Increment the reference count to a NetSurf URL object.
struct nsurl * url
Definition: 401login.c:38
#define ICON_401LOGIN_USERNAME
Definition: 401login.c:39
char * password
Definition: 401login.c:36
void ro_gui_wimp_event_finalise(wimp_w w)
Free any resources associated with a window.
Definition: wimp_event.c:296
nserror ro_warn_user(const char *warning, const char *detail)
Display a warning for a serious problem (eg memory exhaustion).
Definition: gui.c:2110
char * realm
Definition: 401login.c:37
NetSurf URL handling (interface).
#define ICON_401LOGIN_PASSWORD
Definition: 401login.c:40
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
login window session data
Definition: login.c:32
static wimp_window * dialog_401_template
Definition: 401login.c:52
static void ro_gui_401login_close(wimp_w w)
Handle closing of login dialog.
Definition: 401login.c:188
void nsurl_unref(nsurl *url)
Drop a reference to a NetSurf URL object.
#define ICON_401LOGIN_REALM
Definition: 401login.c:38