summaryrefslogtreecommitdiff
path: root/source4/libnet/libnet_time.c
blob: 655a4755038e9d0a9e2e07e4dba73ef047a89953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* 
   Unix SMB/CIFS implementation.
   
   Copyright (C) Stefan Metzmacher	2004
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "libnet/libnet.h"
#include "librpc/gen_ndr/ndr_srvsvc.h"
#include "system/time.h"

/*
 * get the remote time of a server via srvsvc_NetRemoteTOD
 */
static NTSTATUS libnet_RemoteTOD_srvsvc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
{
        NTSTATUS status;
	struct libnet_RpcConnect c;
	struct srvsvc_NetRemoteTOD tod;
	struct tm tm;

	/* prepare connect to the SRVSVC pipe of a timeserver */
	c.level                    = LIBNET_RPC_CONNECT_SERVER;
	c.in.domain_name           = r->srvsvc.in.server_name;
	c.in.dcerpc_iface     	   = &dcerpc_table_srvsvc;

	/* 1. connect to the SRVSVC pipe of a timeserver */
	status = libnet_RpcConnect(ctx, mem_ctx, &c);
	if (!NT_STATUS_IS_OK(status)) {
		r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
						"Connection to SRVSVC pipe of server '%s' failed: %s\n",
						r->srvsvc.in.server_name, nt_errstr(status));
		return status;
	}

	/* prepare srvsvc_NetrRemoteTOD */
	tod.in.server_unc = talloc_asprintf(mem_ctx, "\\%s", c.in.domain_name);

	/* 2. try srvsvc_NetRemoteTOD */
	status = dcerpc_srvsvc_NetRemoteTOD(c.out.dcerpc_pipe, mem_ctx, &tod);
	if (!NT_STATUS_IS_OK(status)) {
		r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
						"srvsvc_NetrRemoteTOD on server '%s' failed: %s\n",
						r->srvsvc.in.server_name, nt_errstr(status));
		goto disconnect;
	}

	/* check result of srvsvc_NetrRemoteTOD */
	if (!W_ERROR_IS_OK(tod.out.result)) {
		r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
						"srvsvc_NetrRemoteTOD on server '%s' failed: %s\n",
						r->srvsvc.in.server_name, win_errstr(tod.out.result));
		status = werror_to_ntstatus(tod.out.result);
		goto disconnect;
	}

	/* need to set the out parameters */
	tm.tm_sec = (int)tod.out.info->secs;
	tm.tm_min = (int)tod.out.info->mins;
	tm.tm_hour = (int)tod.out.info->hours;
	tm.tm_mday = (int)tod.out.info->day;
	tm.tm_mon = (int)tod.out.info->month -1;
	tm.tm_year = (int)tod.out.info->year - 1900;
	tm.tm_wday = -1;
	tm.tm_yday = -1;
	tm.tm_isdst = -1;

	r->srvsvc.out.time = timegm(&tm);
	r->srvsvc.out.time_zone = tod.out.info->timezone * 60;

	goto disconnect;

disconnect:
	/* close connection */
	talloc_free(c.out.dcerpc_pipe);

	return status;
}

static NTSTATUS libnet_RemoteTOD_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
{
	NTSTATUS status;
	union libnet_RemoteTOD r2;

	r2.srvsvc.level			= LIBNET_REMOTE_TOD_SRVSVC;
	r2.srvsvc.in.server_name	= r->generic.in.server_name;

	status = libnet_RemoteTOD(ctx, mem_ctx, &r2);

	r->generic.out.time		= r2.srvsvc.out.time;
	r->generic.out.time_zone	= r2.srvsvc.out.time_zone;

	r->generic.out.error_string	= r2.srvsvc.out.error_string;

	return status;
}

NTSTATUS libnet_RemoteTOD(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
{
	switch (r->generic.level) {
		case LIBNET_REMOTE_TOD_GENERIC:
			return libnet_RemoteTOD_generic(ctx, mem_ctx, r);
		case LIBNET_REMOTE_TOD_SRVSVC:
			return libnet_RemoteTOD_srvsvc(ctx, mem_ctx, r);
	}

	return NT_STATUS_INVALID_LEVEL;
}