703105012efe04feaa64889d5f2caf094859f06e
[kai/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 #ifndef FAST_SHARE_MODES
57 static char *read_share_file(int fd, char *fname, char *progname)
58 {
59   struct stat sb;
60   char *buf;
61   int size;
62
63   if(fstat(fd, &sb) != 0)
64   {
65     printf("%s: ERROR: read_share_file: Failed to do stat on share file %s (%s)\n",
66                   progname, fname, strerror(errno));
67     return 0;
68   }
69
70   if(sb.st_size == 0)
71   {
72      return 0;
73   }
74
75   /* Allocate space for the file */
76   if((buf = (char *)malloc(sb.st_size)) == NULL)
77   {
78     printf("%s: read_share_file: malloc for file size %d fail !\n", 
79               progname, (int)sb.st_size);
80     return 0;
81   }
82
83   if(lseek(fd, 0, SEEK_SET) != 0)
84   {
85     printf("%s: ERROR: read_share_file: Failed to reset position to 0 \
86 for share file %s (%s)\n", progname, fname, strerror(errno));
87     if(buf)
88       free(buf);
89     return 0;
90   }
91
92   if (read(fd,buf,sb.st_size) != sb.st_size)
93   {
94     printf("%s: ERROR: read_share_file: Failed to read share file %s (%s)\n",
95                progname, fname, strerror(errno));
96     if(buf)
97       free(buf);
98     return 0;
99   }
100
101   if (IVAL(buf,SMF_VERSION_OFFSET) != LOCKING_VERSION) {
102     printf("%s: ERROR: read_share_file: share file %s has incorrect \
103 locking version (was %d, should be %d).\n",fname, 
104               progname, IVAL(buf,SMF_VERSION_OFFSET), LOCKING_VERSION);
105     if(buf)
106       free(buf);
107     return 0;
108   }
109
110   /* Sanity check for file contents */
111   size = sb.st_size;
112   size -= SMF_HEADER_LENGTH; /* Remove the header */
113
114   /* Remove the filename component. */
115   size -= SVAL(buf, SMF_FILENAME_LEN_OFFSET);
116
117   /* The remaining size must be a multiple of SMF_ENTRY_LENGTH - error if not. */
118   if((size % SMF_ENTRY_LENGTH) != 0)
119   {
120     printf("%s: ERROR: read_share_file: share file %s is an incorrect length.\n", 
121              progname, fname);
122     if(buf)
123       free(buf);
124     return 0;
125   }
126
127   return buf;
128 }
129 #endif /* FAST_SHARE_MODES */
130
131  int main(int argc, char *argv[])
132 {
133   FILE *f;
134   pstring fname;
135   int uid, c;
136   static pstring servicesf = CONFIGFILE;
137   extern char *optarg;
138   int verbose = 0, brief =0;
139   BOOL firstopen=True;
140   BOOL processes_only=False;
141   int last_pid=0;
142 #ifdef FAST_SHARE_MODES
143   pstring shmem_file_name;
144   share_mode_record *file_scanner_p;
145   smb_shm_offset_t *mode_array;
146   int bytes_free, bytes_used, bytes_overhead, bytes_total;
147 #else /* FAST_SHARE_MODES */
148   void *dir;
149   char *s;
150 #endif /* FAST_SHARE_MODES */
151 #ifdef USE_OPLOCKS
152   int oplock_type;
153 #endif /* USE_OPLOCKS */
154   int i;
155   struct session_record *ptr;
156
157
158   TimeInit();
159   setup_logging(argv[0],True);
160
161   charset_initialise();
162
163   DEBUGLEVEL = 0;
164   dbf = fopen("/dev/null","w");
165
166   if (getuid() != geteuid()) {
167     printf("smbstatus should not be run setuid\n");
168     return(1);
169   }
170
171   while ((c = getopt(argc, argv, "pds:u:b")) != EOF) {
172     switch (c) {
173     case 'b':
174       brief = 1;
175       break;
176     case 'd':
177       verbose = 1;
178       break;
179     case 'p':
180       processes_only = 1;
181       break;
182     case 's':
183       strcpy(servicesf, optarg);
184       break;
185     case 'u':                                       /* added by OH */
186       Ucrit_addUsername(optarg);                    /* added by OH */
187       break;
188     default:
189       fprintf(stderr, "Usage: %s [-d] [-p] [-s configfile] [-u username]\n", *argv); /* changed by OH */
190       return (-1);
191     }
192   }
193
194   get_myname(myhostname, NULL);
195
196   if (!lp_load(servicesf,False)) {
197     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
198     return (-1);
199   }
200
201   if (verbose) {
202     printf("using configfile = %s\n", servicesf);
203     printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
204   }
205
206   strcpy(fname,lp_lockdir());
207   standard_sub_basic(fname);
208   trim_string(fname,"","/");
209   strcat(fname,"/STATUS..LCK");
210
211   f = fopen(fname,"r");
212   if (!f) {
213     printf("Couldn't open status file %s\n",fname);
214     if (!lp_status(-1))
215       printf("You need to have status=yes in your smb config file\n");
216     return(0);
217   }
218   else if (verbose) {
219     printf("Opened status file %s\n", fname);
220   }
221
222   uid = getuid();
223
224   if (!processes_only) {
225     printf("\nSamba version %s\n",VERSION);
226
227     if (brief)
228     {
229       printf("PID     Username  Machine                       Time logged in\n");
230       printf("-------------------------------------------------------------------\n");
231     }
232     else
233     {
234       printf("Service      uid      gid      pid     machine\n");
235       printf("----------------------------------------------\n");
236     }
237   }
238
239   while (!feof(f))
240     {
241       if (fread(&crec,sizeof(crec),1,f) != 1)
242         break;
243       if ( crec.magic == 0x280267 && process_exists(crec.pid) 
244            && Ucrit_checkUsername(uidtoname(crec.uid))                      /* added by OH */
245          )
246       {
247         if (brief)
248         {
249           ptr=srecs;
250           while (ptr!=NULL)
251           {
252             if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0)) 
253             {
254               if (ptr->start > crec.start)
255                 ptr->start=crec.start;
256               break;
257             }
258             ptr=ptr->next;
259           }
260           if (ptr==NULL)
261           {
262             ptr=(struct session_record *) malloc(sizeof(struct session_record));
263             ptr->uid=crec.uid;
264             ptr->pid=crec.pid;
265             ptr->start=crec.start;
266             strncpy(ptr->machine,crec.machine,30);
267             ptr->machine[30]='\0';
268             ptr->next=srecs;
269             srecs=ptr;
270           }
271         }
272         else
273         {
274           Ucrit_addPid(crec.pid);                                             /* added by OH */
275           if (processes_only) {
276             if (last_pid != crec.pid)
277               printf("%d\n",crec.pid);
278             last_pid = crec.pid; /* XXXX we can still get repeats, have to
279                                     add a sort at some time */
280           }
281           else    
282             printf("%-10.10s   %-8s %-8s %5d   %-8s (%s) %s",
283                    crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
284                    crec.machine,crec.addr,
285                    asctime(LocalTime(&crec.start)));
286         }
287       }
288     }
289   fclose(f);
290
291   if (processes_only) exit(0);
292   
293   if (brief)
294   {
295     ptr=srecs;
296     while (ptr!=NULL)
297     {
298       printf("%-8d%-10.10s%-30.30s%s",ptr->pid,uidtoname(ptr->uid),ptr->machine,asctime(LocalTime(&(ptr->start))));
299     ptr=ptr->next;
300     }
301     printf("\n");
302     exit(0);
303   }
304
305   printf("\n");
306
307 #ifdef FAST_SHARE_MODES 
308   /*******************************************************************
309   initialize the shared memory for share_mode management 
310   ******************************************************************/
311    
312   strcpy(shmem_file_name,lp_lockdir());
313   trim_string(shmem_file_name,"","/");
314   if (!*shmem_file_name) exit(-1);
315   strcat(shmem_file_name, "/SHARE_MEM_FILE");
316   if(!smb_shm_open(shmem_file_name, lp_shmem_size())) exit(-1);
317   
318   mode_array = (smb_shm_offset_t *)smb_shm_offset2addr(smb_shm_get_userdef_off());
319   if(mode_array == NULL)
320   {
321     printf("%s: base of shared memory hash array == 0! Exiting.\n", argv[0]);
322     smb_shm_close();
323     exit(-1);
324   }
325
326   for( i = 0; i < lp_shmem_hash_size(); i++)
327   {
328     smb_shm_lock_hash_entry(i);
329     if(mode_array[i] == NULL_OFFSET)
330     {
331       smb_shm_unlock_hash_entry(i);
332       continue;
333     }
334     file_scanner_p = (share_mode_record *)smb_shm_offset2addr(mode_array[i]);
335     while((file_scanner_p != 0) && (file_scanner_p->num_share_mode_entries != 0))
336     {
337       share_mode_entry *entry_scanner_p = 
338                  (share_mode_entry *)smb_shm_offset2addr(
339                                        file_scanner_p->share_mode_entries);
340
341       while(entry_scanner_p != 0)
342       {
343         struct timeval t;
344         int pid = entry_scanner_p->pid;
345         int mode = entry_scanner_p->share_mode;
346      
347         t.tv_sec = entry_scanner_p->time.tv_sec;
348         t.tv_usec = entry_scanner_p->time.tv_usec;
349         strcpy(fname, file_scanner_p->file_name);
350 #ifdef USE_OPLOCKS  
351         oplock_type = entry_scanner_p->op_type;
352 #endif /* USE_OPLOCKS */
353
354 #else /* FAST_SHARE_MODES */
355
356      /* For slow share modes go through all the files in
357         the share mode directory and read the entries in
358         each.
359       */
360
361      dir = opendir(lp_lockdir());
362      if (!dir) 
363      {
364        printf("%s: Unable to open lock directory %s.\n", argv[0], lp_lockdir());
365        return(0);
366      }
367      while ((s=readdirname(dir))) {
368        char *buf;
369        char *base;
370        int fd;
371        pstring lname;
372        uint32 dev,inode;
373        
374        if (sscanf(s,"share.%u.%u",&dev,&inode)!=2) continue;
375        
376        strcpy(lname,lp_lockdir());
377        trim_string(lname,NULL,"/");
378        strcat(lname,"/");
379        strcat(lname,s);
380        
381        fd = open(lname,O_RDWR,0);
382        if (fd < 0) 
383        {
384          printf("%s: Unable to open share file %s.\n", argv[0], lname);
385          continue;
386        }
387
388        /* Lock the share mode file while we read it. */
389        if(fcntl_lock(fd, F_SETLKW, 0, 1, F_WRLCK) == False)
390        {
391          printf("%s: Unable to lock open share file %s.\n", argv[0], lname);
392          close(fd);
393          continue;
394        }
395
396        if(( buf = read_share_file( fd, lname, argv[0] )) == NULL)
397        {
398          close(fd);
399          continue;
400        } 
401        strcpy( fname, &buf[10]);
402        close(fd);
403       
404        base = buf + SMF_HEADER_LENGTH + SVAL(buf,SMF_FILENAME_LEN_OFFSET); 
405        for( i = 0; i < IVAL(buf, SMF_NUM_ENTRIES_OFFSET); i++)
406        {
407          char *p = base + (i*SMF_ENTRY_LENGTH);
408          struct timeval t;
409          int pid = IVAL(p,SME_PID_OFFSET);
410          int mode = IVAL(p,SME_SHAREMODE_OFFSET);
411      
412          t.tv_sec = IVAL(p,SME_SEC_OFFSET);
413          t.tv_usec = IVAL(p,SME_USEC_OFFSET);
414 #ifdef USE_OPLOCKS
415          oplock_type = SVAL(p,SME_OPLOCK_TYPE_OFFSET);
416 #endif /* USE_OPLOCKS */
417 #endif /* FAST_SHARE_MODES */
418
419     fname[sizeof(fname)-1] = 0;
420
421     if (firstopen) {
422       firstopen=False;
423       printf("Locked files:\n");
424 #ifdef USE_OPLOCKS
425       printf("Pid    DenyMode   R/W        Oplock           Name\n");
426       printf("--------------------------------------------------\n");
427 #else /* USE_OPLOCKS */
428       printf("Pid    DenyMode   R/W         Name\n");
429       printf("----------------------------------\n");
430 #endif /* USE_OPLOCKS */
431     }
432
433
434     printf("%-5d  ",pid);
435     switch ((mode>>4)&0xF)
436       {
437       case DENY_NONE: printf("DENY_NONE  "); break;
438       case DENY_ALL:  printf("DENY_ALL   "); break;
439       case DENY_DOS:  printf("DENY_DOS   "); break;
440       case DENY_READ: printf("DENY_READ  "); break;
441       case DENY_WRITE:printf("DENY_WRITE "); break;
442       }
443     switch (mode&0xF) 
444       {
445       case 0: printf("RDONLY     "); break;
446       case 1: printf("WRONLY     "); break;
447       case 2: printf("RDWR       "); break;
448       }
449 #ifdef USE_OPLOCKS
450     if((oplock_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
451       printf("EXCLUSIVE+BATCH ");
452     else if (oplock_type & EXCLUSIVE_OPLOCK)
453       printf("EXCLUSIVE       ");
454     else if (oplock_type & BATCH_OPLOCK)
455       printf("BATCH           ");
456     else
457       printf("NONE            ");
458 #endif /* USE_OPLOCKS */
459     printf(" %s   %s",fname,asctime(LocalTime((time_t *)&t.tv_sec)));
460
461 #ifdef FAST_SHARE_MODES
462
463         entry_scanner_p = (share_mode_entry *)smb_shm_offset2addr(
464                                     entry_scanner_p->next_share_mode_entry);
465       } /* end while entry_scanner_p */
466      file_scanner_p = (share_mode_record *)smb_shm_offset2addr(
467                                     file_scanner_p->next_offset);
468     } /* end while file_scanner_p */
469     smb_shm_unlock_hash_entry(i);
470   } /* end for */
471
472   smb_shm_get_usage(&bytes_free, &bytes_used, &bytes_overhead);
473   bytes_total = bytes_free + bytes_used + bytes_overhead;
474
475   /*******************************************************************
476   deinitialize the shared memory for share_mode management 
477   ******************************************************************/
478   smb_shm_close();
479
480 #else /* FAST_SHARE_MODES */
481     } /* end for i */
482
483     if(buf)
484       free(buf);
485     base = 0;
486   } /* end while */
487   closedir(dir);
488
489 #endif /* FAST_SHARE_MODES */
490   if (firstopen)
491     printf("No locked files\n");
492 #ifdef FAST_SHARE_MODES
493   printf("\nShare mode memory usage (bytes):\n");
494   printf("   %d(%d%%) free + %d(%d%%) used + %d(%d%%) overhead = %d(100%%) total\n",
495          bytes_free, (bytes_free * 100)/bytes_total,
496          bytes_used, (bytes_used * 100)/bytes_total,
497          bytes_overhead, (bytes_overhead * 100)/bytes_total,
498          bytes_total);
499   
500 #endif /* FAST_SHARE_MODES */
501
502   return (0);
503 }
504
505 /* added by OH */
506 void Ucrit_addUsername(pstring username)
507 {
508   strcpy(Ucrit_username, username);
509   if(strlen(Ucrit_username) > 0)
510     Ucrit_IsActive = 1;
511 }
512
513 unsigned int Ucrit_checkUsername(pstring username)
514 {
515   if ( !Ucrit_IsActive) return 1;
516   if (strcmp(Ucrit_username,username) ==0) return 1;
517   return 0;
518 }
519
520 void Ucrit_addPid(int pid)
521 {
522   int i;
523   if ( !Ucrit_IsActive) return;
524   for (i=0;i<Ucrit_MaxPid;i++)
525     if( pid == Ucrit_pid[i] ) return;
526   Ucrit_pid[Ucrit_MaxPid++] = pid;
527 }
528
529 unsigned int   Ucrit_checkPid(int pid)
530 {
531   int i;
532   if ( !Ucrit_IsActive) return 1;
533   for (i=0;i<Ucrit_MaxPid;i++)
534     if( pid == Ucrit_pid[i] ) return 1;
535   return 0;
536 }
537