9dbb1a45847401e3dce97d8a2c947dac8a685859
[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         struct server_id_buf tmp;
38         bool *parseable = (bool *)private_data;
39
40         if (!process_exists(session->pid)) {
41                 return 0;
42         }
43
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);
56         }
57
58         return 0;
59 }
60
61 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
62 {
63         bool parseable;
64
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;
74         }
75
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);
82         }
83
84         if (!parseable) {
85                 d_printf(_("PID     Username      Group         Machine"
86                            "                        \n"
87                            "-------------------------------------------"
88                            "------------------------\n"));
89         }
90
91         sessionid_traverse_read(show_session, &parseable);
92         return 0;
93 }
94
95 static int show_share(const struct connections_key *key,
96                       const struct connections_data *crec,
97                       void *state)
98 {
99         struct server_id_buf tmp;
100
101         if (crec->cnum == TID_FIELD_INVALID)
102                 return 0;
103
104         if (!process_exists(crec->pid)) {
105                 return 0;
106         }
107
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));
112
113         return 0;
114 }
115
116 struct sessionids {
117         int num_entries;
118         struct sessionid *entries;
119 };
120
121 static int collect_pids(const char *key, struct sessionid *session,
122                         void *private_data)
123 {
124         struct sessionids *ids = (struct sessionids *)private_data;
125
126         if (!process_exists(session->pid))
127                 return 0;
128
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;
134         }
135         ids->entries[ids->num_entries-1] = *session;
136
137         return 0;
138 }
139
140 static int show_share_parseable(const struct connections_key *key,
141                                 const struct connections_data *crec,
142                                 void *state)
143 {
144         struct sessionids *ids = (struct sessionids *)state;
145         struct server_id_buf tmp;
146         int i;
147         bool guest = true;
148
149         if (crec->cnum == TID_FIELD_INVALID)
150                 return 0;
151
152         if (!process_exists(crec->pid)) {
153                 return 0;
154         }
155
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;
161                 }
162         }
163
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));
171
172         return 0;
173 }
174
175 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
176 {
177         struct sessionids ids;
178
179         ids.num_entries = 0;
180         ids.entries = NULL;
181
182         sessionid_traverse_read(collect_pids, &ids);
183
184         connections_forall_read(show_share_parseable, &ids);
185
186         SAFE_FREE(ids.entries);
187
188         return 0;
189 }
190
191 static int net_status_shares(struct net_context *c, int argc, const char **argv)
192 {
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;
202         }
203
204         if (argc == 0) {
205
206                 d_printf(_("\nService      pid     machine       "
207                            "Connected at\n"
208                            "-------------------------------------"
209                            "------------------\n"));
210
211                 connections_forall_read(show_share, NULL);
212
213                 return 0;
214         }
215
216         if ((argc != 1) || !strequal(argv[0], "parseable")) {
217                 return net_status_usage(c, argc, argv);
218         }
219
220         return net_status_shares_parseable(c, argc, argv);
221 }
222
223 int net_status(struct net_context *c, int argc, const char **argv)
224 {
225         struct functable func[] = {
226                 {
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.")
234                 },
235                 {
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.")
243                 },
244                 {NULL, NULL, 0, NULL, NULL}
245         };
246         return net_run_function(c, argc, argv, "net status", func);
247 }