From 2ab31511473bc181f8b731f4a2b7423fa2e7f0a0 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Mon, 4 Aug 2014 07:44:05 +0200 Subject: [PATCH] Baculize encrypt_bopen/close --- bacula/src/findlib/bfile.c | 123 ++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 71 deletions(-) diff --git a/bacula/src/findlib/bfile.c b/bacula/src/findlib/bfile.c index 9afdce33d3..09254edc7e 100644 --- a/bacula/src/findlib/bfile.c +++ b/bacula/src/findlib/bfile.c @@ -60,88 +60,69 @@ void pause_msg(const char *file, const char *func, int line, const char *msg) */ static int encrypt_bopen(BFILE *bfd, const char *fname, uint64_t flags, mode_t mode) { -#ifdef xxx - ULONG ulFlags=0; - char *win32_fname=NULL; - char *win32_fname_wchar=NULL; - - if(!(p_OpenEncryptedFileRawA || p_OpenEncryptedFileRawW)) - { - logp("no OpenEncryptedFileRaw pointers.\n"); - return -1; - } - if(p_OpenEncryptedFileRawW && p_MultiByteToWideChar) - { - if(!(win32_fname_wchar=make_win32_path_UTF8_2_wchar_w(fname))) - logp("could not get widename!"); - } - if(!(win32_fname=unix_name_to_win32((char *)fname))) - return -1; - - if((flags & O_CREAT) /* Create */ - || (flags & O_WRONLY)) /* Open existing for write */ - { - ulFlags |= CREATE_FOR_IMPORT; - ulFlags |= OVERWRITE_HIDDEN; - if(bfd->winattr & FILE_ATTRIBUTE_DIRECTORY) - { - mkdir(fname, 0777); - ulFlags |= CREATE_FOR_DIR; - } - } - else - { - /* Open existing for read */ - ulFlags=CREATE_FOR_EXPORT; + ULONG ulFlags = 0; + POOLMEM *win32_fname; + POOLMEM *win32_fname_wchar; + + bfd->mode = BF_CLOSED; + bfd->fid = -1; + + if (!(p_OpenEncryptedFileRawA || p_OpenEncryptedFileRawW)) { + Dmsg0(50, "No OpenEncryptedFileRawA and no OpenEncryptedFileRawW APIs!!!\n"); + return -1; } - if(p_OpenEncryptedFileRawW && p_MultiByteToWideChar) - { - int ret; - // unicode open - ret=p_OpenEncryptedFileRawW((LPCWSTR)win32_fname_wchar, - ulFlags, &(bfd->pvContext)); - if(ret) bfd->mode=BF_CLOSED; - else bfd->mode=BF_READ; - goto end; + /* Convert to Windows path format */ + win32_fname = get_pool_memory(PM_FNAME); + win32_fname_wchar = get_pool_memory(PM_FNAME); + + unix_name_to_win32(&win32_fname, (char *)fname); + + if (p_CreateFileW && p_MultiByteToWideChar) { + make_win32_path_UTF8_2_wchar(&win32_fname_wchar, fname); } - else - { - int ret; - // ascii open - ret=p_OpenEncryptedFileRawA(win32_fname, - ulFlags, &(bfd->pvContext)); - if(ret) bfd->mode=BF_CLOSED; - else bfd->mode=BF_READ; - goto end; + + if ((flags & O_CREAT) || (flags & O_WRONLY)) { + ulFlags = CREATE_FOR_IMPORT | OVERWRITE_HIDDEN; + if (bfd->fattrs & FILE_ATTRIBUTE_DIRECTORY) { + mkdir(fname, 0777); + ulFlags |= CREATE_FOR_DIR; + } + bfd->mode = BF_WRITE; + } else { + /* Open existing for read */ + ulFlags = CREATE_FOR_EXPORT; + bfd->mode = BF_READ; } -end: - if(win32_fname_wchar) free(win32_fname_wchar); - if(win32_fname) free(win32_fname); - return bfd->mode==BF_CLOSED; -#endif - return true; + if (p_OpenEncryptedFileRawW && p_MultiByteToWideChar) { + /* unicode open */ + if (p_OpenEncryptedFileRawW((LPCWSTR)win32_fname_wchar, + ulFlags, &(bfd->pvContext))) { + bfd->mode = BF_CLOSED; + } + } else { + /* ascii open */ + if (p_OpenEncryptedFileRawA(win32_fname, ulFlags, &(bfd->pvContext))) { + bfd->mode = BF_CLOSED; + } + } + free_pool_memory(win32_fname_wchar); + free_pool_memory(win32_fname); + bfd->fid = (bfd->mode == BF_CLOSED) ? -1 : 0; + return bfd->mode==BF_CLOSED ? -1: 1; } static int encrypt_bclose(BFILE *bfd) { -#ifdef xxx - bfd->fattrs = 0; - CloseEncryptedFileRaw(bfd->pvContext); - if(bfd->mode==BF_WRITE) - attribs_set(asfd, - bfd->path, &bfd->statp, bfd->winattr, bfd->conf); - bfd->pvContext=NULL; - bfd->mode=BF_CLOSED; - if(bfd->path) - { - free(bfd->path); - bfd->path=NULL; + if (p_CloseEncryptedFileRaw) { + p_CloseEncryptedFileRaw(bfd->pvContext); } + bfd->pvContext = NULL; + bfd->mode = BF_CLOSED; + bfd->fattrs = 0; + bfd->fid = -1; return 0; -#endif - return -1; } #endif -- 2.39.5