s4:rpc_server/lsa: base dcesrv_lsa_LookupNames2() on dcesrv_lsa_LookupNames_common()
[Samba.git] / source3 / utils / net_status.c
blob0d658a0c36e0eb632a925fb4b1f2a204a802e461
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)
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.
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.
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/>. */
19 #include "includes.h"
20 #include "lib/util/server_id.h"
21 #include "utils/net.h"
22 #include "session.h"
23 #include "messages.h"
24 #include "lib/conn_tdb.h"
26 int net_status_usage(struct net_context *c, int argc, const char **argv)
28 d_printf(_(" net status sessions [parseable] "
29 "Show list of open sessions\n"));
30 d_printf(_(" net status shares [parseable] "
31 "Show list of open shares\n"));
32 return -1;
35 static int show_session(const char *key, struct sessionid *session,
36 void *private_data)
38 struct server_id_buf tmp;
39 bool *parseable = (bool *)private_data;
41 if (!process_exists(session->pid)) {
42 return 0;
45 if (*parseable) {
46 d_printf("%s\\%s\\%s\\%s\\%s\n",
47 server_id_str_buf(session->pid, &tmp),
48 uidtoname(session->uid),
49 gidtoname(session->gid),
50 session->remote_machine, session->hostname);
51 } else {
52 d_printf("%7s %-12s %-12s %-12s (%s)\n",
53 server_id_str_buf(session->pid, &tmp),
54 uidtoname(session->uid),
55 gidtoname(session->gid),
56 session->remote_machine, session->hostname);
59 return 0;
62 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
64 bool parseable;
66 if (c->display_usage) {
67 d_printf( "%s\n"
68 "net status sessions [parseable]\n"
69 " %s\n",
70 _("Usage:"),
71 _("Display open user sessions.\n"
72 " If parseable is specified, output is machine-"
73 "readable."));
74 return 0;
77 if (argc == 0) {
78 parseable = false;
79 } else if ((argc == 1) && strequal(argv[0], "parseable")) {
80 parseable = true;
81 } else {
82 return net_status_usage(c, argc, argv);
85 if (!parseable) {
86 d_printf(_("PID Username Group Machine"
87 " \n"
88 "-------------------------------------------"
89 "------------------------\n"));
92 sessionid_traverse_read(show_session, &parseable);
93 return 0;
96 static int show_share(const struct connections_key *key,
97 const struct connections_data *crec,
98 void *state)
100 struct server_id_buf tmp;
102 if (crec->cnum == TID_FIELD_INVALID)
103 return 0;
105 if (!process_exists(crec->pid)) {
106 return 0;
109 d_printf("%-10.10s %s %-12s %s",
110 crec->servicename, server_id_str_buf(crec->pid, &tmp),
111 crec->machine,
112 time_to_asc(crec->start));
114 return 0;
117 struct sessionids {
118 int num_entries;
119 struct sessionid *entries;
122 static int collect_pids(const char *key, struct sessionid *session,
123 void *private_data)
125 struct sessionids *ids = (struct sessionids *)private_data;
127 if (!process_exists(session->pid))
128 return 0;
130 ids->num_entries += 1;
131 ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
132 if (!ids->entries) {
133 ids->num_entries = 0;
134 return 0;
136 ids->entries[ids->num_entries-1] = *session;
138 return 0;
141 static int show_share_parseable(const struct connections_key *key,
142 const struct connections_data *crec,
143 void *state)
145 struct sessionids *ids = (struct sessionids *)state;
146 struct server_id_buf tmp;
147 int i;
148 bool guest = true;
150 if (crec->cnum == TID_FIELD_INVALID)
151 return 0;
153 if (!process_exists(crec->pid)) {
154 return 0;
157 for (i=0; i<ids->num_entries; i++) {
158 struct server_id id = ids->entries[i].pid;
159 if (serverid_equal(&id, &crec->pid)) {
160 guest = false;
161 break;
165 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
166 crec->servicename, server_id_str_buf(crec->pid, &tmp),
167 guest ? "" : uidtoname(ids->entries[i].uid),
168 guest ? "" : gidtoname(ids->entries[i].gid),
169 crec->machine,
170 guest ? "" : ids->entries[i].hostname,
171 time_to_asc(crec->start));
173 return 0;
176 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
178 struct sessionids ids;
180 ids.num_entries = 0;
181 ids.entries = NULL;
183 sessionid_traverse_read(collect_pids, &ids);
185 connections_forall_read(show_share_parseable, &ids);
187 SAFE_FREE(ids.entries);
189 return 0;
192 static int net_status_shares(struct net_context *c, int argc, const char **argv)
194 if (c->display_usage) {
195 d_printf( "%s\n"
196 "net status shares [parseable]\n"
197 " %s\n",
198 _("Usage:"),
199 _("Display open user shares.\n"
200 " If parseable is specified, output is machine-"
201 "readable."));
202 return 0;
205 if (argc == 0) {
207 d_printf(_("\nService pid machine "
208 "Connected at\n"
209 "-------------------------------------"
210 "------------------\n"));
212 connections_forall_read(show_share, NULL);
214 return 0;
217 if ((argc != 1) || !strequal(argv[0], "parseable")) {
218 return net_status_usage(c, argc, argv);
221 return net_status_shares_parseable(c, argc, argv);
224 int net_status(struct net_context *c, int argc, const char **argv)
226 struct functable func[] = {
228 "sessions",
229 net_status_sessions,
230 NET_TRANSPORT_LOCAL,
231 N_("Show list of open sessions"),
232 N_("net status sessions [parseable]\n"
233 " If parseable is specified, output is presented "
234 "in a machine-parseable fashion.")
237 "shares",
238 net_status_shares,
239 NET_TRANSPORT_LOCAL,
240 N_("Show list of open shares"),
241 N_("net status shares [parseable]\n"
242 " If parseable is specified, output is presented "
243 "in a machine-parseable fashion.")
245 {NULL, NULL, 0, NULL, NULL}
247 return net_run_function(c, argc, argv, "net status", func);