| 1 | /*
|
|---|
| 2 | Samba Unix/Linux SMB client library
|
|---|
| 3 | net time command
|
|---|
| 4 | Copyright (C) 2001 Andrew Tridgell ([email protected])
|
|---|
| 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 | #ifdef __OS2__
|
|---|
| 120 | /* OS/2 native "net time set" routine */
|
|---|
| 121 | result = os2_setdatetime(tv.tv_sec);
|
|---|
| 122 | #else
|
|---|
| 123 | result = settimeofday(&tv,NULL);
|
|---|
| 124 | #endif
|
|---|
| 125 |
|
|---|
| 126 | if (result)
|
|---|
| 127 | d_fprintf(stderr, _("setting system clock failed. Error was (%s)\n"),
|
|---|
| 128 | strerror(errno));
|
|---|
| 129 |
|
|---|
| 130 | return result;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /* display the time on a remote box in a format ready for /bin/date */
|
|---|
| 134 | static int net_time_system(struct net_context *c, int argc, const char **argv)
|
|---|
| 135 | {
|
|---|
| 136 | time_t t;
|
|---|
| 137 |
|
|---|
| 138 | if (c->display_usage) {
|
|---|
| 139 | d_printf( "%s\n"
|
|---|
| 140 | "net time system\n"
|
|---|
| 141 | " %s\n",
|
|---|
| 142 | _("Usage:"),
|
|---|
| 143 | _("Output remote time server time in a format "
|
|---|
| 144 | "ready for /bin/date"));
|
|---|
| 145 | return 0;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | t = nettime(c, NULL);
|
|---|
| 149 | if (t == 0) return -1;
|
|---|
| 150 |
|
|---|
| 151 | printf("%s\n", systime(t));
|
|---|
| 152 |
|
|---|
| 153 | return 0;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /* display the remote time server's offset to UTC */
|
|---|
| 157 | static int net_time_zone(struct net_context *c, int argc, const char **argv)
|
|---|
| 158 | {
|
|---|
| 159 | int zone = 0;
|
|---|
| 160 | int hours, mins;
|
|---|
| 161 | char zsign;
|
|---|
| 162 | time_t t;
|
|---|
| 163 |
|
|---|
| 164 | if (c->display_usage) {
|
|---|
| 165 | d_printf( "%s\n"
|
|---|
| 166 | "net time zone\n"
|
|---|
| 167 | " %s\n",
|
|---|
| 168 | _("Usage:"),
|
|---|
| 169 | _("Display the remote time server's offset to "
|
|---|
| 170 | "UTC"));
|
|---|
| 171 | return 0;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | t = nettime(c, &zone);
|
|---|
|
|---|