From 7e3728443e60e985983472a93e73f75e3faff82c Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Tue, 25 Mar 2003 20:35:36 +0000 Subject: [PATCH] Allow restore as non-root git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@395 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/kernstodo | 2 +- bacula/src/filed/win32/winservice.cpp | 14 ++--- bacula/src/findlib/attribs.c | 18 ++++--- bacula/src/findlib/create_file.c | 1 + bacula/src/findlib/makepath.c | 77 +++++++++++---------------- bacula/src/findlib/save-cwd.c | 10 ++-- 6 files changed, 56 insertions(+), 66 deletions(-) diff --git a/bacula/kernstodo b/bacula/kernstodo index b143903f8f..6cb668b541 100644 --- a/bacula/kernstodo +++ b/bacula/kernstodo @@ -1,5 +1,5 @@ Kern's ToDo List - 13 March 2003 + 25 March 2003 Documentation to do: (a little bit at a time) - Document running a test version. diff --git a/bacula/src/filed/win32/winservice.cpp b/bacula/src/filed/win32/winservice.cpp index d5ffc4c8f4..91e30b2346 100755 --- a/bacula/src/filed/win32/winservice.cpp +++ b/bacula/src/filed/win32/winservice.cpp @@ -71,6 +71,11 @@ bacService::bacService() } else { g_platform_id = osversioninfo.dwPlatformId; } + if (osversioninfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && + osversioninfo.dwMinorVersion == 0) { + /* Running Win95 so no GetFileAttributesEx available */ + NoGetFileAttributesEx = 1; + } } @@ -336,15 +341,6 @@ bacService::BaculaServiceMain() break; } - /* Test for GetFileAttributesEx which is not in Win95 */ - if (GetProcAddress(kerneldll, "GetFileAttributesEx") == NULL) { - NoGetFileAttributesEx = 1; - /*****FIXME***** remove after testing */ - MessageBox(NULL, "winserv NoGetFileAttributesEx", "Bacula", MB_OK); - } else { - MessageBox(NULL, "winserv Got GetFileAttributesEx", "Bacula", MB_OK); - } - // And find the RegisterServiceProcess function DWORD (*RegisterService)(DWORD, DWORD); RegisterService = (DWORD (*)(DWORD, DWORD)) diff --git a/bacula/src/findlib/attribs.c b/bacula/src/findlib/attribs.c index 3d85767616..97823977c6 100755 --- a/bacula/src/findlib/attribs.c +++ b/bacula/src/findlib/attribs.c @@ -170,6 +170,8 @@ int set_attributes(void *jcr, char *fname, char *ofile, char *lname, char *attribsEx, int *ofd) { struct utimbuf ut; + mode_t old_mask; + int stat = 1; #ifdef HAVE_CYGWIN if (set_win32_attributes(jcr, fname, ofile, lname, type, stream, @@ -183,6 +185,7 @@ int set_attributes(void *jcr, char *fname, char *ofile, char *lname, */ #endif + old_mask = umask(0); if (*ofd != -1) { close(*ofd); /* first close file */ *ofd = -1; @@ -194,21 +197,21 @@ int set_attributes(void *jcr, char *fname, char *ofile, char *lname, /* ***FIXME**** optimize -- don't do if already correct */ if (type == FT_LNK) { if (lchown(ofile, statp->st_uid, statp->st_gid) < 0) { - Jmsg2(jcr, M_ERROR, 0, "Unable to set file owner %s: ERR=%s\n", + Jmsg2(jcr, M_WARNING, 0, "Unable to set file owner %s: ERR=%s\n", ofile, strerror(errno)); - return 0; + stat = 0; } } else { if (chown(ofile, statp->st_uid, statp->st_gid) < 0) { - Jmsg2(jcr, M_ERROR, 0, "Unable to set file owner %s: ERR=%s\n", + Jmsg2(jcr, M_WARNING, 0, "Unable to set file owner %s: ERR=%s\n", ofile, strerror(errno)); - return 0; + stat = 0; } } if (chmod(ofile, statp->st_mode) < 0) { Jmsg2(jcr, M_ERROR, 0, "Unable to set file modes %s: ERR=%s\n", ofile, strerror(errno)); - return 0; + stat = 0; } /* @@ -217,9 +220,10 @@ int set_attributes(void *jcr, char *fname, char *ofile, char *lname, if (utime(ofile, &ut) < 0) { Jmsg2(jcr, M_ERROR, 0, "Unable to set file times %s: ERR=%s\n", ofile, strerror(errno)); - return 0; + stat = 0; } - return 1; + umask(old_mask); + return stat; } diff --git a/bacula/src/findlib/create_file.c b/bacula/src/findlib/create_file.c index e8d7673069..8c503d14a7 100644 --- a/bacula/src/findlib/create_file.c +++ b/bacula/src/findlib/create_file.c @@ -149,6 +149,7 @@ int create_file(void *jcr, char *fname, char *ofile, char *lname, stat = !make_path(jcr, ofile, parent_mode, parent_mode, uid, gid, 1, NULL); if (stat == 0) { Dmsg1(0, "Could not make path. %s\n", ofile); + Jmsg1(jcr, M_ERROR, 0, _("Could not make path. %s\n"), ofile); return CF_ERROR; } diff --git a/bacula/src/findlib/makepath.c b/bacula/src/findlib/makepath.c index 53fa1cce30..827b240723 100644 --- a/bacula/src/findlib/makepath.c +++ b/bacula/src/findlib/makepath.c @@ -73,8 +73,9 @@ cleanup(struct saved_cwd *cwd) if (cwd->do_chdir) { int _fail = restore_cwd(cwd, NULL, NULL); free_cwd(cwd); - if (_fail) + if (_fail) { return 1; + } } return 0; } @@ -93,9 +94,11 @@ make_dir(void *jcr, const char *dir, const char *dirpath, mode_t mode, int *crea { int fail = 0; int created_dir; + int save_errno; Dmsg2(300, "make_dir mode=%o dir=%s\n", mode, dir); created_dir = (mkdir(dir, mode) == 0); + save_errno = errno; if (!created_dir) { struct stat stats; @@ -110,7 +113,7 @@ make_dir(void *jcr, const char *dir, const char *dirpath, mode_t mode, int *crea if (stat(dir, &stats)) { Jmsg(jcr, M_ERROR, 0, "Cannot create directory %s: %s\n", - dirpath, strerror(errno)); + dirpath, strerror(save_errno)); fail = 1; } else if (!S_ISDIR(stats.st_mode)) { Jmsg(jcr, M_ERROR, 0, "%s exists but is not a directory\n", quote(dirpath)); @@ -177,7 +180,7 @@ make_path( char *dirpath; /* Temporarily relax umask in case it's overly restrictive. */ - mode_t oldmask = umask (0); + mode_t oldmask = umask(0); /* Make a copy of ARGPATH that we can scribble NULs on. */ dirpath = (char *)alloca(strlen(argpath) + 1); @@ -188,15 +191,13 @@ make_path( or should have set[ug]id or sticky bits set and we are setting their owners, we need to fix their permissions after making them. */ if (((parent_mode & WX_USR) != WX_USR) - || ((owner != (uid_t) -1 || group != (gid_t) -1) - && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0)) - { - tmp_mode = S_IRWXU; - re_protect = 1; - } - else { - tmp_mode = parent_mode; - re_protect = 0; + || ((owner != (uid_t)-1 || group != (gid_t)-1) + && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0)) { + tmp_mode = S_IRWXU; + re_protect = 1; + } else { + tmp_mode = parent_mode; + re_protect = 0; } /* If we can record the current working directory, we may be able @@ -244,27 +245,22 @@ make_path( if (newly_created_dir) { Dmsg0(300, "newly_created_dir\n"); - if (verbose_fmt_string) { - Jmsg(jcr, M_ERROR, 0, verbose_fmt_string, quote(dirpath)); - } - if ((owner != (uid_t) -1 || group != (gid_t) -1) - && chown (basename_dir, owner, group) + if ((owner != (uid_t)-1 || group != (gid_t)-1) + && chown(basename_dir, owner, group) #if defined(AFS) && defined (EPERM) && errno != EPERM #endif ) { - Jmsg(jcr, M_ERROR, 0, "Cannot change owner and/or group of %s: %s\n", - quote (dirpath), strerror(errno)); - umask(oldmask); - cleanup(&cwd); - return 1; + /* Note, if we are restoring as NON-root, this may not be fatal */ + Jmsg(jcr, M_WARNING, 0, "Cannot change owner and/or group of %s: %s\n", + quote(dirpath), strerror(errno)); } Dmsg0(300, "Chown done.\n"); if (re_protect) { struct ptr_list *pnew = (struct ptr_list *) - alloca (sizeof (struct ptr_list)); + alloca(sizeof (struct ptr_list)); pnew->dirname_end = slash; pnew->next = leading_dirs; leading_dirs = pnew; @@ -278,7 +274,7 @@ make_path( stat and mkdir process O(n^2) file name components. */ if (cwd.do_chdir && chdir(basename_dir) < 0) { Jmsg(jcr, M_ERROR, 0, "Cannot chdir to directory, %s: %s\n", - quote (dirpath), strerror(errno)); + quote(dirpath), strerror(errno)); umask(oldmask); cleanup(&cwd); return 1; @@ -307,22 +303,17 @@ make_path( } /* Done creating directories. Restore original umask. */ - umask (oldmask); + umask(oldmask); - if (verbose_fmt_string != NULL) { - Jmsg(jcr, M_ERROR, 0, verbose_fmt_string, dirpath); - } - - if (owner != (uid_t) -1 || group != (gid_t) -1) { + if (owner != (uid_t)-1 || group != (gid_t)-1) { if (chown(basename_dir, owner, group) #ifdef AFS && errno != EPERM #endif ) { - Jmsg(jcr, M_ERROR, 0, "Cannot change owner and/or group of %s: %s\n", - quote (dirpath), strerror(errno)); - retval = 1; + Jmsg(jcr, M_WARNING, 0, "Cannot change owner and/or group of %s: %s\n", + quote(dirpath), strerror(errno)); } } @@ -335,13 +326,13 @@ make_path( Dmsg1(300, "Final chmod mode=%o\n", mode); } if ((mode & ~S_IRWXUGO) && chmod(basename_dir, mode)) { - Jmsg(jcr, M_ERROR, 0, "Cannot change permissions of %s: %s\n", + Jmsg(jcr, M_WARNING, 0, "Cannot change permissions of %s: %s\n", quote(dirpath), strerror(errno)); - retval = 1; } - if (cleanup(&cwd)) + if (cleanup(&cwd)) { return 1; + } /* If the mode for leading directories didn't include owner "wx" privileges, we have to reset their protections to the correct @@ -350,9 +341,8 @@ make_path( *(p->dirname_end) = '\0'; Dmsg2(300, "Reset parent mode=%o dir=%s\n", parent_mode, dirpath); if (chmod(dirpath, parent_mode)) { - Jmsg(jcr, M_ERROR, 0, "Cannot change permissions of %s: %s\n", + Jmsg(jcr, M_WARNING, 0, "Cannot change permissions of %s: %s\n", quote (dirpath), strerror(errno)); - retval = 1; } } } else { @@ -373,21 +363,18 @@ make_path( On System V, users can give away files with chown and then not be able to chmod them. So don't give files away. */ - if ((owner != (uid_t) -1 || group != (gid_t) -1) + if ((owner != (uid_t)-1 || group != (gid_t)-1) && chown(dirpath, owner, group) #ifdef AFS && errno != EPERM #endif - ) - { - Jmsg(jcr, M_ERROR, 0, "Cannot change owner and/or group of %s: %s\n", + ) { + Jmsg(jcr, M_WARNING, 0, "Cannot change owner and/or group of %s: %s\n", quote(dirpath), strerror(errno)); - retval = 1; } if (chmod(dirpath, mode)) { - Jmsg(jcr, M_ERROR, 0, "Cannot change permissions of %s: %s\n", + Jmsg(jcr, M_WARNING, 0, "Cannot change permissions of %s: %s\n", quote(dirpath), strerror(errno)); - retval = 1; } Dmsg2(300, "pathexists chmod mode=%o dir=%s\n", mode, dirpath); } diff --git a/bacula/src/findlib/save-cwd.c b/bacula/src/findlib/save-cwd.c index 89746eec86..d5f8184dbb 100644 --- a/bacula/src/findlib/save-cwd.c +++ b/bacula/src/findlib/save-cwd.c @@ -33,7 +33,7 @@ called, but doing so is ok. Otherwise, return zero. */ int -save_cwd (struct saved_cwd *cwd) +save_cwd(struct saved_cwd *cwd) { static int have_working_fchdir = 1; @@ -51,7 +51,7 @@ save_cwd (struct saved_cwd *cwd) # if __sun__ || sun /* On SunOS 4, fchdir returns EINVAL if accounting is enabled, so we have to fall back to chdir. */ - if (fchdir (cwd->desc)) { + if (fchdir(cwd->desc)) { if (errno == EINVAL) { close(cwd->desc); cwd->desc = -1; @@ -108,8 +108,10 @@ restore_cwd(const struct saved_cwd *cwd, const char *dest, const char *from) void free_cwd(struct saved_cwd *cwd) { - if (cwd->desc >= 0) + if (cwd->desc >= 0) { close(cwd->desc); - if (cwd->name) + } + if (cwd->name) { free_pool_memory(cwd->name); + } } -- 2.39.5