| 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 | #include "session.h"
|
|---|
| 22 | #include "messages.h"
|
|---|
| 23 |
|
|---|
| 24 | int net_status_usage(struct net_context *c, int argc, const char **argv)
|
|---|
| 25 | {
|
|---|
| 26 | d_printf(_(" net status sessions [parseable] "
|
|---|
| 27 | "Show list of open sessions\n"));
|
|---|
| 28 | d_printf(_(" net status shares [parseable] "
|
|---|
| 29 | "Show list of open shares\n"));
|
|---|
| 30 | return -1;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | static int show_session(const char *key, struct sessionid *session,
|
|---|
| 34 | void *private_data)
|
|---|
| 35 | {
|
|---|
| 36 | bool *parseable = (bool *)private_data;
|
|---|
| 37 |
|
|---|
| 38 | if (!process_exists(session->pid)) {
|
|---|
| 39 | return 0;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | if (*parseable) {
|
|---|
| 43 | d_printf("%s\\%s\\%s\\%s\\%s\n",
|
|---|
| 44 | procid_str_static(&session->pid),
|
|---|
| 45 | uidtoname(session->uid),
|
|---|
| 46 | gidtoname(session->gid),
|
|---|
| 47 | session->remote_machine, session->hostname);
|
|---|
| 48 | } else {
|
|---|
| 49 | d_printf("%7s %-12s %-12s %-12s (%s)\n",
|
|---|
| 50 | procid_str_static(&session->pid),
|
|---|
| 51 | uidtoname(session->uid),
|
|---|
| 52 | gidtoname(session->gid),
|
|---|
| 53 | session->remote_machine, session->hostname);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | return 0;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | static int net_status_sessions(struct net_context *c, int argc, const char **argv)
|
|---|
| 60 | {
|
|---|
| 61 | bool parseable;
|
|---|
| 62 |
|
|---|
| 63 | if (c->display_usage) {
|
|---|
| 64 | d_printf( "%s\n"
|
|---|
| 65 | "net status sessions [parseable]\n"
|
|---|
| 66 | " %s\n",
|
|---|
| 67 | _("Usage:"),
|
|---|
| 68 | _("Display open user sessions.\n"
|
|---|
| 69 | " If parseable is specified, output is machine-"
|
|---|
| 70 | "readable."));
|
|---|
| 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 | "-------------------------------------------"
|
|---|
| 86 | "------------------------\n"));
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | sessionid_traverse_read(show_session, &parseable);
|
|---|
| 90 | return 0;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | static int show_share(struct db_record *rec,
|
|---|
| 94 | const struct connections_key *key,
|
|---|
| 95 | const struct connections_data *crec,
|
|---|
| 96 | void *state)
|
|---|
| 97 | {
|
|---|
| 98 | if (crec->cnum == -1)
|
|---|
| 99 | return 0;
|
|---|
| 100 |
|
|---|
| 101 | if (!process_exists(crec->pid)) {
|
|---|
| 102 | return 0;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | d_printf("%-10.10s %s %-12s %s",
|
|---|
| 106 | crec->servicename, procid_str_static(&crec->pid),
|
|---|
| 107 | crec->machine,
|
|---|
| 108 | time_to_asc(crec->start));
|
|---|
| 109 |
|
|---|
| 110 | return 0;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | struct sessionids {
|
|---|
| 114 | int num_entries;
|
|---|
| 115 | struct sessionid *entries;
|
|---|
| 116 | };
|
|---|
| 117 |
|
|---|
| 118 | static int collect_pids(const char *key, struct sessionid *session,
|
|---|
| 119 | void *private_data)
|
|---|
| 120 | {
|
|---|
| 121 | struct sessionids *ids = (struct sessionids *)private_data;
|
|---|
| 122 |
|
|---|
| 123 | if (!process_exists(session->pid))
|
|---|
| 124 | return 0;
|
|---|
| 125 |
|
|---|
| 126 | ids->num_entries += 1;
|
|---|
| 127 | ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
|
|---|
| 128 | if (!ids->entries) {
|
|---|
| 129 | ids->num_entries = 0;
|
|---|
| 130 | return 0;
|
|---|
| 131 | }
|
|---|
| 132 | ids->entries[ids->num_entries-1] = *session;
|
|---|
| 133 |
|
|---|
| 134 | return 0;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | static int show_share_parseable(const struct connections_key *key,
|
|---|
| 138 | const struct connections_data *crec,
|
|---|
| 139 | void *state)
|
|---|
| 140 | {
|
|---|
| 141 | struct sessionids *ids = (struct sessionids *)state;
|
|---|
| 142 | int i;
|
|---|
| 143 | bool guest = true;
|
|---|
| 144 |
|
|---|
| 145 | if (crec->cnum == -1)
|
|---|
| 146 | return 0;
|
|---|
| 147 |
|
|---|
| 148 | if (!process_exists(crec->pid)) {
|
|---|
| 149 | return 0;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | for (i=0; i<ids->num_entries; i++) {
|
|---|
| 153 | struct server_id id = ids->entries[i].pid;
|
|---|
| 154 | if (procid_equal(&id, &crec->pid)) {
|
|---|
| 155 | guest = false;
|
|---|
| 156 | break;
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
|
|---|
| 161 | crec->servicename,procid_str_static(&crec->pid),
|
|---|
| 162 | guest ? "" : uidtoname(ids->entries[i].uid),
|
|---|
| 163 | guest ? "" : gidtoname(ids->entries[i].gid),
|
|---|
| 164 | crec->machine,
|
|---|
| 165 | guest ? "" : ids->entries[i].hostname,
|
|---|
| 166 | time_to_asc(crec->start));
|
|---|
| 167 |
|
|---|
| 168 | return 0;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
|
|---|
| 172 | {
|
|---|
| 173 | struct sessionids ids;
|
|---|
| 174 |
|
|---|
| 175 | ids.num_entries = 0;
|
|---|
| 176 | ids.entries = NULL;
|
|---|
| 177 |
|
|---|
| 178 | sessionid_traverse_read(collect_pids, &ids);
|
|---|
| 179 |
|
|---|
| 180 | connections_forall_read(show_share_parseable, &ids);
|
|---|
| 181 |
|
|---|
| 182 | SAFE_FREE(ids.entries);
|
|---|
| 183 |
|
|---|
| 184 | return 0;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | static int net_status_shares(struct net_context *c, int argc, const char **argv)
|
|---|
| 188 | {
|
|---|
| 189 | if (c->display_usage) {
|
|---|
| 190 | d_printf( "%s\n"
|
|---|
| 191 | "net status shares [parseable]\n"
|
|---|
| 192 | " %s\n",
|
|---|
| 193 | _("Usage:"),
|
|---|
| 194 | _("Display open user shares.\n"
|
|---|
| 195 | " If parseable is specified, output is machine-"
|
|---|
| 196 | "readable."));
|
|---|
| 197 | return 0;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | if (argc == 0) {
|
|---|
| 201 |
|
|---|
| 202 | d_printf(_("\nService pid machine "
|
|---|
| 203 | "Connected at\n"
|
|---|
| 204 | "-------------------------------------"
|
|---|
| 205 | "------------------\n"));
|
|---|
| 206 |
|
|---|
| 207 | connections_forall(show_share, NULL);
|
|---|
| 208 |
|
|---|
| 209 | return 0;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | if ((argc != 1) || !strequal(argv[0], "parseable")) {
|
|---|
| 213 | return net_status_usage(c, argc, argv);
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | return net_status_shares_parseable(c, argc, argv);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | int net_status(struct net_context *c, int argc, const char **argv)
|
|---|
| 220 | {
|
|---|
| 221 | struct functable func[] = {
|
|---|
| 222 | {
|
|---|
| 223 | "sessions",
|
|---|
| 224 | net_status_sessions,
|
|---|
| 225 | NET_TRANSPORT_LOCAL,
|
|---|
| 226 | N_("Show list of open sessions"),
|
|---|
| 227 | N_("net status sessions [parseable]\n"
|
|---|
| 228 | " If parseable is specified, output is presented "
|
|---|
| 229 | "in a machine-parseable fashion.")
|
|---|
| 230 | },
|
|---|
| 231 | {
|
|---|
| 232 | "shares",
|
|---|
| 233 | net_status_shares,
|
|---|
| 234 | NET_TRANSPORT_LOCAL,
|
|---|
| 235 | N_("Show list of open shares"),
|
|---|
| 236 | N_("net status shares [parseable]\n"
|
|---|
| 237 | " If parseable is specified, output is presented "
|
|---|
| 238 | "in a machine-parseable fashion.")
|
|---|
| 239 | },
|
|---|
| 240 | {NULL, NULL, 0, NULL, NULL}
|
|---|
| 241 | };
|
|---|
| 242 | return net_run_function(c, argc, argv, "net status", func);
|
|---|
| 243 | }
|
|---|