mod_userdir.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * mod_userdir... implement the UserDir command. Broken away from the
  18. * Alias stuff for a couple of good and not-so-good reasons:
  19. *
  20. * 1) It shows a real minimal working example of how to do something like
  21. * this.
  22. * 2) I know people who are actually interested in changing this *particular*
  23. * aspect of server functionality without changing the rest of it. That's
  24. * what this whole modular arrangement is supposed to be good at...
  25. *
  26. * Modified by Alexei Kosut to support the following constructs
  27. * (server running at www.foo.com, request for /~bar/one/two.html)
  28. *
  29. * UserDir public_html -> ~bar/public_html/one/two.html
  30. * UserDir /usr/web -> /usr/web/bar/one/two.html
  31. * UserDir /home/ * /www -> /home/bar/www/one/two.html
  32. * NOTE: theses ^ ^ space only added allow it to work in a comment, ignore
  33. * UserDir http://x/users -> (302) http://x/users/bar/one/two.html
  34. * UserDir http://x/ * /y -> (302) http://x/bar/y/one/two.html
  35. * NOTE: here also ^ ^
  36. *
  37. * In addition, you can use multiple entries, to specify alternate
  38. * user directories (a la Directory Index). For example:
  39. *
  40. * UserDir public_html /usr/web http://www.xyz.com/users
  41. *
  42. * Modified by Ken Coar to provide for the following:
  43. *
  44. * UserDir disable[d] username ...
  45. * UserDir enable[d] username ...
  46. *
  47. * If "disabled" has no other arguments, *all* ~<username> references are
  48. * disabled, except those explicitly turned on with the "enabled" keyword.
  49. */
  50. #include "apr_strings.h"
  51. #include "apr_user.h"
  52. #define APR_WANT_STRFUNC
  53. #include "apr_want.h"
  54. #if APR_HAVE_UNISTD_H
  55. #include <unistd.h>
  56. #endif
  57. #include "ap_config.h"
  58. #include "httpd.h"
  59. #include "http_config.h"
  60. #include "http_request.h"
  61. #if !defined(WIN32) && !defined(OS2) && !defined(BEOS) && !defined(NETWARE)
  62. #define HAVE_UNIX_SUEXEC
  63. #endif
  64. #ifdef HAVE_UNIX_SUEXEC
  65. #include "unixd.h" /* Contains the suexec_identity hook used on Unix */
  66. #endif
  67. /*
  68. * The default directory in user's home dir
  69. * In the default install, the module is disabled
  70. */
  71. #ifndef DEFAULT_USER_DIR
  72. #define DEFAULT_USER_DIR NULL
  73. #endif
  74. module AP_MODULE_DECLARE_DATA userdir_module;
  75. typedef struct {
  76. int globally_disabled;
  77. char *userdir;
  78. apr_table_t *enabled_users;
  79. apr_table_t *disabled_users;
  80. } userdir_config;
  81. /*
  82. * Server config for this module: global disablement flag, a list of usernames
  83. * ineligible for UserDir access, a list of those immune to global (but not
  84. * explicit) disablement, and the replacement string for all others.
  85. */
  86. static void *create_userdir_config(apr_pool_t *p, server_rec *s)
  87. {
  88. userdir_config *newcfg = apr_pcalloc(p, sizeof(*newcfg));
  89. newcfg->globally_disabled = 0;
  90. newcfg->userdir = DEFAULT_USER_DIR;
  91. newcfg->enabled_users = apr_table_make(p, 4);
  92. newcfg->disabled_users = apr_table_make(p, 4);
  93. return newcfg;
  94. }
  95. #define O_DEFAULT 0
  96. #define O_ENABLE 1
  97. #define O_DISABLE 2
  98. static const char *set_user_dir(cmd_parms *cmd, void *dummy, const char *arg)
  99. {
  100. userdir_config *s_cfg = ap_get_module_config(cmd->server->module_config,
  101. &userdir_module);
  102. char *username;
  103. const char *usernames = arg;
  104. char *kw = ap_getword_conf(cmd->pool, &usernames);
  105. apr_table_t *usertable;
  106. /* Since we are a raw argument, it is possible for us to be called with
  107. * zero arguments. So that we aren't ambiguous, flat out reject this.
  108. */
  109. if (*kw == '\0') {
  110. return "UserDir requires an argument.";
  111. }
  112. /*
  113. * Let's do the comparisons once.
  114. */
  115. if ((!strcasecmp(kw, "disable")) || (!strcasecmp(kw, "disabled"))) {
  116. /*
  117. * If there are no usernames specified, this is a global disable - we
  118. * need do no more at this point than record the fact.
  119. */
  120. if (strlen(usernames) == 0) {
  121. s_cfg->globally_disabled = 1;
  122. return NULL;
  123. }
  124. usertable = s_cfg->disabled_users;
  125. }
  126. else if ((!strcasecmp(kw, "enable")) || (!strcasecmp(kw, "enabled"))) {
  127. /*
  128. * The "disable" keyword can stand alone or take a list of names, but
  129. * the "enable" keyword requires the list. Whinge if it doesn't have
  130. * it.
  131. */
  132. if (strlen(usernames) == 0) {
  133. return "UserDir \"enable\" keyword requires a list of usernames";
  134. }
  135. usertable = s_cfg->enabled_users;
  136. }
  137. else {
  138. /*
  139. * If the first (only?) value isn't one of our keywords, just copy
  140. * the string to the userdir string.
  141. */
  142. s_cfg->userdir = apr_pstrdup(cmd->pool, arg);
  143. return NULL;
  144. }
  145. /*
  146. * Now we just take each word in turn from the command line and add it to
  147. * the appropriate table.
  148. */
  149. while (*usernames) {
  150. username = ap_getword_conf(cmd->pool, &usernames);
  151. apr_table_setn(usertable, username, kw);
  152. }
  153. return NULL;
  154. }
  155. static const command_rec userdir_cmds[] = {
  156. AP_INIT_RAW_ARGS("UserDir", set_user_dir, NULL, RSRC_CONF,
  157. "the public subdirectory in users' home directories, or "
  158. "'disabled', or 'disabled username username...', or "
  159. "'enabled username username...'"),
  160. {NULL}
  161. };
  162. static int translate_userdir(request_rec *r)
  163. {
  164. ap_conf_vector_t *server_conf;
  165. const userdir_config *s_cfg;
  166. char *name = r->uri;
  167. const char *userdirs;
  168. const char *w, *dname;
  169. char *redirect;
  170. apr_finfo_t statbuf;
  171. /*
  172. * If the URI doesn't match our basic pattern, we've nothing to do with
  173. * it.
  174. */
  175. if (name[0] != '/' || name[1] != '~') {
  176. return DECLINED;
  177. }
  178. server_conf = r->server->module_config;
  179. s_cfg = ap_get_module_config(server_conf, &userdir_module);
  180. userdirs = s_cfg->userdir;
  181. if (userdirs == NULL) {
  182. return DECLINED;
  183. }
  184. dname = name + 2;
  185. w = ap_getword(r->pool, &dname, '/');
  186. /*
  187. * The 'dname' funny business involves backing it up to capture the '/'
  188. * delimiting the "/~user" part from the rest of the URL, in case there
  189. * was one (the case where there wasn't being just "GET /~user HTTP/1.0",
  190. * for which we don't want to tack on a '/' onto the filename).
  191. */
  192. if (dname[-1] == '/') {
  193. --dname;
  194. }
  195. /*
  196. * If there's no username, it's not for us. Ignore . and .. as well.
  197. */
  198. if (w[0] == '\0' || (w[1] == '.' && (w[2] == '\0' || (w[2] == '.' && w[3] == '\0')))) {
  199. return DECLINED;
  200. }
  201. /*
  202. * Nor if there's an username but it's in the disabled list.
  203. */
  204. if (apr_table_get(s_cfg->disabled_users, w) != NULL) {
  205. return DECLINED;
  206. }
  207. /*
  208. * If there's a global interdiction on UserDirs, check to see if this
  209. * name is one of the Blessed.
  210. */
  211. if (s_cfg->globally_disabled
  212. && apr_table_get(s_cfg->enabled_users, w) == NULL) {
  213. return DECLINED;
  214. }
  215. /*
  216. * Special cases all checked, onward to normal substitution processing.
  217. */
  218. while (*userdirs) {
  219. const char *userdir = ap_getword_conf(r->pool, &userdirs);
  220. char *filename = NULL, *x = NULL;
  221. apr_status_t rv;
  222. int is_absolute = ap_os_is_path_absolute(r->pool, userdir);
  223. if (ap_strchr_c(userdir, '*'))
  224. x = ap_getword(r->pool, &userdir, '*');
  225. if (userdir[0] == '\0' || is_absolute) {
  226. if (x) {
  227. #ifdef HAVE_DRIVE_LETTERS
  228. /*
  229. * Crummy hack. Need to figure out whether we have been
  230. * redirected to a URL or to a file on some drive. Since I
  231. * know of no protocols that are a single letter, ignore
  232. * a : as the first or second character, and assume a file
  233. * was specified
  234. */
  235. if (strchr(x + 2, ':'))
  236. #else
  237. if (strchr(x, ':') && !is_absolute)
  238. #endif /* HAVE_DRIVE_LETTERS */
  239. {
  240. redirect = apr_pstrcat(r->pool, x, w, userdir, dname, NULL);
  241. apr_table_setn(r->headers_out, "Location", redirect);
  242. return HTTP_MOVED_TEMPORARILY;
  243. }
  244. else
  245. filename = apr_pstrcat(r->pool, x, w, userdir, NULL);
  246. }
  247. else
  248. filename = apr_pstrcat(r->pool, userdir, "/", w, NULL);
  249. }
  250. else if (x && ap_strchr_c(x, ':')) {
  251. redirect = apr_pstrcat(r->pool, x, w, dname, NULL);
  252. apr_table_setn(r->headers_out, "Location", redirect);
  253. return HTTP_MOVED_TEMPORARILY;
  254. }
  255. else {
  256. #if APR_HAS_USER
  257. char *homedir;
  258. if (apr_uid_homepath_get(&homedir, w, r->pool) == APR_SUCCESS) {
  259. filename = apr_pstrcat(r->pool, homedir, "/", userdir, NULL);
  260. }
  261. #else
  262. return DECLINED;
  263. #endif
  264. }
  265. /*
  266. * Now see if it exists, or we're at the last entry. If we are at the
  267. * last entry, then use the filename generated (if there is one)
  268. * anyway, in the hope that some handler might handle it. This can be
  269. * used, for example, to run a CGI script for the user.
  270. */
  271. if (filename && (!*userdirs
  272. || ((rv = apr_stat(&statbuf, filename, APR_FINFO_MIN,
  273. r->pool)) == APR_SUCCESS
  274. || rv == APR_INCOMPLETE))) {
  275. r->filename = apr_pstrcat(r->pool, filename, dname, NULL);
  276. /* XXX: Does this walk us around FollowSymLink rules?
  277. * When statbuf contains info on r->filename we can save a syscall
  278. * by copying it to r->finfo
  279. */
  280. if (*userdirs && dname[0] == 0)
  281. r->finfo = statbuf;
  282. /* For use in the get_suexec_identity phase */
  283. apr_table_setn(r->notes, "mod_userdir_user", w);
  284. return OK;
  285. }
  286. }
  287. return DECLINED;
  288. }
  289. #ifdef HAVE_UNIX_SUEXEC
  290. static ap_unix_identity_t *get_suexec_id_doer(const request_rec *r)
  291. {
  292. ap_unix_identity_t *ugid = NULL;
  293. #if APR_HAS_USER
  294. const char *username = apr_table_get(r->notes, "mod_userdir_user");
  295. if (username == NULL) {
  296. return NULL;
  297. }
  298. if ((ugid = apr_palloc(r->pool, sizeof(*ugid))) == NULL) {
  299. return NULL;
  300. }
  301. if (apr_uid_get(&ugid->uid, &ugid->gid, username, r->pool) != APR_SUCCESS) {
  302. return NULL;
  303. }
  304. ugid->userdir = 1;
  305. #endif
  306. return ugid;
  307. }
  308. #endif /* HAVE_UNIX_SUEXEC */
  309. static void register_hooks(apr_pool_t *p)
  310. {
  311. static const char * const aszPre[]={ "mod_alias.c",NULL };
  312. static const char * const aszSucc[]={ "mod_vhost_alias.c",NULL };
  313. ap_hook_translate_name(translate_userdir,aszPre,aszSucc,APR_HOOK_MIDDLE);
  314. #ifdef HAVE_UNIX_SUEXEC
  315. ap_hook_get_suexec_identity(get_suexec_id_doer,NULL,NULL,APR_HOOK_FIRST);
  316. #endif
  317. }
  318. module AP_MODULE_DECLARE_DATA userdir_module = {
  319. STANDARD20_MODULE_STUFF,
  320. NULL, /* dir config creater */
  321. NULL, /* dir merger --- default is to override */
  322. create_userdir_config, /* server config */
  323. NULL, /* merge server config */
  324. userdir_cmds, /* command apr_table_t */
  325. register_hooks /* register hooks */
  326. };