s3: Rename new parameter "ldap ref follow" to "ldap follow referral".
[Samba/gebeck_regimport.git] / source3 / utils / net_status.c
blobe7f3d33d557db1d8bf1efc52e048a0a75d49a679
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 "utils/net.h"
22 int net_status_usage(struct net_context *c, int argc, const char **argv)
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;
31 static int show_session(struct db_record *rec, void *private_data)
33 bool *parseable = (bool *)private_data;
34 struct sessionid sessionid;
36 if (rec->value.dsize != sizeof(sessionid))
37 return 0;
39 memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
41 if (!process_exists(sessionid.pid)) {
42 return 0;
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);
57 return 0;
60 static int net_status_sessions(struct net_context *c, int argc, const char **argv)
62 struct db_context *db;
63 bool parseable;
65 if (c->display_usage) {
66 d_printf(_("Usage:\n"
67 "net status sessions [parseable]\n"
68 " Display open user sessions.\n"
69 " If parseable is specified, output is machine-"
70 "readable.\n"));
71 return 0;
74 if (argc == 0) {
75 parseable = false;
76 } else if ((argc == 1) && strequal(argv[0], "parseable")) {
77 parseable = true;
78 } else {
79 return net_status_usage(c, argc, argv);
82 if (!parseable) {
83 d_printf(_("PID Username Group Machine"
84 " \n"
85 "-------------------------------------------"
86 "------------------------\n"));
89 db = db_open(NULL, lock_path("sessionid.tdb"), 0,
90 TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
91 if (db == NULL) {
92 d_fprintf(stderr, _("%s not initialised\n"),
93 lock_path("sessionid.tdb"));
94 return -1;
97 db->traverse_read(db, show_session, &parseable);
98 TALLOC_FREE(db);
100 return 0;
103 static int show_share(struct db_record *rec,
104 const struct connections_key *key,
105 const struct connections_data *crec,
106 void *state)
108 if (crec->cnum == -1)
109 return 0;
111 if (!process_exists(crec->pid)) {
112 return 0;
115 d_printf("%-10.10s %s %-12s %s",
116 crec->servicename, procid_str_static(&crec->pid),
117 crec->machine,
118 time_to_asc(crec->start));
120 return 0;
123 struct sessionids {
124 int num_entries;
125 struct sessionid *entries;
128 static int collect_pid(struct db_record *rec, void *private_data)
130 struct sessionids *ids = (struct sessionids *)private_data;
131 struct sessionid sessionid;
133 if (rec->value.dsize != sizeof(sessionid))
134 return 0;
136 memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
138 if (!process_exists(sessionid.pid))
139 return 0;
141 ids->num_entries += 1;
142 ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
143 if (!ids->entries) {
144 ids->num_entries = 0;
145 return 0;
147 ids->entries[ids->num_entries-1] = sessionid;
149 return 0;
152 static int show_share_parseable(struct db_record *rec,
153 const struct connections_key *key,
154 const struct connections_data *crec,
155 void *state)
157 struct sessionids *ids = (struct sessionids *)state;
158 int i;
159 bool guest = true;
161 if (crec->cnum == -1)
162 return 0;
164 if (!process_exists(crec->pid)) {
165 return 0;
168 for (i=0; i<ids->num_entries; i++) {
169 struct server_id id = ids->entries[i].pid;
170 if (procid_equal(&id, &crec->pid)) {
171 guest = false;
172 break;
176 d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
177 crec->servicename,procid_str_static(&crec->pid),
178 guest ? "" : uidtoname(ids->entries[i].uid),
179 guest ? "" : gidtoname(ids->entries[i].gid),
180 crec->machine,
181 guest ? "" : ids->entries[i].hostname,
182 time_to_asc(crec->start));
184 return 0;
187 static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
189 struct sessionids ids;
190 struct db_context *db;
192 ids.num_entries = 0;
193 ids.entries = NULL;
195 db = db_open(NULL, lock_path("sessionid.tdb"), 0,
196 TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
197 if (db == NULL) {
198 d_fprintf(stderr, _("%s not initialised\n"),
199 lock_path("sessionid.tdb"));
200 return -1;
203 db->traverse_read(db, collect_pid, &ids);
204 TALLOC_FREE(db);
206 connections_forall(show_share_parseable, &ids);
208 SAFE_FREE(ids.entries);
210 return 0;
213 static int net_status_shares(struct net_context *c, int argc, const char **argv)
215 if (c->display_usage) {
216 d_printf(_("Usage:\n"
217 "net status shares [parseable]\n"
218 " Display open user shares.\n"
219 " If parseable is specified, output is machine-"
220 "readable.\n"));
221 return 0;
224 if (argc == 0) {
226 d_printf(_("\nService pid machine "
227 "Connected at\n"
228 "-------------------------------------"
229 "------------------\n"));
231 connections_forall(show_share, NULL);
233 return 0;
236 if ((argc != 1) || !strequal(argv[0], "parseable")) {
237 return net_status_usage(c, argc, argv);
240 return net_status_shares_parseable(c, argc, argv);
243 int net_status(struct net_context *c, int argc, const char **argv)
245 struct functable func[] = {
247 "sessions",
248 net_status_sessions,
249 NET_TRANSPORT_LOCAL,
250 N_("Show list of open sessions"),
251 N_("net status sessions [parseable]\n"
252 " If parseable is specified, output is presented "
253 "in a machine-parseable fashion.")
256 "shares",
257 net_status_shares,
258 NET_TRANSPORT_LOCAL,
259 N_("Show list of open shares"),
260 N_("net status shares [parseable]\n"
261 " If parseable is specified, output is presented "
262 "in a machine-parseable fashion.")
264 {NULL, NULL, 0, NULL, NULL}
266 return net_run_function(c, argc, argv, "net status", func);