updating documentation to reflect code a little bit.
[Samba.git] / source / utils / status.c
blob3d520564895ffca8494e031b2ef1dfeb634d1cbe
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 status reporting
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
26 #ifdef SYSLOG
27 #undef SYSLOG
28 #endif
30 #include "includes.h"
32 struct connect_record crec;
33 extern int DEBUGLEVEL;
34 extern FILE *dbf;
35 extern pstring myhostname;
37 static pstring Ucrit_username = ""; /* added by OH */
38 int Ucrit_pid[100]; /* Ugly !!! */ /* added by OH */
39 int Ucrit_MaxPid=0; /* added by OH */
40 unsigned int Ucrit_IsActive = 0; /* added by OH */
42 int main(int argc, char *argv[])
44 FILE *f;
45 pstring fname;
46 int uid, c, n;
47 static pstring servicesf = CONFIGFILE;
48 extern char *optarg;
49 int verbose = 0;
50 void *dir;
51 char *s;
52 BOOL firstopen=True;
53 BOOL processes_only=False;
54 int last_pid=0;
56 TimeInit();
57 setup_logging(argv[0],True);
59 charset_initialise();
61 DEBUGLEVEL = 0;
62 dbf = fopen("/dev/null","w");
64 if (getuid() != geteuid()) {
65 printf("smbstatus should not be run setuid\n");
66 return(1);
69 while ((c = getopt(argc, argv, "pds:u:")) != EOF) {
70 switch (c) {
71 case 'd':
72 verbose = 1;
73 break;
74 case 'p':
75 processes_only = 1;
76 break;
77 case 's':
78 strcpy(servicesf, optarg);
79 break;
80 case 'u': /* added by OH */
81 Ucrit_addUsername(optarg); /* added by OH */
82 break;
83 default:
84 fprintf(stderr, "Usage: %s [-d] [-p] [-s configfile] [-u username]\n", *argv); /* changed by OH */
85 return (-1);
89 if (!lp_load(servicesf,False)) {
90 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
91 return (-1);
94 get_myname(myhostname, NULL);
96 if (verbose) {
97 printf("using configfile = %s\n", servicesf);
98 printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
101 strcpy(fname,lp_lockdir());
102 standard_sub_basic(fname);
103 trim_string(fname,"","/");
104 strcat(fname,"/STATUS..LCK");
106 f = fopen(fname,"r");
107 if (!f) {
108 printf("Couldn't open status file %s\n",fname);
109 if (!lp_status(-1))
110 printf("You need to have status=yes in your smb config file\n");
111 return(0);
113 else if (verbose) {
114 printf("Opened status file %s\n", fname);
117 uid = getuid();
119 if (!processes_only) {
120 printf("\nSamba version %s\n",VERSION);
122 printf("Service uid gid pid machine\n");
123 printf("----------------------------------------------\n");
126 while (!feof(f))
128 if (fread(&crec,sizeof(crec),1,f) != 1)
129 break;
130 if ( crec.magic == 0x280267 && process_exists(crec.pid)
131 && Ucrit_checkUsername(uidtoname(crec.uid)) /* added by OH */
134 Ucrit_addPid(crec.pid); /* added by OH */
135 if (processes_only) {
136 if (last_pid != crec.pid)
137 printf("%d\n",crec.pid);
138 last_pid = crec.pid; /* XXXX we can still get repeats, have to
139 add a sort at some time */
141 else
142 printf("%-10.10s %-8s %-8s %5d %-8s (%s) %s",
143 crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
144 crec.machine,crec.addr,
145 asctime(LocalTime(&crec.start)));
148 fclose(f);
150 if (processes_only) exit(0);
152 printf("\n");
154 dir = opendir(lp_lockdir());
155 if (!dir) return(0);
156 while ((s=readdirname(dir))) {
157 char buf[16];
158 int pid,mode;
159 time_t t;
160 int fd;
161 pstring lname;
162 int dev,inode;
164 if (sscanf(s,"share.%d.%d",&dev,&inode)!=2) continue;
166 strcpy(lname,lp_lockdir());
167 trim_string(lname,NULL,"/");
168 strcat(lname,"/");
169 strcat(lname,s);
171 fd = open(lname,O_RDONLY,0);
172 if (fd < 0) continue;
173 if (read(fd,buf,16) != 16) continue;
174 n = read(fd,fname,sizeof(fname));
175 fname[MAX(n,0)]=0;
176 close(fd);
178 t = IVAL(buf,0);
179 mode = IVAL(buf,4);
180 pid = IVAL(buf,8);
182 if ( !Ucrit_checkPid(pid) ) /* added by OH */
183 continue;
185 if (IVAL(buf,12) != LOCKING_VERSION || !process_exists(pid)) {
186 if (unlink(lname)==0)
187 printf("Deleted stale share file %s\n",s);
188 continue;
191 fname[sizeof(fname)-1] = 0;
193 if (firstopen) {
194 firstopen=False;
195 printf("Locked files:\n");
196 printf("Pid DenyMode R/W Name\n");
197 printf("------------------------------\n");
201 printf("%-5d ",pid);
202 switch ((mode>>4)&0xF)
204 case DENY_NONE: printf("DENY_NONE "); break;
205 case DENY_ALL: printf("DENY_ALL "); break;
206 case DENY_DOS: printf("DENY_DOS "); break;
207 case DENY_READ: printf("DENY_READ "); break;
208 case DENY_WRITE:printf("DENY_WRITE "); break;
210 switch (mode&0xF)
212 case 0: printf("RDONLY "); break;
213 case 1: printf("WRONLY "); break;
214 case 2: printf("RDWR "); break;
216 printf(" %s %s",fname,asctime(LocalTime(&t)));
218 closedir(dir);
220 if (firstopen)
221 printf("No locked files\n");
223 return (0);
226 /* added by OH */
227 void Ucrit_addUsername(pstring username)
229 strcpy(Ucrit_username, username);
230 if(strlen(Ucrit_username) > 0)
231 Ucrit_IsActive = 1;
234 unsigned int Ucrit_checkUsername(pstring username)
236 if ( !Ucrit_IsActive) return 1;
237 if (strcmp(Ucrit_username,username) ==0) return 1;
238 return 0;
241 void Ucrit_addPid(int pid)
243 int i;
244 if ( !Ucrit_IsActive) return;
245 for (i=0;i<Ucrit_MaxPid;i++)
246 if( pid == Ucrit_pid[i] ) return;
247 Ucrit_pid[Ucrit_MaxPid++] = pid;
250 unsigned int Ucrit_checkPid(int pid)
252 int i;
253 if ( !Ucrit_IsActive) return 1;
254 for (i=0;i<Ucrit_MaxPid;i++)
255 if( pid == Ucrit_pid[i] ) return 1;
256 return 0;