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