2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1994-1995
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 * This program reports current SMB connections
33 struct connect_record crec;
34 extern int DEBUGLEVEL;
36 extern pstring myhostname;
38 static pstring Ucrit_username = ""; /* added by OH */
39 int Ucrit_pid[100]; /* Ugly !!! */ /* added by OH */
40 int Ucrit_MaxPid=0; /* added by OH */
41 unsigned int Ucrit_IsActive = 0; /* added by OH */
42 void Ucrit_addUsername(pstring username); /* added by OH */
43 unsigned int Ucrit_checkUsername(pstring username); /* added by OH */
44 void Ucrit_addPid(int pid); /* added by OH */
45 unsigned int Ucrit_checkPid(int pid); /* added by OH */
47 int main(int argc, char *argv[])
52 static pstring servicesf = CONFIGFILE;
58 BOOL processes_only=False;
62 setup_logging(argv[0],True);
67 dbf = fopen("/dev/null","w");
69 if (getuid() != geteuid()) {
70 printf("smbstatus should not be run setuid\n");
74 while ((c = getopt(argc, argv, "pds:u:")) != EOF) {
83 strcpy(servicesf, optarg);
85 case 'u': /* added by OH */
86 Ucrit_addUsername(optarg); /* added by OH */
89 fprintf(stderr, "Usage: %s [-d] [-p] [-s configfile] [-u username]\n", *argv); /* changed by OH */
94 if (!lp_load(servicesf,False)) {
95 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
99 get_myname(myhostname, NULL);
102 printf("using configfile = %s\n", servicesf);
103 printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
106 strcpy(fname,lp_lockdir());
107 standard_sub_basic(fname);
108 trim_string(fname,"","/");
109 strcat(fname,"/STATUS..LCK");
111 f = fopen(fname,"r");
113 printf("Couldn't open status file %s\n",fname);
115 printf("You need to have status=yes in your smb config file\n");
119 printf("Opened status file %s\n", fname);
124 if (!processes_only) {
125 printf("\nSamba version %s\n",VERSION);
127 printf("Service uid gid pid machine\n");
128 printf("----------------------------------------------\n");
133 if (fread(&crec,sizeof(crec),1,f) != 1)
135 if ( crec.magic == 0x280267 && process_exists(crec.pid)
136 && Ucrit_checkUsername(uidtoname(crec.uid)) /* added by OH */
139 Ucrit_addPid(crec.pid); /* added by OH */
140 if (processes_only) {
141 if (last_pid != crec.pid)
142 printf("%d\n",crec.pid);
143 last_pid = crec.pid; /* XXXX we can still get repeats, have to
144 add a sort at some time */
147 printf("%-10.10s %-8s %-8s %5d %-8s (%s) %s",
148 crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
149 crec.machine,crec.addr,
150 asctime(LocalTime(&crec.start)));
155 if (processes_only) exit(0);
159 dir = opendir(lp_lockdir());
161 while ((s=readdirname(dir))) {
169 if (sscanf(s,"share.%d.%d",&dev,&inode)!=2) continue;
171 strcpy(lname,lp_lockdir());
172 trim_string(lname,NULL,"/");
176 fd = open(lname,O_RDONLY,0);
177 if (fd < 0) continue;
178 if (read(fd,buf,16) != 16) continue;
179 n = read(fd,fname,sizeof(fname));
187 if ( !Ucrit_checkPid(pid) ) /* added by OH */
190 if (IVAL(buf,12) != LOCKING_VERSION || !process_exists(pid)) {
191 if (unlink(lname)==0)
192 printf("Deleted stale share file %s\n",s);
196 fname[sizeof(fname)-1] = 0;
200 printf("Locked files:\n");
201 printf("Pid DenyMode R/W Name\n");
202 printf("------------------------------\n");
207 switch ((mode>>4)&0xF)
209 case DENY_NONE: printf("DENY_NONE "); break;
210 case DENY_ALL: printf("DENY_ALL "); break;
211 case DENY_DOS: printf("DENY_DOS "); break;
212 case DENY_READ: printf("DENY_READ "); break;
213 case DENY_WRITE:printf("DENY_WRITE "); break;
217 case 0: printf("RDONLY "); break;
218 case 1: printf("WRONLY "); break;
219 case 2: printf("RDWR "); break;
221 printf(" %s %s",fname,asctime(LocalTime(&t)));
226 printf("No locked files\n");
232 void Ucrit_addUsername(pstring username)
234 strcpy(Ucrit_username, username);
235 if(strlen(Ucrit_username) > 0)
239 unsigned int Ucrit_checkUsername(pstring username)
241 if ( !Ucrit_IsActive) return 1;
242 if (strcmp(Ucrit_username,username) ==0) return 1;
246 void Ucrit_addPid(int pid)
249 if ( !Ucrit_IsActive) return;
250 for (i=0;i<Ucrit_MaxPid;i++)
251 if( pid == Ucrit_pid[i] ) return;
252 Ucrit_pid[Ucrit_MaxPid++] = pid;
255 unsigned int Ucrit_checkPid(int pid)
258 if ( !Ucrit_IsActive) return 1;
259 for (i=0;i<Ucrit_MaxPid;i++)
260 if( pid == Ucrit_pid[i] ) return 1;