2bb639138e9f46a75c5fe5fa5fc90b2336bd110c
[samba.git] / source3 / utils / net_status.c
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)
5
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.
10
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.
15
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/>.  */
18
19 #include "includes.h"
20 #include "utils/net.h"
21 #include "session.h"
22 #include "messages.h"
23 #include "lib/conn_tdb.h"
24
25 int net_status_usage(struct net_context *c, int argc, const char **argv)
26 {
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;
32 }
33
34 static int show_session(const char *key, struct sessionid *session,
35                         void *private_data)
36 {
37         bool *parseable = (bool *)private_data;
38
39         if (!process_exists(session->pid)) {
40                 return 0;
41         }
42
43         if (*parseable) {
44                 d_printf("%s\\%s\\%s\\%s\\%s\n",
45                          procid_str_static(&session->pid),
46                          uidtoname(session->uid),
47                          gidtoname(session->gid),
48                          session->remote_machine, session->hostname);
49         } else {
50                 d_printf("%7s   %-12s  %-12s  %-12s (%s)\n",
51                          procid_str_static(&session->pid),
52                          uidtoname(session->uid),
53                          gidtoname(session->gid),
54                          session->remote_machine, session->hostname);
55         }
56
57         return 0;
58 }
59
60 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
61 {
62         bool parseable;
63
64         if (c->display_usage) {
65                 d_printf(  "%s\n"
66                            "net status sessions [parseable]\n"
67                            "    %s\n",
68                          _("Usage:"),
69                          _("Display open user sessions.\n"
70                            "    If parseable is specified, output is machine-"
71                            "readable."));
72                 return 0;
73         }
74
75         if (argc == 0) {
76                 parseable = false;
77         } else if ((argc == 1) && strequal(argv[0], "parseable")) {
78                 parseable = true;
79         } else {
80                 return net_status_usage(c, argc, argv);
81         }
82
83         if (!parseable) {
84                 d_printf(_("PID     Username      Group         Machine"
85                            "                        \n"
86                            "-------------------------------------------"
87                            "------------------------\n"));
88         }
89
90         sessionid_traverse_read(show_session, &parseable);
91         return 0;
92 }
93
94 static int show_share(struct db_record *rec,
95                       const struct connections_key *key,
96                       const struct connections_data *crec,
97                       void *state)
98 {
99         if (crec->cnum == -1)
100                 return 0;
101
102         if (!process_exists(crec->pid)) {
103                 return 0;
104         }
105
106         d_printf("%-10.10s   %s   %-12s  %s",
107                crec->servicename, procid_str_static(&crec->pid),
108                crec->machine,
109                time_to_asc(crec->start));
110
111         return 0;
112 }
113
114 struct sessionids {
115         int num_entries;
116         struct sessionid *entries;
117 };
118
119 static int collect_pids(const char *key, struct sessionid *session,
120                         void *private_data)
121 {
122         struct sessionids *ids = (struct sessionids *)private_data;
123
124         if (!process_exists(session->pid))
125                 return 0;
126
127         ids->num_entries += 1;
128         ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
129         if (!ids->entries) {
130                 ids->num_entries = 0;
131                 return 0;
132         }
133         ids->entries[ids->num_entries-1] = *session;
134
135         return 0;
136 }
137
138 static int show_share_parseable(const struct connections_key *key,
139                                 const struct connections_data *crec,
140                                 void *state)
141 {
142         struct sessionids *ids = (struct sessionids *)state;
143         int i;
144         bool guest = true;
145
146         if (crec->cnum == -1)
147                 return 0;
148
149         if (!process_exists(crec->pid)) {
150                 return 0;
151         }
152
153         for (i=0; i<ids->num_entries; i++) {
154                 struct server_id id = ids->entries[i].pid;
155                 if (procid_equal(&id, &crec->pid)) {
156                         guest = false;
157                         break;
158                 }
159         }
160
161         d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
162                  crec->servicename,procid_str_static(&crec->pid),
163                  guest ? "" : uidtoname(ids->entries[i].uid),
164                  guest ? "" : gidtoname(ids->entries[i].gid),
165                  crec->machine,
166                  guest ? "" : ids->entries[i].hostname,
167                  time_to_asc(crec->start));
168
169         return 0;
170 }
171
172 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
173 {
174         struct sessionids ids;
175
176         ids.num_entries = 0;
177         ids.entries = NULL;
178
179         sessionid_traverse_read(collect_pids, &ids);
180
181         connections_forall_read(show_share_parseable, &ids);
182
183         SAFE_FREE(ids.entries);
184
185         return 0;
186 }
187
188 static int net_status_shares(struct net_context *c, int argc, const char **argv)
189 {
190         if (c->display_usage) {
191                 d_printf(  "%s\n"
192                            "net status shares [parseable]\n"
193                            "    %s\n",
194                          _("Usage:"),
195                          _("Display open user shares.\n"
196                            "    If parseable is specified, output is machine-"
197                            "readable."));
198                 return 0;
199         }
200
201         if (argc == 0) {
202
203                 d_printf(_("\nService      pid     machine       "
204                            "Connected at\n"
205                            "-------------------------------------"
206                            "------------------\n"));
207
208                 connections_forall(show_share, NULL);
209
210                 return 0;
211         }
212
213         if ((argc != 1) || !strequal(argv[0], "parseable")) {
214                 return net_status_usage(c, argc, argv);
215         }
216
217         return net_status_shares_parseable(c, argc, argv);
218 }
219
220 int net_status(struct net_context *c, int argc, const char **argv)
221 {
222         struct functable func[] = {
223                 {
224                         "sessions",
225                         net_status_sessions,
226                         NET_TRANSPORT_LOCAL,
227                         N_("Show list of open sessions"),
228                         N_("net status sessions [parseable]\n"
229                            "    If parseable is specified, output is presented "
230                            "in a machine-parseable fashion.")
231                 },
232                 {
233                         "shares",
234                         net_status_shares,
235                         NET_TRANSPORT_LOCAL,
236                         N_("Show list of open shares"),
237                         N_("net status shares [parseable]\n"
238                            "    If parseable is specified, output is presented "
239                            "in a machine-parseable fashion.")
240                 },
241                 {NULL, NULL, 0, NULL, NULL}
242         };
243         return net_run_function(c, argc, argv, "net status", func);
244 }