r23779: Change from v2 or later to v3 or later.
[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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "includes.h"
21 #include "utils/net.h"
22
23 static int show_session(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
24                         void *state)
25 {
26         BOOL *parseable = (BOOL *)state;
27         struct sessionid sessionid;
28
29         if (dbuf.dsize != sizeof(sessionid))
30                 return 0;
31
32         memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
33
34         if (!process_exists(sessionid.pid)) {
35                 return 0;
36         }
37
38         if (*parseable) {
39                 d_printf("%s\\%s\\%s\\%s\\%s\n",
40                          procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
41                          gidtoname(sessionid.gid), 
42                          sessionid.remote_machine, sessionid.hostname);
43         } else {
44                 d_printf("%7s   %-12s  %-12s  %-12s (%s)\n",
45                          procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
46                          gidtoname(sessionid.gid), 
47                          sessionid.remote_machine, sessionid.hostname);
48         }
49
50         return 0;
51 }
52
53 static int net_status_sessions(int argc, const char **argv)
54 {
55         TDB_CONTEXT *tdb;
56         BOOL parseable;
57
58         if (argc == 0) {
59                 parseable = False;
60         } else if ((argc == 1) && strequal(argv[0], "parseable")) {
61                 parseable = True;
62         } else {
63                 return net_help_status(argc, argv);
64         }
65
66         if (!parseable) {
67                 d_printf("PID     Username      Group         Machine"
68                          "                        \n");
69                 d_printf("-------------------------------------------"
70                          "------------------------\n");
71         }
72
73         tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
74                            TDB_DEFAULT, O_RDONLY, 0);
75
76         if (tdb == NULL) {
77                 d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
78                 return -1;
79         }
80
81         tdb_traverse(tdb, show_session, &parseable);
82         tdb_close(tdb);
83
84         return 0;
85 }
86
87 static int show_share(struct db_record *rec,
88                       const struct connections_key *key,
89                       const struct connections_data *crec,
90                       void *state)
91 {
92         if (crec->cnum == -1)
93                 return 0;
94
95         if (!process_exists(crec->pid)) {
96                 return 0;
97         }
98
99         d_printf("%-10.10s   %s   %-12s  %s",
100                crec->servicename, procid_str_static(&crec->pid),
101                crec->machine,
102                time_to_asc(crec->start));
103
104         return 0;
105 }
106
107 struct sessionids {
108         int num_entries;
109         struct sessionid *entries;
110 };
111
112 static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
113                        void *state)
114 {
115         struct sessionids *ids = (struct sessionids *)state;
116         struct sessionid sessionid;
117
118         if (dbuf.dsize != sizeof(sessionid))
119                 return 0;
120
121         memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
122
123         if (!process_exists(sessionid.pid)) 
124                 return 0;
125
126         ids->num_entries += 1;
127         ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
128         if (!ids->entries) {
129                 ids->num_entries = 0;
130                 return 0;
131         }
132         ids->entries[ids->num_entries-1] = sessionid;
133
134         return 0;
135 }
136
137 static int show_share_parseable(struct db_record *rec,
138                                 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(int argc, const char **argv)
173 {
174         struct sessionids ids;
175         TDB_CONTEXT *tdb;
176
177         ids.num_entries = 0;
178         ids.entries = NULL;
179
180         tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
181                            TDB_DEFAULT, O_RDONLY, 0);
182
183         if (tdb == NULL) {
184                 d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
185                 return -1;
186         }
187
188         tdb_traverse(tdb, collect_pid, &ids);
189         tdb_close(tdb);
190
191         connections_forall(show_share_parseable, &ids);
192
193         SAFE_FREE(ids.entries);
194
195         return 0;
196 }
197
198 static int net_status_shares(int argc, const char **argv)
199 {
200         if (argc == 0) {
201
202                 d_printf("\nService      pid     machine       "
203                          "Connected at\n");
204                 d_printf("-------------------------------------"
205                          "------------------\n");
206
207                 connections_forall(show_share, NULL);
208
209                 return 0;
210         }
211
212         if ((argc != 1) || !strequal(argv[0], "parseable")) {
213                 return net_help_status(argc, argv);
214         }
215
216         return net_status_shares_parseable(argc, argv);
217 }
218
219 int net_status(int argc, const char **argv)
220 {
221         struct functable func[] = {
222                 {"sessions", net_status_sessions},
223                 {"shares", net_status_shares},
224                 {NULL, NULL}
225         };
226         return net_run_function(argc, argv, func, net_help_status);
227 }