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