1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-25 23:21:54 +03:00
samba-mirror/source3/utils/net_status.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

247 lines
5.9 KiB
C
Raw Normal View History

/*
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 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, 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 "conn_tdb.h"
int net_status_usage(struct net_context *c, int argc, const char **argv)
{
2009-08-11 10:35:46 +04:00
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;
}
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(session->pid)) {
return 0;
}
if (*parseable) {
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("%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(struct net_context *c, int argc, const char **argv)
{
bool parseable;
if (c->display_usage) {
2010-01-19 13:43:54 +03:00
d_printf( "%s\n"
2009-08-11 10:35:46 +04:00
"net status sessions [parseable]\n"
2010-01-19 13:43:54 +03:00
" %s\n",
_("Usage:"),
_("Display open user sessions.\n"
2009-08-11 10:35:46 +04:00
" If parseable is specified, output is machine-"
2010-01-19 13:43:54 +03:00
"readable."));
return 0;
}
if (argc == 0) {
parseable = false;
} else if ((argc == 1) && strequal(argv[0], "parseable")) {
parseable = true;
} else {
return net_status_usage(c, argc, argv);
}
if (!parseable) {
2009-08-11 10:35:46 +04:00
d_printf(_("PID Username Group Machine"
" \n"
"-------------------------------------------"
"------------------------\n"));
}
sessionid_traverse_read(show_session, &parseable);
return 0;
}
static int show_share(const struct connections_data *crec,
void *state)
{
struct server_id_buf tmp;
if (crec->cnum == TID_FIELD_INVALID)
return 0;
if (!process_exists(crec->pid)) {
return 0;
}
d_printf("%-10.10s %s %-12s %s",
crec->servicename, server_id_str_buf(crec->pid, &tmp),
crec->machine,
time_to_asc(nt_time_to_unix(crec->start)));
return 0;
}
struct sessionids {
int num_entries;
struct sessionid *entries;
};
static int collect_pids(const char *key, struct sessionid *session,
void *private_data)
{
struct sessionids *ids = (struct sessionids *)private_data;
if (!process_exists(session->pid))
return 0;
ids->num_entries += 1;
ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
r13915: Fixed a very interesting class of realloc() bugs found by Coverity. realloc can return NULL in one of two cases - (1) the realloc failed, (2) realloc succeeded but the new size requested was zero, in which case this is identical to a free() call. The error paths dealing with these two cases should be different, but mostly weren't. Secondly the standard idiom for dealing with realloc when you know the new size is non-zero is the following : tmp = realloc(p, size); if (!tmp) { SAFE_FREE(p); return error; } else { p = tmp; } However, there were *many* *many* places in Samba where we were using the old (broken) idiom of : p = realloc(p, size) if (!p) { return error; } which will leak the memory pointed to by p on realloc fail. This commit (hopefully) fixes all these cases by moving to a standard idiom of : p = SMB_REALLOC(p, size) if (!p) { return error; } Where if the realloc returns null due to the realloc failing or size == 0 we *guarentee* that the storage pointed to by p has been freed. This allows me to remove a lot of code that was dealing with the standard (more verbose) method that required a tmp pointer. This is almost always what you want. When a realloc fails you never usually want the old memory, you want to free it and get into your error processing asap. For the 11 remaining cases where we really do need to keep the old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR, which can be used as follows : tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size); if (!tmp) { SAFE_FREE(p); return error; } else { p = tmp; } SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the pointer p, even on size == 0 or realloc fail. All this is done by a hidden extra argument to Realloc(), BOOL free_old_on_error which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR macros (and their array counterparts). It remains to be seen what this will do to our Coverity bug count :-). Jeremy. (This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if (!ids->entries) {
ids->num_entries = 0;
return 0;
}
ids->entries[ids->num_entries-1] = *session;
return 0;
}
static int show_share_parseable(const struct connections_data *crec,
void *state)
{
struct sessionids *ids = (struct sessionids *)state;
struct server_id_buf tmp;
int i;
bool guest = true;
if (crec->cnum == TID_FIELD_INVALID)
return 0;
if (!process_exists(crec->pid)) {
return 0;
}
for (i=0; i<ids->num_entries; i++) {
struct server_id id = ids->entries[i].pid;
if (server_id_equal(&id, &crec->pid)) {
guest = false;
break;
}
}
d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
crec->servicename, server_id_str_buf(crec->pid, &tmp),
guest ? "" : uidtoname(ids->entries[i].uid),
guest ? "" : gidtoname(ids->entries[i].gid),
crec->machine,
guest ? "" : ids->entries[i].hostname,
time_to_asc(nt_time_to_unix(crec->start)));
return 0;
}
static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
{
struct sessionids ids;
ids.num_entries = 0;
ids.entries = NULL;
sessionid_traverse_read(collect_pids, &ids);
connections_forall_read(show_share_parseable, &ids);
SAFE_FREE(ids.entries);
return 0;
}
static int net_status_shares(struct net_context *c, int argc, const char **argv)
{
if (c->display_usage) {
2010-01-19 13:43:54 +03:00
d_printf( "%s\n"
2009-08-11 10:35:46 +04:00
"net status shares [parseable]\n"
2010-01-19 13:43:54 +03:00
" %s\n",
_("Usage:"),
_("Display open user shares.\n"
2009-08-11 10:35:46 +04:00
" If parseable is specified, output is machine-"
2010-01-19 13:43:54 +03:00
"readable."));
return 0;
}
if (argc == 0) {
2009-08-11 10:35:46 +04:00
d_printf(_("\nService pid machine "
"Connected at\n"
"-------------------------------------"
"------------------\n"));
connections_forall_read(show_share, NULL);
return 0;
}
if ((argc != 1) || !strequal(argv[0], "parseable")) {
return net_status_usage(c, argc, argv);
}
return net_status_shares_parseable(c, argc, argv);
}
int net_status(struct net_context *c, int argc, const char **argv)
{
struct functable func[] = {
{
"sessions",
net_status_sessions,
NET_TRANSPORT_LOCAL,
2009-08-11 10:35:46 +04:00
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,
2009-08-11 10:35:46 +04:00
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(c, argc, argv, "net status", func);
}