dav/main.c

Mon, 12 Aug 2013 15:58:30 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 12 Aug 2013 15:58:30 +0200
changeset 8
4503498deb22
parent 5
88625853ae74
child 11
5db6178d8b58
permissions
-rw-r--r--

creates a .dav dir if it doesn't exist

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2012 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <libxml/xmlerror.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ucx/string.h>

#include "webdav.h"
#include "utils.h"
#include "config.h"
#include "crypto.h"
#include "main.h"

void xmlerrorfnc(void * ctx, const char * msg, ... ) {
    // nothing
}

int main(int argc, char **argv) {
    xmlGenericErrorFunc fnc = xmlerrorfnc;
    initGenericErrorDefaultFunc(&fnc);
    load_config();
    
    if(argc < 2) {
        print_usage();
        return -1;
    }
    
    if(!strcmp(argv[1], "list") || !strcmp(argv[1], "ls")) {
        return cmd_list(argc - 2, argv + 2);
    } else if(!strcmp(argv[1], "get")) {
        return cmd_get(argc - 2, argv + 2);
    } else if(!strcmp(argv[1], "put")) {
        return cmd_put(argc - 2, argv + 2);
    }
    
    print_usage();
    return -1;
}

void print_usage() {
    
}

void url_get_parts(char *url, char **root, char **path) {
    size_t ulen = strlen(url);
    *root = NULL;
    *path = NULL;
    
    int s;
    if(ulen > 7 && !strncmp(url, "http://", 7)) {
        s = 7;
    } else if(ulen > 8 && !strncmp(url, "https://", 8)) {
        s = 8;
    } else {
        s = 1;
    }
    
    for(int i=s;i<ulen;i++) {
        char c = url[i];
        if(c == '/') {
            sstr_t r = sstrn(url, i);
            sstr_t p = sstrsubs(sstr(url), i);
            if(p.length == 0) {
                p = sstrn("/", 1);
            }
            *root = sstrdup(r).ptr;
            *path = sstrdup(p).ptr;
            return;
        }
    }
    
    *root = strdup(url);
    *path = strdup("/");
}


int cmd_list(int argc, char **argv) {
    if(argc == 0) {
        return -1;
    }
    
    
    DavContext *ctx = dav_context_new();
    DavSession *sn = NULL;
    char *url = argv[0];
    char *root = NULL;
    char *path = NULL;
    url_get_parts(url, &root, &path);
    
    Repository *repo = get_repository(root);
    if(repo) {
        sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
    } else {
        sn = dav_session_new(ctx, root);
    }
    
    //printf("baseurl: %s\n", sn->base_url);
    
    DavResource *ls = dav_get(sn, path, NULL);
    if(!ls) {
        fprintf(stderr, "error\n");
        return -1;
    }
    DavResource *child = ls->children;
    while(child) {
        printf("%s\n", child->name);
        child = child->next;
    }
    
    return 0;
}

int cmd_get(int argc, char **argv) {
    if(argc == 0) {
        return -1;
    }
    
    DavContext *ctx = dav_context_new();
    dav_add_namespace(ctx, "U", "http://www.uap-core.de/");
    DavSession *sn = NULL;
    char *url = argv[0];
    char *root = NULL;
    char *path = NULL;
    url_get_parts(url, &root, &path);
    
    Repository *repo = get_repository(root);
    if(repo) {
        sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
    } else {
        sn = dav_session_new(ctx, root);
    }
    
    DavResource *res = dav_get(sn, path, "U:crypto-key");
    if(!res) {
        fprintf(stderr, "error\n");
        return -1;
    }
    FILE *out = fopen(res->name, "w");
    if(!out) {
        fprintf(stderr, "cannot open output file\n");
        return -1;
    }
    
    AESDecrypter *dec = NULL;
    char *keyprop = dav_get_property_ns(res, "http://www.uap-core.de/", "crypto-key");
    if(repo) {
        Key *key = get_key(keyprop);
        if(repo->encrypt && key) {
            dec = aes_decrypter_new(key, out, (dav_write_func)fwrite);
        }
    }
    
    int ret;
    if(dec && keyprop) {
        ret = dav_get_content(res, dec, (dav_write_func)aes_write);
    } else {
        ret = dav_get_content(res, out, (dav_write_func)fwrite);
    }
    if(dec) {
        aes_decrypter_close(dec);
    }
    fclose(out);
    if(ret) {
        unlink(res->name);
    }
    
    return 0;
}

int cmd_put(int argc, char **argv) {
    if(argc < 2) {
        return -1;
    }
    
    DavContext *ctx = dav_context_new();
    DavSession *sn = NULL;
    char *url = argv[0];
    char *file = argv[1];
    char *root = NULL;
    char *path = NULL;
    url_get_parts(url, &root, &path);
    
    Repository *repo = get_repository(root);
    if(repo) {
        sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
    } else {
        sn = dav_session_new(ctx, root);
    }
    
    DavResource *res = dav_resource_new(sn, path);
    if(!res) {
        fprintf(stderr, "error\n");
        return -1;
    }
    FILE *in = fopen(file, "r");
    if(!in) {
        fprintf(stderr, "cannot open input file\n");
        return -1;
    }
    AESEncrypter *enc = NULL;
    if(repo) {
        Key *key = get_key(repo->default_key);
        if(repo->encrypt && key) {
            enc = aes_encrypter_new(key, in, (dav_read_func)fread);
        }
    }
    if(enc) {
        dav_set_content(res, enc, (dav_read_func)aes_read);
        dav_set_property_ns(res, "http://www.uap-core.de/", "crypto-key", repo->default_key);
    } else {
        dav_set_content(res, in, (dav_read_func)fread);
    }
    
    if(dav_store(res)) {
        fprintf(stderr, "cannot upload file\n");
        fclose(in);
        return -1;
    }
    fclose(in);
    return 0;
}

mercurial