Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-trivial
[jelmer/samba4-debian.git] / source / smb_server / management.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    management calls for smb server
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smb_server/smb_server.h"
24 #include "smbd/service_stream.h"
25 #include "lib/messaging/irpc.h"
26 #include "librpc/gen_ndr/ndr_irpc.h"
27 #include "auth/auth.h"
28
29 /*
30   return a list of open sessions
31 */
32 static NTSTATUS smbsrv_session_information(struct irpc_message *msg, 
33                                            struct smbsrv_information *r)
34 {
35         struct smbsrv_connection *smb_conn = talloc_get_type(msg->private, struct smbsrv_connection);
36         int i=0, count=0;
37         struct smbsrv_session *sess;
38
39         /* count the number of sessions */
40         for (sess=smb_conn->sessions.list; sess; sess=sess->next) {
41                 count++;
42         }
43
44         r->out.info.sessions.num_sessions = count;
45         r->out.info.sessions.sessions = talloc_array(r, struct smbsrv_session_info, count);
46         NT_STATUS_HAVE_NO_MEMORY(r->out.info.sessions.sessions);
47
48         for (sess=smb_conn->sessions.list; sess; sess=sess->next) {
49                 struct smbsrv_session_info *info = &r->out.info.sessions.sessions[i];
50                 struct socket_address *client_addr;
51                 client_addr = socket_get_peer_addr(smb_conn->connection->socket, r);
52                 
53                 if (client_addr) {
54                         info->client_ip = client_addr->addr;
55                 } else {
56                         info->client_ip = NULL;
57                 }
58
59                 info->vuid         = sess->vuid;
60                 info->account_name = sess->session_info->server_info->account_name;
61                 info->domain_name  = sess->session_info->server_info->domain_name;
62                 
63                 info->connect_time = timeval_to_nttime(&sess->statistics.connect_time);
64                 info->auth_time    = timeval_to_nttime(&sess->statistics.auth_time);
65                 info->last_use_time= timeval_to_nttime(&sess->statistics.last_request_time);
66                 i++;
67         }       
68
69         return NT_STATUS_OK;
70 }
71
72 /*
73   return a list of tree connects
74 */
75 static NTSTATUS smbsrv_tcon_information(struct irpc_message *msg, 
76                                         struct smbsrv_information *r)
77 {
78         struct smbsrv_connection *smb_conn = talloc_get_type(msg->private, struct smbsrv_connection);
79         int i=0, count=0;
80         struct smbsrv_tcon *tcon;
81
82         /* count the number of tcons */
83         for (tcon=smb_conn->smb_tcons.list; tcon; tcon=tcon->next) {
84                 count++;
85         }
86
87         r->out.info.tcons.num_tcons = count;
88         r->out.info.tcons.tcons = talloc_array(r, struct smbsrv_tcon_info, count);
89         NT_STATUS_HAVE_NO_MEMORY(r->out.info.tcons.tcons);
90
91         for (tcon=smb_conn->smb_tcons.list; tcon; tcon=tcon->next) {
92                 struct smbsrv_tcon_info *info = &r->out.info.tcons.tcons[i];
93                 struct socket_address *client_addr;
94                 client_addr = socket_get_peer_addr(smb_conn->connection->socket, r);
95                 
96                 if (client_addr) {
97                         info->client_ip = client_addr->addr;
98                 } else {
99                         info->client_ip = NULL;
100                 }
101
102                 info->tid          = tcon->tid;
103                 info->share_name   = tcon->share_name;
104                 info->connect_time = timeval_to_nttime(&tcon->statistics.connect_time);
105                 info->last_use_time= timeval_to_nttime(&tcon->statistics.last_request_time);
106                 i++;
107         }
108
109         return NT_STATUS_OK;
110 }
111
112 /*
113   serve smbserver information via irpc
114 */
115 static NTSTATUS smbsrv_information(struct irpc_message *msg, 
116                                    struct smbsrv_information *r)
117 {
118         switch (r->in.level) {
119         case SMBSRV_INFO_SESSIONS:
120                 return smbsrv_session_information(msg, r);
121         case SMBSRV_INFO_TCONS:
122                 return smbsrv_tcon_information(msg, r);
123         }
124
125         return NT_STATUS_OK;
126 }
127
128 /*
129   initialise irpc management calls on a connection
130 */
131 void smbsrv_management_init(struct smbsrv_connection *smb_conn)
132 {
133         IRPC_REGISTER(smb_conn->connection->msg_ctx, irpc, SMBSRV_INFORMATION, 
134                       smbsrv_information, smb_conn);
135 }