aboutsummaryrefslogtreecommitdiffstats
path: root/main/lighttpd/0006-add-support-for-Free-BSD-extended-attributes.patch
blob: f3affb9119e03975a6bf0973830a6d821a607730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
From 4d55d4ada3ebbdd6b99855fe0767d26490955a22 Mon Sep 17 00:00:00 2001
From: Moritz Wilhelmy <mw@barfooze.de>
Date: Thu, 22 May 2014 08:30:13 +0000
Subject: [PATCH 06/29] add support for (Free)BSD extended attributes

enable with `./configure --with-attr` and `mimetype.use-xattr =
"enable"` in the config.

set attribute with:

    setextattr user Content-Type text/plain path/to/www/file

From: Moritz Wilhelmy <mw@barfooze.de>

git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2966 152afb58-edef-0310-8abb-c4023f1b3aa9
---
 NEWS                 |  1 +
 configure.ac         | 25 +++++++++++++++++--------
 src/mod_dirlisting.c | 17 ++++++++++++++---
 src/stat_cache.c     | 21 +++++++++++++++++++--
 4 files changed, 51 insertions(+), 13 deletions(-)

diff --git a/NEWS b/NEWS
index 0bf0313..84a1c80 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,7 @@ NEWS
   * use keep-alive timeout while waiting for HTTP headers; use always the read timeout while waiting for the HTTP body
   * fix bad shift in conditional netmask ".../0" handling
   * add more mime types and a script to generate mime.conf (fixes #2579)
+  * add support for (Free)BSD extended attributes
 
 - 1.4.35 - 2014-03-12
   * [network/ssl] fix build error if TLSEXT is disabled
diff --git a/configure.ac b/configure.ac
index ae35234..48e6b52 100644
--- a/configure.ac
+++ b/configure.ac
@@ -218,14 +218,23 @@ AC_ARG_WITH(attr, AC_HELP_STRING([--with-attr],[enable extended attribute suppor
 [WITH_ATTR=$withval],[WITH_ATTR=no])
 AC_MSG_RESULT($withval)
 if test "$WITH_ATTR" != "no"; then
- AC_CHECK_LIB(attr, attr_get, [
-	AC_CHECK_HEADERS([attr/attributes.h],[
-		ATTR_LIB=-lattr
-		AC_DEFINE([HAVE_XATTR], [1], [libattr])
-		AC_DEFINE([HAVE_ATTR_ATTRIBUTES_H], [1])
-	])
- ])
- AC_SUBST(ATTR_LIB)
+  # libattr (linux only?)
+  AC_CHECK_LIB(attr, attr_get, [
+    AC_CHECK_HEADERS([attr/attributes.h],[
+      ATTR_LIB=-lattr
+      AC_DEFINE([HAVE_XATTR], [1], [libattr])
+      AC_DEFINE([HAVE_ATTR_ATTRIBUTES_H], [1])
+    ])
+  ])
+  AC_SUBST(ATTR_LIB)
+
+  # (Free)BSD extattr
+  AC_CHECK_FUNC([extattr_get_file], [
+    AC_CHECK_HEADERS([sys/extattr.h],[
+      AC_DEFINE([HAVE_EXTATTR], [1], [BSD extended attributes])
+      AC_DEFINE([HAVE_SYS_EXTATTR_H], [1])
+    ])
+  ])
 fi
 
 dnl openssl on solaris needs -lsocket -lnsl
diff --git a/src/mod_dirlisting.c b/src/mod_dirlisting.c
index cd5809e..6aba403 100644
--- a/src/mod_dirlisting.c
+++ b/src/mod_dirlisting.c
@@ -31,6 +31,10 @@
 #include <attr/attributes.h>
 #endif
 
+#ifdef HAVE_SYS_EXTATTR_H
+#include <sys/extattr.h>
+#endif
+
 #include "version.h"
 
 /* plugin config for all request/connections */
@@ -644,7 +648,7 @@ static int http_list_directory(server *srv, connection *con, plugin_data *p, buf
 	size_t k;
 	const char *content_type;
 	long name_max;
-#ifdef HAVE_XATTR
+#if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
 	char attrval[128];
 	int attrlen;
 #endif
@@ -820,8 +824,7 @@ static int http_list_directory(server *srv, connection *con, plugin_data *p, buf
 		tmp = files.ent[i];
 
 		content_type = NULL;
-#ifdef HAVE_XATTR
-
+#if defined(HAVE_XATTR)
 		if (con->conf.use_xattr) {
 			memcpy(path_file, DIRLIST_ENT_NAME(tmp), tmp->namelen + 1);
 			attrlen = sizeof(attrval) - 1;
@@ -830,6 +833,14 @@ static int http_list_directory(server *srv, connection *con, plugin_data *p, buf
 				content_type = attrval;
 			}
 		}
+#elif defined(HAVE_EXTATTR)
+		if (con->conf.use_xattr) {
+			memcpy(path_file, DIRLIST_ENT_NAME(tmp), tmp->namelen + 1);
+			if(-1 != (attrlen = extattr_get_file(path, EXTATTR_NAMESPACE_USER, "Content-Type", attrval, sizeof(attrval)-1))) {
+				attrval[attrlen] = '\0';
+				content_type = attrval;
+			}
+		}
 #endif
 
 		if (content_type == NULL) {
diff --git a/src/stat_cache.c b/src/stat_cache.c
index 480aae4..9007325 100644
--- a/src/stat_cache.c
+++ b/src/stat_cache.c
@@ -18,6 +18,10 @@
 # include <attr/attributes.h>
 #endif
 
+#ifdef HAVE_SYS_EXTATTR_H
+# include <sys/extattr.h>
+#endif
+
 #ifdef HAVE_FAM_H
 # include <fam.h>
 #endif
@@ -210,7 +214,7 @@ void stat_cache_free(stat_cache *sc) {
 	free(sc);
 }
 
-#ifdef HAVE_XATTR
+#if defined(HAVE_XATTR)
 static int stat_cache_attr_get(buffer *buf, char *name) {
 	int attrlen;
 	int ret;
@@ -224,6 +228,19 @@ static int stat_cache_attr_get(buffer *buf, char *name) {
 	}
 	return ret;
 }
+#elif defined(HAVE_EXTATTR)
+static int stat_cache_attr_get(buffer *buf, char *name) {
+	ssize_t attrlen = 1024;
+
+	buffer_prepare_copy(buf, attrlen);
+
+	if (-1 != (attrlen = extattr_get_file(name, EXTATTR_NAMESPACE_USER, "Content-Type", buf->ptr, attrlen-1))) {
+		buf->used = attrlen + 1;
+		buf->ptr[attrlen] = '\0';
+		return 0;
+	}
+	return -1;
+}
 #endif
 
 /* the famous DJB hash function for strings */
@@ -592,7 +609,7 @@ handler_t stat_cache_get_entry(server *srv, connection *con, buffer *name, stat_
 	if (S_ISREG(st.st_mode)) {
 		/* determine mimetype */
 		buffer_reset(sce->content_type);
-#ifdef HAVE_XATTR
+#if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
 		if (con->conf.use_xattr) {
 			stat_cache_attr_get(sce->content_type, name->ptr);
 		}
-- 
2.4.5