LCOV - code coverage report
Current view: top level - source4/libnet - libnet_time.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 50 56 89.3 %
Date: 2021-09-23 10:06:22 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    
       4             :    Copyright (C) Stefan Metzmacher      2004
       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             : 
      20             : #include "includes.h"
      21             : #include "libnet/libnet.h"
      22             : #include "system/time.h"
      23             : #include "librpc/gen_ndr/ndr_srvsvc_c.h"
      24             : 
      25             : /*
      26             :  * get the remote time of a server via srvsvc_NetRemoteTOD
      27             :  */
      28          14 : static NTSTATUS libnet_RemoteTOD_srvsvc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
      29             : {
      30           5 :         NTSTATUS status;
      31           5 :         struct libnet_RpcConnect c;
      32           5 :         struct srvsvc_NetRemoteTOD tod;
      33          14 :         struct srvsvc_NetRemoteTODInfo *info = NULL;
      34           5 :         struct tm tm;
      35             : 
      36          14 :         ZERO_STRUCT(c);
      37             : 
      38             :         /* prepare connect to the SRVSVC pipe of a timeserver */
      39          14 :         c.level             = LIBNET_RPC_CONNECT_SERVER;
      40          14 :         c.in.name           = r->srvsvc.in.server_name;
      41          14 :         c.in.dcerpc_iface   = &ndr_table_srvsvc;
      42             : 
      43             :         /* 1. connect to the SRVSVC pipe of a timeserver */
      44          14 :         status = libnet_RpcConnect(ctx, mem_ctx, &c);
      45          14 :         if (!NT_STATUS_IS_OK(status)) {
      46           1 :                 r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
      47             :                                                 "Connection to SRVSVC pipe of server '%s' failed: %s",
      48             :                                                 r->srvsvc.in.server_name, nt_errstr(status));
      49           1 :                 return status;
      50             :         }
      51             : 
      52             :         /* prepare srvsvc_NetrRemoteTOD */
      53          13 :         tod.in.server_unc = talloc_asprintf(mem_ctx, "\\%s", c.in.name);
      54          13 :         tod.out.info = &info;
      55             : 
      56             :         /* 2. try srvsvc_NetRemoteTOD */
      57          13 :         status = dcerpc_srvsvc_NetRemoteTOD_r(c.out.dcerpc_pipe->binding_handle, mem_ctx, &tod);
      58          13 :         if (!NT_STATUS_IS_OK(status)) {
      59           0 :                 r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
      60             :                                                 "srvsvc_NetrRemoteTOD on server '%s' failed: %s",
      61             :                                                 r->srvsvc.in.server_name, nt_errstr(status));
      62           0 :                 goto disconnect;
      63             :         }
      64             : 
      65             :         /* check result of srvsvc_NetrRemoteTOD */
      66          13 :         if (!W_ERROR_IS_OK(tod.out.result)) {
      67           0 :                 r->srvsvc.out.error_string = talloc_asprintf(mem_ctx,
      68             :                                                 "srvsvc_NetrRemoteTOD on server '%s' failed: %s",
      69             :                                                 r->srvsvc.in.server_name, win_errstr(tod.out.result));
      70           0 :                 status = werror_to_ntstatus(tod.out.result);
      71           0 :                 goto disconnect;
      72             :         }
      73             : 
      74             :         /* need to set the out parameters */
      75          13 :         tm.tm_sec = (int)info->secs;
      76          13 :         tm.tm_min = (int)info->mins;
      77          13 :         tm.tm_hour = (int)info->hours;
      78          13 :         tm.tm_mday = (int)info->day;
      79          13 :         tm.tm_mon = (int)info->month -1;
      80          13 :         tm.tm_year = (int)info->year - 1900;
      81          13 :         tm.tm_wday = -1;
      82          13 :         tm.tm_yday = -1;
      83          13 :         tm.tm_isdst = -1;
      84             : 
      85          13 :         r->srvsvc.out.time = timegm(&tm);
      86          13 :         r->srvsvc.out.time_zone = info->timezone * 60;
      87             : 
      88          13 :         goto disconnect;
      89             : 
      90          13 : disconnect:
      91             :         /* close connection */
      92          13 :         talloc_free(c.out.dcerpc_pipe);
      93             : 
      94          13 :         return status;
      95             : }
      96             : 
      97          14 : static NTSTATUS libnet_RemoteTOD_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
      98             : {
      99           5 :         NTSTATUS status;
     100           5 :         union libnet_RemoteTOD r2;
     101             : 
     102          14 :         r2.srvsvc.level                 = LIBNET_REMOTE_TOD_SRVSVC;
     103          14 :         r2.srvsvc.in.server_name        = r->generic.in.server_name;
     104             : 
     105          14 :         status = libnet_RemoteTOD(ctx, mem_ctx, &r2);
     106             : 
     107          14 :         r->generic.out.time          = r2.srvsvc.out.time;
     108          14 :         r->generic.out.time_zone     = r2.srvsvc.out.time_zone;
     109             : 
     110          14 :         r->generic.out.error_string  = r2.srvsvc.out.error_string;
     111             : 
     112          14 :         return status;
     113             : }
     114             : 
     115          28 : NTSTATUS libnet_RemoteTOD(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_RemoteTOD *r)
     116             : {
     117          28 :         switch (r->generic.level) {
     118          14 :                 case LIBNET_REMOTE_TOD_GENERIC:
     119          14 :                         return libnet_RemoteTOD_generic(ctx, mem_ctx, r);
     120          14 :                 case LIBNET_REMOTE_TOD_SRVSVC:
     121          14 :                         return libnet_RemoteTOD_srvsvc(ctx, mem_ctx, r);
     122             :         }
     123             : 
     124           0 :         return NT_STATUS_INVALID_LEVEL;
     125             : }

Generated by: LCOV version 1.13