libcli/security Provide a common, top level libcli/security/security.h
[kamenim/samba.git] / source3 / web / statuspage.c
index 459b679d8172b249f24429388db2447972713e23..85bd33b7de0d5da33432cfb56c23858c63abffc3 100644 (file)
@@ -5,7 +5,7 @@
    
    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 2 of the License, or
+   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,
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "web/swat_proto.h"
+#include "libcli/security/security.h"
+
+#define _(x) lang_msg_rotate(talloc_tos(),x)
 
 #define PIDMAP         struct PidMap
 
 
 PIDMAP {
        PIDMAP  *next, *prev;
-       struct process_id pid;
+       struct server_id pid;
        char    *machine;
 };
 
 static PIDMAP  *pidmap;
 static int     PID_or_Machine;         /* 0 = show PID, else show Machine name */
 
-static struct process_id smbd_pid;
+static struct server_id smbd_pid;
 
 /* from 2nd call on, remove old list */
 static void initPid2Machine (void)
 {
        /* show machine name rather PID on table "Open Files"? */
        if (PID_or_Machine) {
-               PIDMAP *p;
+               PIDMAP *p, *next;
 
-               for (p = pidmap; p != NULL; ) {
+               for (p = pidmap; p != NULL; p = next) {
+                       next = p->next;
                        DLIST_REMOVE(pidmap, p);
                        SAFE_FREE(p->machine);
                        SAFE_FREE(p);
@@ -55,7 +58,7 @@ static void initPid2Machine (void)
 }
 
 /* add new PID <-> Machine name mapping */
-static void addPid2Machine (struct process_id pid, char *machine)
+static void addPid2Machine (struct server_id pid, const char *machine)
 {
        /* show machine name rather PID on table "Open Files"? */
        if (PID_or_Machine) {
@@ -75,7 +78,7 @@ static void addPid2Machine (struct process_id pid, char *machine)
 }
 
 /* lookup PID <-> Machine name mapping */
-static char *mapPid2Machine (struct process_id pid)
+static char *mapPid2Machine (struct server_id pid)
 {
        static char pidbuf [64];
        PIDMAP *map;
@@ -98,11 +101,20 @@ static char *mapPid2Machine (struct process_id pid)
        return pidbuf;
 }
 
-static char *tstring(time_t t)
+static const char *tstring(TALLOC_CTX *ctx, time_t t)
 {
-       static pstring buf;
-       pstrcpy(buf, time_to_asc(&t));
-       all_string_sub(buf," ","&nbsp;",sizeof(buf));
+       char *buf;
+       buf = talloc_strdup(ctx, time_to_asc(t));
+       if (!buf) {
+               return "";
+       }
+       buf = talloc_all_string_sub(ctx,
+                       buf,
+                       " ",
+                       "&nbsp;");
+       if (!buf) {
+               return "";
+       }
        return buf;
 }
 
@@ -112,7 +124,9 @@ static void print_share_mode(const struct share_mode_entry *e,
                             void *dummy)
 {
        char           *utf8_fname;
+       char           *utf8_sharepath;
        int deny_mode;
+       size_t converted_size;
 
        if (!is_valid_share_mode_entry(e)) {
                return;
@@ -159,28 +173,25 @@ static void print_share_mode(const struct share_mode_entry *e,
                printf("NONE            ");
        printf("</td>");
 
-       push_utf8_allocate(&utf8_fname, fname);
-       printf("<td>%s</td><td>%s</td></tr>\n",
-              utf8_fname,tstring(e->time.tv_sec));
-       SAFE_FREE(utf8_fname);
+       push_utf8_talloc(talloc_tos(), &utf8_fname, fname, &converted_size);
+       push_utf8_talloc(talloc_tos(), &utf8_sharepath, sharepath,
+                        &converted_size);
+       printf("<td>%s</td><td>%s</td><td>%s</td></tr>\n",
+              utf8_sharepath,utf8_fname,tstring(talloc_tos(),e->time.tv_sec));
+       TALLOC_FREE(utf8_fname);
 }
 
 
 /* kill off any connections chosen by the user */
-static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
+static int traverse_fn1(const struct connections_key *key,
+                       const struct connections_data *crec,
+                       void *private_data)
 {
-       struct connections_data crec;
-
-       if (dbuf.dsize != sizeof(crec))
-               return 0;
-
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1 && process_exists(crec.pid)) {
+       if (crec->cnum == -1 && process_exists(crec->pid)) {
                char buf[30];
-               slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec.pid));
+               slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec->pid));
                if (cgi_variable(buf)) {
-                       kill_pid(crec.pid);
+                       kill_pid(crec->pid);
                        sleep(SLEEP_TIME);
                }
        }
@@ -188,28 +199,23 @@ static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* st
 }
 
 /* traversal fn for showing machine connections */
-static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
+static int traverse_fn2(const struct connections_key *key,
+                        const struct connections_data *crec,
+                        void *private_data)
 {
-       struct connections_data crec;
-
-       if (dbuf.dsize != sizeof(crec))
-               return 0;
-
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-       
-       if (crec.cnum == -1 || !process_exists(crec.pid) ||
-           procid_equal(&crec.pid, &smbd_pid))
+       if (crec->cnum == -1 || !process_exists(crec->pid) ||
+           procid_equal(&crec->pid, &smbd_pid))
                return 0;
 
-       addPid2Machine (crec.pid, crec.machine);
+       addPid2Machine (crec->pid, crec->machine);
 
        printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
-              procid_str_static(&crec.pid),
-              crec.machine,crec.addr,
-              tstring(crec.start));
+              procid_str_static(&crec->pid),
+              crec->machine, crec->addr,
+              tstring(talloc_tos(),crec->start));
        if (geteuid() == 0) {
                printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
-                      procid_str_static(&crec.pid));
+                      procid_str_static(&crec->pid));
        }
        printf("</tr>\n");
 
@@ -217,23 +223,18 @@ static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* st
 }
 
 /* traversal fn for showing share connections */
-static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
+static int traverse_fn3(const struct connections_key *key,
+                        const struct connections_data *crec,
+                        void *private_data)
 {
-       struct connections_data crec;
-
-       if (dbuf.dsize != sizeof(crec))
-               return 0;
-
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1 || !process_exists(crec.pid))
+       if (crec->cnum == -1 || !process_exists(crec->pid))
                return 0;
 
        printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
-              crec.name,uidtoname(crec.uid),
-              gidtoname(crec.gid),procid_str_static(&crec.pid),
-              crec.machine,
-              tstring(crec.start));
+              crec->servicename, uidtoname(crec->uid),
+              gidtoname(crec->gid),procid_str_static(&crec->pid),
+              crec->machine,
+              tstring(talloc_tos(),crec->start));
        return 0;
 }
 
@@ -244,9 +245,9 @@ void status_page(void)
        const char *v;
        int autorefresh=0;
        int refresh_interval=30;
-       TDB_CONTEXT *tdb;
        int nr_running=0;
-       BOOL waitup = False;
+       bool waitup = False;
+       TALLOC_CTX *ctx = talloc_stackframe();
 
        smbd_pid = pid_to_procid(pidfile_pid("smbd"));
 
@@ -322,9 +323,8 @@ void status_page(void)
                PID_or_Machine = 0;
        }
 
-       tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
-       if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
+       connections_forall_read(traverse_fn1, NULL);
+
        initPid2Machine ();
 
        printf("<H2>%s</H2>\n", _("Server Status"));
@@ -344,15 +344,9 @@ void status_page(void)
 
        printf("<p>\n");
 
-       if (!tdb) {
-               /* open failure either means no connections have been
-                   made */
-       }
-
-
        printf("<table>\n");
 
-       printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
+       printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), samba_version_string());
 
        fflush(stdout);
        printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
@@ -419,7 +413,7 @@ void status_page(void)
        }
        printf("</tr>\n");
 
-       if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
+       connections_forall_read(traverse_fn2, NULL);
 
        printf("</table><p>\n");
 
@@ -428,21 +422,20 @@ void status_page(void)
        printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
                _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
 
-       if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
+       connections_forall_read(traverse_fn3, NULL);
 
        printf("</table><p>\n");
 
        printf("<h3>%s</h3>\n", _("Open Files"));
        printf("<table border=1>\n");
-       printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n", _("PID"), _("Sharing"), _("R/W"), _("Oplock"), _("File"), _("Date"));
+       printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n",
+               _("PID"), _("UID"), _("Sharing"), _("R/W"), _("Oplock"), _("Share"), _("File"), _("Date"));
 
-       locking_init(1);
+       locking_init_readonly();
        share_mode_forall(print_share_mode, NULL);
        locking_end();
        printf("</table>\n");
 
-       if (tdb) tdb_close(tdb);
-
        printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
        printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
 
@@ -459,4 +452,5 @@ void status_page(void)
                       refresh_interval*1000);
                printf("//-->\n</script>\n");
        }
+       TALLOC_FREE(ctx);
 }