]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/vfs/ea.c
Fix incompatible func args warnings
[netatalk.git] / libatalk / vfs / ea.c
index e6222b98dd0099a7e99a45ae805aa813c6b3a7d8..e5d4fdb8a29449041ee5a2cca4b820df453714d0 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  $Id: ea.c,v 1.6 2009-10-15 15:35:05 franklahm Exp $
+  $Id: ea.c,v 1.9 2009-10-22 12:35:39 franklahm Exp $
   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
 
   This program is free software; you can redistribute it and/or modify
  * - store EAs in files "fileWithEAs::EA::testEA1" and "fileWithEAs::EA::testEA2"
  */
 
+/* 
+ * Build mode for EA header from file mode
+ */
+static inline mode_t ea_header_mode(mode_t mode)
+{
+    /* Same as ad_hf_mode(mode) */
+    mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
+    /* Owner must be able to open, read and w-lock it, in order to chmod from eg 0000 -> 0xxxx*/
+    mode |= S_IRUSR | S_IWUSR;
+    return mode;
+}
+
+/* 
+ * Build mode for EA file from file mode
+ */
+static inline mode_t ea_mode(mode_t mode)
+{
+    /* Same as ad_hf_mode(mode) */
+    mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
+    return mode;
+}
+
+/*
+  Taken form afpd/desktop.c
+*/
+static char *mtoupath(const struct vol *vol, const char *mpath)
+{
+    static char  upath[ MAXPATHLEN + 2]; /* for convert_charset dest_len parameter +2 */
+    const char   *m;
+    char         *u;
+    size_t       inplen;
+    size_t       outlen;
+    uint16_t     flags = CONV_ESCAPEHEX;
+
+    if (!mpath)
+        return NULL;
+
+    if ( *mpath == '\0' ) {
+        return( "." );
+    }
+
+    m = mpath;
+    u = upath;
+
+    inplen = strlen(m);
+    outlen = MAXPATHLEN;
+
+    if ((size_t)-1 == (outlen = convert_charset(CH_UTF8_MAC,
+                                                vol->v_volcharset,
+                                                vol->v_maccharset,
+                                                m, inplen, u, outlen, &flags)) ) {
+        return NULL;
+    }
+
+    return( upath );
+}
+
+
 /*
  * Function: unpack_header
  *
@@ -231,7 +289,7 @@ static int pack_header(struct ea * restrict ea)
  *    ea           (r) ea handle
  *    eaname       (r) name of EA or NULL
  *
- * Returns: pointer to name in static buffer
+ * Returns: pointer to name in static buffer, NULL on error
  *
  * Effects:
  *
@@ -255,6 +313,8 @@ static char * ea_path(const struct ea * restrict ea,
 
     if (eaname) {
         strlcat(pathbuf, "::", MAXPATHLEN + 1);
+        if ((eaname = mtoupath(ea->vol, eaname)) == NULL)
+            return NULL;
         strlcat(pathbuf, eaname, MAXPATHLEN + 1);
     }
 
@@ -477,8 +537,12 @@ static int write_ea(const struct ea * restrict ea,
     struct stat st;
     char *eaname;
 
-    eaname = ea_path(ea, attruname);
-    LOG(log_maxdebug, logtype_afpd, "write_ea: ea_apth: %s", eaname);
+    if ((eaname = ea_path(ea, attruname)) == NULL) {
+        LOG(log_error, logtype_afpd, "write_ea('%s'): ea_path error", attruname);
+        return AFPERR_MISC;
+    }
+
+    LOG(log_maxdebug, logtype_afpd, "write_ea('%s')", eaname);
 
     /* Check if it exists, remove if yes*/
     if ((stat(eaname, &st)) == 0) {
@@ -503,7 +567,7 @@ static int write_ea(const struct ea * restrict ea,
     }
 
     if ((write(fd, ibuf, attrsize)) != attrsize) {
-        LOG(log_error, logtype_afpd, "write_ea: short write: %s", eaname);
+        LOG(log_error, logtype_afpd, "write_ea('%s'): write: %s", eaname, strerror(errno));
         ret = -1;
         goto exit;
     }
@@ -533,7 +597,10 @@ static int delete_ea_file(const struct ea * restrict ea,
     char *eafile;
     struct stat st;
 
-    eafile = ea_path(ea, eaname);
+    if ((eafile = ea_path(ea, eaname)) == NULL) {
+        LOG(log_error, logtype_afpd, "delete_ea_file('%s'): ea_path error", eaname);
+        return -1;
+    }
 
     /* Check if it exists, remove if yes*/
     if ((stat(eafile, &st)) == 0) {
@@ -655,7 +722,7 @@ static int ea_open(const struct vol * restrict vol,
 
     /* Now lock, open and read header file from disk */
     if ((ea->ea_fd = open(eaname, (ea->ea_flags & EA_RDWR) ? O_RDWR : O_RDONLY)) == -1) {
-        LOG(log_error, logtype_afpd, "ea_open: error on open for header: %s", eaname);
+        LOG(log_error, logtype_afpd, "ea_open('%s'): error: %s", eaname, strerror(errno));
         ret = -1;
         goto exit;
     }
@@ -841,12 +908,7 @@ exit:
  *
  * Copies EA size into rbuf in network order. Increments *rbuflen +4.
  */
-int get_easize(const struct vol * restrict vol,
-               char * restrict rbuf,
-               int * restrict rbuflen,
-               const char * restrict uname,
-               int oflag,
-               const char * restrict attruname)
+int get_easize(VFS_FUNC_ARGS_EA_GETSIZE)
 {
     int ret = AFPERR_MISC, count = 0;
     uint32_t uint32;
@@ -902,18 +964,13 @@ int get_easize(const struct vol * restrict vol,
  *
  * Copies EA into rbuf. Increments *rbuflen accordingly.
  */
-int get_eacontent(const struct vol * restrict vol,
-                  char * restrict rbuf,
-                  int * restrict rbuflen,
-                  const char * restrict uname,
-                  int oflag,
-                  const char * restrict attruname,
-                  int maxreply)
+int get_eacontent(VFS_FUNC_ARGS_EA_GETCONTENT)
 {
     int ret = AFPERR_MISC, count = 0, fd = -1;
     uint32_t uint32;
     size_t toread;
     struct ea ea;
+    char *eafile;
 
     LOG(log_debug, logtype_afpd, "get_eacontent('%s/%s')", uname, attruname);
 
@@ -924,7 +981,12 @@ int get_eacontent(const struct vol * restrict vol,
 
     while (count < ea.ea_count) {
         if (strcmp(attruname, (*ea.ea_entries)[count].ea_name) == 0) {
-            if ((fd = open(ea_path(&ea, attruname), O_RDONLY)) == -1) {
+            if ( (eafile = ea_path(&ea, attruname)) == NULL) {
+                ret = AFPERR_MISC;
+                break;
+            }
+
+            if ((fd = open(eafile, O_RDONLY)) == -1) {
                 ret = AFPERR_MISC;
                 break;
             }
@@ -985,11 +1047,7 @@ int get_eacontent(const struct vol * restrict vol,
  * Copies names of all EAs of uname as consecutive C strings into rbuf.
  * Increments *buflen accordingly.
  */
-int list_eas(const struct vol * restrict vol,
-             char * restrict attrnamebuf,
-             int * restrict buflen,
-             const char * restrict uname,
-             int oflag)
+int list_eas(VFS_FUNC_ARGS_EA_LIST)
 {
     int count = 0, attrbuflen = *buflen, ret = AFP_OK, len;
     char *buf = attrnamebuf;
@@ -1622,9 +1680,15 @@ int ea_renamefile(VFS_FUNC_ARGS_RENAMEFILE)
         easize = (*srcea.ea_entries)[count].ea_size;
 
         /* Build src and dst paths for rename() */
-        eapath = ea_path(&srcea, eaname);
+        if ((eapath = ea_path(&srcea, eaname)) == NULL) {
+            ret = AFPERR_MISC;
+            goto exit;
+        }
         strcpy(srceapath, eapath);
-        eapath = ea_path(&dstea, eaname);
+        if ((eapath = ea_path(&dstea, eaname)) == NULL) {
+            ret = AFPERR_MISC;
+            goto exit;
+        }
 
         LOG(log_maxdebug, logtype_afpd, "ea_renamefile('%s/%s'): moving EA '%s' to '%s'",
             src, dst, srceapath, eapath);
@@ -1713,9 +1777,15 @@ int ea_copyfile(VFS_FUNC_ARGS_COPYFILE)
         easize = (*srcea.ea_entries)[count].ea_size;
 
         /* Build src and dst paths for copy_file() */
-        eapath = ea_path(&srcea, eaname);
+        if ((eapath = ea_path(&srcea, eaname)) == NULL) {
+            ret = AFPERR_MISC;
+            goto exit;
+        }
         strcpy(srceapath, eapath);
-        eapath = ea_path(&dstea, eaname);
+        if ((eapath = ea_path(&dstea, eaname)) == NULL) {
+            ret = AFPERR_MISC;
+            goto exit;
+        }
 
         LOG(log_maxdebug, logtype_afpd, "ea_copyfile('%s/%s'): copying EA '%s' to '%s'",
             src, dst, srceapath, eapath);
@@ -1777,7 +1847,10 @@ int ea_chown(VFS_FUNC_ARGS_CHOWN)
     }
 
     while (count < ea.ea_count) {
-        eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name);
+        if ((eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name)) == NULL) {
+            ret = AFPERR_MISC;
+            goto exit;
+        }
         if ((chown(eaname, uid, gid)) != 0) {
             switch (errno) {
             case EPERM:
@@ -1802,3 +1875,156 @@ exit:
 
     return ret;
 }
+
+int ea_chmod_file(VFS_FUNC_ARGS_SETFILEMODE)
+{
+    LOG(log_debug, logtype_afpd, "ea_chmod_file('%s')", name);
+
+    int count = 0, ret = AFP_OK;
+    const char *eaname;
+    struct ea ea;
+
+    /* Open EA stuff */
+    if ((ea_open(vol, name, EA_RDWR, &ea)) != 0) {
+        if (errno == ENOENT)
+            /* no EA files, nothing to do */
+            return AFP_OK;
+        else
+            return AFPERR_MISC;
+    }
+
+    /* Set mode on EA header file */
+    if ((setfilmode(ea_path(&ea, NULL), ea_header_mode(mode), NULL, vol->v_umask)) != 0) {
+        LOG(log_error, logtype_afpd, "ea_chmod_file('%s'): %s", ea_path(&ea, NULL), strerror(errno));
+        switch (errno) {
+        case EPERM:
+        case EACCES:
+            ret = AFPERR_ACCESS;
+            goto exit;
+        default:
+            ret = AFPERR_MISC;
+            goto exit;
+        }
+    }
+
+    /* Set mode on EA files */
+    while (count < ea.ea_count) {
+        if ((eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name)) == NULL) {
+            ret = AFPERR_MISC;
+            goto exit;
+        }
+        if ((setfilmode(eaname, ea_mode(mode), NULL, vol->v_umask)) != 0) {
+            LOG(log_error, logtype_afpd, "ea_chmod_file('%s'): %s", eaname, strerror(errno));
+            switch (errno) {
+            case EPERM:
+            case EACCES:
+                ret = AFPERR_ACCESS;
+                goto exit;
+            default:
+                ret = AFPERR_MISC;
+                goto exit;
+            }
+            continue;
+        }
+
+        count++;
+    }
+
+exit:
+    if ((ea_close(&ea)) != 0) {
+        LOG(log_error, logtype_afpd, "ea_chmod_file('%s'): error closing ea handle", name);
+        return AFPERR_MISC;
+    }
+
+    return ret;
+}
+
+int ea_chmod_dir(VFS_FUNC_ARGS_SETDIRUNIXMODE)
+{
+    LOG(log_debug, logtype_afpd, "ea_chmod_dir('%s')", name);
+
+    int ret = AFP_OK;
+    uid_t uid;
+    const char *eaname;
+    const char *eaname_safe = NULL;
+    struct ea ea;
+
+    /* .AppleDouble already might be inaccesible, so we must run as id 0 */
+    uid = geteuid();
+    if (seteuid(0)) {
+        LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): seteuid: %s", name, strerror(errno));
+        ret = AFPERR_MISC;
+        goto exit;
+    }
+
+    /* Open EA stuff */
+    if ((ea_open(vol, name, EA_RDWR, &ea)) != 0) {
+        if (errno == ENOENT)
+            /* no EA files, nothing to do */
+            return AFP_OK;
+        else
+            return AFPERR_MISC;
+    }
+
+    /* Set mode on EA header */
+    if ((setfilmode(ea_path(&ea, NULL), ea_header_mode(mode), NULL, vol->v_umask)) != 0) {
+        LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): %s", ea_path(&ea, NULL), strerror(errno));
+        switch (errno) {
+        case EPERM:
+        case EACCES:
+            ret = AFPERR_ACCESS;
+            goto exit;
+        default:
+            ret = AFPERR_MISC;
+            goto exit;
+        }
+    }
+
+    /* Set mode on EA files */
+    int count = 0;
+    while (count < ea.ea_count) {
+        eaname = (*ea.ea_entries)[count].ea_name;
+        /*
+         * Be careful with EA names from the EA header!
+         * Eg NFS users might have access to them, can inject paths using ../ or /.....
+         * FIXME:
+         * Until the EA code escapes / in EA name requests from the client, these therefor wont work.
+         */
+        if ((eaname_safe = strrchr(eaname, '/'))) {
+            LOG(log_warning, logtype_afpd, "ea_chmod_dir('%s'): contains a slash", eaname);
+            eaname = eaname_safe;
+        }
+        if ((eaname = ea_path(&ea, eaname)) == NULL) {
+            ret = AFPERR_MISC;
+            goto exit;
+        }
+        if ((setfilmode(eaname, ea_mode(mode), NULL, vol->v_umask)) != 0) {
+            LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): %s", eaname, strerror(errno));
+            switch (errno) {
+            case EPERM:
+            case EACCES:
+                ret = AFPERR_ACCESS;
+                goto exit;
+            default:
+                ret = AFPERR_MISC;
+                goto exit;
+            }
+            continue;
+        }
+
+        count++;
+    }
+
+exit:
+    if (seteuid(uid) < 0) {
+        LOG(log_error, logtype_afpd, "can't seteuid back: %s", strerror(errno));
+        exit(EXITERR_SYS);
+    }
+
+    if ((ea_close(&ea)) != 0) {
+        LOG(log_error, logtype_afpd, "ea_chmod_dir('%s'): error closing ea handle", name);
+        return AFPERR_MISC;
+    }
+
+    return ret;
+}