r23792: convert Samba4 to GPLv3
[bbaumbach/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "libnet/libnet.h"
22 #include "system/time.h"
23 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
24
25 /*
26  * get the remote time of a server via srvsvc_NetRemoteTOD
27  */
28 static NTSTATUS libnet_RemoteTOD_srvsvc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
29 {
30         NTSTATUS status;
31         struct libnet_RpcConnect c;
32         struct srvsvc_NetRemoteTOD tod;
33         struct tm tm;
34
35         /* prepare connect to the SRVSVC pipe of a timeserver */
36         c.level             = LIBNET_RPC_CONNECT_SERVER;
37         c.in.name           = r->srvsvc.in.server_name;
38         c.in.dcerpc_iface   = &dcerpc_table_srvsvc;
39
40         /* 1. connect to the SRVSVC pipe of a timeserver */
41         status = libnet_RpcConnect(ctx, mem_ctx, &c);
42         if (!NT_STATUS_IS_OK(status)) {
43                 r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
44                                                 "Connection to SRVSVC pipe of server '%s' failed: %s",
45                                                 r->srvsvc.in.server_name, nt_errstr(status));
46                 return status;
47         }
48
49         /* prepare srvsvc_NetrRemoteTOD */
50         tod.in.server_unc = talloc_asprintf(mem_ctx, "\\%s", c.in.name);
51
52         /* 2. try srvsvc_NetRemoteTOD */
53         status = dcerpc_srvsvc_NetRemoteTOD(c.out.dcerpc_pipe, mem_ctx, &tod);
54         if (!NT_STATUS_IS_OK(status)) {
55                 r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
56                                                 "srvsvc_NetrRemoteTOD on server '%s' failed: %s",
57                                                 r->srvsvc.in.server_name, nt_errstr(status));
58                 goto disconnect;
59         }
60
61         /* check result of srvsvc_NetrRemoteTOD */
62         if (!W_ERROR_IS_OK(tod.out.result)) {
63                 r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
64                                                 "srvsvc_NetrRemoteTOD on server '%s' failed: %s",
65                                                 r->srvsvc.in.server_name, win_errstr(tod.out.result));
66                 status = werror_to_ntstatus(tod.out.result);
67                 goto disconnect;
68         }
69
70         /* need to set the out parameters */
71         tm.tm_sec = (int)tod.out.info->secs;
72         tm.tm_min = (int)tod.out.info->mins;
73         tm.tm_hour = (int)tod.out.info->hours;
74         tm.tm_mday = (int)tod.out.info->day;
75         tm.tm_mon = (int)tod.out.info->month -1;
76         tm.tm_year = (int)tod.out.info->year - 1900;
77         tm.tm_wday = -1;
78         tm.tm_yday = -1;
79         tm.tm_isdst = -1;
80
81         r->srvsvc.out.time = timegm(&tm);
82         r->srvsvc.out.time_zone = tod.out.info->timezone * 60;
83
84         goto disconnect;
85
86 disconnect:
87         /* close connection */
88         talloc_free(c.out.dcerpc_pipe);
89
90         return status;
91 }
92
93 static NTSTATUS libnet_RemoteTOD_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
94 {
95         NTSTATUS status;
96         union libnet_RemoteTOD r2;
97
98         r2.srvsvc.level                 = LIBNET_REMOTE_TOD_SRVSVC;
99         r2.srvsvc.in.server_name        = r->generic.in.server_name;
100
101         status = libnet_RemoteTOD(ctx, mem_ctx, &r2);
102
103         r->generic.out.time             = r2.srvsvc.out.time;
104         r->generic.out.time_zone        = r2.srvsvc.out.time_zone;
105
106         r->generic.out.error_string     = r2.srvsvc.out.error_string;
107
108         return status;
109 }
110
111 NTSTATUS libnet_RemoteTOD(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
112 {
113         switch (r->generic.level) {
114                 case LIBNET_REMOTE_TOD_GENERIC:
115                         return libnet_RemoteTOD_generic(ctx, mem_ctx, r);
116                 case LIBNET_REMOTE_TOD_SRVSVC:
117                         return libnet_RemoteTOD_srvsvc(ctx, mem_ctx, r);
118         }
119
120         return NT_STATUS_INVALID_LEVEL;
121 }