git.fiddlerwoaroof.com
Browse code

add username to error log

Greg Wiley authored on 26/04/2017 18:11:05
Showing 9 changed files
... ...
@@ -15,42 +15,50 @@
15 15
 #include "sys_syslog.h"
16 16
 #include "logger.h"
17 17
 
18
-namespace {
19
-    class impl : public logger_ifc {
20
-        private:
21
-            sys_syslog syslog_;
22
-        public:
23
-            impl(const sys_syslog &sys_syslog) : syslog_(sys_syslog) {}
24
-            void log (int result, const std::string &user_name,
25
-              const std::string &token) {
26
-                std::string message;
27
-                int facility;
28
-                int priority;
29
-                switch (result) {
30
-                   case PAM_SUCCESS:
31
-                       facility = LOG_AUTHPRIV;
32
-                       priority = LOG_NOTICE;
33
-                       message = user_name + " " + token + " " + "success";
34
-                       break;
35
-                   case PAM_AUTH_ERR:
36
-                       facility = LOG_AUTHPRIV;
37
-                       priority = LOG_NOTICE;
38
-                       message = user_name + " " + token + " " + "fail";
39
-                       break;
40
-                    default:
41
-                        facility = LOG_AUTH;
42
-                        priority = LOG_ERR;
43
-                        message = "pam returned error";
44
-                        break;
45
-                }
46
-
47
-                syslog_.openlog("dual-control", 0, facility);
48
-                syslog_.syslog(priority, message.c_str());
49
-               syslog_.closelog();
50
-            }
51
-    };
18
+namespace
19
+{
20
+class impl : public logger_ifc
21
+{
22
+private:
23
+    sys_syslog syslog_;
24
+public:
25
+    impl (const sys_syslog &sys_syslog) : syslog_ (sys_syslog) {}
26
+    void log (int result, const std::string &user_name,
27
+              const std::string &token)
28
+    {
29
+        std::string message;
30
+        int facility;
31
+        int priority;
32
+
33
+        switch (result) {
34
+        case PAM_SUCCESS:
35
+            facility = LOG_AUTHPRIV;
36
+            priority = LOG_NOTICE;
37
+            message = user_name + " " + token + " " + "success";
38
+            break;
39
+
40
+        case PAM_AUTH_ERR:
41
+            facility = LOG_AUTHPRIV;
42
+            priority = LOG_NOTICE;
43
+            message = user_name + " " + token + " " + "fail";
44
+            break;
45
+
46
+        default:
47
+            facility = LOG_AUTH;
48
+            priority = LOG_ERR;
49
+            message = user_name + " pam returned error";
50
+            break;
51
+        }
52
+
53
+        syslog_.openlog ("dual-control", 0, facility);
54
+        syslog_.syslog (priority, message.c_str());
55
+        syslog_.closelog();
56
+    }
57
+};
52 58
 }
53 59
 
54
-logger logger::create(const sys_syslog &sys_syslog) {
55
-    return logger(delegate(new impl(sys_syslog)));
60
+logger logger::create (const sys_syslog &sys_syslog)
61
+{
62
+    return logger (delegate (new impl (sys_syslog)));
56 63
 }
64
+
... ...
@@ -27,8 +27,8 @@ public:
27 27
 
28 28
 class logger : public logger_ifc
29 29
 {
30
-    public:
31
-        typedef std::shared_ptr<logger_ifc> delegate;
30
+public:
31
+    typedef std::shared_ptr<logger_ifc> delegate;
32 32
 private:
33 33
     delegate delegate_;
34 34
 public:
... ...
@@ -40,7 +40,7 @@ public:
40 40
     {
41 41
         delegate_->log (result, user_name, token);
42 42
     }
43
-    static logger create(const sys_syslog &sys_syslog);
43
+    static logger create (const sys_syslog &sys_syslog);
44 44
 };
45 45
 
46 46
 #endif
... ...
@@ -18,91 +18,98 @@
18 18
 #include "logger.h"
19 19
 #include "test_util.h"
20 20
 
21
-class mock_syslog : public sys_syslog_ifc {
22
-    public:
23
-        int facility;
24
-        std::string message;
25
-        int priority;
26
-        bool closed;
27
-        std::string ident;
28
-        mock_syslog() : closed(false), facility(-1000), priority(-1000) {}
29
-        void openlog(const char *ident, int logopt, int facility) {
30
-            this->facility = facility;
31
-            this->ident = ident;
32
-        }
33
-        void vsyslog(int priority, const char *message, va_list args) {
34
-            this->priority = priority;
35
-            this->message = message;
36
-        }
37
-        void closelog()
38
-        {
39
-            this->closed = true;
40
-        }
41
-
21
+class mock_syslog : public sys_syslog_ifc
22
+{
23
+public:
24
+    int facility;
25
+    std::string message;
26
+    int priority;
27
+    bool closed;
28
+    std::string ident;
29
+    mock_syslog() : closed (false), facility (-1000), priority (-1000) {}
30
+    void openlog (const char *ident, int logopt, int facility)
31
+    {
32
+        this->facility = facility;
33
+        this->ident = ident;
34
+    }
35
+    void vsyslog (int priority, const char *message, va_list args)
36
+    {
37
+        this->priority = priority;
38
+        this->message = message;
39
+    }
40
+    void closelog()
41
+    {
42
+        this->closed = true;
43
+    }
42 44
 
43 45
 };
44 46
 
45
-
46
-int logs_success() {
47
+int logs_success()
48
+{
47 49
     //given
48 50
     mock_syslog *capture = new mock_syslog;
49
-    sys_syslog::delegate test_delegate(capture);
50
-    sys_syslog test_syslog(test_delegate);
51
-    logger logger = logger::create(test_syslog);
52
-    std::string user("user");
53
-    std::string token("token");
51
+    sys_syslog::delegate test_delegate (capture);
52
+    sys_syslog test_syslog (test_delegate);
53
+    logger logger = logger::create (test_syslog);
54
+    std::string user ("user");
55
+    std::string token ("token");
54 56
 
55 57
     //when
56
-    logger.log(PAM_SUCCESS, user, token);
58
+    logger.log (PAM_SUCCESS, user, token);
57 59
 
58 60
     //then
59
-    check(capture->facility == LOG_AUTHPRIV, "facility does not match");
60
-    check(capture->message == user + " " + token + " " + "success", "message does not match");
61
-    check(capture->priority == LOG_NOTICE, "priority does not match");
62
-    check(capture->closed, "syslog not closed");
63
-    check(capture->ident == "dual-control", "dual-control");
61
+    check (capture->facility == LOG_AUTHPRIV, "facility does not match");
62
+    check (capture->message == user + " " + token + " " + "success",
63
+           "message does not match");
64
+    check (capture->priority == LOG_NOTICE, "priority does not match");
65
+    check (capture->closed, "syslog not closed");
66
+    check (capture->ident == "dual-control", "dual-control");
64 67
     succeed();
65 68
 }
66 69
 
67
-int logs_failure() {
70
+int logs_failure()
71
+{
68 72
     //given
69 73
     mock_syslog *capture = new mock_syslog;
70
-    sys_syslog::delegate test_delegate(capture);
71
-    sys_syslog test_syslog(test_delegate);
72
-    logger logger = logger::create(test_syslog);
73
-    std::string user("user");
74
-    std::string token("token");
74
+    sys_syslog::delegate test_delegate (capture);
75
+    sys_syslog test_syslog (test_delegate);
76
+    logger logger = logger::create (test_syslog);
77
+    std::string user ("user");
78
+    std::string token ("token");
75 79
 
76 80
     //when
77
-    logger.log(PAM_AUTH_ERR, user, token);
81
+    logger.log (PAM_AUTH_ERR, user, token);
78 82
 
79 83
     //then
80
-    check(capture->facility == LOG_AUTHPRIV, "facility does not match");
81
-    check(capture->message == user + " " + token + " " + "fail", "message does not match");
82
-    check(capture->priority == LOG_NOTICE, "priority does not match");
83
-    check(capture->closed, "syslog not closed");
84
-    check(capture->ident == "dual-control", "dual-control");
84
+    check (capture->facility == LOG_AUTHPRIV, "facility does not match");
85
+    check (capture->message == user + " " + token + " " + "fail",
86
+           "message does not match");
87
+    check (capture->priority == LOG_NOTICE, "priority does not match");
88
+    check (capture->closed, "syslog not closed");
89
+    check (capture->ident == "dual-control", "dual-control");
85 90
     succeed();
86 91
 }
87 92
 
88
-int logs_pam_service_error() {
93
+int logs_pam_service_error()
94
+{
89 95
     //given
90 96
     mock_syslog *capture = new mock_syslog;
91
-    sys_syslog::delegate test_delegate(capture);
92
-    sys_syslog test_syslog(test_delegate);
93
-    logger logger = logger::create(test_syslog);
94
-    std::string user("user");
95
-    std::string token("token");
97
+    sys_syslog::delegate test_delegate (capture);
98
+    sys_syslog test_syslog (test_delegate);
99
+    logger logger = logger::create (test_syslog);
100
+    std::string user ("user");
101
+    std::string token ("token");
96 102
 
97 103
     //when
98
-    logger.log(PAM_SERVICE_ERR, user, token);
104
+    logger.log (PAM_SERVICE_ERR, user, token);
99 105
 
100 106
     //then
101
-    check(capture->facility == LOG_AUTH, "facility does not match");
102
-    check(capture->message == "pam returned error", "message does not match");
103
-    check(capture->priority == LOG_ERR, "priority does not match");
104
-    check(capture->closed, "syslog not closed");
105
-    check(capture->ident == "dual-control", "dual-control");
107
+    check (capture->facility == LOG_AUTH, "facility does not match");
108
+    check (capture->message == user + " pam returned error",
109
+           "message does not match");
110
+    check (capture->priority == LOG_ERR, "priority does not match");
111
+    check (capture->closed, "syslog not closed");
112
+    check (capture->ident == "dual-control", "dual-control");
106 113
     succeed();
107 114
 }
108 115
 
... ...
@@ -122,7 +129,6 @@ int main (int numargs, char **args)
122 129
     return !run_tests();
123 130
 }
124 131
 
125
-
126 132
 /*
127 133
 int logged_priority = -1000;
128 134
 const char *logged_message = "";
... ...
@@ -1,23 +1,35 @@
1
+/* Copyright (C) CJ Affiliate
2
+ *
3
+ * You may use, distribute and modify this code under  the
4
+ * terms of the  GNU General Public License  version 2  or
5
+ * later.
6
+ *
7
+ * You should have received a copy of the license with this
8
+ * file. If not, you will find a copy in the "LICENSE" file
9
+ * at https://github.com/cjdev/dual-control.
10
+ */
11
+
1 12
 #include <memory>
2 13
 #include <fstream>
3 14
 
4 15
 #include "sys_fstream.h"
5 16
 
6
-namespace {
7
-class impl : public fstreams_ifc {
8
-    public:
9
-        pstream open_fstream(const std::string &file_path) {
10
-            return pstream(new std::ifstream(file_path));
11
-        }
17
+namespace
18
+{
19
+class impl : public fstreams_ifc
20
+{
21
+public:
22
+    pstream open_fstream (const std::string &file_path)
23
+    {
24
+        return pstream (new std::ifstream (file_path));
25
+    }
12 26
 };
13 27
 
14
-
15 28
 }
16 29
 
17
-
18
-fstreams fstreams::create()  {
19
-    static fstreams singleton(delegate(new impl));
30
+fstreams fstreams::create()
31
+{
32
+    static fstreams singleton (delegate (new impl));
20 33
     return singleton;
21 34
 }
22 35
 
23
-
... ...
@@ -1,31 +1,45 @@
1
+/* Copyright (C) CJ Affiliate
2
+ *
3
+ * You may use, distribute and modify this code under  the
4
+ * terms of the  GNU General Public License  version 2  or
5
+ * later.
6
+ *
7
+ * You should have received a copy of the license with this
8
+ * file. If not, you will find a copy in the "LICENSE" file
9
+ * at https://github.com/cjdev/dual-control.
10
+ */
11
+
1 12
 #ifndef SYS_FSTREAM_H
2 13
 #define SYS_FSTREAM_H
3 14
 
4 15
 #include <memory>
5 16
 #include <sstream>
6 17
 
7
-class fstreams_ifc {
8
-    public:
9
-        typedef std::shared_ptr<std::istream> pstream;
10
-        virtual pstream open_fstream(const std::string &file_path) {
11
-            return pstream(new std::istringstream(""));
12
-        }
18
+class fstreams_ifc
19
+{
20
+public:
21
+    typedef std::shared_ptr<std::istream> pstream;
22
+    virtual pstream open_fstream (const std::string &file_path)
23
+    {
24
+        return pstream (new std::istringstream (""));
25
+    }
13 26
 };
14 27
 
15
-class fstreams : public fstreams_ifc {
16
-    public:
17
-        typedef std::shared_ptr<fstreams_ifc> delegate;
18
-    private:
19
-        delegate delegate_;
20
-    public:
21
-        fstreams(const delegate &delegate) : delegate_(delegate){}
22
-        fstreams() : fstreams(delegate(new fstreams_ifc)) {}
23
-        pstream open_fstream(const std::string &file_path) {
24
-            return delegate_->open_fstream(file_path);
25
-        }
26
-        static fstreams create();
28
+class fstreams : public fstreams_ifc
29
+{
30
+public:
31
+    typedef std::shared_ptr<fstreams_ifc> delegate;
32
+private:
33
+    delegate delegate_;
34
+public:
35
+    fstreams (const delegate &delegate) : delegate_ (delegate) {}
36
+    fstreams() : fstreams (delegate (new fstreams_ifc)) {}
37
+    pstream open_fstream (const std::string &file_path)
38
+    {
39
+        return delegate_->open_fstream (file_path);
40
+    }
41
+    static fstreams create();
27 42
 };
28 43
 
29
-
30 44
 #endif
31 45
 
... ...
@@ -1,26 +1,43 @@
1
+/* Copyright (C) CJ Affiliate
2
+ *
3
+ * You may use, distribute and modify this code under  the
4
+ * terms of the  GNU General Public License  version 2  or
5
+ * later.
6
+ *
7
+ * You should have received a copy of the license with this
8
+ * file. If not, you will find a copy in the "LICENSE" file
9
+ * at https://github.com/cjdev/dual-control.
10
+ */
11
+
1 12
 #include <syslog.h>
2 13
 
3 14
 #include "sys_syslog.h"
4 15
 
5
-namespace {
6
-class impl : public sys_syslog_ifc {
7
-    public:
8
-        void openlog(const char *ident, int logopt, int facility) {
9
-            ::openlog(ident, logopt, facility);
10
-        }
16
+namespace
17
+{
18
+class impl : public sys_syslog_ifc
19
+{
20
+public:
21
+    void openlog (const char *ident, int logopt, int facility)
22
+    {
23
+        ::openlog (ident, logopt, facility);
24
+    }
11 25
 
12
-        void vsyslog(int priority, const char *message, va_list args) {
13
-           ::vsyslog(priority, message, args);
14
-        }
26
+    void vsyslog (int priority, const char *message, va_list args)
27
+    {
28
+        ::vsyslog (priority, message, args);
29
+    }
15 30
 
16
-        void closelog() {
17
-            ::closelog();
18
-        }
31
+    void closelog()
32
+    {
33
+        ::closelog();
34
+    }
19 35
 };
20 36
 }
21 37
 
22
-sys_syslog sys_syslog::create() {
23
-    static sys_syslog singleton(sys_syslog::delegate(new impl));
38
+sys_syslog sys_syslog::create()
39
+{
40
+    static sys_syslog singleton (sys_syslog::delegate (new impl));
24 41
     return singleton;
25 42
 }
26 43
 
... ...
@@ -1,3 +1,14 @@
1
+/* Copyright (C) CJ Affiliate
2
+ *
3
+ * You may use, distribute and modify this code under  the
4
+ * terms of the  GNU General Public License  version 2  or
5
+ * later.
6
+ *
7
+ * You should have received a copy of the license with this
8
+ * file. If not, you will find a copy in the "LICENSE" file
9
+ * at https://github.com/cjdev/dual-control.
10
+ */
11
+
1 12
 #ifndef SYS_SYSLOG_H
2 13
 #define SYS_SYSLOG_H
3 14
 
... ...
@@ -5,38 +16,45 @@
5 16
 #include <cstdarg>
6 17
 #include <syslog.h>
7 18
 
8
-class sys_syslog_ifc {
9
-    public:
10
-        virtual void openlog(const char *ident, int logopt, int facility) {}
11
-        virtual void vsyslog(int priority, const char *message, va_list args) {}
12
-        virtual void closelog() {}
19
+class sys_syslog_ifc
20
+{
21
+public:
22
+    virtual void openlog (const char *ident, int logopt, int facility) {}
23
+    virtual void vsyslog (int priority, const char *message, va_list args) {}
24
+    virtual void closelog() {}
13 25
 };
14 26
 
15
-class sys_syslog : public sys_syslog_ifc {
16
-    public:
17
-        typedef std::shared_ptr<sys_syslog_ifc> delegate;
18
-    private:
19
-        delegate delegate_;
20
-    public:
21
-       sys_syslog(const delegate &delegate) : delegate_(delegate) {}
22
-       sys_syslog() : sys_syslog(delegate(new sys_syslog_ifc)) {}
23
-        void openlog(const char *ident, int logopt, int facility) {
24
-            delegate_->openlog(ident, logopt, facility);
25
-        }
26
-        void syslog(int priority, const char *format, ...) {
27
-            va_list vargs;
28
-            va_start(vargs, format);
29
-            vsyslog(priority, format, vargs);
30
-            va_end(vargs);
31
-        }
32
-         void vsyslog(int priority, const char *message, va_list vargs) {
33
-            delegate_->vsyslog(priority, message, vargs);
34
-        }
35
-        void closelog() {
36
-           delegate_->closelog();
37
-        }
27
+class sys_syslog : public sys_syslog_ifc
28
+{
29
+public:
30
+    typedef std::shared_ptr<sys_syslog_ifc> delegate;
31
+private:
32
+    delegate delegate_;
33
+public:
34
+    sys_syslog (const delegate &delegate) : delegate_ (delegate) {}
35
+    sys_syslog() : sys_syslog (delegate (new sys_syslog_ifc)) {}
36
+    void openlog (const char *ident, int logopt, int facility)
37
+    {
38
+        delegate_->openlog (ident, logopt, facility);
39
+    }
40
+    void syslog (int priority, const char *format, ...)
41
+    {
42
+        va_list vargs;
43
+        va_start (vargs, format);
44
+        vsyslog (priority, format, vargs);
45
+        va_end (vargs);
46
+    }
47
+    void vsyslog (int priority, const char *message, va_list vargs)
48
+    {
49
+        delegate_->vsyslog (priority, message, vargs);
50
+    }
51
+    void closelog()
52
+    {
53
+        delegate_->closelog();
54
+    }
38 55
 
39
-        static sys_syslog create();
56
+    static sys_syslog create();
40 57
 };
41 58
 
42 59
 #endif
60
+
... ...
@@ -26,20 +26,20 @@ private:
26 26
     fstreams fstreams_;
27 27
 public:
28 28
     user_token_supplier_impl (fstreams &fstreams) :
29
-        fstreams_(fstreams) {}
29
+        fstreams_ (fstreams) {}
30 30
     std::string token (user &user)
31 31
     {
32 32
         const std::string file_path (user.home_directory() + "/.dual_control");
33
-        std::vector<char> line(7);
33
+        std::vector<char> line (7);
34 34
 
35
-        fstreams::pstream stream(fstreams_.open_fstream(file_path));
35
+        fstreams::pstream stream (fstreams_.open_fstream (file_path));
36 36
 
37 37
         if (!stream->good()) {
38 38
             return "";
39 39
         }
40 40
 
41
-        stream->getline(line.data(), line.size());
42
-        return std::string(line.data());
41
+        stream->getline (line.data(), line.size());
42
+        return std::string (line.data());
43 43
     }
44 44
 };
45 45
 }
... ...
@@ -38,22 +38,25 @@ public:
38 38
     }
39 39
 };
40 40
 
41
-class fake_fstreams : public fstreams_ifc {
42
-    private:
43
-        std::string expected_file_path_;
44
-        std::string file_contents_;
45
-    public:
46
-        fake_fstreams(const std::string &expected_file_path, const std::string &file_contents)
47
-            : expected_file_path_(expected_file_path),
48
-              file_contents_(file_contents) {}
49
-        pstream open_fstream(const std::string &file_path) {
50
-            if (file_path == expected_file_path_) {
51
-                return fstreams::pstream(new std::istringstream(file_contents_));
52
-            } else {
53
-                return fstreams_ifc::open_fstream(file_path);
54
-            }
55
-
41
+class fake_fstreams : public fstreams_ifc
42
+{
43
+private:
44
+    std::string expected_file_path_;
45
+    std::string file_contents_;
46
+public:
47
+    fake_fstreams (const std::string &expected_file_path,
48
+                   const std::string &file_contents)
49
+        : expected_file_path_ (expected_file_path),
50
+          file_contents_ (file_contents) {}
51
+    pstream open_fstream (const std::string &file_path)
52
+    {
53
+        if (file_path == expected_file_path_) {
54
+            return fstreams::pstream (new std::istringstream (file_contents_));
55
+        } else {
56
+            return fstreams_ifc::open_fstream (file_path);
56 57
         }
58
+
59
+    }
57 60
 };
58 61
 
59 62
 int reads_from_the_right_file ()
... ...
@@ -62,8 +65,9 @@ int reads_from_the_right_file ()
62 65
     std::string home_directory = "/somedir";
63 66
     // hardcoded file name is .dual_control in the user's home directory
64 67
     std::string token_file = home_directory + "/.dual_control";
65
-    std::string token("123456");
66
-    fstreams test_streams(fstreams::delegate(new fake_fstreams(token_file, token)));
68
+    std::string token ("123456");
69
+    fstreams test_streams (fstreams::delegate (new fake_fstreams (token_file,
70
+                           token)));
67 71
 
68 72
     //file_reader test_file_reader (file_reader::delegate (new fake_file_reader));
69 73
     user test_user (user::delegate (new fake_user (home_directory)));
... ...
@@ -84,12 +88,12 @@ int returns_empty_string_if_file_open_fail()
84 88
     std::string home_directory = "/somedir";
85 89
     // hardcoded file name is .dual_control in the user's home directory
86 90
     std::string token_file = home_directory + "/.not_dual_control";
87
-    fstreams test_streams(fstreams::delegate(new fake_fstreams(token_file, "654321")));
91
+    fstreams test_streams (fstreams::delegate (new fake_fstreams (token_file,
92
+                           "654321")));
88 93
     user test_user (user::delegate (new fake_user (home_directory)));
89 94
     user_token_supplier supplier (user_token_supplier::create (
90 95
                                       test_streams));
91 96
 
92
-
93 97
     //when
94 98
     std::string actual = supplier.token (test_user);
95 99