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