| 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 2 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, write to the Free Software
|
|---|
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|---|
| 19 |
|
|---|
| 20 | #include "includes.h"
|
|---|
| 21 | #include "utils/net.h"
|
|---|
| 22 |
|
|---|
| 23 | static int show_session(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
|
|---|
| 24 | void *state)
|
|---|
| 25 | {
|
|---|
| 26 | BOOL *parseable = (BOOL *)state;
|
|---|
| 27 | struct sessionid sessionid;
|
|---|
| 28 |
|
|---|
| 29 | if (dbuf.dsize != sizeof(sessionid))
|
|---|
| 30 | return 0;
|
|---|
| 31 |
|
|---|
| 32 | memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
|
|---|
| 33 |
|
|---|
| 34 | if (!process_exists_by_pid(sessionid.pid)) {
|
|---|
| 35 | return 0;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | if (*parseable) {
|
|---|
| 39 | d_printf("%d\\%s\\%s\\%s\\%s\n",
|
|---|
| 40 | (int)sessionid.pid, uidtoname(sessionid.uid),
|
|---|
| 41 | gidtoname(sessionid.gid),
|
|---|
| 42 | sessionid.remote_machine, sessionid.hostname);
|
|---|
| 43 | } else {
|
|---|
| 44 | d_printf("%5d %-12s %-12s %-12s (%s)\n",
|
|---|
| 45 | (int)sessionid.pid, uidtoname(sessionid.uid),
|
|---|
| 46 | gidtoname(sessionid.gid),
|
|---|
| 47 | sessionid.remote_machine, sessionid.hostname);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | return 0;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | static int net_status_sessions(int argc, const char **argv)
|
|---|
| 54 | {
|
|---|
| 55 | TDB_CONTEXT *tdb;
|
|---|
| 56 | BOOL parseable;
|
|---|
| 57 |
|
|---|
| 58 | if (argc == 0) {
|
|---|
| 59 | parseable = False;
|
|---|
| 60 | } else if ((argc == 1) && strequal(argv[0], "parseable")) {
|
|---|
| 61 | parseable = True;
|
|---|
| 62 | } else {
|
|---|
| 63 | return net_help_status(argc, argv);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | if (!parseable) {
|
|---|
| 67 | d_printf("PID Username Group Machine"
|
|---|
| 68 | " \n");
|
|---|
| 69 | d_printf("-------------------------------------------"
|
|---|
| 70 | "------------------------\n");
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
|
|---|
| 74 | TDB_DEFAULT, O_RDONLY, 0);
|
|---|
| 75 |
|
|---|
| 76 | if (tdb == NULL) {
|
|---|
| 77 | d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
|
|---|
| 78 | return -1;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | tdb_traverse(tdb, show_session, &parseable);
|
|---|
| 82 | tdb_close(tdb);
|
|---|
| 83 |
|
|---|
| 84 | return 0;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | static int show_share(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
|
|---|
| 88 | void *state)
|
|---|
| 89 | {
|
|---|
| 90 | struct connections_data crec;
|
|---|
| 91 |
|
|---|
| 92 | if (dbuf.dsize != sizeof(crec))
|
|---|
| 93 | return 0;
|
|---|
| 94 |
|
|---|
| 95 | memcpy(&crec, dbuf.dptr, sizeof(crec));
|
|---|
| 96 |
|
|---|
| 97 | if (crec.cnum == -1)
|
|---|
| 98 | return 0;
|
|---|
| 99 |
|
|---|
| 100 | if (!process_exists(crec.pid)) {
|
|---|
| 101 | return 0;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | d_printf("%-10.10s %s %-12s %s",
|
|---|
| 105 | crec.servicename,procid_str_static(&crec.pid),
|
|---|
| 106 | crec.machine,
|
|---|
| 107 | time_to_asc(crec.start));
|
|---|
| 108 |
|
|---|
| 109 | return 0;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | struct sessionids {
|
|---|
| 113 | int num_entries;
|
|---|
| 114 | struct sessionid *entries;
|
|---|
| 115 | };
|
|---|
| 116 |
|
|---|
| 117 | static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
|
|---|
| 118 | void *state)
|
|---|
| 119 | {
|
|---|
| 120 | struct sessionids *ids = (struct sessionids *)state;
|
|---|
| 121 | struct sessionid sessionid;
|
|---|
| 122 |
|
|---|
| 123 | if (dbuf.dsize != sizeof(sessionid))
|
|---|
| 124 | return 0;
|
|---|
| 125 |
|
|---|
| 126 | memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
|
|---|
| 127 |
|
|---|
| 128 | if (!process_exists_by_pid(sessionid.pid))
|
|---|
| 129 | return 0;
|
|---|
| 130 |
|
|---|
| 131 | ids->num_entries += 1;
|
|---|
| 132 | ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
|
|---|
| 133 | if (!ids->entries) {
|
|---|
| 134 | ids->num_entries = 0;
|
|---|
| 135 | return 0;
|
|---|
| 136 | }
|
|---|
| 137 | ids->entries[ids->num_entries-1] = sessionid;
|
|---|
| 138 |
|
|---|
| 139 | return 0;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | static int show_share_parseable(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
|
|---|
| 143 | void *state)
|
|---|
| 144 | {
|
|---|
| 145 | struct sessionids *ids = (struct sessionids *)state;
|
|---|
| 146 | struct connections_data crec;
|
|---|
| 147 | int i;
|
|---|
| 148 | BOOL guest = True;
|
|---|
| 149 |
|
|---|
| 150 | if (dbuf.dsize != sizeof(crec))
|
|---|
| 151 | return 0;
|
|---|
| 152 |
|
|---|
| 153 | memcpy(&crec, dbuf.dptr, sizeof(crec));
|
|---|
| 154 |
|
|---|
| 155 | if (crec.cnum == -1)
|
|---|
| 156 | return 0;
|
|---|
| 157 |
|
|---|
| 158 | if (!process_exists(crec.pid)) {
|
|---|
| 159 | return 0;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | for (i=0; i<ids->num_entries; i++) {
|
|---|
| 163 | struct process_id id = pid_to_procid(ids->entries[i].pid);
|
|---|
| 164 | if (procid_equal(&id, &crec.pid)) {
|
|---|
| 165 | guest = False;
|
|---|
| 166 | break;
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
|
|---|
| 171 | crec.servicename,procid_str_static(&crec.pid),
|
|---|
| 172 | guest ? "" : uidtoname(ids->entries[i].uid),
|
|---|
| 173 | guest ? "" : gidtoname(ids->entries[i].gid),
|
|---|
| 174 | crec.machine,
|
|---|
| 175 | guest ? "" : ids->entries[i].hostname,
|
|---|
| 176 | time_to_asc(crec.start));
|
|---|
| 177 |
|
|---|
| 178 | return 0;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | static int net_status_shares_parseable(int argc, const char **argv)
|
|---|
| 182 | {
|
|---|
| 183 | struct sessionids ids;
|
|---|
| 184 | TDB_CONTEXT *tdb;
|
|---|
| 185 |
|
|---|
| 186 | ids.num_entries = 0;
|
|---|
| 187 | ids.entries = NULL;
|
|---|
| 188 |
|
|---|
| 189 | tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
|
|---|
| 190 | TDB_DEFAULT, O_RDONLY, 0);
|
|---|
| 191 |
|
|---|
| 192 | if (tdb == NULL) {
|
|---|
| 193 | d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
|
|---|
| 194 | return -1;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | tdb_traverse(tdb, collect_pid, &ids);
|
|---|
| 198 | tdb_close(tdb);
|
|---|
| 199 |
|
|---|
| 200 | tdb = tdb_open_log(lock_path("connections.tdb"), 0,
|
|---|
| 201 | TDB_DEFAULT, O_RDONLY, 0);
|
|---|
| 202 |
|
|---|
| 203 | if (tdb == NULL) {
|
|---|
| 204 | d_fprintf(stderr, "%s not initialised\n", lock_path("connections.tdb"));
|
|---|
| 205 | d_fprintf(stderr, "This is normal if no SMB client has ever "
|
|---|
| 206 | "connected to your server.\n");
|
|---|
| 207 | return -1;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | tdb_traverse(tdb, show_share_parseable, &ids);
|
|---|
| 211 | tdb_close(tdb);
|
|---|
| 212 |
|
|---|
| 213 | SAFE_FREE(ids.entries);
|
|---|
| 214 |
|
|---|
| 215 | return 0;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | static int net_status_shares(int argc, const char **argv)
|
|---|
| 219 | {
|
|---|
| 220 | TDB_CONTEXT *tdb;
|
|---|
| 221 |
|
|---|
| 222 | if (argc == 0) {
|
|---|
| 223 |
|
|---|
| 224 | d_printf("\nService pid machine "
|
|---|
| 225 | "Connected at\n");
|
|---|
| 226 | d_printf("-------------------------------------"
|
|---|
| 227 | "------------------\n");
|
|---|
| 228 |
|
|---|
| 229 | tdb = tdb_open_log(lock_path("connections.tdb"), 0,
|
|---|
| 230 | TDB_DEFAULT, O_RDONLY, 0);
|
|---|
| 231 |
|
|---|
| 232 | if (tdb == NULL) {
|
|---|
| 233 | d_fprintf(stderr, "%s not initialised\n",
|
|---|
| 234 | lock_path("connections.tdb"));
|
|---|
| 235 | d_fprintf(stderr, "This is normal if no SMB client has "
|
|---|
| 236 | "ever connected to your server.\n");
|
|---|
| 237 | return -1;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | tdb_traverse(tdb, show_share, NULL);
|
|---|
| 241 | tdb_close(tdb);
|
|---|
| 242 |
|
|---|
| 243 | return 0;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | if ((argc != 1) || !strequal(argv[0], "parseable")) {
|
|---|
| 247 | return net_help_status(argc, argv);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | return net_status_shares_parseable(argc, argv);
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | int net_status(int argc, const char **argv)
|
|---|
| 254 | {
|
|---|
| 255 | struct functable func[] = {
|
|---|
| 256 | {"sessions", net_status_sessions},
|
|---|
| 257 | {"shares", net_status_shares},
|
|---|
| 258 | {NULL, NULL}
|
|---|
| 259 | };
|
|---|
| 260 | return net_run_function(argc, argv, func, net_help_status);
|
|---|
| 261 | }
|
|---|