- added hostname support to smbstatus
[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-1995
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
22 /*
23  * This program reports current SMB connections
24  */
25
26 #ifdef SYSLOG
27 #undef SYSLOG
28 #endif
29
30 #include "includes.h"
31 #include "loadparm.h"
32
33 struct connect_record crec;
34 extern int DEBUGLEVEL;
35 extern FILE *dbf;
36 extern pstring myhostname;
37
38 static pstring Ucrit_username = "";                   /* added by OH */
39 int            Ucrit_pid[100];  /* Ugly !!! */        /* added by OH */
40 int            Ucrit_MaxPid=0;                        /* added by OH */
41 unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
42 void           Ucrit_addUsername(pstring username);   /* added by OH */
43 unsigned int   Ucrit_checkUsername(pstring username); /* added by OH */
44 void           Ucrit_addPid(int pid);                 /* added by OH */
45 unsigned int   Ucrit_checkPid(int pid);               /* added by OH */
46
47 int main(int argc, char *argv[])
48 {
49   FILE *f;
50   pstring fname;
51   int uid, c, n;
52   static pstring servicesf = CONFIGFILE;
53   extern char *optarg;
54   int verbose = 0;
55   void *dir;
56   char *s;
57   BOOL firstopen=True;
58   BOOL processes_only=False;
59   int last_pid=0;
60
61   setup_logging(argv[0],True);
62
63   charset_initialise();
64
65   DEBUGLEVEL = 0;
66   dbf = fopen("/dev/null","w");
67
68   if (getuid() != geteuid()) {
69     printf("smbstatus should not be run setuid\n");
70     return(1);
71   }
72
73   while ((c = getopt(argc, argv, "pds:u:")) != EOF) {
74     switch (c) {
75     case 'd':
76       verbose = 1;
77       break;
78     case 'p':
79       processes_only = 1;
80       break;
81     case 's':
82       strcpy(servicesf, optarg);
83       break;
84     case 'u':                                       /* added by OH */
85       Ucrit_addUsername(optarg);                    /* added by OH */
86       break;
87     default:
88       fprintf(stderr, "Usage: %s [-d] [-p] [-s configfile] [-u username]\n", *argv); /* changed by OH */
89       return (-1);
90     }
91   }
92
93   if (!lp_load(servicesf,False)) {
94     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
95     return (-1);
96   }
97
98   get_myname(myhostname, NULL);
99
100   if (verbose) {
101     printf("using configfile = %s\n", servicesf);
102     printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
103   }
104
105   strcpy(fname,lp_lockdir());
106   standard_sub_basic(fname);
107   trim_string(fname,"","/");
108   strcat(fname,"/STATUS..LCK");
109
110   f = fopen(fname,"r");
111   if (!f) {
112     printf("Couldn't open status file %s\n",fname);
113     if (!lp_status(-1))
114       printf("You need to have status=yes in your smb config file\n");
115     return(0);
116   }
117   else if (verbose) {
118     printf("Opened status file %s\n", fname);
119   }
120
121   uid = getuid();
122
123   if (!processes_only) {
124     printf("\nSamba version %s\n",VERSION);
125
126     printf("Service      uid      gid      pid     machine\n");
127     printf("----------------------------------------------\n");
128   }
129
130   while (!feof(f))
131     {
132       if (fread(&crec,sizeof(crec),1,f) != 1)
133         break;
134       if ( crec.magic == 0x280267 && process_exists(crec.pid) 
135            && Ucrit_checkUsername(uidtoname(crec.uid))                      /* added by OH */
136          )
137       {
138         Ucrit_addPid(crec.pid);                                             /* added by OH */
139         if (processes_only) {
140           if (last_pid != crec.pid)
141             printf("%d\n",crec.pid);
142           last_pid = crec.pid; /* XXXX we can still get repeats, have to
143                                   add a sort at some time */
144         }
145         else      
146           printf("%-10.10s   %-8s %-8s %5d   %-8s (%s) %s",
147                  crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
148                  crec.machine,crec.addr,
149                  asctime(LocalTime(&crec.start,GMT_TO_LOCAL)));
150       }
151     }
152   fclose(f);
153
154   if (processes_only) exit(0);
155
156   printf("\n");
157
158   dir = opendir(lp_lockdir());
159   if (!dir) return(0);
160   while ((s=readdirname(dir))) {
161     char buf[16];
162     int pid,mode;
163     time_t t;
164     int fd;
165     pstring lname;
166     int dev,inode;
167
168     if (sscanf(s,"share.%d.%d",&dev,&inode)!=2) continue;
169
170     strcpy(lname,lp_lockdir());
171     trim_string(lname,NULL,"/");
172     strcat(lname,"/");
173     strcat(lname,s);
174
175     fd = open(lname,O_RDONLY,0);
176     if (fd < 0) continue;
177     if (read(fd,buf,16) != 16) continue;
178     n = read(fd,fname,sizeof(fname));
179     fname[MAX(n,0)]=0;
180     close(fd);
181
182     t = IVAL(buf,0);
183     mode = IVAL(buf,4);
184     pid = IVAL(buf,8);
185
186     if ( !Ucrit_checkPid(pid) )             /* added by OH */
187         continue;
188
189     if (IVAL(buf,12) != LOCKING_VERSION || !process_exists(pid)) {
190       if (unlink(lname)==0)
191         printf("Deleted stale share file %s\n",s);
192       continue;
193     }
194
195     fname[sizeof(fname)-1] = 0;
196
197     if (firstopen) {
198       firstopen=False;
199       printf("Locked files:\n");
200       printf("Pid    DenyMode   R/W     Name\n");
201       printf("------------------------------\n");
202     }
203
204
205     printf("%-5d  ",pid);
206     switch ((mode>>4)&0xF)
207       {
208       case DENY_NONE: printf("DENY_NONE  "); break;
209       case DENY_ALL:  printf("DENY_ALL   "); break;
210       case DENY_DOS:  printf("DENY_DOS   "); break;
211       case DENY_READ: printf("DENY_READ  "); break;
212       case DENY_WRITE:printf("DENY_WRITE "); break;
213       }
214     switch (mode&0xF) 
215       {
216       case 0: printf("RDONLY "); break;
217       case 1: printf("WRONLY "); break;
218       case 2: printf("RDWR   "); break;
219       }
220     printf(" %s   %s",fname,asctime(LocalTime(&t,GMT_TO_LOCAL)));
221   }
222   closedir(dir);
223
224   if (firstopen)
225     printf("No locked files\n");
226
227   return (0);
228 }
229
230 /* added by OH */
231 void Ucrit_addUsername(pstring username)
232 {
233   strcpy(Ucrit_username, username);
234   if(strlen(Ucrit_username) > 0)
235     Ucrit_IsActive = 1;
236 }
237
238 unsigned int Ucrit_checkUsername(pstring username)
239 {
240   if ( !Ucrit_IsActive) return 1;
241   if (strcmp(Ucrit_username,username) ==0) return 1;
242   return 0;
243 }
244
245 void Ucrit_addPid(int pid)
246 {
247   int i;
248   if ( !Ucrit_IsActive) return;
249   for (i=0;i<Ucrit_MaxPid;i++)
250     if( pid == Ucrit_pid[i] ) return;
251   Ucrit_pid[Ucrit_MaxPid++] = pid;
252 }
253
254 unsigned int   Ucrit_checkPid(int pid)
255 {
256   int i;
257   if ( !Ucrit_IsActive) return 1;
258   for (i=0;i<Ucrit_MaxPid;i++)
259     if( pid == Ucrit_pid[i] ) return 1;
260   return 0;
261 }
262