summaryrefslogtreecommitdiffstats
path: root/source3/utils/net_status.c
blob: d6027433a29c3708a4bc94ea5b64a525aa297f4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
   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 "utils/net.h"
#include "session.h"
#include "messages.h"

int net_status_usage(struct net_context *c, int argc, const char **argv)
{
	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)
{
	bool *parseable = (bool *)private_data;

	if (!process_exists(session->pid)) {
		return 0;
	}

	if (*parseable) {
		d_printf("%s\\%s\\%s\\%s\\%s\n",
			 procid_str_static(&session->pid),
			 uidtoname(session->uid),
			 gidtoname(session->gid),
			 session->remote_machine, session->hostname);
	} else {
		d_printf("%7s   %-12s  %-12s  %-12s (%s)\n",
			 procid_str_static(&session->pid),
			 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) {
		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;
	} else if ((argc == 1) && strequal(argv[0], "parseable")) {
		parseable = true;
	} else {
		return net_status_usage(c, argc, argv);
	}

	if (!parseable) {
		d_printf(_("PID     Username      Group         Machine"
			   "                        \n"
		           "-------------------------------------------"
			   "------------------------\n"));
	}

	sessionid_traverse_read(show_session, &parseable);
	return 0;
}

static int show_share(struct db_record *rec,
		      const struct connections_key *key,
		      const struct connections_data *crec,
		      void *state)
{
	if (crec->cnum == -1)
		return 0;

	if (!process_exists(crec->pid)) {
		return 0;
	}

	d_printf("%-10.10s   %s   %-12s  %s",
	       crec->servicename, procid_str_static(&crec->pid),
	       crec->machine,
	       time_to_asc(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);
	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_key *key,
				const struct connections_data *crec,
				void *state)
{
	struct sessionids *ids = (struct sessionids *)state;
	int i;
	bool guest = true;

	if (crec->cnum == -1)
		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 (procid_equal(&id, &crec->pid)) {
			guest = false;
			break;
		}
	}

	d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
		 crec->servicename,procid_str_static(&crec->pid),
		 guest ? "" : uidtoname(ids->entries[i].uid),
		 guest ? "" : gidtoname(ids->entries[i].gid),
		 crec->machine,
		 guest ? "" : ids->entries[i].hostname,
		 time_to_asc(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) {
		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"
		           "-------------------------------------"
			   "------------------\n"));

		connections_forall(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,
			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(c, argc, argv, "net status", func);
}