UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
web_main.cpp
Go to the documentation of this file.
1 
7 /*
8 Copyright (C) 2002-2020 UFO: Alien Invasion.
9 
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 See the GNU General Public License for more details.m
20 
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25 
26 #include "web_main.h"
27 #include "../cl_shared.h"
28 #include "../ui/ui_main.h"
29 #include "web_cgame.h"
30 #include "../../common/sha1.h"
31 
36 
44 bool WEB_GetURL (const char* url, http_callback_t callback, void* userdata)
45 {
46  char buf[576];
47  char passwordEncoded[512];
48  HTTP_Encode(web_password->string, passwordEncoded, sizeof(passwordEncoded));
49  char usernameEncoded[128];
50  HTTP_Encode(web_username->string, usernameEncoded, sizeof(usernameEncoded));
51  const char sep = strchr(url, '?') ? '&' : '?';
52  if (!Com_sprintf(buf, sizeof(buf), "%s%cusername=%s&password=%s", url, sep, usernameEncoded, passwordEncoded)) {
53  Com_Printf("overflow in url length: '%s'\n", buf);
54  return false;
55  }
56  return HTTP_GetURL(buf, callback, userdata);
57 }
58 
65 bool WEB_GetToFile (const char* url, FILE* file)
66 {
67  char buf[576];
68  char passwordEncoded[MAX_VAR];
69  HTTP_Encode(web_password->string, passwordEncoded, sizeof(passwordEncoded));
70  char usernameEncoded[MAX_VAR];
71  HTTP_Encode(web_username->string, usernameEncoded, sizeof(usernameEncoded));
72  if (!Com_sprintf(buf, sizeof(buf), "%s?username=%s&password=%s", url, usernameEncoded, passwordEncoded)) {
73  Com_Printf("overflow in url length: '%s'\n", buf);
74  return false;
75  }
76  return HTTP_GetToFile(buf, file);
77 }
78 
87 bool WEB_PutFile (const char* formName, const char* fileName, const char* url, upparam_t* params)
88 {
89  upparam_t paramUser;
90  upparam_t paramPassword;
91  paramUser.name = "username";
92  paramUser.value = web_username->string;
93  paramUser.next = &paramPassword;
94  paramPassword.name = "password";
95  paramPassword.value = web_password->string;
96  paramPassword.next = nullptr;
97  if (params != nullptr) {
98  params->next = &paramUser;
99  } else {
100  params = &paramUser;
101  }
102  return HTTP_PutFile(formName, fileName, url, params);
103 }
104 
110 static void WEB_AuthResponse (const char* response, void* userdata)
111 {
112  if (response == nullptr) {
113  Cvar_Set("web_password", "%s", "");
114  Cvar_Set("web_userid", "0");
115  return;
116  }
117  Com_DPrintf(DEBUG_CLIENT, "response: '%s'\n", response);
118  if (Q_streq(response, "0")) {
119  /* failed */
120  Cvar_Set("web_password", "%s", "");
121  Cvar_Set("web_userid", "0");
122  } else {
123  Cvar_Set("web_userid", "%i", atoi(response));
124  }
125 }
126 
135 bool WEB_Auth (const char* username, const char* password)
136 {
137  char digest[41];
138  char user[MAX_VAR];
139  Q_strncpyz(user, username, sizeof(user));
140  Q_strlwr(user);
141  char combined[512];
142  Com_sprintf(combined, sizeof(combined), "%s%s", user, password);
143  Com_SHA1Buffer((const byte*)combined, strlen(combined), digest);
144  Cvar_Set("web_username", "%s", username);
145  Cvar_Set("web_password", "%s", digest);
146  if (!WEB_GetURL(web_authurl->string, WEB_AuthResponse)) {
147  Cvar_Set("web_password", "%s", "");
148  Cvar_Set("web_userid", "0");
149  return false;
150  }
151  /* if the password is still set, the auth was successful */
152  return Q_strvalid(web_password->string);
153 }
154 
159 static void WEB_Auth_f (void)
160 {
161  if (Cmd_Argc() != 3) {
162  Com_Printf("Usage: %s <username> <password>\n", Cmd_Argv(0));
163  return;
164  }
165  if (WEB_Auth(Cmd_Argv(1), Cmd_Argv(2))) {
166  UI_ExecuteConfunc("web_authsuccessful");
167  } else {
168  UI_ExecuteConfunc("web_authfailed");
169  }
170 }
171 
176 bool WEB_CheckAuth (void)
177 {
178  if (Q_strnull(web_password->string)) {
179  UI_PushWindow("webauth");
180  return false;
181  }
182  return true;
183 }
184 
185 void WEB_InitStartup (void)
186 {
187  Cmd_AddCommand("web_auth", WEB_Auth_f, "Perform the authentication against the UFOAI server");
188 
189  web_username = Cvar_Get("web_username", Sys_GetCurrentUser(), CVAR_ARCHIVE, "The username for the UFOAI server.");
190  /* if the password is a non-empty string, this means that username and password
191  * are valid, and the authentication was successful */
192  web_password = Cvar_Get("web_password", "", CVAR_ARCHIVE, "The encrypted password for the UFOAI server.");
193  web_userid = Cvar_Get("web_userid", "0", 0, "Your userid for the UFOAI server");
194  web_authurl = Cvar_Get("web_authurl", WEB_API_SERVER "api/auth.php", CVAR_ARCHIVE,
195  "The url to perform the authentication against.");
196 
197  WEB_CGameCvars();
199 
200  Com_Printf("\n------- web initialization ---------\n");
201  if (Q_strvalid(web_password->string)) {
202  Com_Printf("... using username '%s'\n", web_username->string);
203  if (!WEB_GetURL(web_authurl->string, WEB_AuthResponse)) {
204  Cvar_Set("web_password", "%s", "");
205  }
206  if (Q_strvalid(web_password->string)) {
207  Com_Printf("... login successful\n");
208  } else {
209  Com_Printf("... login failed\n");
210  }
211  } else {
212  Com_Printf("... web access not yet configured\n");
213  }
214 }
bool Q_strnull(const char *string)
Definition: shared.h:138
const char * Cmd_Argv(int arg)
Returns a given argument.
Definition: cmd.cpp:516
void Cmd_AddCommand(const char *cmdName, xcommand_t function, const char *desc)
Add a new command to the script interface.
Definition: cmd.cpp:744
struct upparam_s * next
Definition: http.h:62
cvar_t * web_userid
Definition: web_main.cpp:35
bool Com_SHA1Buffer(const unsigned char *buf, unsigned int len, char digest[41])
Definition: sha1.cpp:358
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition: cvar.h:71
const char * value
Definition: http.h:61
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition: shared.cpp:494
#define FILE
Definition: test_webapi.cpp:30
bool WEB_Auth(const char *username, const char *password)
Performs a web auth request.
Definition: web_main.cpp:135
static const char * user
Definition: test_webapi.cpp:35
cvar_t * web_password
Definition: web_main.cpp:34
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
#define WEB_API_SERVER
Definition: web_main.h:31
void(* http_callback_t)(const char *response, void *userdata)
Definition: http.h:65
static cvar_t * web_authurl
Definition: web_main.cpp:32
voidpf void * buf
Definition: ioapi.h:42
#define CVAR_ARCHIVE
Definition: cvar.h:40
const char * name
Definition: http.h:60
void WEB_CGameCommands(void)
Definition: web_cgame.cpp:411
#define Q_strvalid(string)
Definition: shared.h:141
static void WEB_AuthResponse(const char *response, void *userdata)
The callback for the web auth request.
Definition: web_main.cpp:110
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
uiNode_t * UI_PushWindow(const char *name, const char *parentName, linkedList_t *params)
Push a window onto the window stack.
Definition: ui_windows.cpp:170
#define DEBUG_CLIENT
Definition: defines.h:59
cvar_t * Cvar_Get(const char *var_name, const char *var_value, int flags, const char *desc)
Init or return a cvar.
Definition: cvar.cpp:342
bool WEB_GetToFile(const char *url, FILE *file)
Downloads the given url directly into the given file. The login credentials are automatically added a...
Definition: web_main.cpp:65
bool WEB_PutFile(const char *formName, const char *fileName, const char *url, upparam_t *params)
Uploads a file to the server with the login credentials.
Definition: web_main.cpp:87
#define MAX_VAR
Definition: shared.h:36
bool HTTP_PutFile(const char *formName, const char *fileName, const char *url, const upparam_t *params)
Definition: http.cpp:265
int Cmd_Argc(void)
Return the number of arguments of the current command. "command parameter" will result in a argc of 2...
Definition: cmd.cpp:505
cvar_t * web_username
Definition: web_main.cpp:33
void Com_DPrintf(int level, const char *fmt,...)
A Com_Printf that only shows up if the "developer" cvar is set.
Definition: common.cpp:398
bool HTTP_GetToFile(const char *url, FILE *file, const char *postfields)
Downloads the given url into the given file.
Definition: http.cpp:336
bool WEB_CheckAuth(void)
Pushes the webauth window if the password is not yet set.
Definition: web_main.cpp:176
char * Q_strlwr(char *str)
Converts a string to lowercase.
Definition: shared.cpp:438
const char * Sys_GetCurrentUser(void)
void WEB_InitStartup(void)
Definition: web_main.cpp:185
cvar_t * password
Definition: g_main.cpp:67
bool HTTP_Encode(const char *url, char *out, size_t outLength)
This function converts the given url to an URL encoded string. All input characters that are not a-z...
Definition: http.cpp:352
char * string
Definition: cvar.h:73
UFOAI web interface management. Authentication as well as uploading/downloading stuff to and from you...
bool HTTP_GetURL(const char *url, http_callback_t callback, void *userdata, const char *postfields)
Downloads the given url and return the data to the callback (optional)
Definition: http.cpp:374
Definition: http.h:59
void WEB_CGameCvars(void)
Definition: web_cgame.cpp:403
cvar_t * Cvar_Set(const char *varName, const char *value,...)
Sets a cvar value.
Definition: cvar.cpp:615
#define Q_streq(a, b)
Definition: shared.h:136
UFOAI web interface management. c(lient)game related stuff.
uint8_t byte
Definition: ufotypes.h:34
static void WEB_Auth_f(void)
Console callback for handling the web auth.
Definition: web_main.cpp:159
void UI_ExecuteConfunc(const char *fmt,...)
Executes confunc - just to identify those confuncs in the code - in this frame.
Definition: ui_main.cpp:110
bool WEB_GetURL(const char *url, http_callback_t callback, void *userdata)
Downloads the given url and notify the callback. The login credentials are automatically added as GET...
Definition: web_main.cpp:44