1 /*
2  * SNOOPY LOGGER
3  *
4  * File: snoopy/datasource/login.c
5  *
6  * Copyright (c) 2014-2015 Bostjan Skufca <bostjan@a2o.si>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22 
23 
24 
25 /*
26  * Includes order: from local to global
27  */
28 #include "login.h"
29 
30 #include "snoopy.h"
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 
37 
38 /*
39  * Local defines
40  */
41 #define   SNOOPY_DATASOURCE_LOGIN_loginSizeMaxWithoutNull   254
42 #define   SNOOPY_DATASOURCE_LOGIN_loginSizeMaxWithNull      255
43 
44 
45 
46 /*
47  * SNOOPY DATA SOURCE: login
48  *
49  * Description:
50  *     Returns literal login name of logged-in user executing this process
51  *
52  * Params:
53  *     result: pointer to string, to write result into
54  *     arg:    (ignored)
55  *
56  * Return:
57  *     number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE
58  */
snoopy_datasource_login(char * const result,char const * const arg)59 int snoopy_datasource_login (char * const result, char const * const arg)
60 {
61     int          loginSizeMaxWithoutNull = SNOOPY_DATASOURCE_LOGIN_loginSizeMaxWithoutNull;
62     int          loginSizeMaxWithNull    = SNOOPY_DATASOURCE_LOGIN_loginSizeMaxWithNull;
63     static char  login[SNOOPY_DATASOURCE_LOGIN_loginSizeMaxWithNull];
64     const char * loginptr = NULL;
65 
66     /*
67      * Retrive the user login trying in order:
68      * - The login information from the processus.
69      * - the SUDO_USER environment variable.
70      * - the LOGNAME environment variable.
71      *
72      * TIP to use with sudo and keep LOGNAME, add this in /etc/sudoers:
73      * Defaults        env_reset
74      * Defaults        env_keep="LOGNAME"
75     */
76     if (0 != getlogin_r(login, loginSizeMaxWithNull)) {
77         loginptr = getenv("SUDO_USER");
78         if (!loginptr) {
79             loginptr = getenv("LOGNAME");
80         }
81         if (!loginptr) {
82             strcpy(login, "(unknown)");
83         } else {
84             strncpy(login, loginptr, loginSizeMaxWithoutNull);   // Coverity suggests using -1 size here
85             if ((int)strlen(loginptr) > loginSizeMaxWithoutNull) {
86                 login[loginSizeMaxWithoutNull] = '\0';
87             }
88         }
89     }
90     return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", login);
91 }
92