Move everything to use the common pidfile functions.
[samba.git] / source3 / web / statuspage.c
index 0a909886ce7e13935e6b1f21f985cc53a4111beb..d25797064332429712c9b3d4a0f1230ae3c763a9 100644 (file)
    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"
+#include "locking/proto.h"
+#include "librpc/gen_ndr/open_files.h"
+#include "lib/conn_tdb.h"
+#include "../lib/util/pidfile.h"
+
+#define _(x) lang_msg_rotate(talloc_tos(),x)
 
 #define PIDMAP         struct PidMap
 
@@ -42,9 +48,10 @@ 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 +62,7 @@ static void initPid2Machine (void)
 }
 
 /* add new PID <-> Machine name mapping */
-static void addPid2Machine (struct server_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) {
@@ -83,7 +90,7 @@ static char *mapPid2Machine (struct server_id pid)
        /* show machine name rather PID on table "Open Files"? */
        if (PID_or_Machine) {
                for (map = pidmap; map != NULL; map = map->next) {
-                       if (procid_equal(&pid, &map->pid)) {
+                       if (serverid_equal(&pid, &map->pid)) {
                                if (map->machine == NULL)       /* no machine name */
                                        break;                  /* show PID */
 
@@ -98,11 +105,20 @@ static char *mapPid2Machine (struct server_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 +128,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,20 +177,21 @@ 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(struct db_record *rec,
-                       const struct connections_key *key,
+static int traverse_fn1(const struct connections_key *key,
                        const struct connections_data *crec,
                        void *private_data)
 {
-       if (crec->cnum == -1 && process_exists(crec->pid)) {
+       if (crec->cnum == TID_FIELD_INVALID && process_exists(crec->pid)) {
                char buf[30];
                slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec->pid));
                if (cgi_variable(buf)) {
@@ -184,13 +203,12 @@ static int traverse_fn1(struct db_record *rec,
 }
 
 /* traversal fn for showing machine connections */
-static int traverse_fn2(struct db_record *rec,
-                        const struct connections_key *key,
+static int traverse_fn2(const struct connections_key *key,
                         const struct connections_data *crec,
                         void *private_data)
 {
-       if (crec->cnum == -1 || !process_exists(crec->pid) ||
-           procid_equal(&crec->pid, &smbd_pid))
+       if (crec->cnum == TID_FIELD_INVALID || !process_exists(crec->pid) ||
+           serverid_equal(&crec->pid, &smbd_pid))
                return 0;
 
        addPid2Machine (crec->pid, crec->machine);
@@ -198,7 +216,7 @@ static int traverse_fn2(struct db_record *rec,
        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));
+              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));
@@ -209,19 +227,18 @@ static int traverse_fn2(struct db_record *rec,
 }
 
 /* traversal fn for showing share connections */
-static int traverse_fn3(struct db_record *rec,
-                        const struct connections_key *key,
+static int traverse_fn3(const struct connections_key *key,
                         const struct connections_data *crec,
                         void *private_data)
 {
-       if (crec->cnum == -1 || !process_exists(crec->pid))
+       if (crec->cnum == TID_FIELD_INVALID || !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->servicename, uidtoname(crec->uid),
               gidtoname(crec->gid),procid_str_static(&crec->pid),
               crec->machine,
-              tstring(crec->start));
+              tstring(talloc_tos(),crec->start));
        return 0;
 }
 
@@ -233,9 +250,15 @@ void status_page(void)
        int autorefresh=0;
        int refresh_interval=30;
        int nr_running=0;
-       BOOL waitup = False;
+       bool waitup = False;
+       TALLOC_CTX *ctx = talloc_stackframe();
+       const char form_name[] = "status";
 
-       smbd_pid = pid_to_procid(pidfile_pid("smbd"));
+       smbd_pid = pid_to_procid(pidfile_pid(lp_piddir(), "smbd"));
+
+       if (!verify_xsrf_token(form_name)) {
+               goto output_page;
+       }
 
        if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
                stop_smbd();
@@ -309,13 +332,15 @@ void status_page(void)
                PID_or_Machine = 0;
        }
 
-       connections_forall(traverse_fn1, NULL);
+       connections_forall_read(traverse_fn1, NULL);
+
        initPid2Machine ();
 
+output_page:
        printf("<H2>%s</H2>\n", _("Server Status"));
 
        printf("<FORM method=post>\n");
+       print_xsrf_token(cgi_user_name(), cgi_user_pass(), form_name);
 
        if (!autorefresh) {
                printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
@@ -332,7 +357,7 @@ void status_page(void)
 
        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"));
@@ -399,7 +424,7 @@ void status_page(void)
        }
        printf("</tr>\n");
 
-       connections_forall(traverse_fn2, NULL);
+       connections_forall_read(traverse_fn2, NULL);
 
        printf("</table><p>\n");
 
@@ -408,15 +433,16 @@ 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"));
 
-       connections_forall(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");
@@ -437,4 +463,5 @@ void status_page(void)
                       refresh_interval*1000);
                printf("//-->\n</script>\n");
        }
+       TALLOC_FREE(ctx);
 }