* the system at any one time. The key can be forcibly generated, revoking
* any previous control keys. To be used in conjunction with HTTP
* basic authentication.
- * This function will generate a JSON response that indicates success/failure.
* @param context The context to work in
* @param force Whether to force key generation or not.
- */
-void FCGI_LockControl(FCGIContext *context, bool force) {
+ * @return true on success, false otherwise (eg someone else already in control)
+ */
+bool FCGI_LockControl(FCGIContext *context, bool force) {
time_t now = time(NULL);
bool expired = now - context->control_timestamp > CONTROL_TIMEOUT;
-
+
if (force || !*(context->control_key) || expired)
{
SHA_CTX sha1ctx;
for (i = 0; i < 20; i++)
sprintf(context->control_key + i * 2, "%02x", sha1[i]);
snprintf(context->control_ip, 16, "%s", getenv("REMOTE_ADDR"));
+ return true;
}
+ return false;
}
/**
*/
void FCGI_ReleaseControl(FCGIContext *context) {
*(context->control_key) = 0;
- FCGI_BeginJSON(context, STATUS_OK);
- FCGI_EndJSON();
return;
}
typedef void (*ModuleHandler) (FCGIContext *context, char *params);
-extern void FCGI_LockControl(FCGIContext *context, bool force);
+extern bool FCGI_LockControl(FCGIContext *context, bool force);
extern void FCGI_ReleaseControl(FCGIContext *context);
extern bool FCGI_HasControl(FCGIContext *context, const char *key);
extern char *FCGI_KeyPair(char *in, const char **key, const char **value);
* @param params - Parameter string, UNUSED
*/
void Logout_Handler(FCGIContext * context, char * params)
-{
+{
FCGI_ReleaseControl(context);
+ FCGI_AcceptJSON(context, "Logged out", "0");
}
*/
void Login_Handler(FCGIContext * context, char * params)
{
-
- if (context->control_key[0] != '\0')
- {
- FCGI_RejectJSON(context, "Someone is already logged in.");
- return;
- }
-
char * user; // The username supplied through CGI
char * pass; // The password supplied through CGI
}
else
{
- FCGI_LockControl(context, false);
- // Give the user a cookie
- FCGI_AcceptJSON(context, "Logged in", context->control_key);
+ if (FCGI_LockControl(context, false))
+ {
+ // Give the user a cookie
+ FCGI_AcceptJSON(context, "Logged in", context->control_key);
+ }
+ else
+ {
+ FCGI_RejectJSON(context, "Someone else is already logged in");
+ }
}
}