first cut at using the tdb code for the connections structure, the
[kai/samba.git] / source / utils / status.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    status reporting
5    Copyright (C) Andrew Tridgell 1994-1998
6    
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.
11    
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.
16    
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.
20
21    Revision History:
22
23    12 aug 96: Erik.Devriendt@te6.siemens.be
24    added support for shared memory implementation of share mode locking
25
26    21-Jul-1998: rsharpe@ns.aus.com (Richard Sharpe)
27    Added -L (locks only) -S (shares only) flags and code
28
29 */
30
31 /*
32  * This program reports current SMB connections
33  */
34
35 #define NO_SYSLOG
36
37 #include "includes.h"
38
39 struct session_record{
40   pid_t pid;
41   uid_t uid;
42   char machine[31];
43   time_t start;
44   struct session_record *next;
45 } *srecs;
46
47 extern int DEBUGLEVEL;
48 extern FILE *dbf;
49
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;
58
59 /* we need these because we link to locking*.o */
60  void become_root(BOOL save_dir) {}
61  void unbecome_root(BOOL restore_dir) {}
62
63
64 /* added by OH */
65 static void Ucrit_addUsername(char *username)
66 {
67         pstrcpy(Ucrit_username, username);
68         if(strlen(Ucrit_username) > 0)
69                 Ucrit_IsActive = 1;
70 }
71
72 static unsigned int Ucrit_checkUsername(char *username)
73 {
74         if ( !Ucrit_IsActive) return 1;
75         if (strcmp(Ucrit_username,username) ==0) return 1;
76         return 0;
77 }
78
79 static void Ucrit_addPid(pid_t pid)
80 {
81         int i;
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;
86 }
87
88 static unsigned int Ucrit_checkPid(pid_t pid)
89 {
90         int i;
91         if ( !Ucrit_IsActive) return 1;
92         for (i=0;i<Ucrit_MaxPid;i++)
93                 if( pid == Ucrit_pid[i] ) return 1;
94         return 0;
95 }
96
97
98 static void print_share_mode(share_mode_entry *e, char *fname)
99 {
100         static int count;
101         if (count==0) {
102                 printf("Locked files:\n");
103                 printf("Pid    DenyMode   R/W        Oplock           Name\n");
104                 printf("--------------------------------------------------\n");
105         }
106         count++;
107
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;
116           }
117           switch (e->share_mode&0xF) {
118           case 0: printf("RDONLY     "); break;
119           case 1: printf("WRONLY     "); break;
120           case 2: printf("RDWR       "); break;
121           }
122
123           if((e->op_type & 
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)
130                 printf("BATCH           ");
131           else if (e->op_type & LEVEL_II_OPLOCK)
132                 printf("LEVEL_II        ");
133           else
134                 printf("NONE            ");
135
136           printf(" %s   %s",dos_to_unix(fname,False),
137              asctime(LocalTime((time_t *)&e->time.tv_sec)));
138         }
139 }
140
141
142 /*******************************************************************
143  dump the elements of the profile structure
144   ******************************************************************/
145 static int profile_dump(void)
146 {
147 #ifndef WITH_PROFILE
148         fprintf(stderr,"ERROR: not compiled with profile support\n");
149         return -1;
150 #else
151         if (!profile_setup(True)) {
152                 fprintf(stderr,"Failed to initialise profile memory\n");
153                 return -1;
154         }
155
156         printf("smb_count:\t%u\n", profile_p->smb_count);
157         printf("uid_changes:\t%u\n", profile_p->uid_changes);
158         return 0;
159 #endif
160 }
161
162
163 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf)
164 {
165         static pid_t last_pid;
166         struct session_record *ptr;
167         struct connections_data crec;
168         static int doneone;
169
170         memcpy(&crec, dbuf.dptr, sizeof(crec));
171
172         if (crec.cnum == -1) return 0;
173
174         if (!process_exists(crec.pid) || !Ucrit_checkUsername(uidtoname(crec.uid))) {
175                 return 0;
176         }
177
178         if (brief) {
179                 if (!doneone) {
180                         printf("\nSamba version %s\n",VERSION);
181                         printf("PID     Username  Machine                       Time logged in\n");
182                         printf("-------------------------------------------------------------------\n");
183                         doneone = 1;
184                 }
185
186                 ptr=srecs;
187                 while (ptr!=NULL) {
188                         if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0)) {
189                                 if (ptr->start > crec.start)
190                                         ptr->start=crec.start;
191                                 break;
192                         }
193                         ptr=ptr->next;
194                 }
195                 if (ptr==NULL) {
196                         ptr=(struct session_record *) malloc(sizeof(struct session_record));
197                         ptr->uid=crec.uid;
198                         ptr->pid=crec.pid;
199                         ptr->start=crec.start;
200                         strncpy(ptr->machine,crec.machine,30);
201                         ptr->machine[30]='\0';
202                         ptr->next=srecs;
203                         srecs=ptr;
204                 }
205         } else {
206                 if (!doneone) {
207                         printf("\nSamba version %s\n",VERSION);
208                         printf("Service      uid      gid      pid     machine\n");
209                         printf("----------------------------------------------\n");
210                         doneone = 1;
211                 }
212
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 */
219                 } else {
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)));
224                 }
225         }
226
227         return 0;
228 }
229
230
231
232
233  int main(int argc, char *argv[])
234 {
235         pstring fname;
236         int c;
237         static pstring servicesf = CONFIGFILE;
238         extern char *optarg;
239         int profile_only = 0;
240         TDB_CONTEXT *tdb;
241         struct session_record *ptr;
242
243         TimeInit();
244         setup_logging(argv[0],True);
245         
246         charset_initialise();
247         
248         DEBUGLEVEL = 0;
249         dbf = stderr;
250         
251         if (getuid() != geteuid()) {
252                 printf("smbstatus should not be run setuid\n");
253                 return(1);
254         }
255         
256         while ((c = getopt(argc, argv, "pdLSs:u:bP")) != EOF) {
257                 switch (c) {
258                 case 'b':
259                         brief = 1;
260                         break;
261                 case 'd':
262                         verbose = 1;
263                         break;
264                 case 'L':
265                         locks_only = 1;
266                         break;
267                 case 'p':
268                         processes_only = 1;
269                         break;
270                 case 'P':
271                         profile_only = 1;
272                         break;
273                 case 'S':
274                         shares_only = 1;
275                         break;
276                 case 's':
277                         pstrcpy(servicesf, optarg);
278                         break;
279                 case 'u':                                       /* added by OH */
280                         Ucrit_addUsername(optarg);                    /* added by OH */
281                         break;
282                 default:
283                         fprintf(stderr, "Usage: %s [-P] [-d] [-L] [-p] [-S] [-s configfile] [-u username]\n", *argv); /* changed by OH */
284                         return (-1);
285                 }
286         }
287         
288         if (!lp_load(servicesf,False,False,False)) {
289                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
290                 return (-1);
291         }
292         
293         if (verbose) {
294                 printf("using configfile = %s\n", servicesf);
295                 printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
296         }
297         
298         if (profile_only) {
299                 return profile_dump();
300         }
301         
302         pstrcpy(fname,lp_lockdir());
303         standard_sub_basic(fname);
304         trim_string(fname,"","/");
305         pstrcat(fname,"/connections.tdb");
306
307         tdb = tdb_open(fname, 0, O_RDONLY, 0);
308         if (!tdb) {
309                 printf("Couldn't open status file %s\n",fname);
310                 if (!lp_status(-1))
311                         printf("You need to have status=yes in your smb config file\n");
312                 return(0);
313         }  else if (verbose) {
314                 printf("Opened status file %s\n", fname);
315         }
316
317         if (locks_only) goto locks;
318
319         tdb_traverse(tdb, traverse_fn1);
320
321  locks:
322         if (processes_only) exit(0);
323   
324         if (brief)  {
325                 ptr=srecs;
326                 while (ptr!=NULL) {
327                         printf("%-8d%-10.10s%-30.30s%s",
328                                (int)ptr->pid,uidtoname(ptr->uid),
329                                ptr->machine,
330                                asctime(LocalTime(&(ptr->start))));
331                         ptr=ptr->next;
332                 }
333                 printf("\n");
334                 exit(0);
335         }
336
337         printf("\n");
338
339         if (!shares_only) {
340                 if (!locking_init(1)) {
341                         printf("Can't initialise shared memory - exiting\n");
342                         exit(1);
343                 }
344                 
345                 if (share_mode_forall(print_share_mode) <= 0)
346                         printf("No locked files\n");
347                 
348                 printf("\n");
349                 
350                 share_status(stdout);
351                 
352                 locking_end();
353         }
354
355         return (0);
356 }
357