Re: Run external RewriteMap program as non-root

2015-03-05 Thread Jan Kaluža

On 03/05/2015 12:53 PM, Yann Ylavic wrote:

On Thu, Mar 5, 2015 at 12:08 PM, Jan Kaluža  wrote:

On 03/05/2015 07:55 AM, Jan Kaluža wrote:


3. Execute it where it is now (post_config), but set user/group using
apr_procattr_t. So far I think this would duplicate the code of
mod_unixd and would probably have to also handle the windows equivalent
of that module (if there's any).



I've been thinking about this one more and with introduction of third
argument to RewriteMap, it could be possible with patch similar to attached
one.

You can do "RewriteMap MapName prg:/path user:group" with the patch.

This could be even backported to 2.4.x.


I'm fine with this one too (unix only?).


Still thinking about good RewriteMap syntax to pass "password" for 
Windows. But If people don't mind, having this unix only is also 
solution :).





Re: Run external RewriteMap program as non-root

2015-03-05 Thread Eric Covener
On Thu, Mar 5, 2015 at 4:48 AM, André Malo  wrote:
> 5) Let it drop the privileges by itself.
>
> I actually tend to 5 :-)


+1 (as a new option as described in a followup)

-- 
Eric Covener
[email protected]


Re: Run external RewriteMap program as non-root

2015-03-05 Thread Yann Ylavic
On Thu, Mar 5, 2015 at 12:08 PM, Jan Kaluža  wrote:
> On 03/05/2015 07:55 AM, Jan Kaluža wrote:
>>
>> 3. Execute it where it is now (post_config), but set user/group using
>> apr_procattr_t. So far I think this would duplicate the code of
>> mod_unixd and would probably have to also handle the windows equivalent
>> of that module (if there's any).
>
>
> I've been thinking about this one more and with introduction of third
> argument to RewriteMap, it could be possible with patch similar to attached
> one.
>
> You can do "RewriteMap MapName prg:/path user:group" with the patch.
>
> This could be even backported to 2.4.x.

I'm fine with this one too (unix only?).


Re: Run external RewriteMap program as non-root

2015-03-05 Thread Jan Kaluža

On 03/05/2015 07:55 AM, Jan Kaluža wrote:

Hi,

currently, the External Rewriting Program (RewriteMap "prg:") is run as
root. I would like to change it but I see three ways how to do it:

1. Execute it right after drop_privileges hook. This looks like best
way, but I haven't found any hook which could be used for that (except
drop_privileges with APR_HOOK_REALLY_LAST, which does not seem as proper
place to me).

2. Execute it in child_init. This is done after drop_privileges, so the
user/group is good. The "problem" here is that it would execute one
rewrite program per child. Right now I'm not sure if it's really
problem. It could be useful to have more instances of rewriting program
to make its bottleneck lower.

3. Execute it where it is now (post_config), but set user/group using
apr_procattr_t. So far I think this would duplicate the code of
mod_unixd and would probably have to also handle the windows equivalent
of that module (if there's any).


I've been thinking about this one more and with introduction of third 
argument to RewriteMap, it could be possible with patch similar to 
attached one.


You can do "RewriteMap MapName prg:/path user:group" with the patch.

This could be even backported to 2.4.x.

Jan Kaluza


What way do you think is the best, or would you do it differently?

I'm attaching patch for number 2.

Regards,
Jan Kaluza


Index: modules/mappers/mod_rewrite.c
===
--- modules/mappers/mod_rewrite.c	(revision 1663642)
+++ modules/mappers/mod_rewrite.c	(working copy)
@@ -267,6 +267,8 @@
 const char *dbdq;  /* SQL SELECT statement for rewritemap */
 const char *checkfile2;/* filename to check for map existence
   NULL if only one file   */
+const char *user;
+const char *group;
 } rewritemap_entry;
 
 /* special pattern types for RewriteCond */
@@ -1171,6 +1173,7 @@
 
 static apr_status_t rewritemap_program_child(apr_pool_t *p,
  const char *progname, char **argv,
+ const char *user, const char *group,
  apr_file_t **fpout,
  apr_file_t **fpin)
 {
@@ -1183,6 +1186,8 @@
   APR_FULL_BLOCK, APR_NO_PIPE))
 && APR_SUCCESS == (rc=apr_procattr_dir_set(procattr,
  ap_make_dirstr_parent(p, argv[0])))
+&& (!user || APR_SUCCESS == (rc=apr_procattr_user_set(procattr, user, "")))
+&& (!group || APR_SUCCESS == (rc=apr_procattr_group_set(procattr, group)))
 && APR_SUCCESS == (rc=apr_procattr_cmdtype_set(procattr, APR_PROGRAM))
 && APR_SUCCESS == (rc=apr_procattr_child_errfn_set(procattr,
rewrite_child_errfn))
@@ -1240,6 +1245,7 @@
 }
 
 rc = rewritemap_program_child(p, map->argv[0], map->argv,
+  map->user, map->group,
   &fpout, &fpin);
 if (rc != APR_SUCCESS || fpin == NULL || fpout == NULL) {
 ap_log_error(APLOG_MARK, APLOG_ERR, rc, s, APLOGNO(00654)
@@ -3018,7 +3024,7 @@
 }
 
 static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
-  const char *a2)
+  const char *a2, const char *a3)
 {
 rewrite_server_conf *sconf;
 rewritemap_entry *newmap;
@@ -3124,6 +3130,11 @@
 
 newmap->type  = MAPTYPE_PRG;
 newmap->checkfile = newmap->argv[0];
+if (a3) {
+char *tok_cntx;
+newmap->user = apr_strtok(apr_pstrdup(cmd->pool, a3), ":", &tok_cntx);
+newmap->group = apr_strtok(NULL, ":", &tok_cntx);
+}
 }
 else if (strncasecmp(a2, "int:", 4) == 0) {
 newmap->type  = MAPTYPE_INT;
@@ -5205,8 +5216,8 @@
  "an input string and a to be applied regexp-pattern"),
 AP_INIT_RAW_ARGS("RewriteRule", cmd_rewriterule, NULL, OR_FILEINFO,
  "an URL-applied regexp-pattern and a substitution URL"),
-AP_INIT_TAKE2(   "RewriteMap",  cmd_rewritemap,  NULL, RSRC_CONF,
- "a mapname and a filename"),
+AP_INIT_TAKE23(   "RewriteMap",  cmd_rewritemap,  NULL, RSRC_CONF,
+ "a mapname and a filename and options"),
 { NULL }
 };
 


Re: Run external RewriteMap program as non-root

2015-03-05 Thread Yann Ylavic
On Thu, Mar 5, 2015 at 10:48 AM, André Malo  wrote:
>
> 5) Let it drop the privileges by itself.
>
> I actually tend to 5 :-)

+1


Re: Run external RewriteMap program as non-root

2015-03-05 Thread André Malo
* Jan Kaluža wrote:

> Hi,
>
> currently, the External Rewriting Program (RewriteMap "prg:") is run as
> root. I would like to change it but I see three ways how to do it:
>
> 1. Execute it right after drop_privileges hook. This looks like best
> way, but I haven't found any hook which could be used for that (except
> drop_privileges with APR_HOOK_REALLY_LAST, which does not seem as proper
> place to me).
>
> 2. Execute it in child_init. This is done after drop_privileges, so the
> user/group is good. The "problem" here is that it would execute one
> rewrite program per child. Right now I'm not sure if it's really
> problem. It could be useful to have more instances of rewriting program
> to make its bottleneck lower.
>
> 3. Execute it where it is now (post_config), but set user/group using
> apr_procattr_t. So far I think this would duplicate the code of
> mod_unixd and would probably have to also handle the windows equivalent
> of that module (if there's any).

May be

4) Invoke suexec somehow

and

5) Let it drop the privileges by itself.

I actually tend to 5 :-)

nd
-- 
Winnetous Erbe: 


Re: Run external RewriteMap program as non-root

2015-03-05 Thread Jan Kaluža

On 03/05/2015 09:54 AM, Jan Kaluža wrote:

On 03/05/2015 09:03 AM, Ruediger Pluem wrote:



On 03/05/2015 07:55 AM, Jan Kaluža wrote:

Hi,

currently, the External Rewriting Program (RewriteMap "prg:") is run
as root. I would like to change it but I see three
ways how to do it:

1. Execute it right after drop_privileges hook. This looks like best
way, but I haven't found any hook which could be
used for that (except drop_privileges with APR_HOOK_REALLY_LAST,
which does not seem as proper place to me).

2. Execute it in child_init. This is done after drop_privileges, so
the user/group is good. The "problem" here is that
it would execute one rewrite program per child. Right now I'm not
sure if it's really problem. It could be useful to
have more instances of rewriting program to make its bottleneck lower.

3. Execute it where it is now (post_config), but set user/group using
apr_procattr_t. So far I think this would
duplicate the code of mod_unixd and would probably have to also
handle the windows equivalent of that module (if there's
any).

What way do you think is the best, or would you do it differently?

I'm attaching patch for number 2.


I would tend to 2. as well, but as far as I remember using the
rewritemap program is synchronized across all processes.
This raises two questions:

1. Does rewriting still work with the current patch?


It does work for me. I've done some tests with curl and ab with
prefork/event/worker MPMs.


2. If it does can stuff be optimized to move from a server wide lock
to a process wide lock (or even no lock for
prefork) to remove the contention here?


This could be possible, I will look at it.


Attached patch does it and works for me. RewriteMap with external 
program is also 24% faster with prefork with this patch.


Jan Kaluza


OTOH looking at the topic of backwards compatibility existing rewrite
programs
might rely on not working in parallel. Some may even have an issue if
more then one copy of them is running in parallel,
albeit not processing stuff in parallel which of course would cause an
issue with the proposed patch. Furthermore
existing setups might expect to be run as root. But this stuff only
needs to be considered when we think about
backporting and is moot for trunk.


Right, I'm currently thinking only about trunk. For the 2.4.x, we would
have to do it differently with backward compatibility in mind. I think
something like option 1 with configuration directive to enable new
behaviour would be more acceptable for 2.4.x. We would have single
rewritemap program in this case running as an apache user only if admin
wants it.


Regards

Rüdiger



Regards,
Jan Kaluza



Index: modules/mappers/mod_rewrite.c
===
--- modules/mappers/mod_rewrite.c	(revision 1663642)
+++ modules/mappers/mod_rewrite.c	(working copy)
@@ -52,7 +52,7 @@
 #include "apr_user.h"
 #include "apr_lib.h"
 #include "apr_signal.h"
-#include "apr_global_mutex.h"
+#include "apr_thread_mutex.h"
 #include "apr_dbm.h"
 #include "apr_dbd.h"
 #include "mod_dbd.h"
@@ -100,6 +100,7 @@
 
 #include "mod_rewrite.h"
 #include "ap_expr.h"
+#include "ap_mpm.h"
 
 #if APR_CHARSET_EBCDIC
 #include "util_charset.h"
@@ -416,8 +417,7 @@
 static int proxy_available;
 
 /* Locks/Mutexes */
-static apr_global_mutex_t *rewrite_mapr_lock_acquire = NULL;
-static const char *rewritemap_mutex_type = "rewrite-map";
+static apr_thread_mutex_t *rewrite_mapr_lock_acquire = NULL;
 
 /* Optional functions imported from mod_ssl when loaded: */
 static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *rewrite_ssl_lookup = NULL;
@@ -1437,10 +1437,10 @@
 
 /* take the lock */
 if (rewrite_mapr_lock_acquire) {
-rv = apr_global_mutex_lock(rewrite_mapr_lock_acquire);
+rv = apr_thread_mutex_lock(rewrite_mapr_lock_acquire);
 if (rv != APR_SUCCESS) {
 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00659)
-  "apr_global_mutex_lock(rewrite_mapr_lock_acquire) "
+  "apr_thread_mutex_lock(rewrite_mapr_lock_acquire) "
   "failed");
 return NULL; /* Maybe this should be fatal? */
 }
@@ -1551,10 +1551,10 @@
 
 /* give the lock back */
 if (rewrite_mapr_lock_acquire) {
-rv = apr_global_mutex_unlock(rewrite_mapr_lock_acquire);
+rv = apr_thread_mutex_unlock(rewrite_mapr_lock_acquire);
 if (rv != APR_SUCCESS) {
 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00660)
-  "apr_global_mutex_unlock(rewrite_mapr_lock_acquire) "
+  "apr_thread_mutex_unlock(rewrite_mapr_lock_acquire) "
   "failed");
 return NULL; /* Maybe this should be fatal? */
 }
@@ -2639,13 +2639,21 @@
 static apr_status_t rewritelock_create(server_rec *s, apr_pool_t *p)
 {
 apr_status_t rc;
+int threaded;
 
+/* RewriteMap program is spawned per chil

Re: Run external RewriteMap program as non-root

2015-03-05 Thread Jan Kaluža

On 03/05/2015 09:03 AM, Ruediger Pluem wrote:



On 03/05/2015 07:55 AM, Jan Kaluža wrote:

Hi,

currently, the External Rewriting Program (RewriteMap "prg:") is run as root. I 
would like to change it but I see three
ways how to do it:

1. Execute it right after drop_privileges hook. This looks like best way, but I 
haven't found any hook which could be
used for that (except drop_privileges with APR_HOOK_REALLY_LAST, which does not 
seem as proper place to me).

2. Execute it in child_init. This is done after drop_privileges, so the user/group is 
good. The "problem" here is that
it would execute one rewrite program per child. Right now I'm not sure if it's 
really problem. It could be useful to
have more instances of rewriting program to make its bottleneck lower.

3. Execute it where it is now (post_config), but set user/group using 
apr_procattr_t. So far I think this would
duplicate the code of mod_unixd and would probably have to also handle the 
windows equivalent of that module (if there's
any).

What way do you think is the best, or would you do it differently?

I'm attaching patch for number 2.


I would tend to 2. as well, but as far as I remember using the rewritemap 
program is synchronized across all processes.
This raises two questions:

1. Does rewriting still work with the current patch?


It does work for me. I've done some tests with curl and ab with 
prefork/event/worker MPMs.



2. If it does can stuff be optimized to move from a server wide lock to a 
process wide lock (or even no lock for
prefork) to remove the contention here?


This could be possible, I will look at it.


OTOH looking at the topic of backwards compatibility existing rewrite programs
might rely on not working in parallel. Some may even have an issue if more then 
one copy of them is running in parallel,
albeit not processing stuff in parallel which of course would cause an issue 
with the proposed patch. Furthermore
existing setups might expect to be run as root. But this stuff only needs to be 
considered when we think about
backporting and is moot for trunk.


Right, I'm currently thinking only about trunk. For the 2.4.x, we would 
have to do it differently with backward compatibility in mind. I think 
something like option 1 with configuration directive to enable new 
behaviour would be more acceptable for 2.4.x. We would have single 
rewritemap program in this case running as an apache user only if admin 
wants it.



Regards

Rüdiger



Regards,
Jan Kaluza



Re: Run external RewriteMap program as non-root

2015-03-05 Thread Ruediger Pluem


On 03/05/2015 07:55 AM, Jan Kaluža wrote:
> Hi,
> 
> currently, the External Rewriting Program (RewriteMap "prg:") is run as root. 
> I would like to change it but I see three
> ways how to do it:
> 
> 1. Execute it right after drop_privileges hook. This looks like best way, but 
> I haven't found any hook which could be
> used for that (except drop_privileges with APR_HOOK_REALLY_LAST, which does 
> not seem as proper place to me).
> 
> 2. Execute it in child_init. This is done after drop_privileges, so the 
> user/group is good. The "problem" here is that
> it would execute one rewrite program per child. Right now I'm not sure if 
> it's really problem. It could be useful to
> have more instances of rewriting program to make its bottleneck lower.
> 
> 3. Execute it where it is now (post_config), but set user/group using 
> apr_procattr_t. So far I think this would
> duplicate the code of mod_unixd and would probably have to also handle the 
> windows equivalent of that module (if there's
> any).
> 
> What way do you think is the best, or would you do it differently?
> 
> I'm attaching patch for number 2.

I would tend to 2. as well, but as far as I remember using the rewritemap 
program is synchronized across all processes.
This raises two questions:

1. Does rewriting still work with the current patch?
2. If it does can stuff be optimized to move from a server wide lock to a 
process wide lock (or even no lock for
prefork) to remove the contention here? OTOH looking at the topic of backwards 
compatibility existing rewrite programs
might rely on not working in parallel. Some may even have an issue if more then 
one copy of them is running in parallel,
albeit not processing stuff in parallel which of course would cause an issue 
with the proposed patch. Furthermore
existing setups might expect to be run as root. But this stuff only needs to be 
considered when we think about
backporting and is moot for trunk.

Regards

Rüdiger