auth.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  */
00032 
00033 /*
00034  * $Log: auth.c,v $
00035  * Revision 1.4  2008/07/17 11:29:15  olereinhardt
00036  * Allow authentication for subdirectories
00037  *
00038  * Revision 1.3  2006/10/08 16:43:53  haraldkipp
00039  * Authentication info depended on static memory kept by the caller. Now a
00040  * local copy is held and NutClearAuth (which should have been named
00041  * NutHttpAuthClear, btw.) works correctly.
00042  *
00043  * Revision 1.2  2006/08/25 13:42:16  olereinhardt
00044  * added NutClearAuth(void); Thanks to Peter Sondermanns
00045  *
00046  * Revision 1.1.1.1  2003/05/09 14:41:56  haraldkipp
00047  * Initial using 3.2.1
00048  *
00049  * Revision 1.7  2003/02/04 18:17:31  harald
00050  * Version 3 released
00051  *
00052  * Revision 1.6  2002/06/26 17:29:49  harald
00053  * First pre-release with 2.4 stack
00054  *
00055  */
00056 
00057 #include <string.h>
00058 #include <sys/heap.h>
00059 
00060 #include "dencode.h"
00061 #include <pro/httpd.h>
00062 
00067 
00068 AUTHINFO *authList = 0;
00069 
00073 static AUTHINFO *NutHttpAuthLookup(CONST char *dirname, CONST char *login)
00074 {
00075     AUTHINFO *auth;
00076 
00077     for (auth = authList; auth; auth = auth->auth_next) {
00078         if (dirname && (strstr(dirname, auth->auth_dirname) != dirname))
00079             continue;
00080         if (login && strcmp(login, auth->auth_login))
00081             continue;
00082         break;
00083     }
00084     return auth;
00085 }
00086 
00102 int NutRegisterAuth(CONST char *dirname, CONST char *login)
00103 {
00104     AUTHINFO *auth;
00105 
00106     /* Allocate a new list element. */
00107     if ((auth = NutHeapAlloc(sizeof(AUTHINFO))) != NULL) {
00108         auth->auth_next = authList;
00109         /* Allocate the path component. */
00110         if ((auth->auth_dirname = NutHeapAlloc(strlen(dirname) + 1)) != NULL) {
00111             strcpy(auth->auth_dirname, dirname);
00112             /* Allocate the login component. */
00113             if ((auth->auth_login = NutHeapAlloc(strlen(login) + 1)) != NULL) {
00114                 strcpy(auth->auth_login, login);
00115                 /* Success. Add element to the list and return. */
00116                 authList = auth;
00117                 return 0;
00118             }
00119             /* Allocation failed. */
00120             NutHeapFree(auth->auth_dirname);
00121         }
00122         NutHeapFree(auth);
00123     }
00124     return -1;
00125 }
00126 
00127 
00134 void NutClearAuth(void)
00135 {
00136     AUTHINFO *auth;
00137 
00138     while (authList) {
00139         auth = authList;
00140         authList = auth->auth_next;
00141         NutHeapFree(auth->auth_dirname);
00142         NutHeapFree(auth->auth_login);
00143         NutHeapFree(auth);
00144     }
00145 }
00146 
00158 int NutHttpAuthValidate(REQUEST * req)
00159 {
00160     char *realm;
00161     char *cp = 0;
00162     int rc = -1;
00163 
00164     /*
00165      * Get directory by chopping off filename.
00166      */
00167     realm = req->req_url;
00168     if ((cp = strrchr(realm, '/')) != 0)
00169         *cp = 0;
00170     else
00171         realm = ".";
00172 
00173     /*
00174      * Check if authorization required.
00175      */
00176     if (NutHttpAuthLookup(realm, 0)) {
00177         /*
00178          * Check authorization.
00179          */
00180         if (req->req_auth) {
00181             /*
00182              * Acceptint basic authorization only.
00183              */
00184             if (strncmp(req->req_auth, "Basic ", 6) == 0) {
00185                 NutDecodeBase64(req->req_auth + 6);
00186                 if (NutHttpAuthLookup(realm, req->req_auth + 6))
00187                     rc = 0;
00188             }
00189         }
00190     } else
00191         rc = 0;
00192 
00193     if (cp)
00194         *cp = '/';
00195 
00196     return rc;
00197 }
00198 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/