mirror of
https://github.com/haiwen/seafile.git
synced 2025-01-08 11:57:44 +08:00
Update check-tx processor to v3.
Remove the slow encryption key calculation.
This commit is contained in:
parent
d9824a91d9
commit
13de4a128a
@ -19,6 +19,7 @@ endif
|
||||
proc_headers = $(addprefix processors/, \
|
||||
check-tx-proc.h \
|
||||
check-tx-v2-proc.h \
|
||||
check-tx-v3-proc.h \
|
||||
getcommit-proc.h \
|
||||
sendcommit-proc.h \
|
||||
sendfs-proc.h \
|
||||
@ -93,6 +94,7 @@ common_src = \
|
||||
../common/mq-mgr.c \
|
||||
processors/check-tx-proc.c \
|
||||
processors/check-tx-v2-proc.c \
|
||||
processors/check-tx-v3-proc.c \
|
||||
processors/getcommit-proc.c \
|
||||
processors/sendcommit-proc.c \
|
||||
processors/sendfs-proc.c \
|
||||
|
@ -37,6 +37,11 @@
|
||||
#define SC_BAD_REPO "406"
|
||||
#define SS_BAD_REPO "Repo doesn't exist"
|
||||
|
||||
enum {
|
||||
CHECK_TX_TYPE_UPLOAD,
|
||||
CHECK_TX_TYPE_DOWNLOAD,
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (SeafileCheckTxV2Proc, seafile_check_tx_v2_proc, CCNET_TYPE_PROCESSOR)
|
||||
|
||||
static int start (CcnetProcessor *processor, int argc, char **argv);
|
||||
|
@ -8,11 +8,6 @@
|
||||
|
||||
#include "transfer-mgr.h"
|
||||
|
||||
enum {
|
||||
CHECK_TX_TYPE_UPLOAD,
|
||||
CHECK_TX_TYPE_DOWNLOAD,
|
||||
};
|
||||
|
||||
#define SEAFILE_TYPE_CHECK_TX_V2_PROC (seafile_check_tx_v2_proc_get_type ())
|
||||
#define SEAFILE_CHECK_TX_V2_PROC(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEAFILE_TYPE_CHECK_TX_V2_PROC, SeafileCheckTxV2Proc))
|
||||
#define SEAFILE_IS_CHECK_TX_V2_PROC(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SEAFILE_TYPE_CHECK_TX_PROC))
|
||||
|
274
daemon/processors/check-tx-v3-proc.c
Normal file
274
daemon/processors/check-tx-v3-proc.c
Normal file
@ -0,0 +1,274 @@
|
||||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <ccnet.h>
|
||||
|
||||
#include <openssl/aes.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "seafile-session.h"
|
||||
#include "vc-common.h"
|
||||
#include "seafile-crypt.h"
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "check-tx-v3-proc.h"
|
||||
|
||||
#define SC_GET_TOKEN "301"
|
||||
#define SS_GET_TOKEN "Get token"
|
||||
#define SC_PUT_TOKEN "302"
|
||||
#define SS_PUT_TOKEN "Put token"
|
||||
#define SC_GET_VERSION "303"
|
||||
#define SS_GET_VERSION "Get version"
|
||||
#define SC_VERSION "304"
|
||||
#define SS_VERSION "Version"
|
||||
|
||||
#define SC_ACCESS_DENIED "401"
|
||||
#define SS_ACCESS_DENIED "Access denied"
|
||||
#define SC_PROTOCOL_MISMATCH "405"
|
||||
#define SS_PROTOCOL_MISMATCH "Protocol version mismatch"
|
||||
|
||||
/* Only for upload */
|
||||
#define SC_QUOTA_ERROR "402"
|
||||
#define SS_QUOTA_ERROR "Failed to get quota"
|
||||
#define SC_QUOTA_FULL "403"
|
||||
#define SS_QUOTA_FULL "storage for the repo's owner is full"
|
||||
|
||||
/* Only for download */
|
||||
#define SC_BAD_REPO "406"
|
||||
#define SS_BAD_REPO "Repo doesn't exist"
|
||||
|
||||
enum {
|
||||
CHECK_TX_TYPE_UPLOAD,
|
||||
CHECK_TX_TYPE_DOWNLOAD,
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (SeafileCheckTxV3Proc, seafile_check_tx_v3_proc, CCNET_TYPE_PROCESSOR)
|
||||
|
||||
static int start (CcnetProcessor *processor, int argc, char **argv);
|
||||
static void handle_response (CcnetProcessor *processor,
|
||||
char *code, char *code_msg,
|
||||
char *content, int clen);
|
||||
|
||||
static void
|
||||
release_resource(CcnetProcessor *processor)
|
||||
{
|
||||
CCNET_PROCESSOR_CLASS (seafile_check_tx_v3_proc_parent_class)->release_resource (processor);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
seafile_check_tx_v3_proc_class_init (SeafileCheckTxV3ProcClass *klass)
|
||||
{
|
||||
CcnetProcessorClass *proc_class = CCNET_PROCESSOR_CLASS (klass);
|
||||
|
||||
proc_class->name = "check-tx-proc-v3";
|
||||
proc_class->start = start;
|
||||
proc_class->handle_response = handle_response;
|
||||
proc_class->release_resource = release_resource;
|
||||
}
|
||||
|
||||
static void
|
||||
seafile_check_tx_v3_proc_init (SeafileCheckTxV3Proc *processor)
|
||||
{
|
||||
}
|
||||
|
||||
/* token -> AES encrypt with session key -> rawdata_to_hex -> output */
|
||||
static char *
|
||||
encrypt_token (CcnetProcessor *processor, const char *token)
|
||||
{
|
||||
CcnetPeer *peer = NULL;
|
||||
char *enc_out = NULL;
|
||||
SeafileCrypt *crypt = NULL;
|
||||
unsigned char key[16], iv[16];
|
||||
int len;
|
||||
char *output = NULL;
|
||||
|
||||
if (!token)
|
||||
goto out;
|
||||
|
||||
peer = ccnet_get_peer(seaf->ccnetrpc_client, processor->peer_id);
|
||||
if (!peer || !peer->session_key) {
|
||||
seaf_warning ("[check tx v3] peer or peer session key not exist\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
EVP_BytesToKey (EVP_aes_128_cbc(), /* cipher mode */
|
||||
EVP_sha1(), /* message digest */
|
||||
NULL, /* slat */
|
||||
(unsigned char*)peer->session_key,
|
||||
strlen(peer->session_key),
|
||||
1, /* iteration times */
|
||||
key, /* the derived key */
|
||||
iv); /* IV, initial vector */
|
||||
|
||||
crypt = seafile_crypt_new (CURRENT_ENC_VERSION, key, iv);
|
||||
|
||||
/* encrypt the token with session key, including the trailing null byte */
|
||||
if (seafile_encrypt (&enc_out, &len, token, strlen(token) + 1, crypt) < 0) {
|
||||
seaf_warning ("[check tx v3] failed to encrypt token\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
output = g_malloc (len * 2 + 1);
|
||||
rawdata_to_hex ((unsigned char *)enc_out, output, len);
|
||||
output[len * 2] = '\0';
|
||||
|
||||
|
||||
out:
|
||||
g_free (crypt);
|
||||
g_free (enc_out);
|
||||
if (peer)
|
||||
g_object_unref(peer);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
start (CcnetProcessor *processor, int argc, char **argv)
|
||||
{
|
||||
SeafileCheckTxV3Proc *proc = (SeafileCheckTxV3Proc *)processor;
|
||||
TransferTask *task = proc->task;
|
||||
char *type, *enc_token;
|
||||
GString *buf;
|
||||
|
||||
if (argc != 1) {
|
||||
transition_state_to_error (task, TASK_ERR_CHECK_UPLOAD_START);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
type = argv[0];
|
||||
if (strcmp (type, "upload") == 0)
|
||||
proc->type = CHECK_TX_TYPE_UPLOAD;
|
||||
else
|
||||
proc->type = CHECK_TX_TYPE_DOWNLOAD;
|
||||
|
||||
enc_token = encrypt_token (processor, task->token);
|
||||
if (!enc_token) {
|
||||
transition_state_to_error (task, TASK_ERR_CHECK_UPLOAD_START);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
buf = g_string_new(NULL);
|
||||
g_string_append_printf (buf,
|
||||
"remote %s seafile-check-tx-slave-v3 %s %d %s %s %s",
|
||||
processor->peer_id, type, CURRENT_PROTO_VERSION,
|
||||
task->repo_id, task->to_branch, enc_token);
|
||||
|
||||
ccnet_processor_send_request (processor, buf->str);
|
||||
|
||||
g_free (enc_token);
|
||||
g_string_free (buf, TRUE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
handle_upload_ok (CcnetProcessor *processor, TransferTask *task,
|
||||
char *content, int clen)
|
||||
{
|
||||
if (clen == 0) {
|
||||
ccnet_processor_send_update (processor,
|
||||
SC_GET_TOKEN, SS_GET_TOKEN,
|
||||
NULL, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (clen != 41 || content[clen-1] != '\0') {
|
||||
g_warning ("Bad response content.\n");
|
||||
transfer_task_set_error (task, TASK_ERR_UNKNOWN);
|
||||
ccnet_processor_send_update (processor, SC_BAD_ARGS, SS_BAD_ARGS, NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return;
|
||||
}
|
||||
memcpy (task->remote_head, content, 41);
|
||||
|
||||
/* Check fast-forward here. */
|
||||
if (strcmp (task->head, task->remote_head) != 0 &&
|
||||
!is_fast_forward (task->head, task->remote_head)) {
|
||||
g_warning ("Upload is not fast-forward.\n");
|
||||
transfer_task_set_error (task, TASK_ERR_NOT_FAST_FORWARD);
|
||||
ccnet_processor_send_update (processor, SC_SHUTDOWN, SS_SHUTDOWN, NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return;
|
||||
}
|
||||
ccnet_processor_send_update (processor,
|
||||
SC_GET_TOKEN, SS_GET_TOKEN,
|
||||
NULL, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
handle_download_ok (CcnetProcessor *processor, TransferTask *task,
|
||||
char *content, int clen)
|
||||
{
|
||||
if (clen != 41 || content[clen-1] != '\0') {
|
||||
g_warning ("Bad response content.\n");
|
||||
transfer_task_set_error (task, TASK_ERR_UNKNOWN);
|
||||
ccnet_processor_send_update (processor, SC_BAD_ARGS, SS_BAD_ARGS, NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy (task->head, content, 41);
|
||||
ccnet_processor_send_update (processor,
|
||||
SC_GET_TOKEN, SS_GET_TOKEN,
|
||||
NULL, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
handle_response (CcnetProcessor *processor,
|
||||
char *code, char *code_msg,
|
||||
char *content, int clen)
|
||||
{
|
||||
SeafileCheckTxV3Proc *proc = (SeafileCheckTxV3Proc *)processor;
|
||||
TransferTask *task = proc->task;
|
||||
|
||||
if (strncmp(code, SC_OK, 3) == 0) {
|
||||
if (proc->type == CHECK_TX_TYPE_UPLOAD)
|
||||
handle_upload_ok (processor, task, content, clen);
|
||||
else
|
||||
handle_download_ok (processor, task, content, clen);
|
||||
} else if (strncmp (code, SC_PUT_TOKEN, 3) == 0) {
|
||||
/* In LAN sync, we don't use session token. */
|
||||
if (clen == 0) {
|
||||
ccnet_processor_done (processor, TRUE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (content[clen-1] != '\0') {
|
||||
g_warning ("Bad response content.\n");
|
||||
transfer_task_set_error (task, TASK_ERR_UNKNOWN);
|
||||
ccnet_processor_send_update (processor, SC_BAD_ARGS, SS_BAD_ARGS,
|
||||
NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return;
|
||||
}
|
||||
task->session_token = g_strdup (content);
|
||||
|
||||
ccnet_processor_send_update (processor, SC_GET_VERSION, SS_GET_VERSION,
|
||||
NULL, 0);
|
||||
} else if (strncmp (code, SC_VERSION, 3) == 0) {
|
||||
task->protocol_version = atoi(content);
|
||||
ccnet_processor_done (processor, TRUE);
|
||||
} else {
|
||||
g_warning ("[check tx v3] Bad response: %s %s", code, code_msg);
|
||||
if (strncmp(code, SC_ACCESS_DENIED, 3) == 0)
|
||||
transfer_task_set_error (task, TASK_ERR_ACCESS_DENIED);
|
||||
else if (strncmp(code, SC_QUOTA_ERROR, 3) == 0)
|
||||
transfer_task_set_error (task, TASK_ERR_CHECK_QUOTA);
|
||||
else if (strncmp(code, SC_QUOTA_FULL, 3) == 0)
|
||||
transfer_task_set_error (task, TASK_ERR_QUOTA_FULL);
|
||||
else if (strncmp(code, SC_PROTOCOL_MISMATCH, 3) == 0)
|
||||
transfer_task_set_error (task, TASK_ERR_PROTOCOL_VERSION);
|
||||
else if (strncmp(code, SC_BAD_REPO, 3) == 0)
|
||||
transfer_task_set_error (task, TASK_ERR_BAD_REPO_ID);
|
||||
else
|
||||
transfer_task_set_error (task, TASK_ERR_UNKNOWN);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
}
|
||||
}
|
34
daemon/processors/check-tx-v3-proc.h
Normal file
34
daemon/processors/check-tx-v3-proc.h
Normal file
@ -0,0 +1,34 @@
|
||||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef SEAFILE_CHECK_TX_V3_PROC_H
|
||||
#define SEAFILE_CHECK_TX_V3_PROC_H
|
||||
|
||||
#include <glib-object.h>
|
||||
#include <ccnet/processor.h>
|
||||
|
||||
#include "transfer-mgr.h"
|
||||
|
||||
#define SEAFILE_TYPE_CHECK_TX_V3_PROC (seafile_check_tx_v3_proc_get_type ())
|
||||
#define SEAFILE_CHECK_TX_V3_PROC(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEAFILE_TYPE_CHECK_TX_V3_PROC, SeafileCheckTxV3Proc))
|
||||
#define SEAFILE_IS_CHECK_TX_V3_PROC(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SEAFILE_TYPE_CHECK_TX_PROC))
|
||||
#define SEAFILE_CHECK_TX_V3_PROC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SEAFILE_TYPE_CHECK_TX_V3_PROC, SeafileCheckTxV3ProcClass))
|
||||
#define IS_SEAFILE_CHECK_TX_V3_PROC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SEAFILE_TYPE_CHECK_TX_V3_PROC))
|
||||
#define SEAFILE_CHECK_TX_V3_PROC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SEAFILE_TYPE_CHECK_TX_V3_PROC, SeafileCheckTxV3ProcClass))
|
||||
|
||||
typedef struct _SeafileCheckTxV3Proc SeafileCheckTxV3Proc;
|
||||
typedef struct _SeafileCheckTxV3ProcClass SeafileCheckTxV3ProcClass;
|
||||
|
||||
struct _SeafileCheckTxV3Proc {
|
||||
CcnetProcessor parent_instance;
|
||||
|
||||
int type;
|
||||
TransferTask *task;
|
||||
};
|
||||
|
||||
struct _SeafileCheckTxV3ProcClass {
|
||||
CcnetProcessorClass parent_class;
|
||||
};
|
||||
|
||||
GType seafile_check_tx_v3_proc_get_type ();
|
||||
|
||||
#endif
|
@ -29,6 +29,7 @@
|
||||
#include "mq-mgr.h"
|
||||
|
||||
#include "processors/check-tx-v2-proc.h"
|
||||
#include "processors/check-tx-v3-proc.h"
|
||||
#include "processors/getcommit-proc.h"
|
||||
#include "processors/sendcommit-proc.h"
|
||||
#include "processors/sendfs-proc.h"
|
||||
@ -547,6 +548,10 @@ static void register_processors (CcnetClient *client)
|
||||
"seafile-check-tx-v2",
|
||||
SEAFILE_TYPE_CHECK_TX_V2_PROC);
|
||||
|
||||
ccnet_proc_factory_register_processor (client->proc_factory,
|
||||
"seafile-check-tx-v3",
|
||||
SEAFILE_TYPE_CHECK_TX_V3_PROC);
|
||||
|
||||
ccnet_proc_factory_register_processor (client->proc_factory,
|
||||
"seafile-getcommit",
|
||||
SEAFILE_TYPE_GETCOMMIT_PROC);
|
||||
@ -1251,6 +1256,23 @@ check_download_cb (CcnetProcessor *processor, gboolean success, void *data)
|
||||
save_clone_head (task, task->head);
|
||||
|
||||
start_commit_download (task);
|
||||
} else if (processor->failure == PROC_NO_SERVICE) {
|
||||
/* Talking to an old server. */
|
||||
CcnetProcessor *v2_proc;
|
||||
|
||||
v2_proc = ccnet_proc_factory_create_remote_master_processor (
|
||||
seaf->session->proc_factory, "seafile-check-tx-v2", task->dest_id);
|
||||
if (!v2_proc) {
|
||||
seaf_warning ("failed to create check-tx-v2 proc for download.\n");
|
||||
transition_state_to_error (task, TASK_ERR_CHECK_DOWNLOAD_START);
|
||||
}
|
||||
|
||||
g_signal_connect (v2_proc, "done", (GCallback)check_download_cb, task);
|
||||
|
||||
((SeafileCheckTxV2Proc *)v2_proc)->task = task;
|
||||
if (ccnet_processor_startl (v2_proc, "download", NULL) < 0)
|
||||
seaf_warning ("failed to start check-tx-v2 proc for download.\n");
|
||||
|
||||
} else if (task->state != TASK_STATE_ERROR
|
||||
&& task->runtime_state == TASK_RT_STATE_CHECK) {
|
||||
transfer_task_with_proc_failure (
|
||||
@ -1276,7 +1298,7 @@ start_download (TransferTask *task)
|
||||
return -1;
|
||||
|
||||
processor = ccnet_proc_factory_create_remote_master_processor (
|
||||
seaf->session->proc_factory, "seafile-check-tx-v2", dest_id);
|
||||
seaf->session->proc_factory, "seafile-check-tx-v3", dest_id);
|
||||
if (!processor) {
|
||||
seaf_warning ("failed to create check-tx proc for download.\n");
|
||||
transition_state_to_error (task, TASK_ERR_CHECK_DOWNLOAD_START);
|
||||
@ -1285,7 +1307,7 @@ start_download (TransferTask *task)
|
||||
|
||||
g_signal_connect (processor, "done", (GCallback)check_download_cb, task);
|
||||
|
||||
((SeafileCheckTxV2Proc *)processor)->task = task;
|
||||
((SeafileCheckTxV3Proc *)processor)->task = task;
|
||||
if (ccnet_processor_startl (processor, "download", NULL) < 0) {
|
||||
seaf_warning ("failed to start check-tx proc for download.\n");
|
||||
return -1;
|
||||
@ -1601,6 +1623,23 @@ check_upload_cb (CcnetProcessor *processor, gboolean success, void *data)
|
||||
|
||||
if (success) {
|
||||
start_commit_upload (task);
|
||||
} else if (processor->failure == PROC_NO_SERVICE) {
|
||||
/* Talking to an old server. */
|
||||
CcnetProcessor *v2_proc;
|
||||
|
||||
v2_proc = ccnet_proc_factory_create_remote_master_processor (
|
||||
seaf->session->proc_factory, "seafile-check-tx-v2", task->dest_id);
|
||||
if (!v2_proc) {
|
||||
seaf_warning ("failed to create check-tx-v2 proc for upload.\n");
|
||||
transition_state_to_error (task, TASK_ERR_CHECK_UPLOAD_START);
|
||||
}
|
||||
|
||||
g_signal_connect (v2_proc, "done", (GCallback)check_upload_cb, task);
|
||||
|
||||
((SeafileCheckTxV2Proc *)v2_proc)->task = task;
|
||||
if (ccnet_processor_startl (v2_proc, "upload", NULL) < 0)
|
||||
seaf_warning ("failed to start check-tx-v2 proc for upload.\n");
|
||||
|
||||
} else if (task->state != TASK_STATE_ERROR
|
||||
&& task->runtime_state == TASK_RT_STATE_CHECK) {
|
||||
transfer_task_with_proc_failure (
|
||||
@ -1637,19 +1676,19 @@ start_upload (TransferTask *task)
|
||||
seaf_branch_unref (branch);
|
||||
|
||||
processor = ccnet_proc_factory_create_remote_master_processor (
|
||||
seaf->session->proc_factory, "seafile-check-tx-v2", dest_id);
|
||||
seaf->session->proc_factory, "seafile-check-tx-v3", dest_id);
|
||||
if (!processor) {
|
||||
seaf_warning ("failed to create check-tx proc for upload.\n");
|
||||
seaf_warning ("failed to create check-tx-v3 proc for upload.\n");
|
||||
transition_state_to_error (task, TASK_ERR_CHECK_UPLOAD_START);
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_signal_connect (processor, "done", (GCallback)check_upload_cb, task);
|
||||
|
||||
((SeafileCheckTxV2Proc *)processor)->task = task;
|
||||
((SeafileCheckTxV3Proc *)processor)->task = task;
|
||||
if (ccnet_processor_startl (processor, "upload", NULL) < 0)
|
||||
{
|
||||
seaf_warning ("failed to start check-tx proc for upload.\n");
|
||||
seaf_warning ("failed to start check-tx-v3 proc for upload.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,7 @@ proc_headers = $(addprefix processors/, \
|
||||
sync-repo-slave-proc.h \
|
||||
check-tx-slave-proc.h \
|
||||
check-tx-slave-v2-proc.h \
|
||||
check-tx-slave-v3-proc.h \
|
||||
putcommit-proc.h \
|
||||
putfs-proc.h \
|
||||
putcommit-v2-proc.h \
|
||||
@ -91,6 +92,7 @@ seaf_server_SOURCES = \
|
||||
processors/sync-repo-slave-proc.c \
|
||||
processors/check-tx-slave-proc.c \
|
||||
processors/check-tx-slave-v2-proc.c \
|
||||
processors/check-tx-slave-v3-proc.c \
|
||||
processors/putcommit-proc.c \
|
||||
processors/putfs-proc.c \
|
||||
../common/processors/putblock-proc.c \
|
||||
|
391
server/processors/check-tx-slave-v3-proc.c
Normal file
391
server/processors/check-tx-slave-v3-proc.c
Normal file
@ -0,0 +1,391 @@
|
||||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#include <string.h>
|
||||
#include <ccnet.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <ccnet.h>
|
||||
#include <ccnet/job-mgr.h>
|
||||
#include <ccnet/ccnet-object.h>
|
||||
|
||||
#include <openssl/aes.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define DEBUG_FLAG SEAFILE_DEBUG_TRANSFER
|
||||
#include "log.h"
|
||||
|
||||
#include "seafile-session.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "check-tx-slave-v3-proc.h"
|
||||
|
||||
#define SC_GET_TOKEN "301"
|
||||
#define SS_GET_TOKEN "Get token"
|
||||
#define SC_PUT_TOKEN "302"
|
||||
#define SS_PUT_TOKEN "Put token"
|
||||
#define SC_GET_VERSION "303"
|
||||
#define SS_GET_VERSION "Get version"
|
||||
#define SC_VERSION "304"
|
||||
#define SS_VERSION "Version"
|
||||
|
||||
#define SC_ACCESS_DENIED "401"
|
||||
#define SS_ACCESS_DENIED "Access denied"
|
||||
#define SC_SERVER_ERROR "404"
|
||||
#define SS_SERVER_ERROR "Internal server error"
|
||||
#define SC_PROTOCOL_MISMATCH "405"
|
||||
#define SS_PROTOCOL_MISMATCH "Protocol version mismatch"
|
||||
|
||||
/* Only for upload */
|
||||
#define SC_QUOTA_ERROR "402"
|
||||
#define SS_QUOTA_ERROR "Failed to get quota"
|
||||
#define SC_QUOTA_FULL "403"
|
||||
#define SS_QUOTA_FULL "storage for the repo's owner is full"
|
||||
|
||||
/* Only for download */
|
||||
#define SC_BAD_REPO "406"
|
||||
#define SS_BAD_REPO "Repo doesn't exist"
|
||||
#define SC_NO_BRANCH "407"
|
||||
#define SS_NO_BRANCH "Branch not found"
|
||||
|
||||
enum {
|
||||
INIT,
|
||||
ACCESS_GRANTED,
|
||||
};
|
||||
|
||||
enum {
|
||||
CHECK_TX_TYPE_UPLOAD,
|
||||
CHECK_TX_TYPE_DOWNLOAD,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
int type;
|
||||
|
||||
char repo_id[37];
|
||||
char *branch_name;
|
||||
char *token;
|
||||
char *session_key;
|
||||
|
||||
char *rsp_code;
|
||||
char *rsp_msg;
|
||||
char head_id[41];
|
||||
int has_branch;
|
||||
} SeafileCheckTxSlaveV3ProcPriv;
|
||||
|
||||
#define GET_PRIV(o) \
|
||||
(G_TYPE_INSTANCE_GET_PRIVATE ((o), SEAFILE_TYPE_CHECK_TX_SLAVE_V3_PROC, SeafileCheckTxSlaveV3ProcPriv))
|
||||
|
||||
#define USE_PRIV \
|
||||
SeafileCheckTxSlaveV3ProcPriv *priv = GET_PRIV(processor);
|
||||
|
||||
G_DEFINE_TYPE (SeafileCheckTxSlaveV3Proc, seafile_check_tx_slave_v3_proc, CCNET_TYPE_PROCESSOR)
|
||||
|
||||
static int start (CcnetProcessor *processor, int argc, char **argv);
|
||||
static void handle_update (CcnetProcessor *processor,
|
||||
char *code, char *code_msg,
|
||||
char *content, int clen);
|
||||
static void thread_done (void *result);
|
||||
|
||||
static void
|
||||
release_resource(CcnetProcessor *processor)
|
||||
{
|
||||
USE_PRIV;
|
||||
|
||||
/* g_free works fine even if ptr is NULL. */
|
||||
g_free (priv->token);
|
||||
g_free (priv->session_key);
|
||||
g_free (priv->branch_name);
|
||||
g_free (priv->rsp_code);
|
||||
g_free (priv->rsp_msg);
|
||||
|
||||
CCNET_PROCESSOR_CLASS (seafile_check_tx_slave_v3_proc_parent_class)->release_resource (processor);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
seafile_check_tx_slave_v3_proc_class_init (SeafileCheckTxSlaveV3ProcClass *klass)
|
||||
{
|
||||
CcnetProcessorClass *proc_class = CCNET_PROCESSOR_CLASS (klass);
|
||||
|
||||
proc_class->name = "check-tx-slave-v3-proc";
|
||||
proc_class->start = start;
|
||||
proc_class->handle_update = handle_update;
|
||||
proc_class->release_resource = release_resource;
|
||||
|
||||
g_type_class_add_private (klass, sizeof (SeafileCheckTxSlaveV3ProcPriv));
|
||||
}
|
||||
|
||||
static void
|
||||
seafile_check_tx_slave_v3_proc_init (SeafileCheckTxSlaveV3Proc *processor)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
get_branch_head (CcnetProcessor *processor)
|
||||
{
|
||||
SeafBranch *branch;
|
||||
USE_PRIV;
|
||||
|
||||
branch = seaf_branch_manager_get_branch (seaf->branch_mgr,
|
||||
priv->repo_id, priv->branch_name);
|
||||
if (branch != NULL) {
|
||||
priv->has_branch = 1;
|
||||
memcpy (priv->head_id, branch->commit_id, 41);
|
||||
seaf_branch_unref (branch);
|
||||
|
||||
priv->rsp_code = g_strdup(SC_OK);
|
||||
priv->rsp_msg = g_strdup(SS_OK);
|
||||
} else if (priv->type == CHECK_TX_TYPE_UPLOAD) {
|
||||
priv->rsp_code = g_strdup(SC_OK);
|
||||
priv->rsp_msg = g_strdup(SS_OK);
|
||||
} else {
|
||||
priv->rsp_code = g_strdup(SC_NO_BRANCH);
|
||||
priv->rsp_msg = g_strdup(SS_NO_BRANCH);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
decrypt_token (CcnetProcessor *processor)
|
||||
{
|
||||
USE_PRIV;
|
||||
int hex_len, encrypted_len, token_len;
|
||||
char *encrypted_token = NULL;
|
||||
SeafileCrypt *crypt = NULL;
|
||||
unsigned char key[16], iv[16];
|
||||
char *token = NULL;
|
||||
int ret = 0;
|
||||
|
||||
/* raw data is half the length of hexidecimal */
|
||||
hex_len = strlen(priv->token);
|
||||
if (hex_len % 2 != 0) {
|
||||
seaf_warning ("[check tx slave v3] invalid length of encrypted token\n");
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
encrypted_len = hex_len / 2;
|
||||
encrypted_token = g_malloc (encrypted_len);
|
||||
hex_to_rawdata (priv->token,
|
||||
(unsigned char *)encrypted_token,
|
||||
encrypted_len);
|
||||
|
||||
EVP_BytesToKey (EVP_aes_128_cbc(), /* cipher mode */
|
||||
EVP_sha1(), /* message digest */
|
||||
NULL, /* slat */
|
||||
(unsigned char*)priv->session_key,
|
||||
strlen(priv->session_key),
|
||||
1, /* iteration times */
|
||||
key, /* the derived key */
|
||||
iv); /* IV, initial vector */
|
||||
|
||||
crypt = seafile_crypt_new (CURRENT_ENC_VERSION, key, iv);
|
||||
|
||||
if (seafile_decrypt (&token, &token_len, encrypted_token,
|
||||
encrypted_len, crypt) < 0) {
|
||||
seaf_warning ("[check tx slave v3] failed to decrypt token\n");
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
g_free (priv->token);
|
||||
/* we can use the decrypted data directly, since the trailing null byte is
|
||||
* also included when encrypting in the client */
|
||||
priv->token = token;
|
||||
|
||||
out:
|
||||
g_free (crypt);
|
||||
g_free (encrypted_token);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void *
|
||||
check_tx (void *vprocessor)
|
||||
{
|
||||
CcnetProcessor *processor = vprocessor;
|
||||
USE_PRIV;
|
||||
|
||||
char *user = NULL;
|
||||
char *repo_id = priv->repo_id;
|
||||
|
||||
if (!seaf_repo_manager_repo_exists (seaf->repo_mgr, repo_id)) {
|
||||
priv->rsp_code = g_strdup(SC_BAD_REPO);
|
||||
priv->rsp_msg = g_strdup(SS_BAD_REPO);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (decrypt_token (processor) < 0) {
|
||||
priv->rsp_code = g_strdup(SC_ACCESS_DENIED);
|
||||
priv->rsp_msg = g_strdup(SS_ACCESS_DENIED);
|
||||
goto out;
|
||||
}
|
||||
|
||||
user = seaf_repo_manager_get_email_by_token (
|
||||
seaf->repo_mgr, repo_id, priv->token);
|
||||
|
||||
if (!user) {
|
||||
priv->rsp_code = g_strdup(SC_ACCESS_DENIED);
|
||||
priv->rsp_msg = g_strdup(SS_ACCESS_DENIED);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (priv->type == CHECK_TX_TYPE_UPLOAD &&
|
||||
seaf_quota_manager_check_quota (seaf->quota_mgr, repo_id) < 0) {
|
||||
priv->rsp_code = g_strdup(SC_QUOTA_FULL);
|
||||
priv->rsp_msg = g_strdup(SS_QUOTA_FULL);
|
||||
goto out;
|
||||
}
|
||||
|
||||
char *perm = seaf_repo_manager_check_permission (seaf->repo_mgr,
|
||||
repo_id, user, NULL);
|
||||
if (!perm ||
|
||||
(strcmp (perm, "r") == 0 && priv->type == CHECK_TX_TYPE_UPLOAD))
|
||||
{
|
||||
priv->rsp_code = g_strdup(SC_ACCESS_DENIED);
|
||||
priv->rsp_msg = g_strdup(SS_ACCESS_DENIED);
|
||||
g_free (perm);
|
||||
goto out;
|
||||
}
|
||||
g_free (perm);
|
||||
|
||||
get_branch_head (processor);
|
||||
|
||||
out:
|
||||
g_free (user);
|
||||
return vprocessor;
|
||||
}
|
||||
|
||||
static void
|
||||
thread_done (void *result)
|
||||
{
|
||||
CcnetProcessor *processor = result;
|
||||
USE_PRIV;
|
||||
|
||||
if (processor->delay_shutdown) {
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp (priv->rsp_code, SC_OK) == 0) {
|
||||
if (priv->has_branch) {
|
||||
ccnet_processor_send_response (processor,
|
||||
SC_OK, SS_OK,
|
||||
priv->head_id, 41);
|
||||
} else
|
||||
ccnet_processor_send_response (processor, SC_OK, SS_OK, NULL, 0);
|
||||
processor->state = ACCESS_GRANTED;
|
||||
} else {
|
||||
ccnet_processor_send_response (processor,
|
||||
priv->rsp_code, priv->rsp_msg,
|
||||
NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
start (CcnetProcessor *processor, int argc, char **argv)
|
||||
{
|
||||
char *repo_id, *branch_name, *token;
|
||||
USE_PRIV;
|
||||
|
||||
if (argc != 5) {
|
||||
ccnet_processor_send_response (processor, SC_BAD_ARGS, SS_BAD_ARGS, NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strcmp (argv[0], "upload") == 0) {
|
||||
priv->type = CHECK_TX_TYPE_UPLOAD;
|
||||
} else if (strcmp (argv[0], "download") == 0) {
|
||||
priv->type = CHECK_TX_TYPE_DOWNLOAD;
|
||||
} else {
|
||||
ccnet_processor_send_response (processor, SC_BAD_ARGS, SS_BAD_ARGS, NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
repo_id = argv[2];
|
||||
branch_name = argv[3];
|
||||
token = argv[4];
|
||||
|
||||
if (strlen(repo_id) != 36) {
|
||||
ccnet_processor_send_response (processor, SC_BAD_ARGS, SS_BAD_ARGS, NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (priv->type == CHECK_TX_TYPE_UPLOAD &&
|
||||
strcmp (branch_name, "master") != 0) {
|
||||
ccnet_processor_send_response (processor, SC_BAD_ARGS, SS_BAD_ARGS, NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy (priv->repo_id, repo_id, 37);
|
||||
priv->branch_name = g_strdup(branch_name);
|
||||
|
||||
priv->token = g_strdup(token);
|
||||
|
||||
CcnetPeer *peer = ccnet_get_peer (seaf->ccnetrpc_client, processor->peer_id);
|
||||
if (!peer || !peer->session_key) {
|
||||
seaf_warning ("[check tx slave v3] session key of peer %.10s is null\n",
|
||||
processor->peer_id);
|
||||
ccnet_processor_send_response (processor, SC_BAD_PEER, SS_BAD_PEER, NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
if (peer)
|
||||
g_object_unref (peer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
priv->session_key = g_strdup(peer->session_key);
|
||||
g_object_unref (peer);
|
||||
|
||||
seaf_debug ("[check-tx] %s repo %.8s.\n", argv[0], repo_id);
|
||||
|
||||
ccnet_processor_thread_create (processor, check_tx, thread_done, processor);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
handle_update (CcnetProcessor *processor,
|
||||
char *code, char *code_msg,
|
||||
char *content, int clen)
|
||||
{
|
||||
USE_PRIV;
|
||||
char *token;
|
||||
|
||||
if (processor->state != ACCESS_GRANTED) {
|
||||
ccnet_processor_send_response (processor,
|
||||
SC_BAD_UPDATE_CODE, SS_BAD_UPDATE_CODE,
|
||||
NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strncmp (code, SC_GET_TOKEN, 3) == 0) {
|
||||
token = seaf_token_manager_generate_token (seaf->token_mgr,
|
||||
processor->peer_id,
|
||||
priv->repo_id);
|
||||
ccnet_processor_send_response (processor,
|
||||
SC_PUT_TOKEN, SS_PUT_TOKEN,
|
||||
token, strlen(token) + 1);
|
||||
g_free (token);
|
||||
return;
|
||||
} else if (strncmp (code, SC_GET_VERSION, 3) == 0) {
|
||||
char buf[16];
|
||||
int len;
|
||||
len = snprintf (buf, sizeof(buf), "%d", CURRENT_PROTO_VERSION);
|
||||
ccnet_processor_send_response (processor,
|
||||
SC_VERSION, SS_VERSION,
|
||||
buf, len + 1);
|
||||
ccnet_processor_done (processor, TRUE);
|
||||
return;
|
||||
}
|
||||
|
||||
ccnet_processor_send_response (processor,
|
||||
SC_BAD_UPDATE_CODE, SS_BAD_UPDATE_CODE,
|
||||
NULL, 0);
|
||||
ccnet_processor_done (processor, FALSE);
|
||||
}
|
29
server/processors/check-tx-slave-v3-proc.h
Normal file
29
server/processors/check-tx-slave-v3-proc.h
Normal file
@ -0,0 +1,29 @@
|
||||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
|
||||
#ifndef SEAFILE_CHECK_TX_SLAVE_V3_PROC_H
|
||||
#define SEAFILE_CHECK_TX_SLAVE_V3_PROC_H
|
||||
|
||||
#include <glib-object.h>
|
||||
#include <ccnet/processor.h>
|
||||
|
||||
#define SEAFILE_TYPE_CHECK_TX_SLAVE_V3_PROC (seafile_check_tx_slave_v3_proc_get_type ())
|
||||
#define SEAFILE_CHECK_TX_SLAVE_V3_PROC(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEAFILE_TYPE_CHECK_TX_SLAVE_V3_PROC, SeafileCheckTxSlaveV3Proc))
|
||||
#define SEAFILE_IS_CHECK_TX_SLAVE_V3_PROC(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SEAFILE_TYPE_CHECK_TX_SLAVE_V3_PROC))
|
||||
#define SEAFILE_CHECK_TX_SLAVE_V3_PROC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SEAFILE_TYPE_CHECK_TX_SLAVE_V3_PROC, SeafileCheckTxSlaveV3ProcClass))
|
||||
#define IS_SEAFILE_CHECK_TX_SLAVE_V3_PROC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SEAFILE_TYPE_CHECK_TX_SLAVE_V3_PROC))
|
||||
#define SEAFILE_CHECK_TX_SLAVE_V3_PROC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SEAFILE_TYPE_CHECK_TX_SLAVE_V3_PROC, SeafileCheckTxSlaveV3ProcClass))
|
||||
|
||||
typedef struct _SeafileCheckTxSlaveV3Proc SeafileCheckTxSlaveV3Proc;
|
||||
typedef struct _SeafileCheckTxSlaveV3ProcClass SeafileCheckTxSlaveV3ProcClass;
|
||||
|
||||
struct _SeafileCheckTxSlaveV3Proc {
|
||||
CcnetProcessor parent_instance;
|
||||
};
|
||||
|
||||
struct _SeafileCheckTxSlaveV3ProcClass {
|
||||
CcnetProcessorClass parent_class;
|
||||
};
|
||||
|
||||
GType seafile_check_tx_slave_v3_proc_get_type ();
|
||||
|
||||
#endif
|
@ -23,22 +23,16 @@
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "processors/check-tx-slave-proc.h"
|
||||
#include "processors/check-tx-slave-v2-proc.h"
|
||||
#include "processors/putcommit-proc.h"
|
||||
#include "processors/recvcommit-proc.h"
|
||||
#include "processors/check-tx-slave-v3-proc.h"
|
||||
#include "processors/recvfs-proc.h"
|
||||
#include "processors/putfs-proc.h"
|
||||
#include "processors/putblock-proc.h"
|
||||
#include "processors/putblock-v2-proc.h"
|
||||
#include "processors/recvblock-proc.h"
|
||||
#include "processors/recvblock-v2-proc.h"
|
||||
#include "processors/recvbranch-proc.h"
|
||||
#include "processors/sync-repo-slave-proc.h"
|
||||
#include "processors/putcommit-v2-proc.h"
|
||||
#include "processors/recvcommit-v2-proc.h"
|
||||
#include "processors/recvcommit-v3-proc.h"
|
||||
#include "processors/putrepoemailtoken-proc.h"
|
||||
|
||||
SeafileSession *seaf;
|
||||
SearpcClient *ccnetrpc_client;
|
||||
@ -86,10 +80,14 @@ static void register_processors (CcnetClient *client)
|
||||
SEAFILE_TYPE_PUTBLOCK_PROC, NULL);
|
||||
ccnet_register_service (client, "seafile-recvblock", "basic",
|
||||
SEAFILE_TYPE_RECVBLOCK_PROC, NULL);
|
||||
ccnet_register_service (client, "seafile-put-repo-email-token", "basic",
|
||||
SEAFILE_TYPE_PUTREPOEMAILTOKEN_PROC, NULL);
|
||||
#endif
|
||||
|
||||
ccnet_register_service (client, "seafile-check-tx-slave-v2", "basic",
|
||||
SEAFILE_TYPE_CHECK_TX_SLAVE_V2_PROC, NULL);
|
||||
ccnet_register_service (client, "seafile-check-tx-slave-v3", "basic",
|
||||
SEAFILE_TYPE_CHECK_TX_SLAVE_V3_PROC, NULL);
|
||||
ccnet_register_service (client, "seafile-putblock-v2", "basic",
|
||||
SEAFILE_TYPE_PUTBLOCK_V2_PROC, NULL);
|
||||
ccnet_register_service (client, "seafile-recvblock-v2", "basic",
|
||||
@ -106,8 +104,6 @@ static void register_processors (CcnetClient *client)
|
||||
SEAFILE_TYPE_PUTCOMMIT_V2_PROC, NULL);
|
||||
ccnet_register_service (client, "seafile-recvcommit-v3", "basic",
|
||||
SEAFILE_TYPE_RECVCOMMIT_V3_PROC, NULL);
|
||||
ccnet_register_service (client, "seafile-put-repo-email-token", "basic",
|
||||
SEAFILE_TYPE_PUTREPOEMAILTOKEN_PROC, NULL);
|
||||
}
|
||||
|
||||
#include <searpc.h>
|
||||
|
Loading…
Reference in New Issue
Block a user