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