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