47860cb5849dc591d9b60d6ac1b2031de148d53a
[ira/wip.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
22 int net_status_usage(struct net_context *c, int argc, const char **argv)
23 {
24         d_printf(_("  net status sessions [parseable] "
25                    "Show list of open sessions\n"));
26         d_printf(_("  net status shares [parseable]   "
27                    "Show list of open shares\n"));
28         return -1;
29 }
30
31 static int show_session(struct db_record *rec, void *private_data)
32 {
33         bool *parseable = (bool *)private_data;
34         struct sessionid sessionid;
35
36         if (rec->value.dsize != sizeof(sessionid))
37                 return 0;
38
39         memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
40
41         if (!process_exists(sessionid.pid)) {
42                 return 0;
43         }
44
45         if (*parseable) {
46                 d_printf("%s\\%s\\%s\\%s\\%s\n",
47                          procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
48                          gidtoname(sessionid.gid),
49                          sessionid.remote_machine, sessionid.hostname);
50         } else {
51                 d_printf("%7s   %-12s  %-12s  %-12s (%s)\n",
52                          procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
53                          gidtoname(sessionid.gid),
54                          sessionid.remote_machine, sessionid.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         struct db_context *db;
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         db = db_open(NULL, lock_path("sessionid.tdb"), 0,
92                      TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
93         if (db == NULL) {
94                 d_fprintf(stderr, _("%s not initialised\n"),
95                           lock_path("sessionid.tdb"));
96                 return -1;
97         }
98
99         db->traverse_read(db, show_session, &parseable);
100         TALLOC_FREE(db);
101
102         return 0;
103 }
104
105 static int show_share(struct db_record *rec,
106                       const struct connections_key *key,
107                       const struct connections_data *crec,
108                       void *state)
109 {
110         if (crec->cnum == -1)
111                 return 0;
112
113         if (!process_exists(crec->pid)) {
114                 return 0;
115         }
116
117         d_printf("%-10.10s   %s   %-12s  %s",
118                crec->servicename, procid_str_static(&crec->pid),
119                crec->machine,
120                time_to_asc(crec->start));
121
122         return 0;
123 }
124
125 struct sessionids {
126         int num_entries;
127         struct sessionid *entries;
128 };
129
130 static int collect_pid(struct db_record *rec, void *private_data)
131 {
132         struct sessionids *ids = (struct sessionids *)private_data;
133         struct sessionid sessionid;
134
135         if (rec->value.dsize != sizeof(sessionid))
136                 return 0;
137
138         memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
139
140         if (!process_exists(sessionid.pid))
141                 return 0;
142
143         ids->num_entries += 1;
144         ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
145         if (!ids->entries) {
146                 ids->num_entries = 0;
147                 return 0;
148         }
149         ids->entries[ids->num_entries-1] = sessionid;
150
151         return 0;
152 }
153
154 static int show_share_parseable(const struct connections_key *key,
155                                 const struct connections_data *crec,
156                                 void *state)
157 {
158         struct sessionids *ids = (struct sessionids *)state;
159         int i;
160         bool guest = true;
161
162         if (crec->cnum == -1)
163                 return 0;
164
165         if (!process_exists(crec->pid)) {
166                 return 0;
167         }
168
169         for (i=0; i<ids->num_entries; i++) {
170                 struct server_id id = ids->entries[i].pid;
171                 if (procid_equal(&id, &crec->pid)) {
172                         guest = false;
173                         break;
174                 }
175         }
176
177         d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
178                  crec->servicename,procid_str_static(&crec->pid),
179                  guest ? "" : uidtoname(ids->entries[i].uid),
180                  guest ? "" : gidtoname(ids->entries[i].gid),
181                  crec->machine,
182                  guest ? "" : ids->entries[i].hostname,
183                  time_to_asc(crec->start));
184
185         return 0;
186 }
187
188 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
189 {
190         struct sessionids ids;
191         struct db_context *db;
192
193         ids.num_entries = 0;
194         ids.entries = NULL;
195
196         db = db_open(NULL, lock_path("sessionid.tdb"), 0,
197                      TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
198         if (db == NULL) {
199                 d_fprintf(stderr, _("%s not initialised\n"),
200                           lock_path("sessionid.tdb"));
201                 return -1;
202         }
203
204         db->traverse_read(db, collect_pid, &ids);
205         TALLOC_FREE(db);
206
207         connections_forall_read(show_share_parseable, &ids);
208
209         SAFE_FREE(ids.entries);
210
211         return 0;
212 }
213
214 static int net_status_shares(struct net_context *c, int argc, const char **argv)
215 {
216         if (c->display_usage) {
217                 d_printf(  "%s\n"
218                            "net status shares [parseable]\n"
219                            "    %s\n",
220                          _("Usage:"),
221                          _("Display open user shares.\n"
222                            "    If parseable is specified, output is machine-"
223                            "readable."));
224                 return 0;
225         }
226
227         if (argc == 0) {
228
229                 d_printf(_("\nService      pid     machine       "
230                            "Connected at\n"
231                            "-------------------------------------"
232                            "------------------\n"));
233
234                 connections_forall(show_share, NULL);
235
236                 return 0;
237         }
238
239         if ((argc != 1) || !strequal(argv[0], "parseable")) {
240                 return net_status_usage(c, argc, argv);
241         }
242
243         return net_status_shares_parseable(c, argc, argv);
244 }
245
246 int net_status(struct net_context *c, int argc, const char **argv)
247 {
248         struct functable func[] = {
249                 {
250                         "sessions",
251                         net_status_sessions,
252                         NET_TRANSPORT_LOCAL,
253                         N_("Show list of open sessions"),
254                         N_("net status sessions [parseable]\n"
255                            "    If parseable is specified, output is presented "
256                            "in a machine-parseable fashion.")
257                 },
258                 {
259                         "shares",
260                         net_status_shares,
261                         NET_TRANSPORT_LOCAL,
262                         N_("Show list of open shares"),
263                         N_("net status shares [parseable]\n"
264                            "    If parseable is specified, output is presented "
265                            "in a machine-parseable fashion.")
266                 },
267                 {NULL, NULL, 0, NULL, NULL}
268         };
269         return net_run_function(c, argc, argv, "net status", func);
270 }