auth/credentials: don't ignore "client use kerberos" and --use-kerberos for machine...
[Samba.git] / source3 / utils / net_status.c
bloba22b45cc490dbe18e097444ff2e27cab5d69a4e4
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 "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_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(nt_time_to_unix(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_data *crec,
141 void *state)
143 struct sessionids *ids = (struct sessionids *)state;
144 struct server_id_buf tmp;
145 int i;
146 bool guest = true;
148 if (crec->cnum == TID_FIELD_INVALID)
149 return 0;
151 if (!process_exists(crec->pid)) {
152 return 0;
155 for (i=0; i<ids->num_entries; i++) {
156 struct server_id id = ids->entries[i].pid;
157 if (server_id_equal(&id, &crec->pid)) {
158 guest = false;
159 break;
163 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
164 crec->servicename, server_id_str_buf(crec->pid, &tmp),
165 guest ? "" : uidtoname(ids->entries[i].uid),
166 guest ? "" : gidtoname(ids->entries[i].gid),
167 crec->machine,
168 guest ? "" : ids->entries[i].hostname,
169 time_to_asc(nt_time_to_unix(crec->start)));
171 return 0;
174 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
176 struct sessionids ids;
178 ids.num_entries = 0;
179 ids.entries = NULL;
181 sessionid_traverse_read(collect_pids, &ids);
183 connections_forall_read(show_share_parseable, &ids);
185 SAFE_FREE(ids.entries);
187 return 0;
190 static int net_status_shares(struct net_context *c, int argc, const char **argv)
192 if (c->display_usage) {
193 d_printf( "%s\n"
194 "net status shares [parseable]\n"
195 " %s\n",
196 _("Usage:"),
197 _("Display open user shares.\n"
198 " If parseable is specified, output is machine-"
199 "readable."));
200 return 0;
203 if (argc == 0) {
205 d_printf(_("\nService pid machine "
206 "Connected at\n"
207 "-------------------------------------"
208 "------------------\n"));
210 connections_forall_read(show_share, NULL);
212 return 0;
215 if ((argc != 1) || !strequal(argv[0], "parseable")) {
216 return net_status_usage(c, argc, argv);
219 return net_status_shares_parseable(c, argc, argv);
222 int net_status(struct net_context *c, int argc, const char **argv)
224 struct functable func[] = {
226 "sessions",
227 net_status_sessions,
228 NET_TRANSPORT_LOCAL,
229 N_("Show list of open sessions"),
230 N_("net status sessions [parseable]\n"
231 " If parseable is specified, output is presented "
232 "in a machine-parseable fashion.")
235 "shares",
236 net_status_shares,
237 NET_TRANSPORT_LOCAL,
238 N_("Show list of open shares"),
239 N_("net status shares [parseable]\n"
240 " If parseable is specified, output is presented "
241 "in a machine-parseable fashion.")
243 {NULL, NULL, 0, NULL, NULL}
245 return net_run_function(c, argc, argv, "net status", func);