root/source3/utils/net_status.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. net_status_usage
  2. show_session
  3. net_status_sessions
  4. show_share
  5. collect_pid
  6. show_share_parseable
  7. net_status_shares_parseable
  8. net_status_shares
  9. net_status

   1 /*
   2    Samba Unix/Linux SMB client library
   3    net status command -- possible replacement for smbstatus
   4    Copyright (C) 2003 Volker Lendecke (vl@samba.org)
   5 
   6    This program is free software; you can redistribute it and/or modify
   7    it under the terms of the GNU General Public License as published by
   8    the Free Software Foundation; either version 3 of the License, or
   9    (at your option) any later version.
  10 
  11    This program is distributed in the hope that it will be useful,
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14    GNU General Public License for more details.
  15 
  16    You should have received a copy of the GNU General Public License
  17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
  18 
  19 #include "includes.h"
  20 #include "utils/net.h"
  21 
  22 int net_status_usage(struct net_context *c, int argc, const char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
  23 {
  24         d_printf("  net status sessions [parseable] "
  25                  "Show list of open sessions\n");
  26         d_printf("  net status shares [parseable]   "
  27                  "Show list of open shares\n");
  28         return -1;
  29 }
  30 
  31 static int show_session(struct db_record *rec, void *private_data)
     /* [<][>][^][v][top][bottom][index][help] */
  32 {
  33         bool *parseable = (bool *)private_data;
  34         struct sessionid sessionid;
  35 
  36         if (rec->value.dsize != sizeof(sessionid))
  37                 return 0;
  38 
  39         memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
  40 
  41         if (!process_exists(sessionid.pid)) {
  42                 return 0;
  43         }
  44 
  45         if (*parseable) {
  46                 d_printf("%s\\%s\\%s\\%s\\%s\n",
  47                          procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
  48                          gidtoname(sessionid.gid),
  49                          sessionid.remote_machine, sessionid.hostname);
  50         } else {
  51                 d_printf("%7s   %-12s  %-12s  %-12s (%s)\n",
  52                          procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
  53                          gidtoname(sessionid.gid),
  54                          sessionid.remote_machine, sessionid.hostname);
  55         }
  56 
  57         return 0;
  58 }
  59 
  60 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
  61 {
  62         struct db_context *db;
  63         bool parseable;
  64 
  65         if (c->display_usage) {
  66                 d_printf("Usage:\n"
  67                          "net status sessions [parseable]\n"
  68                          "    Display open user sessions.\n"
  69                          "    If parseable is specified, output is machine-"
  70                          "readable.\n");
  71                 return 0;
  72         }
  73 
  74         if (argc == 0) {
  75                 parseable = false;
  76         } else if ((argc == 1) && strequal(argv[0], "parseable")) {
  77                 parseable = true;
  78         } else {
  79                 return net_status_usage(c, argc, argv);
  80         }
  81 
  82         if (!parseable) {
  83                 d_printf("PID     Username      Group         Machine"
  84                          "                        \n");
  85                 d_printf("-------------------------------------------"
  86                          "------------------------\n");
  87         }
  88 
  89         db = db_open(NULL, lock_path("sessionid.tdb"), 0,
  90                      TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
  91         if (db == NULL) {
  92                 d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
  93                 return -1;
  94         }
  95 
  96         db->traverse_read(db, show_session, &parseable);
  97         TALLOC_FREE(db);
  98 
  99         return 0;
 100 }
 101 
 102 static int show_share(struct db_record *rec,
     /* [<][>][^][v][top][bottom][index][help] */
 103                       const struct connections_key *key,
 104                       const struct connections_data *crec,
 105                       void *state)
 106 {
 107         if (crec->cnum == -1)
 108                 return 0;
 109 
 110         if (!process_exists(crec->pid)) {
 111                 return 0;
 112         }
 113 
 114         d_printf("%-10.10s   %s   %-12s  %s",
 115                crec->servicename, procid_str_static(&crec->pid),
 116                crec->machine,
 117                time_to_asc(crec->start));
 118 
 119         return 0;
 120 }
 121 
 122 struct sessionids {
 123         int num_entries;
 124         struct sessionid *entries;
 125 };
 126 
 127 static int collect_pid(struct db_record *rec, void *private_data)
     /* [<][>][^][v][top][bottom][index][help] */
 128 {
 129         struct sessionids *ids = (struct sessionids *)private_data;
 130         struct sessionid sessionid;
 131 
 132         if (rec->value.dsize != sizeof(sessionid))
 133                 return 0;
 134 
 135         memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
 136 
 137         if (!process_exists(sessionid.pid))
 138                 return 0;
 139 
 140         ids->num_entries += 1;
 141         ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
 142         if (!ids->entries) {
 143                 ids->num_entries = 0;
 144                 return 0;
 145         }
 146         ids->entries[ids->num_entries-1] = sessionid;
 147 
 148         return 0;
 149 }
 150 
 151 static int show_share_parseable(struct db_record *rec,
     /* [<][>][^][v][top][bottom][index][help] */
 152                                 const struct connections_key *key,
 153                                 const struct connections_data *crec,
 154                                 void *state)
 155 {
 156         struct sessionids *ids = (struct sessionids *)state;
 157         int i;
 158         bool guest = true;
 159 
 160         if (crec->cnum == -1)
 161                 return 0;
 162 
 163         if (!process_exists(crec->pid)) {
 164                 return 0;
 165         }
 166 
 167         for (i=0; i<ids->num_entries; i++) {
 168                 struct server_id id = ids->entries[i].pid;
 169                 if (procid_equal(&id, &crec->pid)) {
 170                         guest = false;
 171                         break;
 172                 }
 173         }
 174 
 175         d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
 176                  crec->servicename,procid_str_static(&crec->pid),
 177                  guest ? "" : uidtoname(ids->entries[i].uid),
 178                  guest ? "" : gidtoname(ids->entries[i].gid),
 179                  crec->machine,
 180                  guest ? "" : ids->entries[i].hostname,
 181                  time_to_asc(crec->start));
 182 
 183         return 0;
 184 }
 185 
 186 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
 187 {
 188         struct sessionids ids;
 189         struct db_context *db;
 190 
 191         ids.num_entries = 0;
 192         ids.entries = NULL;
 193 
 194         db = db_open(NULL, lock_path("sessionid.tdb"), 0,
 195                      TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
 196         if (db == NULL) {
 197                 d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
 198                 return -1;
 199         }
 200 
 201         db->traverse_read(db, collect_pid, &ids);
 202         TALLOC_FREE(db);
 203 
 204         connections_forall(show_share_parseable, &ids);
 205 
 206         SAFE_FREE(ids.entries);
 207 
 208         return 0;
 209 }
 210 
 211 static int net_status_shares(struct net_context *c, int argc, const char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
 212 {
 213         if (c->display_usage) {
 214                 d_printf("Usage:\n"
 215                          "net status shares [parseable]\n"
 216                          "    Display open user shares.\n"
 217                          "    If parseable is specified, output is machine-"
 218                          "readable.\n");
 219                 return 0;
 220         }
 221 
 222         if (argc == 0) {
 223 
 224                 d_printf("\nService      pid     machine       "
 225                          "Connected at\n");
 226                 d_printf("-------------------------------------"
 227                          "------------------\n");
 228 
 229                 connections_forall(show_share, NULL);
 230 
 231                 return 0;
 232         }
 233 
 234         if ((argc != 1) || !strequal(argv[0], "parseable")) {
 235                 return net_status_usage(c, argc, argv);
 236         }
 237 
 238         return net_status_shares_parseable(c, argc, argv);
 239 }
 240 
 241 int net_status(struct net_context *c, int argc, const char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
 242 {
 243         struct functable func[] = {
 244                 {
 245                         "sessions",
 246                         net_status_sessions,
 247                         NET_TRANSPORT_LOCAL,
 248                         "Show list of open sessions",
 249                         "net status sessions [parseable]\n"
 250                         "    If parseable is specified, output is presented "
 251                         "in a machine-parseable fashion."
 252                 },
 253                 {
 254                         "shares",
 255                         net_status_shares,
 256                         NET_TRANSPORT_LOCAL,
 257                         "Show list of open shares",
 258                         "net status shares [parseable]\n"
 259                         "    If parseable is specified, output is presented "
 260                         "in a machine-parseable fashion."
 261                 },
 262                 {NULL, NULL, 0, NULL, NULL}
 263         };
 264         return net_run_function(c, argc, argv, "net status", func);
 265 }

/* [<][>][^][v][top][bottom][index][help] */