r8819: fixed a memory leak in irpc_call()
[samba.git] / source4 / libnet / libnet_time.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Stefan Metzmacher      2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "libnet/libnet.h"
23 #include "librpc/gen_ndr/ndr_srvsvc.h"
24 #include "system/time.h"
25
26 /*
27  * get the remote time of a server via srvsvc_NetRemoteTOD
28  */
29 static NTSTATUS libnet_RemoteTOD_srvsvc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
30 {
31         NTSTATUS status;
32         struct libnet_RpcConnect c;
33         struct srvsvc_NetRemoteTOD tod;
34         struct tm tm;
35
36         /* prepare connect to the SRVSVC pipe of a timeserver */
37         c.level                    = LIBNET_RPC_CONNECT_SERVER;
38         c.in.domain_name           = r->srvsvc.in.server_name;
39         c.in.dcerpc_iface_name     = DCERPC_SRVSVC_NAME;
40         c.in.dcerpc_iface_uuid     = DCERPC_SRVSVC_UUID;
41         c.in.dcerpc_iface_version  = DCERPC_SRVSVC_VERSION;
42
43         /* 1. connect to the SRVSVC pipe of a timeserver */
44         status = libnet_RpcConnect(ctx, mem_ctx, &c);
45         if (!NT_STATUS_IS_OK(status)) {
46                 r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
47                                                 "Connection to SRVSVC pipe of server '%s' failed: %s\n",
48                                                 r->srvsvc.in.server_name, nt_errstr(status));
49                 return status;
50         }
51
52         /* prepare srvsvc_NetrRemoteTOD */
53         tod.in.server_unc = talloc_asprintf(mem_ctx, "\\%s", c.in.domain_name);
54
55         /* 2. try srvsvc_NetRemoteTOD */
56         status = dcerpc_srvsvc_NetRemoteTOD(c.out.dcerpc_pipe, mem_ctx, &tod);
57         if (!NT_STATUS_IS_OK(status)) {
58                 r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
59                                                 "srvsvc_NetrRemoteTOD on server '%s' failed: %s\n",
60                                                 r->srvsvc.in.server_name, nt_errstr(status));
61                 goto disconnect;
62         }
63
64         /* check result of srvsvc_NetrRemoteTOD */
65         if (!W_ERROR_IS_OK(tod.out.result)) {
66                 r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
67                                                 "srvsvc_NetrRemoteTOD on server '%s' failed: %s\n",
68                                                 r->srvsvc.in.server_name, win_errstr(tod.out.result));
69                 status = werror_to_ntstatus(tod.out.result);
70                 goto disconnect;
71         }
72
73         /* need to set the out parameters */
74         tm.tm_sec = (int)tod.out.info->secs;
75         tm.tm_min = (int)tod.out.info->mins;
76         tm.tm_hour = (int)tod.out.info->hours;
77         tm.tm_mday = (int)tod.out.info->day;
78         tm.tm_mon = (int)tod.out.info->month -1;
79         tm.tm_year = (int)tod.out.info->year - 1900;
80         tm.tm_wday = -1;
81         tm.tm_yday = -1;
82         tm.tm_isdst = -1;
83
84         r->srvsvc.out.time = timegm(&tm);
85         r->srvsvc.out.time_zone = tod.out.info->timezone * 60;
86
87         goto disconnect;
88
89 disconnect:
90         /* close connection */
91         talloc_free(c.out.dcerpc_pipe);
92
93         return status;
94 }
95
96 static NTSTATUS libnet_RemoteTOD_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
97 {
98         NTSTATUS status;
99         union libnet_RemoteTOD r2;
100
101         r2.srvsvc.level                 = LIBNET_REMOTE_TOD_SRVSVC;
102         r2.srvsvc.in.server_name        = r->generic.in.server_name;
103
104         status = libnet_RemoteTOD(ctx, mem_ctx, &r2);
105
106         r->generic.out.time             = r2.srvsvc.out.time;
107         r->generic.out.time_zone        = r2.srvsvc.out.time_zone;
108
109         r->generic.out.error_string     = r2.srvsvc.out.error_string;
110
111         return status;
112 }
113
114 NTSTATUS libnet_RemoteTOD(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
115 {
116         switch (r->generic.level) {
117                 case LIBNET_REMOTE_TOD_GENERIC:
118                         return libnet_RemoteTOD_generic(ctx, mem_ctx, r);
119                 case LIBNET_REMOTE_TOD_SRVSVC:
120                         return libnet_RemoteTOD_srvsvc(ctx, mem_ctx, r);
121         }
122
123         return NT_STATUS_INVALID_LEVEL;
124 }