r23779: Change from v2 or later to v3 or later.
[kai/samba.git] / source3 / utils / net_time.c
index 3f5532109c21822aba95cac58e32d32f73cf95ef..7426e49ccc501ddf29642a9c904a0c60c2d11aaa 100644 (file)
@@ -5,7 +5,7 @@
 
    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
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -18,7 +18,7 @@
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #include "includes.h"
-#include "../utils/net.h"
+#include "utils/net.h"
 
 
 /*
@@ -28,18 +28,21 @@ static time_t cli_servertime(const char *host, struct in_addr *ip, int *zone)
 {
        struct nmb_name calling, called;
        time_t ret = 0;
-       extern pstring global_myname;
        struct cli_state *cli = NULL;
+       NTSTATUS status;
 
-       cli = cli_initialise(NULL);
-       if (!cli) goto done;
+       cli = cli_initialise();
+       if (!cli) {
+               goto done;
+       }
 
-       if (!cli_connect(cli, host, ip)) {
-               fprintf(stderr,"Can't contact server\n");
+       status = cli_connect(cli, host, ip);
+       if (!NT_STATUS_IS_OK(status)) {
+               fprintf(stderr,"Can't contact server %s. Error %s\n", host, nt_errstr(status));
                goto done;
        }
 
-       make_nmb_name(&calling, global_myname, 0x0);
+       make_nmb_name(&calling, global_myname(), 0x0);
        if (host) {
                make_nmb_name(&called, host, 0x20);
        } else {
@@ -59,28 +62,30 @@ static time_t cli_servertime(const char *host, struct in_addr *ip, int *zone)
        if (zone) *zone = cli->serverzone;
 
 done:
-       if (cli) cli_shutdown(cli);
+       if (cli) {
+               cli_shutdown(cli);
+       }
        return ret;
 }
 
 /* find the servers time on the opt_host host */
 static time_t nettime(int *zone)
 {
-       extern BOOL opt_have_ip;
-       extern struct in_addr opt_dest_ip;
-       extern char *opt_host; 
        return cli_servertime(opt_host, opt_have_ip? &opt_dest_ip : NULL, zone);
 }
 
 /* return a time as a string ready to be passed to /bin/date */
-static char *systime(time_t t)
+static const char *systime(time_t t)
 {
-       static char s[100];
+       static fstring s;
        struct tm *tm;
 
        tm = localtime(&t);
+       if (!tm) {
+               return "unknown";
+       }
        
-       snprintf(s, sizeof(s), "%02d%02d%02d%02d%04d.%02d", 
+       fstr_sprintf(s, "%02d%02d%02d%02d%04d.%02d", 
                 tm->tm_mon+1, tm->tm_mday, tm->tm_hour, 
                 tm->tm_min, tm->tm_year + 1900, tm->tm_sec);
        return s;
@@ -103,6 +108,7 @@ static int net_time_set(int argc, const char **argv)
 {
        time_t t = nettime(NULL);
        char *cmd;
+       int result;
 
        if (t == 0) return -1;
        
@@ -110,10 +116,13 @@ static int net_time_set(int argc, const char **argv)
           roll your own. I'm putting this in as it works on a large number
           of systems and the user has a choice in whether its used or not */
        asprintf(&cmd, "/bin/date %s", systime(t));
-       system(cmd);
+       result = system(cmd);
+       if (result)
+               d_fprintf(stderr, "%s failed.  Error was (%s)\n",
+                       cmd, strerror(errno));
        free(cmd);
 
-       return 0;
+       return result;
 }
 
 /* display the time on a remote box in a format ready for /bin/date */
@@ -156,9 +165,6 @@ static int net_time_zone(int argc, const char **argv)
 int net_time(int argc, const char **argv)
 {
        time_t t;
-       extern BOOL opt_have_ip;
-       extern struct in_addr opt_dest_ip;
-       extern char *opt_host; 
        struct functable func[] = {
                {"SYSTEM", net_time_system},
                {"SET", net_time_set},
@@ -166,8 +172,10 @@ int net_time(int argc, const char **argv)
                {NULL, NULL}
        };
 
-       if (!opt_host && !opt_have_ip) {
-               d_printf("You must specify a hostname or IP\n");
+       if (!opt_host && !opt_have_ip && 
+           !find_master_ip(opt_target_workgroup, &opt_dest_ip)) {
+               d_fprintf(stderr, "Could not locate a time server.  Try "\
+                                "specifying a target host.\n");
                net_time_usage(argc,argv);
                return -1;
        }