1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*-
* syslog-rs - a syslog client translated from libc to rust
* Copyright (C) 2021  Aleksandr Morozov
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


use std::os::unix::net::UnixDatagram;

use std::{
    net::Shutdown,
    os::unix::{
        prelude::AsRawFd, 
        io::RawFd
    }
};

use nix::errno::Errno;

use crate::{map_error, throw_error};

use super::common::*;
use super::error::{SyRes, SyslogError, SyslogErrCode};

/// A private enum with type of the socket
pub(crate) enum SyslogSocket
{
    /// Not initialized
    None,

    /// Unprivileged socket used
    UnPriv(UnixDatagram),

    /// Privileged socket used
    Priv(UnixDatagram),

    /// A compatibility socket used
    OldLog(UnixDatagram),
}

unsafe impl Sync for SyslogSocket{}
unsafe impl Send for SyslogSocket{}

impl SyslogSocket
{
    /// Creates a default instance
    pub(crate) fn none() -> Self
    {
        return Self::None;
    }

    /// Returns the rawfd without passing the ownership ower the
    /// the unixsocket. Will panic if called on enum None.
    pub(crate) fn get_raw_fd(&self) -> RawFd
    {
        match *self
        {
            Self::None => panic!("Assertion: wrong use of get_raw_fd()"),
            Self::UnPriv(ref s) | 
            Self::Priv(ref s) | 
            Self::OldLog(ref s) =>
            {
                return s.as_raw_fd();
            }
        }
    }

    /// Performs the connection to the syslog's server socket.
    /// Picks the socket automatically.
    pub(crate) fn connect() -> SyRes<Self>
    {
        let sock = 
            UnixDatagram::unbound().map_err(|e|
                map_error!("unbounded unix datagram create failed: {}", e)
            )?;

        if let Ok(_) = sock.connect(PATH_LOG_PRIV)
        {
            return Ok(Self::Priv(sock));
        }
        else if let Ok(_) = sock.connect(PATH_LOG)
        {
            return Ok(Self::UnPriv(sock));
        }
        else if let Ok(_) = sock.connect(PATH_OLDLOG)
        {
            return Ok(Self::OldLog(sock));
        }
        else if let Ok(_) = sock.connect(PATH_OSX)
        {
            return Ok(Self::Priv(sock));
        }
        else
        {
            // failed to open socket
            throw_error!("failed to open connection to syslog server: {} ", 
                        Errno::last());
        };
    }

    /// Writes to the socket. Will panic if instance is enum None.
    pub(crate) fn send(&mut self, msg: &[u8]) -> std::io::Result<usize>
    {
        match *self
        {
            Self::None => panic!("Assertion: wrong use of get_raw_fd()"),
            Self::UnPriv(ref mut s) | 
            Self::Priv(ref mut s) | 
            Self::OldLog(ref mut s) =>
            {
                return s.send(msg);
            }
        } 
    }

    /// Shutdowns the socket. Will panic if instance is enum None.
    pub(crate) fn shutdown(&mut self)
    {
        match *self
        {
            Self::None => panic!("Assertion: wrong use of get_raw_fd()"),
            Self::UnPriv(ref mut s) | 
            Self::Priv(ref mut s) | 
            Self::OldLog(ref mut s) =>
            {
                let _ = s.shutdown(Shutdown::Both);
            }
        }
    }

    /// Returns true is current instance of enum is None.
    pub(crate) fn is_none(&self) -> bool
    {
        match self
        {
            Self::None => return true,
            Self::UnPriv(_) | 
            Self::Priv(_) | 
            Self::OldLog(_) => return false,
        }
    }

    /// Returns true if stream was connected to privileged socket.
    pub(crate) fn is_priv(&self) -> bool
    {
        match self
        {
            Self::None => return false,
            Self::UnPriv(_) => return false,
            Self::Priv(_) => return true,
            Self::OldLog(_) => return false,
        }
    }
}