kcc: Fix typos in original samba_kcc
[Samba.git] / source3 / utils / net_status.c
blob9dbb1a45847401e3dce97d8a2c947dac8a685859
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 "utils/net.h"
21 #include "session.h"
22 #include "messages.h"
23 #include "lib/conn_tdb.h"
25 int net_status_usage(struct net_context *c, int argc, const char **argv)
27 d_printf(_(" net status sessions [parseable] "
28 "Show list of open sessions\n"));
29 d_printf(_(" net status shares [parseable] "
30 "Show list of open shares\n"));
31 return -1;
34 static int show_session(const char *key, struct sessionid *session,
35 void *private_data)
37 struct server_id_buf tmp;
38 bool *parseable = (bool *)private_data;
40 if (!process_exists(session->pid)) {
41 return 0;
44 if (*parseable) {
45 d_printf("%s\\%s\\%s\\%s\\%s\n",
46 server_id_str_buf(session->pid, &tmp),
47 uidtoname(session->uid),
48 gidtoname(session->gid),
49 session->remote_machine, session->hostname);
50 } else {
51 d_printf("%7s %-12s %-12s %-12s (%s)\n",
52 server_id_str_buf(session->pid, &tmp),
53 uidtoname(session->uid),
54 gidtoname(session->gid),
55 session->remote_machine, session->hostname);
58 return 0;
61 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
63 bool parseable;
65 if (c->display_usage) {
66 d_printf( "%s\n"
67 "net status sessions [parseable]\n"
68 " %s\n",
69 _("Usage:"),
70 _("Display open user sessions.\n"
71 " If parseable is specified, output is machine-"
72 "readable."));
73 return 0;
76 if (argc == 0) {
77 parseable = false;
78 } else if ((argc == 1) && strequal(argv[0], "parseable")) {
79 parseable = true;
80 } else {
81 return net_status_usage(c, argc, argv);
84 if (!parseable) {
85 d_printf(_("PID Username Group Machine"
86 " \n"
87 "-------------------------------------------"
88 "------------------------\n"));
91 sessionid_traverse_read(show_session, &parseable);
92 return 0;
95 static int show_share(const struct connections_key *key,
96 const struct connections_data *crec,
97 void *state)
99 struct server_id_buf tmp;
101 if (crec->cnum == TID_FIELD_INVALID)
102 return 0;
104 if (!process_exists(crec->pid)) {
105 return 0;
108 d_printf("%-10.10s %s %-12s %s",
109 crec->servicename, server_id_str_buf(crec->pid, &tmp),
110 crec->machine,
111 time_to_asc(crec->start));
113 return 0;
116 struct sessionids {
117 int num_entries;
118 struct sessionid *entries;
121 static int collect_pids(const char *key, struct sessionid *session,
122 void *private_data)
124 struct sessionids *ids = (struct sessionids *)private_data;
126 if (!process_exists(session->pid))
127 return 0;
129 ids->num_entries += 1;
130 ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
131 if (!ids->entries) {
132 ids->num_entries = 0;
133 return 0;
135 ids->entries[ids->num_entries-1] = *session;
137 return 0;
140 static int show_share_parseable(const struct connections_key *key,
141 const struct connections_data *crec,
142 void *state)
144 struct sessionids *ids = (struct sessionids *)state;
145 struct server_id_buf tmp;
146 int i;
147 bool guest = true;
149 if (crec->cnum == TID_FIELD_INVALID)
150 return 0;
152 if (!process_exists(crec->pid)) {
153 return 0;
156 for (i=0; i<ids->num_entries; i++) {
157 struct server_id id = ids->entries[i].pid;
158 if (serverid_equal(&id, &crec->pid)) {
159 guest = false;
160 break;
164 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
165 crec->servicename, server_id_str_buf(crec->pid, &tmp),
166 guest ? "" : uidtoname(ids->entries[i].uid),
167 guest ? "" : gidtoname(ids->entries[i].gid),
168 crec->machine,
169 guest ? "" : ids->entries[i].hostname,
170 time_to_asc(crec->start));
172 return 0;
175 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
177 struct sessionids ids;
179 ids.num_entries = 0;
180 ids.entries = NULL;
182 sessionid_traverse_read(collect_pids, &ids);
184 connections_forall_read(show_share_parseable, &ids);
186 SAFE_FREE(ids.entries);
188 return 0;
191 static int net_status_shares(struct net_context *c, int argc, const char **argv)
193 if (c->display_usage) {
194 d_printf( "%s\n"
195 "net status shares [parseable]\n"
196 " %s\n",
197 _("Usage:"),
198 _("Display open user shares.\n"
199 " If parseable is specified, output is machine-"
200 "readable."));
201 return 0;
204 if (argc == 0) {
206 d_printf(_("\nService pid machine "
207 "Connected at\n"
208 "-------------------------------------"
209 "------------------\n"));
211 connections_forall_read(show_share, NULL);
213 return 0;
216 if ((argc != 1) || !strequal(argv[0], "parseable")) {
217 return net_status_usage(c, argc, argv);
220 return net_status_shares_parseable(c, argc, argv);
223 int net_status(struct net_context *c, int argc, const char **argv)
225 struct functable func[] = {
227 "sessions",
228 net_status_sessions,
229 NET_TRANSPORT_LOCAL,
230 N_("Show list of open sessions"),
231 N_("net status sessions [parseable]\n"
232 " If parseable is specified, output is presented "
233 "in a machine-parseable fashion.")
236 "shares",
237 net_status_shares,
238 NET_TRANSPORT_LOCAL,
239 N_("Show list of open shares"),
240 N_("net status shares [parseable]\n"
241 " If parseable is specified, output is presented "
242 "in a machine-parseable fashion.")
244 {NULL, NULL, 0, NULL, NULL}
246 return net_run_function(c, argc, argv, "net status", func);