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 | static int show_session(struct db_record *rec, void *private_data)
|
---|
23 | {
|
---|
24 | bool *parseable = (bool *)private_data;
|
---|
25 | struct sessionid sessionid;
|
---|
26 |
|
---|
27 | if (rec->value.dsize != sizeof(sessionid))
|
---|
28 | return 0;
|
---|
29 |
|
---|
30 | memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
|
---|
31 |
|
---|
32 | if (!process_exists(sessionid.pid)) {
|
---|
33 | return 0;
|
---|
34 | }
|
---|
35 |
|
---|
36 | if (*parseable) {
|
---|
37 | d_printf("%s\\%s\\%s\\%s\\%s\n",
|
---|
38 | procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
|
---|
39 | gidtoname(sessionid.gid),
|
---|
40 | sessionid.remote_machine, sessionid.hostname);
|
---|
41 | } else {
|
---|
42 | d_printf("%7s %-12s %-12s %-12s (%s)\n",
|
---|
43 | procid_str_static(&sessionid.pid), uidtoname(sessionid.uid),
|
---|
44 | gidtoname(sessionid.gid),
|
---|
45 | sessionid.remote_machine, sessionid.hostname);
|
---|
46 | }
|
---|
47 |
|
---|
48 | return 0;
|
---|
49 | }
|
---|
50 |
|
---|
51 | static int net_status_sessions(int argc, const char **argv)
|
---|
52 | {
|
---|
53 | struct db_context *db;
|
---|
54 | bool parseable;
|
---|
55 |
|
---|
56 | if (argc == 0) {
|
---|
57 | parseable = False;
|
---|
58 | } else if ((argc == 1) && strequal(argv[0], "parseable")) {
|
---|
59 | parseable = True;
|
---|
60 | } else {
|
---|
61 | return net_help_status(argc, argv);
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (!parseable) {
|
---|
65 | d_printf("PID Username Group Machine"
|
---|
66 | " \n");
|
---|
67 | d_printf("-------------------------------------------"
|
---|
68 | "------------------------\n");
|
---|
69 | }
|
---|
70 |
|
---|
71 | db = db_open(NULL, lock_path("sessionid.tdb"), 0,
|
---|
72 | TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
|
---|
73 | if (db == NULL) {
|
---|
74 | d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
|
---|
75 | return -1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | db->traverse_read(db, show_session, &parseable);
|
---|
79 | TALLOC_FREE(db);
|
---|
80 |
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static int show_share(struct db_record *rec,
|
---|
85 | const struct connections_key *key,
|
---|
86 | const struct connections_data *crec,
|
---|
87 | void *state)
|
---|
88 | {
|
---|
89 | if (crec->cnum == -1)
|
---|
90 | return 0;
|
---|
91 |
|
---|
92 | if (!process_exists(crec->pid)) {
|
---|
93 | return 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | d_printf("%-10.10s %s %-12s %s",
|
---|
97 | crec->servicename, procid_str_static(&crec->pid),
|
---|
98 | crec->machine,
|
---|
99 | time_to_asc(crec->start));
|
---|
100 |
|
---|
101 | return 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 | struct sessionids {
|
---|
105 | int num_entries;
|
---|
106 | struct sessionid *entries;
|
---|
107 | };
|
---|
108 |
|
---|
109 | static int collect_pid(struct db_record *rec, void *private_data)
|
---|
110 | {
|
---|
111 | struct sessionids *ids = (struct sessionids *)private_data;
|
---|
112 | struct sessionid sessionid;
|
---|
113 |
|
---|
114 | if (rec->value.dsize != sizeof(sessionid))
|
---|
115 | return 0;
|
---|
116 |
|
---|
117 | memcpy(&sessionid, rec->value.dptr, sizeof(sessionid));
|
---|
118 |
|
---|
119 | if (!process_exists(sessionid.pid))
|
---|
120 | return 0;
|
---|
121 |
|
---|
122 | ids->num_entries += 1;
|
---|
123 | ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
|
---|
124 | if (!ids->entries) {
|
---|
125 | ids->num_entries = 0;
|
---|
126 | return 0;
|
---|
127 | }
|
---|
128 | ids->entries[ids->num_entries-1] = sessionid;
|
---|
129 |
|
---|
130 | return 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static int show_share_parseable(struct db_record *rec,
|
---|
134 | const struct connections_key *key,
|
---|
135 | const struct connections_data *crec,
|
---|
136 | void *state)
|
---|
137 | {
|
---|
138 | struct sessionids *ids = (struct sessionids *)state;
|
---|
139 | int i;
|
---|
140 | bool guest = True;
|
---|
141 |
|
---|
142 | if (crec->cnum == -1)
|
---|
143 | return 0;
|
---|
144 |
|
---|
145 | if (!process_exists(crec->pid)) {
|
---|
146 | return 0;
|
---|
147 | }
|
---|
148 |
|
---|
149 | for (i=0; i<ids->num_entries; i++) {
|
---|
150 | struct server_id id = ids->entries[i].pid;
|
---|
151 | if (procid_equal(&id, &crec->pid)) {
|
---|
152 | guest = False;
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
|
---|
158 | crec->servicename,procid_str_static(&crec->pid),
|
---|
159 | guest ? "" : uidtoname(ids->entries[i].uid),
|
---|
160 | guest ? "" : gidtoname(ids->entries[i].gid),
|
---|
161 | crec->machine,
|
---|
162 | guest ? "" : ids->entries[i].hostname,
|
---|
163 | time_to_asc(crec->start));
|
---|
164 |
|
---|
165 | return 0;
|
---|
166 | }
|
---|
167 |
|
---|
168 | static int net_status_shares_parseable(int argc, const char **argv)
|
---|
169 | {
|
---|
170 | struct sessionids ids;
|
---|
171 | struct db_context *db;
|
---|
172 |
|
---|
173 | ids.num_entries = 0;
|
---|
174 | ids.entries = NULL;
|
---|
175 |
|
---|
176 | db = db_open(NULL, lock_path("sessionid.tdb"), 0,
|
---|
177 | TDB_CLEAR_IF_FIRST, O_RDONLY, 0644);
|
---|
178 | if (db == NULL) {
|
---|
179 | d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
|
---|
180 | return -1;
|
---|
181 | }
|
---|
182 |
|
---|
183 | db->traverse_read(db, collect_pid, &ids);
|
---|
184 | TALLOC_FREE(db);
|
---|
185 |
|
---|
186 | connections_forall(show_share_parseable, &ids);
|
---|
187 |
|
---|
188 | SAFE_FREE(ids.entries);
|
---|
189 |
|
---|
190 | return 0;
|
---|
191 | }
|
---|
192 |
|
---|
193 | static int net_status_shares(int argc, const char **argv)
|
---|
194 | {
|
---|
195 | if (argc == 0) {
|
---|
196 |
|
---|
197 | d_printf("\nService pid machine "
|
---|
198 | "Connected at\n");
|
---|
199 | d_printf("-------------------------------------"
|
---|
200 | "------------------\n");
|
---|
201 |
|
---|
202 | connections_forall(show_share, NULL);
|
---|
203 |
|
---|
204 | return 0;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if ((argc != 1) || !strequal(argv[0], "parseable")) {
|
---|
208 | return net_help_status(argc, argv);
|
---|
209 | }
|
---|
210 |
|
---|
211 | return net_status_shares_parseable(argc, argv);
|
---|
212 | }
|
---|
213 |
|
---|
214 | int net_status(int argc, const char **argv)
|
---|
215 | {
|
---|
216 | struct functable func[] = {
|
---|
217 | {"sessions", net_status_sessions},
|
---|
218 | {"shares", net_status_shares},
|
---|
219 | {NULL, NULL}
|
---|
220 | };
|
---|
221 | return net_run_function(argc, argv, func, net_help_status);
|
---|
222 | }
|
---|