lib: Add lib/util/server_id.h
[kai/samba-autobuild/.git] / source3 / utils / net_status.c
index 960379b3838e01e533a95bc9c54702a7d7f865c8..0d658a0c36e0eb632a925fb4b1f2a204a802e461 100644 (file)
-/* 
-   Samba Unix/Linux SMB client library 
+/*
+   Samba Unix/Linux SMB client library
    net status command -- possible replacement for smbstatus
    Copyright (C) 2003 Volker Lendecke (vl@samba.org)
 
    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,
    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, 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 "lib/util/server_id.h"
 #include "utils/net.h"
+#include "session.h"
+#include "messages.h"
+#include "lib/conn_tdb.h"
 
-static int show_session(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
-                       void *state)
+int net_status_usage(struct net_context *c, int argc, const char **argv)
 {
-       BOOL *parseable = (BOOL *)state;
-       struct sessionid sessionid;
-
-       if (dbuf.dsize != sizeof(sessionid))
-               return 0;
+       d_printf(_("  net status sessions [parseable] "
+                  "Show list of open sessions\n"));
+       d_printf(_("  net status shares [parseable]   "
+                  "Show list of open shares\n"));
+       return -1;
+}
 
-       memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
+static int show_session(const char *key, struct sessionid *session,
+                       void *private_data)
+{
+       struct server_id_buf tmp;
+       bool *parseable = (bool *)private_data;
 
-       if (!process_exists_by_pid(sessionid.pid)) {
+       if (!process_exists(session->pid)) {
                return 0;
        }
 
        if (*parseable) {
-               d_printf("%d\\%s\\%s\\%s\\%s\n",
-                        (int)sessionid.pid, uidtoname(sessionid.uid),
-                        gidtoname(sessionid.gid), 
-                        sessionid.remote_machine, sessionid.hostname);
+               d_printf("%s\\%s\\%s\\%s\\%s\n",
+                        server_id_str_buf(session->pid, &tmp),
+                        uidtoname(session->uid),
+                        gidtoname(session->gid),
+                        session->remote_machine, session->hostname);
        } else {
-               d_printf("%5d   %-12s  %-12s  %-12s (%s)\n",
-                        (int)sessionid.pid, uidtoname(sessionid.uid),
-                        gidtoname(sessionid.gid), 
-                        sessionid.remote_machine, sessionid.hostname);
+               d_printf("%7s   %-12s  %-12s  %-12s (%s)\n",
+                        server_id_str_buf(session->pid, &tmp),
+                        uidtoname(session->uid),
+                        gidtoname(session->gid),
+                        session->remote_machine, session->hostname);
        }
 
        return 0;
 }
 
-static int net_status_sessions(int argc, const char **argv)
+static int net_status_sessions(struct net_context *c, int argc, const char **argv)
 {
-       TDB_CONTEXT *tdb;
-       BOOL parseable;
+       bool parseable;
+
+       if (c->display_usage) {
+               d_printf(  "%s\n"
+                          "net status sessions [parseable]\n"
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Display open user sessions.\n"
+                          "    If parseable is specified, output is machine-"
+                          "readable."));
+               return 0;
+       }
 
        if (argc == 0) {
-               parseable = False;
+               parseable = false;
        } else if ((argc == 1) && strequal(argv[0], "parseable")) {
-               parseable = True;
+               parseable = true;
        } else {
-               return net_help_status(argc, argv);
+               return net_status_usage(c, argc, argv);
        }
 
        if (!parseable) {
-               d_printf("PID     Username      Group         Machine"
-                        "                        \n");
-               d_printf("-------------------------------------------"
-                        "------------------------\n");
-       }
-
-       tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
-                          TDB_DEFAULT, O_RDONLY, 0);
-
-       if (tdb == NULL) {
-               d_printf("%s not initialised\n", lock_path("sessionid.tdb"));
-               return -1;
+               d_printf(_("PID     Username      Group         Machine"
+                          "                        \n"
+                          "-------------------------------------------"
+                          "------------------------\n"));
        }
 
-       tdb_traverse(tdb, show_session, &parseable);
-       tdb_close(tdb);
-
+       sessionid_traverse_read(show_session, &parseable);
        return 0;
 }
 
-static int show_share(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
+static int show_share(const struct connections_key *key,
+                     const struct connections_data *crec,
                      void *state)
 {
-       struct connections_data crec;
+       struct server_id_buf tmp;
 
-       if (dbuf.dsize != sizeof(crec))
+       if (crec->cnum == TID_FIELD_INVALID)
                return 0;
 
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1)
-               return 0;
-
-       if (!process_exists(crec.pid)) {
+       if (!process_exists(crec->pid)) {
                return 0;
        }
 
        d_printf("%-10.10s   %s   %-12s  %s",
-              crec.name,procid_str_static(&crec.pid),
-              crec.machine,
-              asctime(LocalTime(&crec.start)));
+              crec->servicename, server_id_str_buf(crec->pid, &tmp),
+              crec->machine,
+              time_to_asc(crec->start));
 
        return 0;
 }
@@ -114,144 +119,130 @@ struct sessionids {
        struct sessionid *entries;
 };
 
-static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
-                      void *state)
+static int collect_pids(const char *key, struct sessionid *session,
+                       void *private_data)
 {
-       struct sessionids *ids = (struct sessionids *)state;
-       struct sessionid sessionid;
+       struct sessionids *ids = (struct sessionids *)private_data;
 
-       if (dbuf.dsize != sizeof(sessionid))
-               return 0;
-
-       memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
-
-       if (!process_exists_by_pid(sessionid.pid)) 
+       if (!process_exists(session->pid))
                return 0;
 
        ids->num_entries += 1;
        ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
-       ids->entries[ids->num_entries-1] = sessionid;
+       if (!ids->entries) {
+               ids->num_entries = 0;
+               return 0;
+       }
+       ids->entries[ids->num_entries-1] = *session;
 
        return 0;
 }
 
-static int show_share_parseable(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
+static int show_share_parseable(const struct connections_key *key,
+                               const struct connections_data *crec,
                                void *state)
 {
        struct sessionids *ids = (struct sessionids *)state;
-       struct connections_data crec;
+       struct server_id_buf tmp;
        int i;
-       BOOL guest = True;
+       bool guest = true;
 
-       if (dbuf.dsize != sizeof(crec))
+       if (crec->cnum == TID_FIELD_INVALID)
                return 0;
 
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1)
-               return 0;
-
-       if (!process_exists(crec.pid)) {
+       if (!process_exists(crec->pid)) {
                return 0;
        }
 
        for (i=0; i<ids->num_entries; i++) {
-               struct process_id id = pid_to_procid(ids->entries[i].pid);
-               if (procid_equal(&id, &crec.pid)) {
-                       guest = False;
+               struct server_id id = ids->entries[i].pid;
+               if (serverid_equal(&id, &crec->pid)) {
+                       guest = false;
                        break;
                }
        }
 
        d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
-                crec.name,procid_str_static(&crec.pid),
+                crec->servicename, server_id_str_buf(crec->pid, &tmp),
                 guest ? "" : uidtoname(ids->entries[i].uid),
                 guest ? "" : gidtoname(ids->entries[i].gid),
-                crec.machine, 
+                crec->machine,
                 guest ? "" : ids->entries[i].hostname,
-                asctime(LocalTime(&crec.start)));
+                time_to_asc(crec->start));
 
        return 0;
 }
 
-static int net_status_shares_parseable(int argc, const char **argv)
+static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
 {
        struct sessionids ids;
-       TDB_CONTEXT *tdb;
 
        ids.num_entries = 0;
        ids.entries = NULL;
 
-       tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
-                          TDB_DEFAULT, O_RDONLY, 0);
-
-       if (tdb == NULL) {
-               d_printf("%s not initialised\n", lock_path("sessionid.tdb"));
-               return -1;
-       }
-
-       tdb_traverse(tdb, collect_pid, &ids);
-       tdb_close(tdb);
+       sessionid_traverse_read(collect_pids, &ids);
 
-       tdb = tdb_open_log(lock_path("connections.tdb"), 0,
-                          TDB_DEFAULT, O_RDONLY, 0);
-
-       if (tdb == NULL) {
-               d_printf("%s not initialised\n", lock_path("connections.tdb"));
-               d_printf("This is normal if no SMB client has ever connected "
-                        "to your server.\n");
-               return -1;
-       }
-
-       tdb_traverse(tdb, show_share_parseable, &ids);
-       tdb_close(tdb);
+       connections_forall_read(show_share_parseable, &ids);
 
        SAFE_FREE(ids.entries);
 
        return 0;
 }
 
-static int net_status_shares(int argc, const char **argv)
+static int net_status_shares(struct net_context *c, int argc, const char **argv)
 {
-       TDB_CONTEXT *tdb;
+       if (c->display_usage) {
+               d_printf(  "%s\n"
+                          "net status shares [parseable]\n"
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Display open user shares.\n"
+                          "    If parseable is specified, output is machine-"
+                          "readable."));
+               return 0;
+       }
 
        if (argc == 0) {
 
-               d_printf("\nService      pid     machine       "
-                        "Connected at\n");
-               d_printf("-------------------------------------"
-                        "------------------\n");
-
-               tdb = tdb_open_log(lock_path("connections.tdb"), 0,
-                                  TDB_DEFAULT, O_RDONLY, 0);
-
-               if (tdb == NULL) {
-                       d_printf("%s not initialised\n",
-                                lock_path("connections.tdb"));
-                       d_printf("This is normal if no SMB client has ever "
-                                "connected to your server.\n");
-                       return -1;
-               }
+               d_printf(_("\nService      pid     machine       "
+                          "Connected at\n"
+                          "-------------------------------------"
+                          "------------------\n"));
 
-               tdb_traverse(tdb, show_share, NULL);
-               tdb_close(tdb);
+               connections_forall_read(show_share, NULL);
 
                return 0;
        }
 
        if ((argc != 1) || !strequal(argv[0], "parseable")) {
-               return net_help_status(argc, argv);
+               return net_status_usage(c, argc, argv);
        }
 
-       return net_status_shares_parseable(argc, argv);
+       return net_status_shares_parseable(c, argc, argv);
 }
 
-int net_status(int argc, const char **argv)
+int net_status(struct net_context *c, int argc, const char **argv)
 {
        struct functable func[] = {
-               {"sessions", net_status_sessions},
-               {"shares", net_status_shares},
-               {NULL, NULL}
+               {
+                       "sessions",
+                       net_status_sessions,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Show list of open sessions"),
+                       N_("net status sessions [parseable]\n"
+                          "    If parseable is specified, output is presented "
+                          "in a machine-parseable fashion.")
+               },
+               {
+                       "shares",
+                       net_status_shares,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Show list of open shares"),
+                       N_("net status shares [parseable]\n"
+                          "    If parseable is specified, output is presented "
+                          "in a machine-parseable fashion.")
+               },
+               {NULL, NULL, 0, NULL, NULL}
        };
-       return net_run_function(argc, argv, func, net_help_status);
+       return net_run_function(c, argc, argv, "net status", func);
 }