├── .github
└── workflows
│ └── build.yaml
├── .gitignore
├── ChangeLog
├── LICENSE
├── Makefile.in
├── README.md
├── _config.yml
├── _layouts
└── default.html
├── buildconf.sh
├── configure.ac
├── m4
├── ax_apache.m4
├── ax_libmemcache.m4
└── ax_libmemcached.m4
├── mod_auth_memcookie.c
├── mod_auth_memcookie.h.in
└── samples
├── httpd.conf
├── login.php
└── login_post.php
/.github/workflows/build.yaml:
--------------------------------------------------------------------------------
1 | name: Build CI
2 |
3 | on: [push, pull_request]
4 |
5 | jobs:
6 | build-noble:
7 | env:
8 | DEBIAN_FRONTEND: noninteractive
9 | TZ: Europe/Paris
10 |
11 | runs-on: ubuntu-latest
12 | container: ubuntu:22.04
13 |
14 | steps:
15 | - uses: actions/checkout@v4
16 | - name: setup os
17 | run: |
18 | apt-get update && apt-get install -y software-properties-common
19 | apt-get -y install wget \
20 | autoconf automake gcc make \
21 | apache2 apache2-dev memcached libmemcached-dev
22 | - name: Apache AuthMemeCookie Module - libmemcache version
23 | run: dpkg -s libmemcached-dev && dpkg -L libmemcached-dev
24 | - name: Apache AuthMemeCookie Module - Apache HTTPD Version
25 | run: dpkg -s apache2 && dpkg -L apache2-dev && dpkg -L apache2
26 | - name: Apache AuthMemeCookie Module - configure
27 | run: |
28 | autoconf -f
29 | ./configure --with-apxs=/usr/bin/apxs --with-libmemcached=/usr || (cat config.log; exit 1)
30 | cat mod_auth_memcookie.h
31 | - name: Apache AuthMemeCookie Module - make
32 | run: make && ls -al .libs/mod_auth_memcookie.so
33 | - name: Apache AuthMemeCookie Module - install
34 | run: make install && apache2ctl configtest
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .libs/*
2 | *.la
3 | *.o
4 | *.slo
5 | *.lo
6 | Makefile
7 | config.log
8 | config.status
9 | autom4te.cache
10 | mod_auth_memcookie.h
11 | configure
12 |
--------------------------------------------------------------------------------
/ChangeLog:
--------------------------------------------------------------------------------
1 | 2007 mcarbonneaux
2 | * mod_authmemcookie/1.0 - initial release
3 |
4 | 2007 mcarbonneaux
5 | * mod_authmemcookie/1.01 - bug fix in cookie name.
6 |
7 | 2008-02-05 mcarbonneaux
8 | * mod_authmemcookie/1.02
9 | * bug fix of Auth_memCookie_SessionTableSize to 8 any time...
10 | * complete rewrite of match ip option to make possible to use it behind reverse proxy and to check x-forwarded or via http header in place of apache remote_ip.
11 |
12 | 2014-11-10 mcarbonneaux
13 | * mod_authmemcookie/1.1.0
14 | * Correct handling of "=" in value of the memcache session (E. Dumas).
15 | * Don't breaks basic authentication (Steve Gaarder)
16 | * multi users/groups require support
17 | * no more memory leak when mc_aget2 return NULL
18 | * apache 2.4 partial support (no use the new security model)
19 | * some portability issue (apr_strtok in place of strtok, and variable definition in front of function)
20 |
21 | 2016-06-18 mcarbonneaux
22 | * mod_authmemcookie/1.2.0
23 | * migration from old libmemcache (https://people.freebsd.org/~seanc/libmemcache/) to modern libmemcached(http://libmemcached.org/)
24 |
25 | 2016-06-19 mcarbonneaux
26 | * mod_authmemcookie/2.0.0
27 | * Full support for apache 2.3/2.4 authz/authn model support
28 | * Fix http header not sending to backend
29 | * Add support for setting prefix http header other than "MCAC_"
30 | * Add public zone support (Require mcac-public only in apache 2.3/2.4)
31 | * Http header name sended to backend in uppercase
32 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/Makefile.in:
--------------------------------------------------------------------------------
1 | MODULE_NAME=mod_auth_memcookie
2 | APXS=@APXS@
3 | LDFLAGS_LIBMEMCACHE=-lmemcached -L@LIBMEMCACHED_LIB_DIR@
4 | CFLAGS_LIBMEMCACHE=-I@LIBMEMCACHED_INCLUDE_DIR@
5 |
6 | all: $(MODULE_NAME).la
7 |
8 | $(MODULE_NAME).la: $(MODULE_NAME).c
9 | $(APXS) $(LDFLAGS_LIBMEMCACHE) $(CFLAGS_LIBMEMCACHE) -c $(MODULE_NAME).c
10 |
11 | install: $(MODULE_NAME).la
12 | $(APXS) -i -n $(MODULE_NAME) $(MODULE_NAME).la
13 |
14 | clean:
15 | -rm -f $(MODULE_NAME).o $(MODULE_NAME).lo $(MODULE_NAME).la $(MODULE_NAME).slo
16 | -rm -rf autom4te.cache .libs
17 |
18 | distclean: clean
19 | -rm -f config.log config.status Makefile mod_auth_memcookie.h configure
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://github.com/ZenProjects/Apache-Authmemcookie-Module/actions/workflows/build.yaml)
2 | [](http://www.gnu.org/licenses/gpl-3.0)
3 |
4 | # What is "Auth MemCookie"?
5 |
6 | `Auth MemCookie` is an Apache v2 Authentication and authorization modules are based on `cookie` Authentication mechanism.
7 |
8 | The module doesn’t make Authentication by it self, but verify if Authentication `the cookie` is valid for each url protected by the module. The module validate also if the `authenticated user` have authorization to access url.
9 |
10 | `Authentication` is made externally by an Authentication `html form page` and all Authentication information necessary to the module a stored in [`memcached`](http://memcached.org/) identified by the cookie value `Authentication session id` by this login page.
11 |
12 | # How it Works
13 |
14 | ## Phase 1 : The login Form
15 |
16 | `Authentication` is made by a login form page.
17 |
18 | This login page must `authenticate` the `user` with any `authenticate source` (ldap, /etc/password, file, database....) accessible to `language` of the `page` (php, perl, java... an ldap login page sample in php are in samples directory).
19 |
20 | Then this page must set `cookie` that contains only a key the `Authentication unique id` of the `Authentication session`.
21 |
22 | The login page must `store` authorization and user information of the authenticated user in [`memcached`](http://memcached.org/) identified by the cookie key `Authentication unique id`.
23 |
24 | The `login page` can be developed in `any language you want`, but must be capable to use [`memcached`](http://memcached.org/) (they must have memcache client api for us)
25 |
26 | ## Phase 2 : The Apache v2 Module
27 |
28 | After the user is logged, the Auth MemCookie module check on each protected page by apache ACL is the user is authenticated.
29 |
30 | When authenticating a request Auth MemCookie module walks through the following steps:
31 |
32 | 1. Get the **`session id`**. The **`session id`** is stored in a cookie (by default named `AuthMemCookie`).
33 |
34 | 2. Get the **`session data`**. Auth MemCookie module fetches **`session data`** by looking up the session id on the [memcached](http://memcached.org/) server.
35 |
36 | 3. Verify the **`remote ip`**. Auth MemCookie module checks the ip address stored in the **`session data`** against the ip address of the current request. This step is optional, and can be disabled by setting the **`Auth_memCookie_MatchIP`** option to no.
37 |
38 | 4. Get username and groups from **`session data`**. The username is stored in the **`UserName`** field in the **`session data`** and the groups the user is a member of is stored in the Groups field.
39 |
40 | 5. Check username and groups against [`Require`](http://httpd.apache.org/docs/2.0/mod/core.html#require) configuration directives.
41 |
42 | If any of the steps 1-4 fails, then Auth MemCookie will return a **`HTTP_UNAUTHORIZED`** (401) Authorization Required error. A **`HTTP_FORBIDDEN`** (403) Forbidden error will be returned if the last step fails.
43 |
44 | When a user is successfully authenticated, Auth MemCookie will store all the fields from the **`session data`** in environment variables accessible to the web page. Every field of the **`session data`** will be send http header **`MCAC_`** to the value of the field.
45 |
46 | # *"Session data"* format stored in memcached
47 |
48 | The **`session data`** stored in [memcached](http://memcached.org/) are composed with multiple line in form of **`name`** equal **`value`** ended by **`\r\n`**. some are mandatory, other are optional and the rest are information only (all this field are transmitted to the script language protect the module).
49 |
50 | **`Session data`** format:
51 |
52 | UserName=\r\n
53 | Groups=::...\r\n
54 | RemoteIP=\r\n
55 | Password=\r\n
56 | Expiration=\r\n
57 | Email=\r\n
58 | Name=\r\n
59 | GivenName=\r\n
60 |
61 | - **Username**: are mandatory.
62 | - **Groups**: are mandatory, are used to check group in apache acl. if no group are know for the user, must be blank (Groups=\r\n)
63 | - **RemoteIP**: are mandatory, used by remote ip check function in apache module.
64 | - **Password**: are not mandatory, and is not recommended to store in memcached for security reson, but if stored, is sent to the script language protected by the module.
65 | - The other fields are information only, but they are sent to the langage that are behind the module (via environement variable or http header).
66 |
67 | The session field size is for the moment limited to 10 fields by default.
68 |
69 | # Build dependency
70 |
71 | You must have compiled and installed:
72 |
73 | - [Apache HTTPD](https://httpd.apache.org/) Apache HTTPD server (**Requires v2.0+**).
74 |
75 | - [libevent](http://libevent.org/) use by [memcached](http://memcached.org/).
76 |
77 | - [memcached](http://memcached.org/) the cache daemon it self.
78 |
79 | - [libmemcached](http://libmemcached.org/) the C client API needed to compile the Apache Module (**require v1.0+**).
80 |
81 | - [autoconf](https://www.gnu.org/software/autoconf/autoconf.html) the gnu autotool to rebuild configure script from `autoconf.ac` and m4 files.
82 |
83 | # Compilation
84 |
85 | ```
86 | # autoconf -f
87 | # ./configure --with-apxs=/path/to/apache/httpd/bin/apxs --with-libmemcached=/path/to/libmemcached_install_dir/
88 | # make
89 | # make install
90 | ```
91 |
92 | - where **--with-libmemcached** is the installation directory of libmemcached (**require v1.0+**) library.
93 |
94 | *Generaly with libmemcached os package you need simply to specify **--with-libmemcached=/usr**. They require generaly to install libmemcached developper package.*
95 | * *debian version wheezy, jessie, stretch, buster, sid have v1.0 packaged.*
96 | * *centos/rhel you need v7 to have libmemcached v1.0 packaged*
97 | * *fedora v25+ have v1.0 packaged*
98 | * *ubuntu trusty, xenial, yakkety, zesty, artful have v1.0 packaged.*
99 |
100 | - where **--with-apxs=** is the path of apache apxs2 build script (**require apache v2.0+**).
101 |
102 | *Generaly with apache httpd os package you simply need to specify **--with-apxs=/usr/bin/apxs** or **--with-apxs=/usr/sbin/apxs** (the default path depend on the distribution, you can find it with `type apxs` shell command). They require generaly to install apache developper package.*
103 |
104 | After that the `mod_auth_memcookie.so` is generated in apache `modules` installation directory.
105 |
106 | # How to configure Apache Module
107 |
108 | ## Module configuration option:
109 |
110 | This option can be used in `location` or `directory` apache context.
111 |
112 | - **Auth_memCookie_Memcached_Configuration**
113 |
114 | This configuration directive permit to configure libmemcached initialisation.
115 | The syntax of this directive value are defined her: [http://docs.libmemcached.org/libmemcached_configuration.html](http://bazaar.launchpad.net/~tangent-trunk/libmemcached/1.0/view/head:/docs/source/libmemcached_configuration.rst)
116 |
117 | With that directive you can specify a liste of ip or host adresse(s) and port `:` separed of memcache(s) daemon to be used.
118 |
119 | For exemple:
120 | ```
121 | Auth_memCookie_Memcached_Configuration "--SERVER=host10.example.com:port1 --SERVER=host11.example.com:port2 --SERVER=host10.example.com:port3"
122 | ```
123 |
124 | To use consistant distribution of hash on multiple memcached server add `--DISTRIBUTION=consistent` and for replication `--NUMBER-OF-REPLICAS=2`. But your login page must use the same algorithm than libmemcached library that by default use the ketama distribution algorithm with md5 hash.
125 |
126 | For example with php you can use [PECL Memcached extension](https://pecl.php.net/package/memcached) that are also based on libmemcached library that support the same consistant distribution algorithm before add server list.
127 |
128 | Sample:
129 | ```php
130 | $memcached=new Memcached();
131 | $memcached->setOption( Memcached::OPT_DISTRIBUTION,Memcached::DISTRIBUTION_CONSISTENT);
132 | ```
133 |
134 | - **Auth_memCookie_Memcached_SessionObject_ExpireTime**
135 |
136 | Session object stored in memcached expiry time, in secondes.
137 |
138 | Used only if `Auth_memCookie_Memcached_SessionObject_ExpiryReset` is set to `on`.
139 |
140 | Set to 3600 seconds by default.
141 |
142 | - **Auth_memCookie_Memcached_SessionObject_ExpiryReset**
143 |
144 | Set to `off` to not reset object expiry time in memcache on each url, is set to `on` by default.
145 |
146 | By default on each request authenticated by the module, the object expiry in memcached of the `session data` are reset to make expiry base on inactivity, if set to `off` the expiry are based on session time length.
147 |
148 | - **Auth_memCookie_SessionTableSize**
149 |
150 | Max number of element in session information table, is set to 10 by default.
151 |
152 | - **Auth_memCookie_SetSessionHTTPHeader**
153 |
154 | Set to `on` to set session information to http header of the authenticated users, is set to `off` by default.
155 | Each session field are sended to backend.
156 | Each session field name are prefixed by `Auth_memCookie_SetSessionHTTPHeaderPrefix` changed to uppercase.
157 |
158 | - **Auth_memCookie_SetSessionHTTPHeaderEncode**
159 |
160 | Set to `on` to mime64 encode session information to http header, is set to `off` by default.
161 |
162 | - **Auth_memCookie_SetSessionHTTPHeaderPrefix**
163 |
164 | Set HTTP header prefix, is set to `MCAC_` by default.
165 |
166 | - **Auth_memCookie_CookieName**
167 |
168 | Name of the cookie to used for check authentification, is set to `AuthMemCookie` by default.
169 |
170 | - **Auth_memCookie_MatchIP_Mode**
171 |
172 | To check cookie ip adresse, Set to `1` to use `X-Forwarded-For` http header, to `2` to use `Via` http header, and to `3` to use apache `remote_ip`, is set to `0` by default to desactivate the ip check.
173 |
174 | - **Auth_memCookie_GroupAuthoritative** (only on apache <2.4)
175 |
176 | Set to `off` to allow access control to be passed along to lower modules, for group acl check, is set to `on` by default.
177 |
178 | - **Auth_memCookie_Authoritative**
179 |
180 | Set to `off` to allow access control to be passed along to lower modules, is set to `on` by default.
181 |
182 | - **Auth_memCookie_SilmulateAuthBasic**
183 |
184 | Set to `off` to not fix `http header` and `auth_type` for simulating `auth basic` for scripting language like `php` authentication framework work, is set to `on` by default.
185 |
186 | with this option this $_SERVER variable are normaly set on php:
187 |
188 | ```
189 | AUTH_TYPE = "basic"
190 | PHP_AUTH_USER = "user"
191 | PHP_AUTH_PW = "password"
192 | ```
193 |
194 | - **Auth_memCookie_DisableNoStore**
195 |
196 | Set to `on` to stop the sending of a `Cache-Control` header set to `no-store` with the login screen. This allows the browser to cache the credentials, but at the risk of it being possible for the login form to be resubmitted and revealed to the backend server through XSS. Use at own risk.
197 |
198 | - **Auth_memCookie_SASLAuth**
199 |
200 | Set to `on` to enable SASL authentication. If this is set, `Auth_memCookie_SASLUsername` and
201 | `Auth_memcookie_SASLPassword` should also be set. Defaults to `off`.
202 |
203 | - **Auth_memCookie_SASLUsername**
204 |
205 | Username to use when authenticating to memcached if `Auth_memCookie_SASLAuth` is `on`.
206 | Defaults to `user`.
207 |
208 | - **Auth_memCookie_SASLPassword**
209 |
210 | Password to use when authenticating to memcached if `Auth_memCookie_SASLAuth` is `on`.
211 | Defaults to `pass`.
212 |
213 | # On the backend application
214 |
215 | The application recieve this information:
216 |
217 | - `REMOTE_USER` are set to the user logged name
218 | - `AUTHMEMCOOKIE_PREFIX` are set to value of `Auth_memCookie_SetSessionHTTPHeaderPrefix`
219 | - `AUTHMEMCOOKIE_AUTH` are set to `yes` when protected, `no` when in public zone.
220 |
221 | And all session field (prefixed by `Auth_memCookie_SetSessionHTTPHeaderPrefix`/`AUTHMEMCOOKIE_PREFIX`) if `Auth_memCookie_SetSessionHTTPHeader` is `on`.
222 |
223 | And if `Auth_memCookie_SilmulateAuthBasic` is set, they recieve also this `$_SERVER` variable :
224 |
225 | ```
226 | AUTH_TYPE = "basic"
227 | PHP_AUTH_USER = "user"
228 | PHP_AUTH_PW = "password"
229 | ```
230 |
231 | # Apache 2.3/2.4 [authn/authz model](https://httpd.apache.org/docs/2.4/howto/auth.html)
232 |
233 | The module add some [`Require`/`authz`](https://httpd.apache.org/docs/2.4/mod/mod_authz_core.html#require) provider:
234 |
235 | - **Require mcac-group**
236 |
237 | To limit access to groups specified in session (`groups` session field) by the login script.
238 | Use the same syntax than [`Require group`](https://httpd.apache.org/docs/2.4/mod/mod_authz_groupfile.html#requiredirectives).
239 | But `Require group` on apache 2.3/2.4 work only with [mod_authz_groupfile](https://httpd.apache.org/docs/2.4/mod/mod_authz_groupfile.html).
240 |
241 | They also support multiple groups like that:
242 | ```
243 | Require mcac-group group1 group2 group3
244 | ```
245 |
246 | If one match on group of the `groups` session field they are granted.
247 |
248 | - **Require mcac-public**
249 |
250 | They make possible to specify public access zone.
251 |
252 | In that zone authenticated or not are granted but authenticated can send session information to backend depend on `Auth_memCookie_SetSessionHTTPHeader` flag.
253 |
254 | ```
255 |
256 | Require mcac-public
257 |
258 | ```
259 |
260 | - **[Require valid-user](https://httpd.apache.org/docs/2.4/mod/mod_authz_user.html#requiredirectives)** and **[Require user](https://httpd.apache.org/docs/2.4/mod/mod_authz_user.html#requiredirectives)**
261 |
262 | All the two a provided by [mod_authz_user](https://httpd.apache.org/docs/2.4/mod/mod_authz_user.html) core apache module.
263 |
264 | ## Sample to configure Apache v2.4 Module:
265 |
266 | Configuration sample for using Auth_memcookie apache V2.4 module:
267 |
268 | ```
269 | LoadModule mod_auth_memcookie_module modules/mod_auth_memcookie.so
270 |
271 |
272 |
273 | Auth_memCookie_CookieName myauthcookie
274 | Auth_memCookie_Memcached_Configuration --SERVER=127.0.0.1:11000
275 |
276 | # to redirect unauthorized user to the login page
277 | ErrorDocument 401 "/gestionuser/login.php"
278 |
279 | # to specify if the module are autoritative in this directory
280 | Auth_memCookie_Authoritative on
281 | # must be set without that the refuse authentification
282 | AuthType Cookie
283 | # must be set (apache mandatory) but not used by the module
284 | AuthName "My Login"
285 | require mcac-public
286 |
287 |
288 |
289 |
290 | # to protect juste user authentification
291 |
292 | require valid-user
293 |
294 |
295 | # to protect acces to user in group1
296 |
297 | require mcac-group group1
298 |
299 | ```
300 |
301 | # Apache 2.0/2.2 [authn/authz model](http://httpd.apache.org/docs/2.2/howto/auth.html)
302 |
303 | - **Require group groupname [groupname]...**
304 |
305 | Only users with the specified groups can access the resource.
306 |
307 | - **Require valid-user**
308 |
309 | Any valid user can access the resource.
310 |
311 | - **Require user user_id [user_id]...**
312 |
313 | Only specified users can access the resource.
314 |
315 | all this directive are from [mod_auth_basic](http://httpd.apache.org/docs/2.2/mod/core.html#require) module.
316 |
317 | ## Sample to configure Apache v2.0 Module:
318 |
319 | Configuration sample for using Auth_memcookie apache V2.0 module:
320 |
321 | ```
322 | LoadModule mod_auth_memcookie_module modules/mod_auth_memcookie.so
323 |
324 |
325 |
326 | Auth_memCookie_CookieName myauthcookie
327 | Auth_memCookie_Memcached_Configuration --SERVER=127.0.0.1:11000
328 |
329 | # to redirect unauthorized user to the login page
330 | ErrorDocument 401 "/gestionuser/login.php"
331 |
332 | # to specify if the module are autoritative in this directory
333 | Auth_memCookie_Authoritative on
334 | # must be set without that the refuse authentification
335 | AuthType Cookie
336 | # must be set (apache mandatory) but not used by the module
337 | AuthName "My Login"
338 |
339 |
340 |
341 |
342 | # to protect juste user authentification
343 |
344 | require valid-user
345 |
346 |
347 | # to protect acces to user in group1
348 |
349 | require group group1
350 |
351 | ```
352 |
353 | # Releases notes
354 |
355 | ### News in v2.0
356 |
357 | * Full support for apache 2.3/2.4 authz/authn model support
358 | * Fix HTTP header not sending to backend
359 | * Add support for setting prefix HTTP header other than `"MCAC_"`
360 | * Add public zone support (`Require mcac-public` only in apache 2.3/2.4)
361 | * HTTP header name sended to backend in uppercase
362 |
363 | ### News in v1.2
364 |
365 | * Migration from old [libmemcache (Sean Chittenden)](https://github.com/richp10/libmemcache-1.4.0.rc2-patched) to modern [libmemcached (Brian Aker)](http://libmemcached.org/)
366 |
367 | ### News in v1.1.1
368 |
369 | * Correct handling of "=" in value of the memcache session (E. Dumas).
370 | * Don't breaks basic authentication (Steve Gaarder)
371 | * Multi users/groups require support
372 | * Fix memory leak when mc_aget2 return NULL
373 | * Apache 2.4 partial support (no use the new security model)
374 | * Fix somme portability issue (apr_strtok in place of strtok, and variable definition in front of function)
375 |
--------------------------------------------------------------------------------
/_config.yml:
--------------------------------------------------------------------------------
1 | title: AuthMemCookie Apache Module
2 | description: ""Auth MemCookie" is an Apache v2 Authentication and authorization modules based on "cookie" Authentication mechanism"
3 | google_analytics: UA-3441258-7
4 | repository_url: https://github.com/ZenProjects/Apache-Authmemcookie-Module
5 | repository_name: AuthMemCookie Apache Module
6 | owner_url: https://github.com/mcarbonneaux
7 | owner_name: mcarbonneaux
8 | show_downloads: true
9 | is_project_page: false
10 | theme: jekyll-theme-cayman
11 | keywords: Apache, module, authentication, authorization, sso, memcached, simplesaml, opensso
12 | gems:
13 | - jekyll-mentions
14 |
--------------------------------------------------------------------------------
/_layouts/default.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {{ page.title | default: site.title }}
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
{{ site.title }}
16 |
{{ site.description }}
17 | View on GitHub
18 | Download .zip
19 | Download .tar.gz
20 |
21 |
22 |
23 | {{ content }}
24 |
25 |
30 |
31 |
32 | {% if site.google_analytics %}
33 |
42 | {% endif %}
43 | {% if site.piwik_analytics %}
44 |
45 |
58 |
59 |
60 | {% endif %}
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/buildconf.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | autoconf -f
3 |
--------------------------------------------------------------------------------
/configure.ac:
--------------------------------------------------------------------------------
1 | m4_include([m4/ax_libmemcached.m4])
2 | m4_include([m4/ax_apache.m4])
3 | AC_INIT
4 | AC_HEADER_STDC
5 | LIBMEMCACHED_WITH
6 | APACHE_WITH
7 | AC_CONFIG_HEADERS(mod_auth_memcookie.h)
8 | AC_OUTPUT(Makefile)
9 |
--------------------------------------------------------------------------------
/m4/ax_apache.m4:
--------------------------------------------------------------------------------
1 | AC_DEFUN([APACHE_WITH],[
2 |
3 | AC_ARG_WITH(
4 | apxs,
5 | [ --with-apxs[=EXE] Apache2 apxs script],
6 | ,
7 | [with_apxs="no"]
8 | )
9 |
10 | AC_MSG_CHECKING(for Apache apxs script)
11 |
12 | if test "$with_apxs" = "no"; then
13 | AC_MSG_ERROR( Specify where is the apache apxs script using --with-apxs)
14 | else
15 | AP_INCLUDE_DIR=$($with_apxs -q "includedir")
16 | # make sure that a well known include file exists
17 | if test -e $AP_INCLUDE_DIR/httpd.h; then
18 | AC_MSG_RESULT( found!)
19 | else
20 | AC_MSG_ERROR( $with_apxs not found. )
21 | fi
22 | APXS=$with_apxs
23 | fi
24 |
25 | AC_SUBST(APXS)
26 |
27 | ])
28 |
29 |
30 |
--------------------------------------------------------------------------------
/m4/ax_libmemcache.m4:
--------------------------------------------------------------------------------
1 | AC_DEFUN([LIBMEMCACHE_WITH],[
2 |
3 | AC_ARG_WITH(
4 | libmemcache,
5 | [ --with-libmemcache[=DIR] libmemcache directory - https://people.freebsd.org/~seanc/libmemcache/],
6 | ,
7 | [with_libmemcache="no"]
8 | )
9 |
10 | AC_MSG_CHECKING(for libmemcache library)
11 |
12 | if test "$with_libmemcache" = "no"; then
13 | AC_MSG_ERROR( Specify where is the libmemcache directory using --with-libmemcache)
14 | else
15 | LIBMEMCACHE_INCLUDE_DIR=$with_libmemcache/include
16 | LIBMEMCACHE_LIB_DIR=$with_libmemcache/lib
17 | # make sure that a well known include file exists
18 | if test -e $LIBMEMCACHE_INCLUDE_DIR/memcache.h && test -d $LIBMEMCACHE_LIB_DIR; then
19 | AC_MSG_RESULT( found!)
20 | else
21 | AC_MSG_ERROR( $with_libmemcache not found. )
22 | fi
23 | LIBMEMCACHE_DIR=$with_libmemcache
24 | fi
25 | AC_SUBST(LIBMEMCACHE_DIR)
26 |
27 | ])
28 |
29 |
30 |
--------------------------------------------------------------------------------
/m4/ax_libmemcached.m4:
--------------------------------------------------------------------------------
1 | AC_DEFUN([LIBMEMCACHED_WITH],[
2 |
3 | AC_ARG_WITH(
4 | libmemcached,
5 | [ --with-libmemcached[=DIR] libmemcached directory - http://libmemcached.org/],
6 | ,
7 | [with_libmemcached="no"]
8 | )
9 |
10 | AC_MSG_CHECKING(for libmemcached library)
11 |
12 | if test "$with_libmemcached" = "no"; then
13 | AC_MSG_ERROR( Specify where is the libmemcached directory using --with-libmemcached)
14 | else
15 | ARCH=$(uname -m)
16 | LONG_BIT=$(getconf LONG_BIT)
17 | LIBDIR=lib
18 | if test "$LONG_BIT" -eq 64; then
19 | if test -d $with_libmemcached/lib64; then
20 | LIBDIR=lib64
21 | fi
22 | fi
23 | LIBMEMCACHED_INCLUDE_DIR=$with_libmemcached/include
24 | LIBMEMCACHED_LIB_DIR=$with_libmemcached/$LIBDIR
25 | # make sure that a well known include file exists
26 | if test -e $LIBMEMCACHED_INCLUDE_DIR/libmemcached-1.0/memcached.h && test -d $LIBMEMCACHED_LIB_DIR; then
27 | AC_MSG_RESULT( found!)
28 | else
29 | AC_MSG_ERROR( $with_libmemcached not found. )
30 | fi
31 | LIBMEMCACHED_DIR=$with_libmemcached
32 | fi
33 | AC_MSG_CHECKING(for libmemcached sasl)
34 | if test "$with_libmemcached" != "no"; then
35 | if test -e $LIBMEMCACHED_INCLUDE_DIR/libmemcached-1.0/sasl.h; then
36 | AC_DEFINE(HAVE_MEMCACHED_SASL, 1, [Have SASL support])
37 | AC_MSG_RESULT(found!)
38 | else
39 | AC_MSG_ERROR(not found.)
40 | fi
41 | fi
42 | AC_SUBST(LIBMEMCACHED_DIR)
43 | AC_SUBST(LIBMEMCACHED_INCLUDE_DIR)
44 | AC_SUBST(LIBMEMCACHED_LIB_DIR)
45 |
46 | ])
47 |
48 |
49 |
--------------------------------------------------------------------------------
/mod_auth_memcookie.c:
--------------------------------------------------------------------------------
1 | /* Copyright 1999-2016 Mathieu CARBONNEAUX
2 | *
3 | * Licensed under the Apache License, Version 2.0 (the "License");
4 | * you may not use this file except in compliance with the License.
5 | * You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software
10 | * distributed under the License is distributed on an "AS IS" BASIS,
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | * See the License for the specific language governing permissions and
13 | * limitations under the License.
14 | */
15 |
16 | /*
17 | * mod_auth_memcookie_module: memcached cookies authentication
18 | *
19 | * Autor: Mathieu CARBONNEAUX
20 | *
21 | */
22 |
23 | #include "mod_auth_memcookie.h"
24 | #include
25 | #include
26 |
27 | #include "apr_strings.h"
28 | #include "apr_lib.h"
29 | #include "apr_base64.h"
30 | #include "apr_md5.h" /* for apr_password_validate */
31 | #include "apr_uuid.h"
32 | #include "apr_tables.h"
33 | #define APR_WANT_STRFUNC
34 | #include "apr_want.h"
35 |
36 | #include "ap_config.h"
37 | #include "ap_provider.h"
38 |
39 | #include "httpd.h"
40 | #include "http_config.h"
41 | #include "http_core.h"
42 | #include "http_log.h"
43 | #include "http_protocol.h"
44 | #include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
45 | #include "util_md5.h"
46 |
47 | #include "mod_auth.h"
48 |
49 | #include "libmemcached-1.0/memcached.h"
50 |
51 | #define LOGTAG_PREFIX "Auth_memCookie: "
52 | #define VERSION "2.0.1"
53 | #define unless(c) if(!(c))
54 |
55 | /* apache module name */
56 | module AP_MODULE_DECLARE_DATA mod_auth_memcookie_module;
57 |
58 | /* config structure */
59 | typedef struct {
60 | char * szAuth_memCookie_memCached_Configuration;
61 | apr_time_t tAuth_memCookie_MemcacheObjectExpiry;
62 | int nAuth_memCookie_MemcacheObjectExpiryReset;
63 |
64 | int nAuth_memCookie_SetSessionHTTPHeader;
65 | int nAuth_memCookie_SetSessionHTTPHeaderEncode;
66 | char * szAuth_memCookie_SetSessionHTTPHeaderPrefix;
67 | int nAuth_memCookie_SessionTableSize;
68 |
69 | char * szAuth_memCookie_CookieName;
70 |
71 | #if MODULE_MAGIC_NUMBER_MAJOR <= 20051115
72 | int nAuth_memCookie_GroupAuthoritative;
73 | #endif
74 | int nAuth_memCookie_Authoritative;
75 | int nAuth_memCookie_MatchIP_Mode;
76 |
77 | int nAuth_memCookie_authbasicfix;
78 | int nAuth_memCookie_disable_no_store;
79 | #ifdef HAVE_MEMCACHED_SASL
80 | int nAuth_memCookie_SASLAuth;
81 | char * szAuth_memCookie_SASLUsername;
82 | char * szAuth_memCookie_SASLPassword;
83 | #endif
84 | } strAuth_memCookie_config_rec;
85 |
86 | /***********************************************************************
87 | *
88 | * extract_cookie
89 | *
90 | * Look through 'Cookie' header for indicated cookie; extract it
91 | * and URL-unescape it. Return the cookie on success, NULL on failure.
92 | *
93 | ***********************************************************************/
94 | static char * extract_cookie(request_rec *r, const char *szCookie_name)
95 | {
96 | char *szRaw_cookie_start=NULL, *szRaw_cookie_end;
97 | char *szCookie;
98 | /* get cookie string */
99 | char*szRaw_cookie = (char*)apr_table_get( r->headers_in, "Cookie");
100 | unless(szRaw_cookie) return 0;
101 |
102 | /* loop to search cookie name in cookie header */
103 | do {
104 | /* search cookie name in cookie string */
105 | unless (szRaw_cookie =strstr(szRaw_cookie, szCookie_name)) return 0;
106 | szRaw_cookie_start=szRaw_cookie;
107 | /* search '=' */
108 | unless (szRaw_cookie = strchr(szRaw_cookie, '=')) return 0;
109 | } while (strncmp(szCookie_name,szRaw_cookie_start,szRaw_cookie-szRaw_cookie_start)!=0);
110 |
111 | /* skip '=' */
112 | szRaw_cookie++;
113 |
114 | /* search end of cookie name value: ';' or end of cookie strings */
115 | unless ((szRaw_cookie_end = strchr(szRaw_cookie, ';')) || (szRaw_cookie_end = strchr(szRaw_cookie, '\0'))) return 0;
116 |
117 | /* dup the value string found in apache pool and set the result pool ptr to szCookie ptr */
118 | unless (szCookie = apr_pstrndup(r->pool, szRaw_cookie, szRaw_cookie_end-szRaw_cookie)) return 0;
119 | /* unescape the value string */
120 | unless (ap_unescape_url(szCookie) == 0) return 0;
121 |
122 | return szCookie;
123 | }
124 |
125 | /************************************************************************************
126 | *
127 | * fix_headers_in
128 | *
129 | * Function to fix any headers in the input request that may be relied on by an
130 | * application. e.g. php uses the Authorization header when logging the request
131 | * in apache and not r->user (like it ought to). It is applied after the request
132 | * has been authenticated.
133 | *
134 | ************************************************************************************/
135 | static void fix_headers_in(request_rec *r,char*szPassword)
136 | {
137 |
138 | char *szUser=NULL;
139 |
140 | /* Set an Authorization header in the input request table for php and
141 | other applications that use it to obtain the username (mainly to fix
142 | apache logging of php scripts). We only set this if there is no header
143 | already present. */
144 |
145 | if (apr_table_get(r->headers_in,"Authorization")==NULL)
146 | {
147 |
148 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "fixing apache Authorization header for this request using user:%s",r->user);
149 |
150 | /* concat username and ':' */
151 | if (szPassword!=NULL) szUser=(char*)apr_pstrcat(r->pool,r->user,":",szPassword,NULL);
152 | else szUser=(char*)apr_pstrcat(r->pool,r->user,":",NULL);
153 |
154 | /* alloc memory for the estimated encode size of the username */
155 | char *szB64_enc_user=(char*)apr_palloc(r->pool,apr_base64_encode_len(strlen(szUser))+1);
156 | unless (szB64_enc_user) {
157 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "memory alloc failed!");
158 | return;
159 | }
160 |
161 | /* encode username in base64 format */
162 | apr_base64_encode(szB64_enc_user,szUser,strlen(szUser));
163 |
164 |
165 | /* set authorization header */
166 | apr_table_set(r->headers_in,"Authorization", (char*)apr_pstrcat(r->pool,"Basic ",szB64_enc_user,NULL));
167 | apr_table_set(r->subprocess_env,"PHP_AUTH_DIGEST_RAW", (char*)apr_pstrcat(r->pool,"Basic ",szB64_enc_user,NULL));
168 | apr_table_set(r->subprocess_env,"HTTP_AUTHORIZATION", (char*)apr_pstrcat(r->pool,"Basic ",szB64_enc_user,NULL));
169 |
170 | /* force auth type to basic */
171 | r->ap_auth_type=apr_pstrdup(r->pool,"Basic");
172 | }
173 |
174 | return;
175 | }
176 |
177 | /***********************************************************************************
178 | *
179 | * Auth_memCookie_get_session
180 | *
181 | * Get session with szCookieValue key from memcached server.
182 | *
183 | ***********************************************************************************/
184 | static apr_table_t *Auth_memCookie_get_session(request_rec *r, strAuth_memCookie_config_rec *conf, char *szCookieValue)
185 | {
186 | char *szMemcached_Configuration=conf->szAuth_memCookie_memCached_Configuration;
187 | apr_time_t tExpireTime=conf->tAuth_memCookie_MemcacheObjectExpiry;
188 |
189 | memcached_st *memc=NULL;
190 | uint32_t flags=0;
191 | memcached_return_t rc;
192 |
193 | apr_table_t *pMySession=NULL;
194 | size_t nGetKeyLen=strlen(szCookieValue);
195 | size_t nGetLen=0;
196 | char *szTokenPos;
197 | char *szFieldTokenPos;
198 | char *szField;
199 | char *szValue;
200 | char *szFieldName;
201 | char *szFieldValue;
202 | char *szMyValue;
203 | int nbInfo=0;
204 |
205 | /* check if libmemcached configuration are set */
206 | unless(conf->szAuth_memCookie_memCached_Configuration) {
207 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "No Auth_memCookie_memCached_Configuration specified");
208 | return NULL;
209 | }
210 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "libmemcached configuration are %s",conf->szAuth_memCookie_memCached_Configuration);
211 |
212 | /* init memcache lib */
213 | unless(memc=memcached(szMemcached_Configuration,strlen(szMemcached_Configuration))) {
214 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "memcache lib init failed");
215 | return NULL;
216 | }
217 |
218 | #ifdef HAVE_MEMCACHED_SASL
219 | /* set sasl auth data */
220 | if(conf->nAuth_memCookie_SASLAuth) {
221 | rc = memcached_set_sasl_auth_data(memc, conf->szAuth_memCookie_SASLUsername, conf->szAuth_memCookie_SASLPassword);
222 | if(rc != MEMCACHED_SUCCESS) {
223 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX
224 | "Failed to set SASL credentials: %s", memcached_last_error_message(memc));
225 | }
226 | }
227 | #endif
228 |
229 | unless(pMySession=apr_table_make(r->pool,conf->nAuth_memCookie_SessionTableSize)) {
230 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOGTAG_PREFIX "apr_tablemake failed");
231 | return NULL;
232 | }
233 |
234 | /* get value for the key 'szCookieValue' in memcached server */
235 | unless(szValue=(char*)memcached_get(memc,szCookieValue,nGetKeyLen,&nGetLen,&flags,&rc)) {
236 | if(rc == MEMCACHED_SUCCESS) {
237 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r, LOGTAG_PREFIX
238 | "memcached_get did not return data for key '%s'",szCookieValue);
239 | } else {
240 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX
241 | "memcached_get call failed: %s", memcached_last_error_message(memc));
242 | }
243 | #ifdef HAVE_MEMCACHED_SASL
244 | /* free sasl auth data */
245 | if(conf->nAuth_memCookie_SASLAuth) {
246 | rc = memcached_destroy_sasl_auth_data(memc);
247 | if(rc != MEMCACHED_SUCCESS) {
248 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX
249 | "Failed to free SASL auth data: %s", memcached_last_error_message(memc));
250 | }
251 | }
252 | #endif
253 | /* free the libmemcached session */
254 | memcached_free(memc);
255 | return NULL;
256 | }
257 |
258 | /* dup szValue in pool */
259 | szMyValue=apr_pstrdup(r->pool,szValue);
260 |
261 | /* split szValue into struct strAuthSession */
262 | /* szValue is formated multi line (\r\n) with name=value on each line */
263 | /* must containe UserName,Groups,RemoteIP fieldname */
264 | szTokenPos=NULL;
265 | for(szField=apr_strtok(szMyValue,"\r\n",&szTokenPos);szField;szField=apr_strtok(NULL,"\r\n",&szTokenPos)) {
266 | szFieldTokenPos=NULL;
267 | szFieldName=apr_strtok(szField,"=",&szFieldTokenPos);
268 | szFieldValue=apr_strtok(NULL,"\r\n",&szFieldTokenPos);
269 | if (szFieldName!=NULL&&szFieldValue!=NULL) {
270 | /* add key and value in pMySession table */
271 | apr_table_set(pMySession,szFieldName,szFieldValue);
272 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "session information '%s'='%s'",szFieldName,szFieldValue);
273 |
274 | /* count the number of element added to table to check table size not reached */
275 | nbInfo++;
276 | if (nbInfo>conf->nAuth_memCookie_SessionTableSize) {
277 | ap_log_rerror(APLOG_MARK,APLOG_ERR|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "maximum session information reached!");
278 | return NULL;
279 | }
280 | }
281 | }
282 |
283 | if (!apr_table_get(pMySession,"UserName")) {
284 | ap_log_rerror(APLOG_MARK,APLOG_ERR|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "Username not found in Session value(key:%s) found = %s",szCookieValue,szValue);
285 | pMySession=NULL;
286 | } else if (conf->nAuth_memCookie_MatchIP_Mode!=0&&!apr_table_get(pMySession,"RemoteIP")) {
287 | ap_log_rerror(APLOG_MARK,APLOG_ERR|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "MatchIP_Mode activated and RemoteIP not found in Session value(key:%s) found = %s",szCookieValue,szValue);
288 | pMySession=NULL;
289 | } else {
290 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "Value for Session (key:%s) found => Username=%s Groups=%s RemoteIp=%s",
291 | szCookieValue,
292 | apr_table_get(pMySession,"UserName"),
293 | apr_table_get(pMySession,"Groups"),
294 | apr_table_get(pMySession,"RemoteIP"));
295 | }
296 |
297 | /* reset expire time in memcached */
298 | if (conf->nAuth_memCookie_MemcacheObjectExpiryReset&&pMySession) {
299 | if ((rc=memcached_set(memc,szCookieValue,nGetKeyLen,szValue,nGetLen,tExpireTime,flags))) {
300 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r,LOGTAG_PREFIX "Expire time with memcached_set (key:%s) failed with errcode=%s",szCookieValue,memcached_last_error_message(memc));
301 | pMySession=NULL;
302 | }
303 | }
304 |
305 | /* free memcached_get retruned valued */
306 | if (!szValue) free(szValue);
307 |
308 | #ifdef HAVE_MEMCACHED_SASL
309 | /* free sasl auth data */
310 | if(conf->nAuth_memCookie_SASLAuth) {
311 | rc = memcached_destroy_sasl_auth_data(memc);
312 | if(rc != MEMCACHED_SUCCESS) {
313 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX
314 | "Failed to free SASL auth data: %s", memcached_last_error_message(memc));
315 | }
316 | }
317 | #endif
318 | /* free the libmemcached session */
319 | memcached_free(memc);
320 |
321 | /* set the good username found in request structure */
322 | if (pMySession!=NULL&&apr_table_get(pMySession,"UserName")!=NULL) r->user=(char*)apr_table_get(pMySession,"UserName");
323 |
324 | return pMySession;
325 | }
326 |
327 | /**************************************
328 | *
329 | * get_Auth_memCookie_grp
330 | *
331 | * check if szGroup are in szGroups.
332 | *
333 | **************************************/
334 | static int get_Auth_memCookie_grp(request_rec *r, const char *szGroup, const char *szGroups)
335 | {
336 | char *szMyGroups;
337 | char *szMyGroup;
338 |
339 | /* Add delimiters at start and end of groups string */
340 | /* and search group with delimiters */
341 | szMyGroups=apr_pstrcat(r->pool,":",szGroups,":",NULL);
342 | szMyGroup=apr_pstrcat(r->pool,":",szGroup,":",NULL);
343 |
344 | if (!strstr(szMyGroups,szMyGroup))
345 | return DECLINED;
346 |
347 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "group found=%s", szGroup);
348 | return OK;
349 | }
350 |
351 | char* strupr(char* s)
352 | {
353 | char* tmp = s;
354 |
355 | for (;*tmp;++tmp) {
356 | *tmp = toupper((unsigned char) *tmp);
357 | }
358 |
359 | return s;
360 | }
361 |
362 | /***************************************************************
363 | *
364 | * Auth_memCookie_DoSetHeader
365 | *
366 | * user apr_table_do to set session information in header http.
367 | *
368 | **************************************************************/
369 | static int Auth_memCookie_DoSetHeader(void*rec,const char *szKey, const char *szValue)
370 | {
371 | strAuth_memCookie_config_rec *conf=NULL;
372 | request_rec *r=(request_rec*)rec;
373 | char *szB64_enc_string=NULL;
374 |
375 | /* get apache config */
376 | conf = ap_get_module_config(r->per_dir_config, &mod_auth_memcookie_module);
377 |
378 | /* prefix each variable with "szAuth_memCookie_SetSessionHTTPHeaderPrefix" (by default MCAC_) */
379 | char*szHeaderName=apr_pstrcat(r->pool,conf->szAuth_memCookie_SetSessionHTTPHeaderPrefix,szKey,NULL);
380 | strupr(szHeaderName);
381 |
382 | if (conf->nAuth_memCookie_SetSessionHTTPHeaderEncode) {
383 | /* alloc memory for the estimated encode size of the string */
384 | szB64_enc_string=(char*)apr_palloc(r->pool,apr_base64_encode_len(strlen(szValue))+1);
385 | unless (szB64_enc_string) {
386 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "memory alloc for encoding http header failed!");
387 | return 0;
388 | }
389 |
390 | /* encode string in base64 format */
391 | apr_base64_encode(szB64_enc_string,szValue,strlen(szValue));
392 |
393 | /* set string header */
394 | apr_table_setn(r->subprocess_env,szHeaderName,(char*)szB64_enc_string);
395 | apr_table_setn(r->headers_in,szHeaderName,(char*)szB64_enc_string);
396 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "Send HTTP Header %s=%s", szHeaderName,szB64_enc_string);
397 | }
398 | else
399 | {
400 | /* set string header */
401 | apr_table_setn(r->subprocess_env,szHeaderName,(char*)szValue);
402 | apr_table_setn(r->headers_in,szHeaderName,(char*)szValue);
403 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "Send HTTP Header %s=%s", szHeaderName,szValue);
404 | }
405 | return 1;
406 | }
407 |
408 | /****************************************************************************
409 | *
410 | * Auth_memCookie_Return_Safe_Unauthorized
411 | *
412 | * potential security issue: if we return a login to the browser, we must
413 | * send a no-store to make sure a well behaved browser will not try and
414 | * send the login details a second time if the back button is pressed.
415 | *
416 | * if the user has full control over the backend, the
417 | * AuthCookieDisableNoStore can be used to turn this off.
418 | *
419 | ****************************************************************************/
420 | static int Auth_memCookie_Return_Safe_Unauthorized(request_rec *r)
421 | {
422 | strAuth_memCookie_config_rec *conf=NULL;
423 |
424 | /* get mod_auth_memcookie_module apache config */
425 | conf = ap_get_module_config(r->per_dir_config, &mod_auth_memcookie_module);
426 |
427 | if (!conf->nAuth_memCookie_disable_no_store) {
428 | apr_table_addn(r->headers_out, "Cache-Control", "no-store");
429 | apr_table_addn(r->err_headers_out, "Cache-Control", "no-store");
430 | }
431 |
432 | return HTTP_UNAUTHORIZED;
433 | }
434 |
435 | /******************************************************************************
436 | *
437 | * Auth_memCookie_check_cookie
438 | *
439 | * This is the Authentication phase (authn), they verify
440 | * if authentification cookie is set and if is know in memcache server.
441 | *
442 | * If the login is not valid, a 401 Not Authorized will be returned.
443 | *
444 | * It is up to the webmaster to ensure this screen displays a suitable login
445 | * form to give the user the opportunity to log in.
446 | *****************************************************************************/
447 | static int Auth_memCookie_check_cookie(request_rec *r)
448 | {
449 | strAuth_memCookie_config_rec *conf=NULL;
450 | char *szCookieValue=NULL;
451 | apr_table_t *pAuthSession=NULL;
452 | apr_status_t tRetStatus;
453 | char *szRemoteIP=NULL;
454 | const char *current_auth = NULL;
455 |
456 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "ap_hook_check_user_id in");
457 |
458 | /* get mod_auth_memcookie_module apache config */
459 | conf = ap_get_module_config(r->per_dir_config, &mod_auth_memcookie_module);
460 |
461 | /* check if module are authoritative */
462 | unless(conf->nAuth_memCookie_Authoritative)
463 | return DECLINED;
464 |
465 | /* set szRemoteIP in case of conf->nAuth_memCookie_MatchIP_Mode value */
466 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "check MatchIP_Mode:%d",conf->nAuth_memCookie_MatchIP_Mode);
467 | if (conf->nAuth_memCookie_MatchIP_Mode==2&&apr_table_get(r->headers_in,"Via")!=NULL)
468 | szRemoteIP=apr_pstrdup(r->pool,apr_table_get(r->headers_in,"Via"));
469 | else if (conf->nAuth_memCookie_MatchIP_Mode==1&&apr_table_get(r->headers_in,"X-Forwarded-For")!=NULL)
470 | szRemoteIP=apr_pstrdup(r->pool,apr_table_get(r->headers_in,"X-Forwarded-For"));
471 | else
472 | #if MODULE_MAGIC_NUMBER_MAJOR > 20051115
473 | szRemoteIP=apr_pstrdup(r->pool,r->useragent_ip);
474 | #else
475 | szRemoteIP=apr_pstrdup(r->pool,r->connection->remote_ip);
476 | #endif
477 |
478 |
479 | /*
480 | * XSS security warning: using cookies to store private data only works
481 | * when the administrator has full control over the source website. When
482 | * in forward-proxy mode, websites are public by definition, and so can
483 | * never be secure. Abort the auth attempt in this case.
484 | */
485 | if (PROXYREQ_PROXY == r->proxyreq) {
486 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOGTAG_PREFIX
487 | "mcac auth cannot be used for proxy "
488 | "requests due to XSS risk, access denied: %s", r->uri);
489 | return HTTP_INTERNAL_SERVER_ERROR;
490 | }
491 |
492 | /* We need an authentication realm. */
493 | if (!ap_auth_name(r)) {
494 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOGTAG_PREFIX
495 | "need AuthName: %s", r->uri);
496 | return HTTP_INTERNAL_SERVER_ERROR;
497 | }
498 |
499 | /* check auth type if they are setted to "Cookie" */
500 | current_auth = ap_auth_type(r);
501 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "AuthType are '%s'", current_auth);
502 | unless(strncasecmp("cookie",current_auth,6)==0) {
503 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "Auth type not specified as 'Cookie'");
504 | return DECLINED; //IIG: Allow basic auth to be set
505 | }
506 |
507 | /* check if the cookie name are set */
508 | unless(conf->szAuth_memCookie_CookieName) {
509 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "No Auth_memCookie_CookieName specified");
510 | return Auth_memCookie_Return_Safe_Unauthorized(r);
511 | }
512 |
513 | /* get cookie named "szAuth_memCookie_CookieName" */
514 | unless(szCookieValue = extract_cookie(r, conf->szAuth_memCookie_CookieName))
515 | {
516 | ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "cookie not found! not authorized! RemoteIP:%s",szRemoteIP);
517 | return Auth_memCookie_Return_Safe_Unauthorized(r);
518 | }
519 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "got cookie; value is %s", szCookieValue);
520 |
521 | /* get session name "szCookieValue" from memcached */
522 | if((pAuthSession = Auth_memCookie_get_session(r, conf, szCookieValue))==NULL) {
523 | ap_log_rerror(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "AuthSession %s not found: %s", szCookieValue, r->filename);
524 | return Auth_memCookie_Return_Safe_Unauthorized(r);
525 | }
526 |
527 | /* push session returned structure in request pool */
528 | if ((tRetStatus=apr_pool_userdata_setn(pAuthSession,"SESSION",NULL,r->pool))) {
529 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "apr_pool_userdata_setn Apr Error: %d", tRetStatus);
530 | return Auth_memCookie_Return_Safe_Unauthorized(r);
531 | }
532 |
533 | /* check remote ip if option is enabled */
534 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "check ip: remote_ip=%s cookie_ip=%s", szRemoteIP ,apr_table_get(pAuthSession,"RemoteIP"));
535 | if (conf->nAuth_memCookie_MatchIP_Mode!=0) {
536 | if (strcmp(szRemoteIP ,apr_table_get(pAuthSession,"RemoteIP"))) {
537 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "unauthorized, by ip. user:%s remote_ip:%s != cookie_ip:%s",
538 | apr_table_get(pAuthSession,"UserName"),szRemoteIP ,apr_table_get(pAuthSession,"RemoteIP"));
539 | return Auth_memCookie_Return_Safe_Unauthorized(r);
540 | }
541 | }
542 |
543 | /* send http header of the session value to the backend */
544 | if (conf->nAuth_memCookie_SetSessionHTTPHeader) {
545 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "nAuth_memCookie_SetSessionHTTPHeader is set then send http header...");
546 | apr_table_do(Auth_memCookie_DoSetHeader,r,pAuthSession,NULL);
547 | }
548 |
549 | /* set MCAC_SESSIONKEY var for scripts language */
550 | apr_table_setn(r->subprocess_env, apr_pstrcat(r->pool,conf->szAuth_memCookie_SetSessionHTTPHeaderPrefix,"SESSIONKEY",NULL),szCookieValue);
551 | apr_table_setn(r->headers_in, apr_pstrcat(r->pool,conf->szAuth_memCookie_SetSessionHTTPHeaderPrefix,"SESSIONKEY",NULL),szCookieValue);
552 |
553 | /* HTTP Header Prefix */
554 | apr_table_setn(r->subprocess_env,"AUTHMEMCOOKIE_PREFIX",conf->szAuth_memCookie_SetSessionHTTPHeaderPrefix);
555 | apr_table_setn(r->headers_in,"AUTHMEMCOOKIE_PREFIX",conf->szAuth_memCookie_SetSessionHTTPHeaderPrefix);
556 |
557 | /* cookie found the user is authentified */
558 | apr_table_setn(r->subprocess_env,"AUTHMEMCOOKIE_AUTH","yes");
559 | apr_table_setn(r->headers_in,"AUTHMEMCOOKIE_AUTH","yes");
560 |
561 | /* set REMOTE_USER var for scripts language */
562 | apr_table_setn(r->subprocess_env,"REMOTE_USER",apr_table_get(pAuthSession,"UserName"));
563 | apr_table_setn(r->headers_in,"REMOTE_USER",apr_table_get(pAuthSession,"UserName"));
564 |
565 | /* log authorisation ok */
566 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "authentication ok");
567 |
568 | /* fix http header for php */
569 | if (conf->nAuth_memCookie_authbasicfix) fix_headers_in(r,(char*)apr_table_get(pAuthSession,"Password"));
570 |
571 | /* if all is ok return auth ok */
572 | return OK;
573 | }
574 |
575 | #if MODULE_MAGIC_NUMBER_MAJOR > 20051115
576 |
577 | static authz_status Auth_memCookie_public_authz_checker(request_rec *r, const char *require_args, const void *parsed_require_args) {
578 |
579 | strAuth_memCookie_config_rec *conf=NULL;
580 | char *szMyUser=r->user;
581 | char *szCookieValue=NULL;
582 |
583 | apr_table_t *pAuthSession=NULL;
584 |
585 | authz_status nReturn=AUTHZ_GRANTED;
586 |
587 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "Auth_memCookie_public_authz_checker in");
588 | if (!szMyUser) {
589 |
590 | /* get apache config */
591 | conf = ap_get_module_config(r->per_dir_config, &mod_auth_memcookie_module);
592 |
593 | /* check if the cookie name are set */
594 | unless(conf->szAuth_memCookie_CookieName) {
595 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "No Auth_memCookie_CookieName specified");
596 | return AUTHZ_GENERAL_ERROR;
597 | }
598 |
599 | /* get cookie named "szAuth_memCookie_CookieName" */
600 | unless(szCookieValue = extract_cookie(r, conf->szAuth_memCookie_CookieName))
601 | {
602 | ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "cookie not found, continue !");
603 | return nReturn;
604 | }
605 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "got cookie; value is %s", szCookieValue);
606 |
607 | /* get session name "szCookieValue" from memcached */
608 | if((pAuthSession = Auth_memCookie_get_session(r, conf, szCookieValue))==NULL) {
609 | ap_log_rerror(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "AuthSession %s not found: %s", szCookieValue, r->filename);
610 | return nReturn;
611 | }
612 |
613 | /* send http header of the session value to the backend */
614 | if (conf->nAuth_memCookie_SetSessionHTTPHeader) {
615 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, LOGTAG_PREFIX "nAuth_memCookie_SetSessionHTTPHeader is set then send http header...");
616 | apr_table_do(Auth_memCookie_DoSetHeader,r,pAuthSession,NULL);
617 | }
618 |
619 | /* set MCAC_SESSIONKEY var for scripts language */
620 | apr_table_setn(r->subprocess_env, apr_pstrcat(r->pool,conf->szAuth_memCookie_SetSessionHTTPHeaderPrefix,"SESSIONKEY",NULL),szCookieValue);
621 | apr_table_setn(r->headers_in, apr_pstrcat(r->pool,conf->szAuth_memCookie_SetSessionHTTPHeaderPrefix,"SESSIONKEY",NULL),szCookieValue);
622 |
623 | /* HTTP Header Prefix */
624 | apr_table_setn(r->subprocess_env,"AUTHMEMCOOKIE_PREFIX",conf->szAuth_memCookie_SetSessionHTTPHeaderPrefix);
625 | apr_table_setn(r->headers_in,"AUTHMEMCOOKIE_PREFIX",conf->szAuth_memCookie_SetSessionHTTPHeaderPrefix);
626 |
627 | /* cookie found but they are in public zone */
628 | apr_table_setn(r->subprocess_env,"AUTHMEMCOOKIE_AUTH","no");
629 | apr_table_setn(r->headers_in,"AUTHMEMCOOKIE_AUTH","no");
630 |
631 | }
632 | return nReturn;
633 | }
634 | /**************************************************
635 | *
636 | * Auth_memCookie_check_auth
637 | *
638 | * Authorization phase (authz) in apache >2.3 :
639 | * Checking authoriszation group for the authenticated cookie
640 | **************************************************/
641 |
642 | static authz_status Auth_memCookie_group_authz_checker(request_rec *r, const char *require_args, const void *parsed_require_args) {
643 |
644 | char *szMyUser=r->user;
645 | //int m = r->method_number;
646 |
647 | const char *err = NULL;
648 |
649 | const ap_expr_info_t *expr = parsed_require_args;
650 | const char *require;
651 |
652 | const char *szGroups;
653 | const char *szGroup;
654 | const char *t;
655 |
656 | apr_table_t *pAuthSession=NULL;
657 | apr_status_t tRetStatus;
658 |
659 |
660 | if (!szMyUser) {
661 | return AUTHZ_DENIED_NO_USER;
662 | }
663 |
664 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "Auth_memCookie_group_authz_checker in");
665 |
666 | // no cookie session validated go denied
667 | if((tRetStatus=apr_pool_userdata_get((void**)&pAuthSession,"SESSION",r->pool))) {
668 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r,LOGTAG_PREFIX "apr_pool_userdata_get Apr Error: %d", tRetStatus);
669 | return AUTHZ_DENIED;
670 | }
671 |
672 | szGroups=apr_table_get(pAuthSession,"Groups");
673 | if (szGroups==NULL) {
674 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOGTAG_PREFIX
675 | "authorization denied: no group asociated to the session");
676 | return AUTHZ_DENIED;
677 | }
678 |
679 | require = ap_expr_str_exec(r, expr, &err);
680 | if (err) {
681 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOGTAG_PREFIX
682 | "authorization denied: Can't evaluate require expression: %s", err);
683 | return AUTHZ_DENIED;
684 | }
685 |
686 | t = require;
687 | while ((szGroup = ap_getword_conf(r->pool, &t)) && szGroup[0]) {
688 | ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r ,LOGTAG_PREFIX "check group '%s' in '%s'",szGroup,szGroups);
689 | if (get_Auth_memCookie_grp(r,szGroup, szGroups)==OK) {
690 | ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r ,LOGTAG_PREFIX "the user logged '%s' as the good group %s and is authorized",szMyUser,szGroup);
691 | return AUTHZ_GRANTED;
692 | }
693 | }
694 |
695 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOGTAG_PREFIX
696 | "Authorization of user %s to access %s failed, reason: "
697 | "user is not part of the 'require'ed group(s).",
698 | r->user, r->uri);
699 |
700 | return AUTHZ_DENIED;
701 | }
702 |
703 | #else
704 |
705 | /**************************************************
706 | *
707 | * Auth_memCookie_check_auth
708 | *
709 | * Authorization phase in apache 2.0 to 2.2 :
710 | * Checking authoriszation for user and group of the authenticated cookie
711 | **************************************************/
712 |
713 | static int Auth_memCookie_check_auth(request_rec *r)
714 | {
715 | strAuth_memCookie_config_rec *conf=NULL;
716 | char *szMyUser=r->user;
717 | char *szUser;
718 | int m = r->method_number;
719 |
720 | const apr_array_header_t *reqs_arr=NULL;
721 | require_line *reqs=NULL;
722 |
723 | register int x;
724 | const char *szRequireLine;
725 | char *szRequire_cmd;
726 | const char *szGroup;
727 | const char *szGroups;
728 |
729 | apr_table_t *pAuthSession=NULL;
730 | apr_status_t tRetStatus;
731 |
732 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "Auth_memCookie_check_auth in");
733 |
734 | /* get apache config */
735 | conf = ap_get_module_config(r->per_dir_config, &mod_auth_memcookie_module);
736 |
737 | /* check if module are authoritative */
738 | unless(conf->nAuth_memCookie_Authoritative)
739 | return DECLINED;
740 |
741 | /* check if module are authoritative in group check */
742 | unless(conf->nAuth_memCookie_GroupAuthoritative)
743 | return DECLINED;
744 |
745 | // no cookie session validated go in next auth check stage (DECLINED)
746 | if((tRetStatus=apr_pool_userdata_get((void**)&pAuthSession,"SESSION",r->pool))) {
747 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r,LOGTAG_PREFIX "apr_pool_userdata_get Apr Error: %d", tRetStatus);
748 | return DECLINED;
749 | }
750 |
751 | /* get require line */
752 | reqs_arr = ap_requires(r);
753 | reqs = reqs_arr ? (require_line *) reqs_arr->elts : NULL;
754 |
755 | /* decline if no require line found */
756 | if (!reqs_arr) return DECLINED;
757 |
758 | /* walk throug the array to check eatch require command */
759 | for (x = 0; x < reqs_arr->nelts; x++) {
760 |
761 | // skip require elt if request method 'm' not match the require method_mask
762 | if (!(reqs[x].method_mask & (AP_METHOD_BIT << m)))
763 | continue;
764 |
765 | /* get require line */
766 | szRequireLine = reqs[x].requirement;
767 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "Require Line is '%s'", szRequireLine);
768 |
769 | /* get the first word in require line */
770 | szRequire_cmd = ap_getword_white(r->pool, &szRequireLine);
771 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "Require Cmd is '%s'", szRequire_cmd);
772 |
773 | if (szRequire_cmd) {
774 | /* if require cmd are valid-user, they are already authenticated (session cookie found) then allow and return OK */
775 | if (!strcmp("valid-user",szRequire_cmd)) {
776 | ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO, 0,r,LOGTAG_PREFIX "Require Cmd valid-user");
777 | return OK;
778 | } else if (!strcmp("user",szRequire_cmd)) { /* check the required users */
779 | szUser=NULL;
780 | while (*szRequireLine && (szUser = ap_getword_conf(r->pool, &szRequireLine))) {
781 | if (szUser==NULL) {
782 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r ,LOGTAG_PREFIX "user %s not in user", szMyUser);
783 | continue;
784 | }
785 | ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r ,LOGTAG_PREFIX "check user '%s' vs '%s'",szUser,szMyUser);
786 | if (!strcmp(szMyUser, szUser)) {
787 | ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r ,LOGTAG_PREFIX "the user logged '%s' is authorized",szMyUser);
788 | return OK;
789 | }
790 | }
791 | } else if ((!strcmp("group",szRequire_cmd))||(!strcmp("mcac-group",szRequire_cmd))) { /* check the required groups */
792 | szGroups=apr_table_get(pAuthSession,"Groups");
793 | if (szGroups==NULL) {
794 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOGTAG_PREFIX
795 | "authorization denied: no group asociated to the session");
796 | return AUTH_DENIED;
797 | }
798 | while(*szRequireLine && (szGroup = ap_getword_white(r->pool, &szRequireLine))) {
799 | ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r ,LOGTAG_PREFIX "check group '%s' in '%s'",szGroup,szGroups);
800 | if (get_Auth_memCookie_grp(r, szGroup, szGroups)==OK) {
801 | ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r ,LOGTAG_PREFIX "the user logged '%s' as the good group %s and is authorized",szMyUser,szGroup);
802 | return OK;
803 | }
804 | }
805 | }
806 | }
807 | }
808 |
809 | ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r ,LOGTAG_PREFIX "the user logged '%s' not authorized",szMyUser);
810 | /* forbid by default */
811 | return HTTP_FORBIDDEN;
812 | }
813 |
814 | #endif
815 |
816 | #if MODULE_MAGIC_NUMBER_MAJOR > 20051115
817 |
818 | static const char *Auth_memCookie_authz_parse_config( cmd_parms *cmd, const char *require_line, const void **parsed_require_line)
819 | {
820 | const char *expr_err = NULL;
821 | ap_expr_info_t *expr;
822 |
823 | expr = ap_expr_parse_cmd(cmd, require_line, AP_EXPR_FLAG_STRING_RESULT,
824 | &expr_err, NULL);
825 |
826 | if (expr_err)
827 | return apr_pstrcat(cmd->temp_pool,
828 | "Cannot parse expression in require line: ",
829 | expr_err, NULL);
830 |
831 | *parsed_require_line = expr;
832 |
833 | return NULL;
834 | }
835 |
836 | static const authz_provider Auth_memCookie_authz_public_provider = {
837 | &Auth_memCookie_public_authz_checker,
838 | NULL,
839 | };
840 |
841 | static const authz_provider Auth_memCookie_authz_group_provider = {
842 | &Auth_memCookie_group_authz_checker,
843 | &Auth_memCookie_authz_parse_config,
844 | };
845 |
846 | #endif
847 |
848 | /**************************************************
849 | * register module hook
850 | **************************************************/
851 | static void register_hooks(apr_pool_t *p)
852 | {
853 | // Authz refactoring are done in 20060110 with 2.3.0 creation
854 | // https://github.com/apache/httpd/commit/ce3c76283dd74148c631030116a7b74dbaf18ba5
855 | // apache >=2.3 model
856 | #if MODULE_MAGIC_NUMBER_MAJOR > 20051115
857 | ap_hook_check_authn(Auth_memCookie_check_cookie, NULL, NULL, APR_HOOK_FIRST, AP_AUTH_INTERNAL_PER_CONF);
858 |
859 | ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "mcac-group",
860 | AUTHZ_PROVIDER_VERSION,
861 | &Auth_memCookie_authz_group_provider,
862 | AP_AUTH_INTERNAL_PER_CONF);
863 |
864 | ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "mcac-public",
865 | AUTHZ_PROVIDER_VERSION,
866 | &Auth_memCookie_authz_public_provider,
867 | AP_AUTH_INTERNAL_PER_CONF);
868 |
869 | #else
870 | // apache 2.0 to 2.2 model
871 | ap_hook_check_user_id(Auth_memCookie_check_cookie, NULL, NULL, APR_HOOK_FIRST);
872 | ap_hook_auth_checker(Auth_memCookie_check_auth, NULL, NULL, APR_HOOK_FIRST);
873 | #endif
874 | }
875 |
876 | /************************************************************************************
877 | * Apache CONFIG Phase:
878 | ************************************************************************************/
879 | static void *create_Auth_memCookie_dir_config(apr_pool_t *p, char *d)
880 | {
881 | strAuth_memCookie_config_rec *conf = apr_palloc(p, sizeof(*conf));
882 |
883 | conf->szAuth_memCookie_memCached_Configuration = apr_pstrdup(p,"--SERVER=127.0.0.1:11211");
884 | conf->szAuth_memCookie_CookieName = apr_pstrdup(p,"AuthMemCookie");
885 | conf->tAuth_memCookie_MemcacheObjectExpiry = 3600; /* memcache object expire time, 1H by default */
886 | conf->nAuth_memCookie_MemcacheObjectExpiryReset = 1; /* fortress is secure by default, reset object expire time in memcache by default */
887 | conf->nAuth_memCookie_MatchIP_Mode = 0; /* method used in matchip, use (0) remote ip by default, if set to 1 for use ip from x_forwarded_for http header and 2 for use Via http header */
888 | #if MODULE_MAGIC_NUMBER_MAJOR <= 20051115
889 | conf->nAuth_memCookie_GroupAuthoritative = 1; /* group are handled by this module by default */
890 | #endif
891 | conf->nAuth_memCookie_Authoritative = 1; /* is set by default */
892 | conf->nAuth_memCookie_authbasicfix = 1; /* fix header for php auth by default */
893 | conf->nAuth_memCookie_SetSessionHTTPHeader = 0; /* set session information in http header of authenticated user */
894 | conf->nAuth_memCookie_SetSessionHTTPHeaderEncode = 0; /* encode http header groups value by default */
895 | conf->szAuth_memCookie_SetSessionHTTPHeaderPrefix = apr_pstrdup(p,"MCAC_");
896 | conf->nAuth_memCookie_SessionTableSize=10; /* Max number of element in session information table, 10 by default */
897 | conf->nAuth_memCookie_disable_no_store=0; /* no store cache option are the default */
898 | #ifdef HAVE_MEMCACHED_SASL
899 | conf->nAuth_memCookie_SASLAuth = 0; /* Disabled by default */
900 | conf->szAuth_memCookie_SASLUsername = apr_pstrdup(p, "user");
901 | conf->szAuth_memCookie_SASLPassword = apr_pstrdup(p, "pass");
902 | #endif
903 |
904 | return conf;
905 | }
906 |
907 | static const char *cmd_MatchIP_Mode(cmd_parms *cmd, void *InDirConf, const char *p1) {
908 | strAuth_memCookie_config_rec *conf=(strAuth_memCookie_config_rec*)InDirConf;
909 |
910 | if ((strcasecmp("1",p1)==0) || (strcasecmp("X-Forwarded-For",p1)==0))
911 | {
912 | conf->nAuth_memCookie_MatchIP_Mode=1;
913 | }
914 | else if ((strcasecmp("2",p1)==0) || (strcasecmp("Via",p1)==0))
915 | {
916 | conf->nAuth_memCookie_MatchIP_Mode=2;
917 | }
918 | else if ((strcasecmp("3",p1)==0) || (strcasecmp("RemoteIP",p1)==0))
919 | {
920 | conf->nAuth_memCookie_MatchIP_Mode=3;
921 | }
922 | else
923 | {
924 | conf->nAuth_memCookie_MatchIP_Mode=0;
925 | }
926 |
927 | return NULL;
928 | }
929 |
930 | /* apache config fonction of the module */
931 | static const command_rec Auth_memCookie_cmds[] =
932 | {
933 | AP_INIT_TAKE1("Auth_memCookie_Memcached_Configuration", ap_set_string_slot,
934 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, szAuth_memCookie_memCached_Configuration),
935 | OR_AUTHCFG, "libmemcached configuration - http://docs.libmemcached.org/libmemcached_configuration.html"),
936 | AP_INIT_TAKE1("Auth_memCookie_Memcached_SessionObject_ExpireTime", ap_set_int_slot,
937 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, tAuth_memCookie_MemcacheObjectExpiry),
938 | OR_AUTHCFG, "Session object in memcached expiry time, in secondes."),
939 | AP_INIT_TAKE1("Auth_memCookie_SessionTableSize", ap_set_int_slot,
940 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, nAuth_memCookie_SessionTableSize),
941 | OR_AUTHCFG, "Max number of element in session information table. is set to 10 by default"),
942 | AP_INIT_FLAG ("Auth_memCookie_Memcached_SessionObject_ExpiryReset", ap_set_flag_slot,
943 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, nAuth_memCookie_MemcacheObjectExpiryReset),
944 | OR_AUTHCFG, "Set to 'off' to not reset object expiry time in memcache... is 'on' by default"),
945 | AP_INIT_FLAG ("Auth_memCookie_SetSessionHTTPHeader", ap_set_flag_slot,
946 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, nAuth_memCookie_SetSessionHTTPHeader),
947 | OR_AUTHCFG, "Set to 'on' to set session information to http header of the authenticated users, is set 'off' by default"),
948 | AP_INIT_FLAG ("Auth_memCookie_SetSessionHTTPHeaderEncode", ap_set_flag_slot,
949 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, nAuth_memCookie_SetSessionHTTPHeaderEncode),
950 | OR_AUTHCFG, "Set to 'on' to mime64 encode session information to http header, is set 'off' by default"),
951 | AP_INIT_TAKE1("Auth_memCookie_SetSessionHTTPHeaderPrefix", ap_set_string_slot,
952 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, szAuth_memCookie_SetSessionHTTPHeaderPrefix),
953 | OR_AUTHCFG, "Set HTTP header prefix - set to 'MCAC_' by default"),
954 | AP_INIT_TAKE1("Auth_memCookie_CookieName", ap_set_string_slot,
955 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, szAuth_memCookie_CookieName),
956 | OR_AUTHCFG, "Name of cookie to set"),
957 | AP_INIT_TAKE1("Auth_memCookie_MatchIP_Mode", cmd_MatchIP_Mode, NULL,
958 | OR_AUTHCFG, "To check cookie ip adresse, Set to '1' to use 'X-Forwarded-For' http header, to '2' to use 'Via' http header, and to '3' to use apache remote_ip. set to '0' by default to desactivate the ip check."),
959 | #if MODULE_MAGIC_NUMBER_MAJOR <= 20051115
960 | AP_INIT_FLAG ("Auth_memCookie_GroupAuthoritative", ap_set_flag_slot,
961 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, nAuth_memCookie_GroupAuthoritative),
962 | OR_AUTHCFG, "Set to 'off' to allow access control to be passed along to lower modules, for group acl check, is set to 'on' by default."),
963 | #endif
964 | AP_INIT_FLAG ("Auth_memCookie_Authoritative", ap_set_flag_slot,
965 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, nAuth_memCookie_Authoritative),
966 | OR_AUTHCFG, "Set to 'off' to allow access control to be passed along to lower modules, is set to 'on' by default"),
967 | AP_INIT_FLAG ("Auth_memCookie_SilmulateAuthBasic", ap_set_flag_slot,
968 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, nAuth_memCookie_authbasicfix),
969 | OR_AUTHCFG, "Set to 'off' to fix http header and auth_type for simulating auth basic for scripting language like php auth framework work, is set to 'on' by default"),
970 | AP_INIT_FLAG ("Auth_memCookie_DisableNoStore", ap_set_flag_slot,
971 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, nAuth_memCookie_disable_no_store),
972 | OR_AUTHCFG,
973 | "Set to 'on' to stop the sending of a Cache-Control no-store header with "
974 | "the login screen. This allows the browser to cache the credentials, but "
975 | "at the risk of it being possible for the login form to be resubmitted "
976 | "and revealed to the backend server through XSS. Use at own risk."),
977 | #ifdef HAVE_MEMCACHED_SASL
978 | AP_INIT_FLAG ("Auth_memCookie_SASLAuth", ap_set_flag_slot,
979 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, nAuth_memCookie_SASLAuth),
980 | OR_AUTHCFG, "Set to 'on' to use SASL authentication. If this is set then "
981 | "Auth_memCookie_SASLUsername and Auth_memCookie_SASLPassword should also be set. "
982 | "Set to 'off' by default"),
983 | AP_INIT_TAKE1("Auth_memCookie_SASLUsername", ap_set_string_slot,
984 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, szAuth_memCookie_SASLUsername),
985 | OR_AUTHCFG, "User name to use for SASL authentication to memcached. "
986 | "Set to 'user' by default"),
987 | AP_INIT_TAKE1("Auth_memCookie_SASLPassword", ap_set_string_slot,
988 | (void *)APR_OFFSETOF(strAuth_memCookie_config_rec, szAuth_memCookie_SASLPassword),
989 | OR_AUTHCFG, "Password to use for SASL authentication to memcached. "
990 | "Set to 'pass' by default"),
991 | #endif
992 | {NULL}
993 | };
994 |
995 | /* apache module structure */
996 | module AP_MODULE_DECLARE_DATA mod_auth_memcookie_module =
997 | {
998 | STANDARD20_MODULE_STUFF,
999 | create_Auth_memCookie_dir_config, /* dir config creater */
1000 | NULL, /* dir merger --- default is to override */
1001 | NULL, /* server config */
1002 | NULL, /* merge server config */
1003 | Auth_memCookie_cmds, /* command apr_table_t */
1004 | register_hooks /* register hooks */
1005 | };
1006 |
--------------------------------------------------------------------------------
/mod_auth_memcookie.h.in:
--------------------------------------------------------------------------------
1 | #ifndef MOD_AUTH_MEMCOOKIE_H
2 | #define MOD_AUTH_MEMCOOKIE_H
3 |
4 | // if sasl is defined
5 | #undef HAVE_MEMCACHED_SASL
6 |
7 | #endif
8 |
--------------------------------------------------------------------------------
/samples/httpd.conf:
--------------------------------------------------------------------------------
1 | # Configuration example for using auth_memcookie module
2 | LoadModule mod_auth_memcookie_module modules/mod_auth_memcookie.so
3 |
4 |
5 |
6 | Auth_memCookie_CookieName myauthcookie
7 | Auth_memCookie_Memcached_Configuration --server=127.0.0.1:11000
8 |
9 | # to fix header for php buggy authentification mecanism
10 | Auth_memCookie_SilmulateAuthBasic on
11 |
12 | # to redirect unauthorized user to the login page
13 | ErrorDocument 401 "/login.php"
14 |
15 | Auth_memCookie_Authoritative on
16 | AuthType Cookie
17 | AuthName "Login Intranet"
18 | require mcac-public
19 |
20 |
21 |
22 |
23 |
24 | require valid-user
25 |
26 |
27 | require user user1 user2 user3
28 |
29 |
30 | require mcac-group group1 group2
31 |
32 |
--------------------------------------------------------------------------------
/samples/login.php:
--------------------------------------------------------------------------------
1 |
17 |
18 |
19 | page de login
20 |
21 |
22 |