# HG changeset patch # User Mike Becker # Date 1585407886 -3600 # Node ID 8d40b5ccc43e4aa462068559f44c50d8e674959a # Parent 7384da29c2b408f829197cf6d7ad5ba80a0d3e84 adds check for encryption key (otherwise segfault when trying to create a fresh encrypted file without a key) diff -r 7384da29c2b4 -r 8d40b5ccc43e dav/main.c --- a/dav/main.c Sat Mar 28 15:58:30 2020 +0100 +++ b/dav/main.c Sat Mar 28 16:04:46 2020 +0100 @@ -1124,6 +1124,46 @@ return ret == 0 ? CURL_SEEKFUNC_OK : CURL_SEEKFUNC_CANTSEEK; } +static int check_encryption_key(CmdArgs *a, DavSession *sn) { + // override the session key if the -k option is specified + char *keyname = cmd_getoption(a, "key"); + if(keyname) { + DavKey *key = dav_context_get_key(ctx, keyname); + if(key) { + sn->key = key; + } else { + fprintf(stderr, "Key %s not found!\nAbort.\n", keyname); + return 1; + } + + /* + * If a key is explicitly specified, we can safely assume that the user + * wants to encrypt. For security reasons we report an error, if no + * encryption is enabled. + */ + if(!DAV_IS_ENCRYPTED(sn)) { + fprintf(stderr, "A key has been explicitly specified, but no " + "encryption is requested.\n" + "You have the following options:\n" + " - pass '-c' as command line argument to request encryption\n" + " - activate encryption in the config.xml\n" + " - don't use '-k ' " + "(warning: encryption will NOT happen)\n"); + return 1; + } + } + + // if encryption is requested, but we still don't know the key, report error + if(DAV_IS_ENCRYPTED(sn) && !(sn->key)) { + fprintf(stderr, "Encryption has been requested, " + "but no default key is configured.\n" + "You may specify a custom key with the '-k' option.\n"); + return 1; + } + + return 0; +} + int cmd_edit(CmdArgs *a) { if(a->argc != 1) { fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many"); @@ -1139,6 +1179,11 @@ if(set_session_config(sn, a)) { return -1; } + // TODO: implement locking feature + + if(check_encryption_key(a, sn)) { + return -1; + } char *version = cmd_getoption(a, "version"); DavResource *res; @@ -1356,46 +1401,6 @@ return tar_end_file(tar); } -static int check_encryption_key(CmdArgs *a, DavSession *sn) { - // override the session key if the -k option is specified - char *keyname = cmd_getoption(a, "key"); - if(keyname) { - DavKey *key = dav_context_get_key(ctx, keyname); - if(key) { - sn->key = key; - } else { - fprintf(stderr, "Key %s not found!\nAbort.\n", keyname); - return 1; - } - - /* - * If a key is explicitly specified, we can safely assume that the user - * wants to encrypt. For security reasons we report an error, if no - * encryption is enabled. - */ - if(!DAV_IS_ENCRYPTED(sn)) { - fprintf(stderr, "A key has been explicitly specified, but no " - "encryption is requested.\n" - "You have the following options:\n" - " - pass '-c' as command line argument to request encryption\n" - " - activate encryption in the config.xml\n" - " - don't use '-k ' " - "(warning: encryption will NOT happen)\n"); - return 1; - } - } - - // if encryption is requested, but we still don't know the key, report error - if(DAV_IS_ENCRYPTED(sn) && !(sn->key)) { - fprintf(stderr, "Encryption has been requested, " - "but no default key is configured.\n" - "You may specify a custom key with the '-k' option.\n"); - return 1; - } - - return 0; -} - int cmd_put(CmdArgs *a, DavBool import) { if(a->argc < 2) { fprintf(stderr, "Too few arguments\n"); @@ -1433,7 +1438,7 @@ if(check_encryption_key(a, sn)) { // TODO: free return -1; - } + } DavBool printfile = FALSE; DavBool ignoredirerr = FALSE;