s3-auth: use auth.h where needed.
[kai/samba-autobuild/.git] / source3 / web / cgi.c
index 28f64f89adeaa7ff87dede2a6bedab7091899e96..94590a28dee5497824f4d16e7367c21ea596c678 100644 (file)
@@ -1,24 +1,28 @@
 /* 
    some simple CGI helper routines
    Copyright (C) Andrew Tridgell 1997-1998
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 
 #include "includes.h"
+#include "system/passwd.h"
+#include "system/filesys.h"
 #include "web/swat_proto.h"
+#include "intl/lang_tdb.h"
+#include "auth.h"
 
 #define MAX_VARIABLES 10000
 
@@ -53,7 +57,7 @@ static char *grab_line(FILE *f, int *cl)
 
        while ((*cl)) {
                int c;
-       
+
                if (i == len) {
                        char *ret2;
                        if (len == 0) len = 1024;
@@ -62,7 +66,7 @@ static char *grab_line(FILE *f, int *cl)
                        if (!ret2) return ret;
                        ret = ret2;
                }
-       
+
                c = fgetc(f);
                (*cl)--;
 
@@ -70,7 +74,7 @@ static char *grab_line(FILE *f, int *cl)
                        (*cl) = 0;
                        break;
                }
-               
+
                if (c == '\r') continue;
 
                if (strchr_m("\n&", c)) break;
@@ -78,7 +82,7 @@ static char *grab_line(FILE *f, int *cl)
                ret[i++] = c;
 
        }
-       
+
        if (ret) {
                ret[i] = 0;
        }
@@ -132,14 +136,14 @@ void cgi_load_variables(void)
                while (len && (line=grab_line(f, &len))) {
                        p = strchr_m(line,'=');
                        if (!p) continue;
-                       
+
                        *p = 0;
-                       
+
                        variables[num_variables].name = SMB_STRDUP(line);
                        variables[num_variables].value = SMB_STRDUP(p+1);
 
                        SAFE_FREE(line);
-                       
+
                        if (!variables[num_variables].name || 
                            !variables[num_variables].value)
                                continue;
@@ -154,7 +158,7 @@ void cgi_load_variables(void)
                               variables[num_variables].name,
                               variables[num_variables].value);
 #endif
-                       
+
                        num_variables++;
                        if (num_variables == MAX_VARIABLES) break;
                }
@@ -169,9 +173,9 @@ void cgi_load_variables(void)
                     tok=strtok_r(NULL, "&;", &saveptr)) {
                        p = strchr_m(tok,'=');
                        if (!p) continue;
-                       
+
                        *p = 0;
-                       
+
                        variables[num_variables].name = SMB_STRDUP(tok);
                        variables[num_variables].value = SMB_STRDUP(p+1);
 
@@ -205,16 +209,16 @@ void cgi_load_variables(void)
                char *dest = NULL;
                size_t dest_len;
 
-               convert_string_allocate(frame, CH_UTF8, CH_UNIX,
-                              variables[i].name, -1,
-                              &dest, &dest_len, True);
+               convert_string_talloc(frame, CH_UTF8, CH_UNIX,
+                              variables[i].name, strlen(variables[i].name),
+                              &dest, &dest_len);
                SAFE_FREE(variables[i].name);
                variables[i].name = SMB_STRDUP(dest ? dest : "");
 
                dest = NULL;
-               convert_string_allocate(frame, CH_UTF8, CH_UNIX,
-                              variables[i].value, -1,
-                              &dest, &dest_len, True);
+               convert_string_talloc(frame, CH_UTF8, CH_UNIX,
+                              variables[i].value, strlen(variables[i].value),
+                              &dest, &dest_len);
                SAFE_FREE(variables[i].value);
                variables[i].value = SMB_STRDUP(dest ? dest : "");
                TALLOC_FREE(frame);
@@ -314,7 +318,7 @@ static void cgi_web_auth(void)
                exit(0);
        }
 
-       pwd = getpwnam_alloc(NULL, user);
+       pwd = Get_Pwnam_alloc(talloc_tos(), user);
        if (!pwd) {
                printf("%sCannot find user %s<br>%s\n", head, user, tail);
                exit(0);
@@ -339,6 +343,8 @@ static bool cgi_handle_authorization(char *line)
        char *p;
        fstring user, user_pass;
        struct passwd *pass = NULL;
+       const char *rhost;
+       char addr[INET6_ADDRSTRLEN];
 
        if (!strnequal(line,"Basic ", 6)) {
                goto err;
@@ -357,42 +363,44 @@ static bool cgi_handle_authorization(char *line)
 
        convert_string(CH_UTF8, CH_UNIX, 
                       line, -1, 
-                      user, sizeof(user), True);
+                      user, sizeof(user));
 
        convert_string(CH_UTF8, CH_UNIX, 
                       p+1, -1, 
-                      user_pass, sizeof(user_pass), True);
+                      user_pass, sizeof(user_pass));
 
        /*
         * Try and get the user from the UNIX password file.
         */
-       
-       pass = getpwnam_alloc(NULL, user);
-       
+
+       pass = Get_Pwnam_alloc(talloc_tos(), user);
+
+       rhost = client_name(1);
+       if (strequal(rhost,"UNKNOWN"))
+               rhost = client_addr(1, addr, sizeof(addr));
+
        /*
         * Validate the password they have given.
         */
-       
-       if NT_STATUS_IS_OK(pass_check(pass, user, user_pass, 
-                     strlen(user_pass), NULL, False)) {
-               
+
+       if NT_STATUS_IS_OK(pass_check(pass, user, rhost, user_pass, false)) {
                if (pass) {
                        /*
                         * Password was ok.
                         */
-                       
+
                        if ( initgroups(pass->pw_name, pass->pw_gid) != 0 )
                                goto err;
 
                        become_user_permanently(pass->pw_uid, pass->pw_gid);
-                       
+
                        /* Save the users name */
                        C_user = SMB_STRDUP(user);
                        TALLOC_FREE(pass);
                        return True;
                }
        }
-       
+
 err:
        cgi_setup_error("401 Bad Authorization", 
                        "WWW-Authenticate: Basic realm=\"SWAT\"\r\n",
@@ -442,22 +450,22 @@ static void cgi_download(char *file)
                }
        }
 
-       if (sys_stat(file, &st) != 0) 
-       {
+       if (sys_stat(file, &st, false) != 0)    {
                cgi_setup_error("404 File Not Found","",
                                "The requested file was not found");
        }
 
-       if (S_ISDIR(st.st_mode))
+       if (S_ISDIR(st.st_ex_mode))
        {
                snprintf(buf, sizeof(buf), "%s/index.html", file);
-               if (!file_exist(buf, &st) || !S_ISREG(st.st_mode))
+               if (!file_exist_stat(buf, &st, false)
+                   || !S_ISREG(st.st_ex_mode))
                {
                        cgi_setup_error("404 File Not Found","",
                                        "The requested file was not found");
                }
        }
-       else if (S_ISREG(st.st_mode))
+       else if (S_ISREG(st.st_ex_mode))
        {
                snprintf(buf, sizeof(buf), "%s", file);
        }
@@ -488,16 +496,19 @@ static void cgi_download(char *file)
                        printf("Content-Type: text/html\r\n");
                }
        }
-       printf("Expires: %s\r\n", http_timestring(time(NULL)+EXPIRY_TIME));
+       printf("Expires: %s\r\n", 
+                  http_timestring(talloc_tos(), time(NULL)+EXPIRY_TIME));
 
        lang = lang_tdb_current();
        if (lang) {
                printf("Content-Language: %s\r\n", lang);
        }
 
-       printf("Content-Length: %d\r\n\r\n", (int)st.st_size);
+       printf("Content-Length: %d\r\n\r\n", (int)st.st_ex_size);
        while ((l=read(fd,buf,sizeof(buf)))>0) {
-               fwrite(buf, 1, l, stdout);
+               if (fwrite(buf, 1, l, stdout) != l) {
+                       break;
+               }
        }
        close(fd);
        exit(0);
@@ -505,6 +516,87 @@ static void cgi_download(char *file)
 
 
 
+/* return true if the char* contains ip addrs only.  Used to avoid
+name lookup calls */
+
+static bool only_ipaddrs_in_list(const char **list)
+{
+       bool only_ip = true;
+
+       if (!list) {
+               return true;
+       }
+
+       for (; *list ; list++) {
+               /* factor out the special strings */
+               if (strequal(*list, "ALL") || strequal(*list, "FAIL") ||
+                   strequal(*list, "EXCEPT")) {
+                       continue;
+               }
+
+               if (!is_ipaddress(*list)) {
+                       /*
+                        * If we failed, make sure that it was not because
+                        * the token was a network/netmask pair. Only
+                        * network/netmask pairs have a '/' in them.
+                        */
+                       if ((strchr_m(*list, '/')) == NULL) {
+                               only_ip = false;
+                               DEBUG(3,("only_ipaddrs_in_list: list has "
+                                       "non-ip address (%s)\n",
+                                       *list));
+                               break;
+                       }
+               }
+       }
+
+       return only_ip;
+}
+
+/* return true if access should be allowed to a service for a socket */
+static bool check_access(int sock, const char **allow_list,
+                        const char **deny_list)
+{
+       bool ret = false;
+       bool only_ip = false;
+       char addr[INET6_ADDRSTRLEN];
+
+       if ((!deny_list || *deny_list==0) && (!allow_list || *allow_list==0)) {
+               return true;
+       }
+
+       /* Bypass name resolution calls if the lists
+        * only contain IP addrs */
+       if (only_ipaddrs_in_list(allow_list) &&
+           only_ipaddrs_in_list(deny_list)) {
+               only_ip = true;
+               DEBUG (3, ("check_access: no hostnames "
+                          "in host allow/deny list.\n"));
+               ret = allow_access(deny_list,
+                                  allow_list,
+                                  "",
+                                  get_peer_addr(sock,addr,sizeof(addr)));
+       } else {
+               DEBUG (3, ("check_access: hostnames in "
+                          "host allow/deny list.\n"));
+               ret = allow_access(deny_list,
+                                  allow_list,
+                                  get_peer_name(sock,true),
+                                  get_peer_addr(sock,addr,sizeof(addr)));
+       }
+
+       if (ret) {
+               DEBUG(2,("Allowed connection from %s (%s)\n",
+                        only_ip ? "" : get_peer_name(sock,true),
+                        get_peer_addr(sock,addr,sizeof(addr))));
+       } else {
+               DEBUG(0,("Denied connection from %s (%s)\n",
+                        only_ip ? "" : get_peer_name(sock,true),
+                        get_peer_addr(sock,addr,sizeof(addr))));
+       }
+
+       return(ret);
+}
 
 /**
  * @brief Setup the CGI framework.
@@ -604,7 +696,7 @@ void cgi_setup(const char *rootdir, int auth_required)
        }
 
        printf("HTTP/1.0 200 OK\r\nConnection: close\r\n");
-       printf("Date: %s\r\n", http_timestring(time(NULL)));
+       printf("Date: %s\r\n", http_timestring(talloc_tos(), time(NULL)));
        baseurl = "";
        pathinfo = url+1;
 }
@@ -654,7 +746,8 @@ const char *cgi_remote_addr(void)
 {
        if (inetd_server) {
                char addr[INET6_ADDRSTRLEN];
-               return get_peer_addr(1,addr,sizeof(addr));
+               get_peer_addr(1,addr,sizeof(addr));
+               return talloc_strdup(talloc_tos(), addr);
        }
        return getenv("REMOTE_ADDR");
 }