/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to client.cpp

  • Committer: Teddy Hogeborn
  • Date: 2019-08-02 22:16:53 UTC
  • Revision ID: teddy@recompile.se-20190802221653-ic1iko9hbefzwsk7
Fix bug in server Debian package: Fails to start on first install

There has been a very long-standing bug where installation of the
server (the "mandos" Debian package) would fail to start the server
properly right after installation.  It would work on manual (re)start
after installation, or after reboot, and even after package purge and
reinstall, it would then work the first time.  The problem, it turns
out, is when the new "_mandos" user (and corresponding group) is
created, the D-Bus server is not reloaded, and is therefore not aware
of that user, and does not recognize the user and group name in the
/etc/dbus-1/system.d/mandos.conf file.  The Mandos server, when it
tries to start and access the D-Bus, is then not permitted to connect
to its D-Bus bus name, and disables D-Bus use as a fallback measure;
i.e. the server works, but it is not controllable via D-Bus commands
(via mandos-ctl or mandos-monitor).  The next time the D-Bus daemon is
reloaded for any reason, the new user & group would become visible to
the D-Bus daemon and after that, any restart of the Mandos server
would succeed and it would bind to its D-Bus name properly, and
thereby be visible and controllable by mandos-ctl & mandos-monitor.
This was mostly invisible when using sysvinit, but systemd makes the
problem visible since the systemd service file for the Mandos server
is configured to not consider the Mandos server "started" until the
D-Bus name has been bound; this makes the starting of the service wait
for 90 seconds and then fail with a timeout error.

Fixing this should also make the Debian CI autopkgtest tests work.

* debian/mandos.postinst (configure): After creating (or renaming)
                                      user & group, reload D-Bus
                                      daemon (if present).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
extern "C" {
2
 
#include <sys/types.h>          // getaddrinfo, gai_strerror, socket, inet_pton
3
 
                                // connect
4
 
#include <sys/socket.h>         // getaddrinfo, gai_strerror, socket, inet_pton
5
 
                                // connect
6
 
#include <unistd.h>             // close
7
 
#include <netdb.h>              // getaddrinfo, gai_strerror
8
 
#include <arpa/inet.h>          // inet_pton
9
 
#include <sys/select.h>         // select
10
 
#include <gnutls/gnutls.h>
11
 
}
12
 
 
13
 
#include <cstdio>               // fprintf
14
 
#include <cerrno>               // perror
15
 
#include <cstring>              // memset
16
 
 
17
 
#define SOCKET_ERR(err,s) if(err<0) {perror(s);return(1);}
18
 
#define PORT 49001
19
 
#define CERTFILE "client-cert.pem"
20
 
#define KEYFILE "client-key.pem"
21
 
#define CAFILE "ca.pem"
22
 
 
23
 
gnutls_certificate_credentials_t x509_cred;
24
 
 
25
 
gnutls_session_t
26
 
initgnutls(){
27
 
  gnutls_session_t session;
28
 
 
29
 
  gnutls_global_init ();
30
 
 
31
 
  /* X509 stuff */
32
 
  gnutls_certificate_allocate_credentials (&x509_cred);
33
 
  gnutls_certificate_set_x509_trust_file (x509_cred, CAFILE, GNUTLS_X509_FMT_PEM);
34
 
  gnutls_certificate_set_x509_key_file (x509_cred, CERTFILE, KEYFILE,
35
 
                                        GNUTLS_X509_FMT_PEM);
36
 
 
37
 
  //Gnutls stuff
38
 
  gnutls_init (&session, GNUTLS_CLIENT);
39
 
  gnutls_set_default_priority (session);
40
 
  gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
41
 
  return session;
42
 
}
43
 
 
44
 
 
45
 
int main (){
46
 
  int sd, ret;
47
 
  char buffer[512];
48
 
  struct sockaddr_in6 to;
49
 
  struct sockaddr_in6 from;
50
 
  gnutls_session_t session;
51
 
  fd_set rfds_orig;
52
 
  struct timeval timeout;
53
 
 
54
 
  session = initgnutls ();
55
 
 
56
 
  sd = socket(PF_INET6, SOCK_DGRAM, 0);
57
 
  SOCKET_ERR(sd,"socket");
58
 
 
59
 
  {
60
 
    int flag = 1;
61
 
    ret = setsockopt(sd, SOL_SOCKET, SO_BROADCAST, & flag, sizeof(flag));
62
 
    SOCKET_ERR(ret,"setsockopt broadcast");
63
 
  }
64
 
 
65
 
  setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
66
 
  SOCKET_ERR(ret,"setsockopt bindtodevice");
67
 
 
68
 
  memset (&to, '\0', sizeof (to));
69
 
  to.sin6_family = AF_INET6;
70
 
  ret = inet_pton(AF_INET6, "ff02::1" , &to.sin6_addr);
71
 
  SOCKET_ERR(ret,"setsockopt bindtodevice");
72
 
  to.sin6_port = htons (PORT);  // Server Port number
73
 
 
74
 
  FD_ZERO(&rfds_orig);
75
 
  FD_SET(sd, &rfds_orig);
76
 
 
77
 
  timeout.tv_sec = 10;
78
 
  timeout.tv_usec = 0;
79
 
 
80
 
 
81
 
  for(;;){
82
 
    sendto(sd, "Marco", 5, 0, reinterpret_cast<const sockaddr*>(&to), sizeof(to));
83
 
 
84
 
    fd_set rfds = rfds_orig;
85
 
 
86
 
    ret = select(sd+1, &rfds, 0, 0, & timeout);
87
 
    SOCKET_ERR(sd,"select");
88
 
 
89
 
    if (ret){
90
 
      socklen_t from_len = sizeof(from);
91
 
      ret = recvfrom(sd,buffer,512,0, reinterpret_cast<sockaddr *>(& from),
92
 
                     & from_len);
93
 
      SOCKET_ERR(ret,"recv");
94
 
 
95
 
      if (strncmp(buffer,"Polo", 4) == 0){
96
 
        break;
97
 
      }
98
 
    }
99
 
  }
100
 
 
101
 
  write(1,buffer,ret);
102
 
  write(1,"\n",1);
103
 
 
104
 
  //shutdown procedure
105
 
  close(sd);
106
 
 
107
 
  sleep(1);
108
 
 
109
 
  sd = socket(PF_INET6, SOCK_STREAM, 0);
110
 
  SOCKET_ERR(sd,"socket");
111
 
 
112
 
  setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
113
 
  SOCKET_ERR(ret,"setsockopt bindtodevice");
114
 
 
115
 
  memset(&to,0,sizeof(to));
116
 
  to.sin6_family = from.sin6_family;
117
 
  to.sin6_port   = from.sin6_port;
118
 
  to.sin6_addr   = from.sin6_addr;
119
 
  to.sin6_scope_id   = from.sin6_scope_id;
120
 
 
121
 
  ret = connect(sd,reinterpret_cast<struct sockaddr *>(&to),sizeof(to));
122
 
  SOCKET_ERR(ret,"connect");
123
 
 
124
 
  gnutls_transport_set_ptr (session, reinterpret_cast<gnutls_transport_ptr_t> (sd));
125
 
 
126
 
  ret = gnutls_handshake (session);
127
 
 
128
 
  if (ret < 0)
129
 
    {
130
 
      fprintf (stderr, "*** Handshake failed\n");
131
 
      gnutls_perror (ret);
132
 
      return 1;
133
 
    }
134
 
  printf ("- Handshake was completed\n");
135
 
 
136
 
  //message to be seent
137
 
  gnutls_record_send (session, "The secret message is \"squeamish ossifrage\"\n", 44);
138
 
 
139
 
  //shutdown procedure
140
 
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
141
 
  close(sd);
142
 
  gnutls_deinit (session);
143
 
  gnutls_certificate_free_credentials (x509_cred);
144
 
  gnutls_global_deinit ();
145
 
 
146
 
  close(sd);
147
 
 
148
 
  return 0;
149
 
}