2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 12 aug 96: Erik.Devriendt@te6.siemens.be
24 added support for shared memory implementation of share mode locking
26 21-Jul-1998: rsharpe@ns.aus.com (Richard Sharpe)
27 Added -L (locks only) -S (shares only) flags and code
32 * This program reports current SMB connections
39 struct session_record{
44 struct session_record *next;
47 extern int DEBUGLEVEL;
50 static pstring Ucrit_username = ""; /* added by OH */
51 static pid_t Ucrit_pid[100]; /* Ugly !!! */ /* added by OH */
52 static int Ucrit_MaxPid=0; /* added by OH */
53 static unsigned int Ucrit_IsActive = 0; /* added by OH */
54 static int verbose, brief;
55 static int shares_only = 0; /* Added by RJS */
56 static int locks_only = 0; /* Added by RJS */
57 static BOOL processes_only=False;
59 /* we need these because we link to locking*.o */
60 void become_root(BOOL save_dir) {}
61 void unbecome_root(BOOL restore_dir) {}
65 static void Ucrit_addUsername(char *username)
67 pstrcpy(Ucrit_username, username);
68 if(strlen(Ucrit_username) > 0)
72 static unsigned int Ucrit_checkUsername(char *username)
74 if ( !Ucrit_IsActive) return 1;
75 if (strcmp(Ucrit_username,username) ==0) return 1;
79 static void Ucrit_addPid(pid_t pid)
82 if ( !Ucrit_IsActive) return;
83 for (i=0;i<Ucrit_MaxPid;i++)
84 if( pid == Ucrit_pid[i] ) return;
85 Ucrit_pid[Ucrit_MaxPid++] = pid;
88 static unsigned int Ucrit_checkPid(pid_t pid)
91 if ( !Ucrit_IsActive) return 1;
92 for (i=0;i<Ucrit_MaxPid;i++)
93 if( pid == Ucrit_pid[i] ) return 1;
98 static void print_share_mode(share_mode_entry *e, char *fname)
102 printf("Locked files:\n");
103 printf("Pid DenyMode R/W Oplock Name\n");
104 printf("--------------------------------------------------\n");
108 if (Ucrit_checkPid(e->pid)) {
109 printf("%-5d ",(int)e->pid);
110 switch ((e->share_mode>>4)&0xF) {
111 case DENY_NONE: printf("DENY_NONE "); break;
112 case DENY_ALL: printf("DENY_ALL "); break;
113 case DENY_DOS: printf("DENY_DOS "); break;
114 case DENY_READ: printf("DENY_READ "); break;
115 case DENY_WRITE:printf("DENY_WRITE "); break;
117 switch (e->share_mode&0xF) {
118 case 0: printf("RDONLY "); break;
119 case 1: printf("WRONLY "); break;
120 case 2: printf("RDWR "); break;
124 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
125 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
126 printf("EXCLUSIVE+BATCH ");
127 else if (e->op_type & EXCLUSIVE_OPLOCK)
128 printf("EXCLUSIVE ");
129 else if (e->op_type & BATCH_OPLOCK)
131 else if (e->op_type & LEVEL_II_OPLOCK)
136 printf(" %s %s",dos_to_unix(fname,False),
137 asctime(LocalTime((time_t *)&e->time.tv_sec)));
142 /*******************************************************************
143 dump the elements of the profile structure
144 ******************************************************************/
145 static int profile_dump(void)
148 fprintf(stderr,"ERROR: not compiled with profile support\n");
151 if (!profile_setup(True)) {
152 fprintf(stderr,"Failed to initialise profile memory\n");
156 printf("smb_count:\t%u\n", profile_p->smb_count);
157 printf("uid_changes:\t%u\n", profile_p->uid_changes);
163 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf)
165 static pid_t last_pid;
166 struct session_record *ptr;
167 struct connections_data crec;
170 memcpy(&crec, dbuf.dptr, sizeof(crec));
172 if (crec.cnum == -1) return 0;
174 if (!process_exists(crec.pid) || !Ucrit_checkUsername(uidtoname(crec.uid))) {
180 printf("\nSamba version %s\n",VERSION);
181 printf("PID Username Machine Time logged in\n");
182 printf("-------------------------------------------------------------------\n");
188 if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0)) {
189 if (ptr->start > crec.start)
190 ptr->start=crec.start;
196 ptr=(struct session_record *) malloc(sizeof(struct session_record));
199 ptr->start=crec.start;
200 strncpy(ptr->machine,crec.machine,30);
201 ptr->machine[30]='\0';
207 printf("\nSamba version %s\n",VERSION);
208 printf("Service uid gid pid machine\n");
209 printf("----------------------------------------------\n");
213 Ucrit_addPid(crec.pid);
214 if (processes_only) {
215 if (last_pid != crec.pid)
216 printf("%d\n",(int)crec.pid);
217 last_pid = crec.pid; /* XXXX we can still get repeats, have to
218 add a sort at some time */
220 printf("%-10.10s %-8s %-8s %5d %-8s (%s) %s",
221 crec.name,uidtoname(crec.uid),gidtoname(crec.gid),(int)crec.pid,
222 crec.machine,crec.addr,
223 asctime(LocalTime(&crec.start)));
233 int main(int argc, char *argv[])
237 static pstring servicesf = CONFIGFILE;
239 int profile_only = 0;
241 struct session_record *ptr;
244 setup_logging(argv[0],True);
246 charset_initialise();
251 if (getuid() != geteuid()) {
252 printf("smbstatus should not be run setuid\n");
256 while ((c = getopt(argc, argv, "pdLSs:u:bP")) != EOF) {
277 pstrcpy(servicesf, optarg);
279 case 'u': /* added by OH */
280 Ucrit_addUsername(optarg); /* added by OH */
283 fprintf(stderr, "Usage: %s [-P] [-d] [-L] [-p] [-S] [-s configfile] [-u username]\n", *argv); /* changed by OH */
288 if (!lp_load(servicesf,False,False,False)) {
289 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
294 printf("using configfile = %s\n", servicesf);
295 printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
299 return profile_dump();
302 pstrcpy(fname,lp_lockdir());
303 standard_sub_basic(fname);
304 trim_string(fname,"","/");
305 pstrcat(fname,"/connections.tdb");
307 tdb = tdb_open(fname, 0, O_RDONLY, 0);
309 printf("Couldn't open status file %s\n",fname);
311 printf("You need to have status=yes in your smb config file\n");
313 } else if (verbose) {
314 printf("Opened status file %s\n", fname);
317 if (locks_only) goto locks;
319 tdb_traverse(tdb, traverse_fn1);
322 if (processes_only) exit(0);
327 printf("%-8d%-10.10s%-30.30s%s",
328 (int)ptr->pid,uidtoname(ptr->uid),
330 asctime(LocalTime(&(ptr->start))));
340 if (!locking_init(1)) {
341 printf("Can't initialise shared memory - exiting\n");
345 if (share_mode_forall(print_share_mode) <= 0)
346 printf("No locked files\n");
350 share_status(stdout);