first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[tprouty/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 connect_record crec;
40
41 struct session_record{
42   pid_t pid;
43   uid_t uid;
44   char machine[31];
45   time_t start;
46   struct session_record *next;
47 } *srecs;
48
49 extern int DEBUGLEVEL;
50 extern FILE *dbf;
51
52 static pstring Ucrit_username = "";                   /* added by OH */
53 pid_t   Ucrit_pid[100];  /* Ugly !!! */        /* added by OH */
54 int            Ucrit_MaxPid=0;                        /* added by OH */
55 unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
56
57 int            shares_only = 0;            /* Added by RJS */
58 int            locks_only  = 0;            /* Added by RJS */
59
60 /* we need these because we link to locking*.o */
61  void become_root(BOOL save_dir) {}
62  void unbecome_root(BOOL restore_dir) {}
63
64
65 /* added by OH */
66 static void Ucrit_addUsername(char *username)
67 {
68         pstrcpy(Ucrit_username, username);
69         if(strlen(Ucrit_username) > 0)
70                 Ucrit_IsActive = 1;
71 }
72
73 static unsigned int Ucrit_checkUsername(char *username)
74 {
75         if ( !Ucrit_IsActive) return 1;
76         if (strcmp(Ucrit_username,username) ==0) return 1;
77         return 0;
78 }
79
80 static void Ucrit_addPid(pid_t pid)
81 {
82         int i;
83         if ( !Ucrit_IsActive) return;
84         for (i=0;i<Ucrit_MaxPid;i++)
85                 if( pid == Ucrit_pid[i] ) return;
86         Ucrit_pid[Ucrit_MaxPid++] = pid;
87 }
88
89 static unsigned int Ucrit_checkPid(pid_t pid)
90 {
91         int i;
92         if ( !Ucrit_IsActive) return 1;
93         for (i=0;i<Ucrit_MaxPid;i++)
94                 if( pid == Ucrit_pid[i] ) return 1;
95         return 0;
96 }
97
98
99 static void print_share_mode(share_mode_entry *e, char *fname)
100 {
101         static int count;
102         if (count==0) {
103                 printf("Locked files:\n");
104                 printf("Pid    DenyMode   R/W        Oplock           Name\n");
105                 printf("--------------------------------------------------\n");
106         }
107         count++;
108
109         if (Ucrit_checkPid(e->pid)) {
110           printf("%-5d  ",(int)e->pid);
111           switch ((e->share_mode>>4)&0xF) {
112           case DENY_NONE: printf("DENY_NONE  "); break;
113           case DENY_ALL:  printf("DENY_ALL   "); break;
114           case DENY_DOS:  printf("DENY_DOS   "); break;
115           case DENY_READ: printf("DENY_READ  "); break;
116           case DENY_WRITE:printf("DENY_WRITE "); break;
117           }
118           switch (e->share_mode&0xF) {
119           case 0: printf("RDONLY     "); break;
120           case 1: printf("WRONLY     "); break;
121           case 2: printf("RDWR       "); break;
122           }
123
124           if((e->op_type & 
125              (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
126               (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
127                 printf("EXCLUSIVE+BATCH ");
128           else if (e->op_type & EXCLUSIVE_OPLOCK)
129                 printf("EXCLUSIVE       ");
130           else if (e->op_type & BATCH_OPLOCK)
131                 printf("BATCH           ");
132           else if (e->op_type & LEVEL_II_OPLOCK)
133                 printf("LEVEL_II        ");
134           else
135                 printf("NONE            ");
136
137           printf(" %s   %s",dos_to_unix(fname,False),
138              asctime(LocalTime((time_t *)&e->time.tv_sec)));
139         }
140 }
141
142
143 /*******************************************************************
144  dump the elements of the profile structure
145   ******************************************************************/
146 static int profile_dump(void)
147 {
148 #ifndef WITH_PROFILE
149         fprintf(stderr,"ERROR: not compiled with profile support\n");
150         return -1;
151 #else
152         if (!profile_setup(True)) {
153                 fprintf(stderr,"Failed to initialise profile memory\n");
154                 return -1;
155         }
156
157         printf("smb_count:\t%u\n", profile_p->smb_count);
158         printf("uid_changes:\t%u\n", profile_p->uid_changes);
159         return 0;
160 #endif
161 }
162
163
164
165  int main(int argc, char *argv[])
166 {
167   FILE *f;
168   pstring fname;
169   int c;
170   static pstring servicesf = CONFIGFILE;
171   extern char *optarg;
172   int verbose = 0, brief =0;
173   BOOL processes_only=False;
174   pid_t last_pid=(pid_t)0;
175   struct session_record *ptr;
176   int profile_only = 0;
177
178   TimeInit();
179   setup_logging(argv[0],True);
180
181   charset_initialise();
182
183   DEBUGLEVEL = 0;
184   dbf = stderr;
185
186   if (getuid() != geteuid()) {
187     printf("smbstatus should not be run setuid\n");
188     return(1);
189   }
190
191   while ((c = getopt(argc, argv, "pdLSs:u:bP")) != EOF) {
192     switch (c) {
193     case 'b':
194       brief = 1;
195       break;
196     case 'd':
197       verbose = 1;
198       break;
199     case 'L':
200       locks_only = 1;
201       break;
202     case 'p':
203       processes_only = 1;
204       break;
205     case 'P':
206       profile_only = 1;
207       break;
208     case 'S':
209       shares_only = 1;
210       break;
211     case 's':
212       pstrcpy(servicesf, optarg);
213       break;
214     case 'u':                                       /* added by OH */
215       Ucrit_addUsername(optarg);                    /* added by OH */
216       break;
217     default:
218       fprintf(stderr, "Usage: %s [-P] [-d] [-L] [-p] [-S] [-s configfile] [-u username]\n", *argv); /* changed by OH */
219       return (-1);
220     }
221   }
222
223   if (!lp_load(servicesf,False,False,False)) {
224     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
225     return (-1);
226   }
227
228   if (verbose) {
229     printf("using configfile = %s\n", servicesf);
230     printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
231   }
232
233   if (profile_only) {
234         return profile_dump();
235   }
236
237   pstrcpy(fname,lp_lockdir());
238   standard_sub_basic(fname);
239   trim_string(fname,"","/");
240   pstrcat(fname,"/STATUS..LCK");
241
242   f = sys_fopen(fname,"r");
243   if (!f) {
244     printf("Couldn't open status file %s\n",fname);
245     if (!lp_status(-1))
246       printf("You need to have status=yes in your smb config file\n");
247     return(0);
248   }
249   else if (verbose) {
250     printf("Opened status file %s\n", fname);
251   }
252
253   if (!locks_only) {
254
255     if (!processes_only) {
256       printf("\nSamba version %s\n",VERSION);
257
258       if (brief)
259         {
260           printf("PID     Username  Machine                       Time logged in\n");
261           printf("-------------------------------------------------------------------\n");
262         }
263       else
264         {
265           printf("Service      uid      gid      pid     machine\n");
266           printf("----------------------------------------------\n");
267         }
268     }
269
270     while (!feof(f))
271       {
272         if (fread(&crec,sizeof(crec),1,f) != 1)
273           break;
274         if (crec.cnum == -1) continue;
275         if ( crec.magic == 0x280267 && process_exists(crec.pid) 
276              && Ucrit_checkUsername(uidtoname(crec.uid))                      /* added by OH */
277              )
278           {
279             if (brief)
280               {
281                 ptr=srecs;
282                 while (ptr!=NULL)
283                   {
284                     if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0)) 
285                       {
286                         if (ptr->start > crec.start)
287                           ptr->start=crec.start;
288                         break;
289                       }
290                     ptr=ptr->next;
291                   }
292                 if (ptr==NULL)
293                   {
294                     ptr=(struct session_record *) malloc(sizeof(struct session_record));
295                     ptr->uid=crec.uid;
296                     ptr->pid=crec.pid;
297                     ptr->start=crec.start;
298                     strncpy(ptr->machine,crec.machine,30);
299                     ptr->machine[30]='\0';
300                     ptr->next=srecs;
301                     srecs=ptr;
302                   }
303               }
304             else
305               {
306                 Ucrit_addPid(crec.pid);                                             /* added by OH */
307                 if (processes_only) {
308                   if (last_pid != crec.pid)
309                     printf("%d\n",(int)crec.pid);
310                   last_pid = crec.pid; /* XXXX we can still get repeats, have to
311                                     add a sort at some time */
312                 }
313                 else      
314                   printf("%-10.10s   %-8s %-8s %5d   %-8s (%s) %s",
315                          crec.name,uidtoname(crec.uid),gidtoname(crec.gid),(int)crec.pid,
316                          crec.machine,crec.addr,
317                          asctime(LocalTime(&crec.start)));
318               }
319           }
320       }
321     fclose(f);
322   }
323   if (processes_only) exit(0);
324   
325   if (brief)
326   {
327     ptr=srecs;
328     while (ptr!=NULL)
329     {
330       printf("%-8d%-10.10s%-30.30s%s",(int)ptr->pid,uidtoname(ptr->uid),ptr->machine,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