s3/net: don't use external "date" to make "net time set" more portable
[metze/samba/wip.git] / source3 / utils / net_time.c
1 /*
2    Samba Unix/Linux SMB client library
3    net time command
4    Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
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 #include "includes.h"
20 #include "utils/net.h"
21
22 /*
23   return the time on a server. This does not require any authentication
24 */
25 static time_t cli_servertime(const char *host, struct sockaddr_storage *pss, int *zone)
26 {
27         struct nmb_name calling, called;
28         time_t ret = 0;
29         struct cli_state *cli = NULL;
30         NTSTATUS status;
31
32         cli = cli_initialise();
33         if (!cli) {
34                 goto done;
35         }
36
37         status = cli_connect(cli, host, pss);
38         if (!NT_STATUS_IS_OK(status)) {
39                 fprintf(stderr, _("Can't contact server %s. Error %s\n"),
40                         host, nt_errstr(status));
41                 goto done;
42         }
43
44         make_nmb_name(&calling, global_myname(), 0x0);
45         if (host) {
46                 make_nmb_name(&called, host, 0x20);
47         } else {
48                 make_nmb_name(&called, "*SMBSERVER", 0x20);
49         }
50
51         if (!cli_session_request(cli, &calling, &called)) {
52                 fprintf(stderr, _("Session request failed\n"));
53                 goto done;
54         }
55         status = cli_negprot(cli);
56         if (!NT_STATUS_IS_OK(status)) {
57                 fprintf(stderr, _("Protocol negotiation failed: %s\n"),
58                         nt_errstr(status));
59                 goto done;
60         }
61
62         ret = cli->servertime;
63         if (zone) *zone = cli->serverzone;
64
65 done:
66         if (cli) {
67                 cli_shutdown(cli);
68         }
69         return ret;
70 }
71
72 /* find the servers time on the opt_host host */
73 static time_t nettime(struct net_context *c, int *zone)
74 {
75         return cli_servertime(c->opt_host,
76                               c->opt_have_ip? &c->opt_dest_ip : NULL, zone);
77 }
78
79 /* return a time as a string ready to be passed to /bin/date */
80 static const char *systime(time_t t)
81 {
82         struct tm *tm;
83
84         tm = localtime(&t);
85         if (!tm) {
86                 return "unknown";
87         }
88
89         return talloc_asprintf(talloc_tos(), "%02d%02d%02d%02d%04d.%02d",
90                                tm->tm_mon+1, tm->tm_mday, tm->tm_hour,
91                                tm->tm_min, tm->tm_year + 1900, tm->tm_sec);
92 }
93
94 int net_time_usage(struct net_context *c, int argc, const char **argv)
95 {
96         d_printf(_(
97 "net time\n\tdisplays time on a server\n\n"
98 "net time system\n\tdisplays time on a server in a format ready for /bin/date\n\n"
99 "net time set\n\truns /bin/date with the time from the server\n\n"
100 "net time zone\n\tdisplays the timezone in hours from GMT on the remote computer\n\n"
101 "\n"));
102         net_common_flags_usage(c, argc, argv);
103         return -1;
104 }
105
106 /* try to set the system clock */
107 static int net_time_set(struct net_context *c, int argc, const char **argv)
108 {
109         struct timeval tv;
110         int result;
111
112         tv.tv_sec = nettime(c, NULL);
113         tv.tv_usec=0;
114
115         if (tv.tv_sec == 0) return -1;
116
117         result = settimeofday(&tv,0);
118
119         if (result)
120                 d_fprintf(stderr, _("setting system clock failed.  Error was (%s)\n"),
121                         strerror(errno));
122
123         return result;
124 }
125
126 /* display the time on a remote box in a format ready for /bin/date */
127 static int net_time_system(struct net_context *c, int argc, const char **argv)
128 {
129         time_t t;
130
131         if (c->display_usage) {
132                 d_printf(  "%s\n"
133                            "net time system\n"
134                            "    %s\n",
135                          _("Usage:"),
136                          _("Output remote time server time in a format "
137                            "ready for /bin/date"));
138                 return 0;
139         }
140
141         t = nettime(c, NULL);
142         if (t == 0) return -1;
143
144         printf("%s\n", systime(t));
145
146         return 0;
147 }
148
149 /* display the remote time server's offset to UTC */
150 static int net_time_zone(struct net_context *c, int argc, const char **argv)
151 {
152         int zone = 0;
153         int hours, mins;
154         char zsign;
155         time_t t;
156
157         if (c->display_usage) {
158                 d_printf(  "%s\n"
159                            "net time zone\n"
160                            "   %s\n",
161                          _("Usage:"),
162                          _("Display the remote time server's offset to "
163                            "UTC"));
164                 return 0;
165         }
166
167         t = nettime(c, &zone);
168
169         if (t == 0) return -1;
170
171         zsign = (zone > 0) ? '-' : '+';
172         if (zone < 0) zone = -zone;
173
174         zone /= 60;
175         hours = zone / 60;
176         mins = zone % 60;
177
178         printf("%c%02d%02d\n", zsign, hours, mins);
179
180         return 0;
181 }
182
183 /* display or set the time on a host */
184 int net_time(struct net_context *c, int argc, const char **argv)
185 {
186         time_t t;
187         struct functable func[] = {
188                 {
189                         "system",
190                         net_time_system,
191                         NET_TRANSPORT_LOCAL,
192                         N_("Display time ready for /bin/date"),
193                         N_("net time system\n"
194                            "    Display time ready for /bin/date")
195                 },
196                 {
197                         "set",
198                         net_time_set,
199                         NET_TRANSPORT_LOCAL,
200                         N_("Set the system time from time server"),
201                         N_("net time set\n"
202                            "    Set the system time from time server")
203                 },
204                 {
205                         "zone",
206                         net_time_zone,
207                         NET_TRANSPORT_LOCAL,
208                         N_("Display timezone offset from UTC"),
209                         N_("net time zone\n"
210                            "    Display timezone offset from UTC")
211                 },
212                 {NULL, NULL, 0, NULL, NULL}
213         };
214
215         if (argc != 0) {
216                 return net_run_function(c, argc, argv, "net time", func);
217         }
218
219         if (c->display_usage) {
220                 d_printf(  "%s\n"
221                            "net time\n"
222                            "    %s\n",
223                          _("Usage:"),
224                          _("Display the remote time server's time"));
225                 net_display_usage_from_functable(func);
226                 return 0;
227         }
228
229         if (!c->opt_host && !c->opt_have_ip &&
230             !find_master_ip(c->opt_target_workgroup, &c->opt_dest_ip)) {
231                 d_fprintf(stderr, _("Could not locate a time server.  Try "
232                                     "specifying a target host.\n"));
233                 net_time_usage(c, argc,argv);
234                 return -1;
235         }
236
237         /* default - print the time */
238         t = cli_servertime(c->opt_host, c->opt_have_ip? &c->opt_dest_ip : NULL,
239                            NULL);
240         if (t == 0) return -1;
241
242         d_printf("%s", ctime(&t));
243         return 0;
244 }