summaryrefslogtreecommitdiffstats
path: root/modules-available/statistics_reporting/inc/remotereport.inc.php
blob: fa84e7e56faf70017b863c019f3fb010d6357091 (plain) (blame)
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
<?php

class RemoteReport
{

	const ENABLED_ID = 'statistics-reporting-enabled';
	const NEXT_SUBMIT_ID = 'statistics-reporting-next';

	/**
	 * Enable or disable remote reporting of usage statistics.
	 *
	 * @param bool|string $isEnabled true or 'on' if reporting should be enabled
	 */
	public static function setReportingEnabled($isEnabled)
	{
		$value = ($isEnabled === true || $isEnabled === 'on') ? 'on' : 'off';
		Property::set(self::ENABLED_ID, $value);
	}

	/**
	 * Returns whether remote reporting is enabled or not.
	 * Defaults to on.
	 *
	 * @return bool true if reporting is on, false if off
	 */
	public static function isReportingEnabled()
	{
		return Property::get(self::ENABLED_ID, 'on') === 'on';
	}

	/**
	 * Get the timestamp of the end of the next 7 day interval to
	 * report statistics for. Usually if this is < time() you want
	 * to generate the report.
	 *
	 * @return int timestamp of the end of the reporting time frame
	 */
	public static function getReportingTimestamp()
	{
		$ts = Property::get(self::NEXT_SUBMIT_ID, 0);
		if ($ts === 0) {
			// No timestamp stored yet - might be a fresh install
			// schedule for next time
			self::writeNextReportingTimestamp();
			$ts = Property::get(self::NEXT_SUBMIT_ID, 0);
		} elseif ($ts < strtotime('last monday - 14 days')) {
			// Too long ago, move forward
			$ts = strtotime('last monday - 14 days');
		}
		return $ts;
	}

	/**
	 * Update the timestamp of the next scheduled statistics report.
	 * This sets the end of the next 7 day interval to the start of
	 * next monday (00:00).
	 */
	public static function writeNextReportingTimestamp()
	{
		Property::set(self::NEXT_SUBMIT_ID, strtotime('next monday'), 60 * 24 * 14);
	}

	/**
	 * Generate the multi-dimensional array containing the anonymized
	 * (weekly) statistics to report.
	 *
	 * @param int $to end timestamp
	 * @param int[] $days list of days to generate aggregated stats for
	 * @return array wrapped up statistics, ready for reporting
	 */
	public static function generateReport($to, $days = false) {
		if ($days === false) {
			$days = [7, 30, 90];
		}
		GetData::$salt = bin2hex(Util::randomBytes(20, false));
		GetData::$lowerTimeBound = 7;
		GetData::$upperTimeBound = 20;
		$result = array();
		foreach ($days as $day) {
			if (isset($result['days' . $day]))
				continue;
			$from = strtotime("-{$day} days", $to);
			GetData::$from = $from;
			GetData::$to = $to;
			$data = array('total' => GetData::total(GETDATA_ANONYMOUS));
			$data['perLocation'] = array_values(GetData::perLocation(GETDATA_ANONYMOUS));
			$data['perVM'] = GetData::perVM(GETDATA_ANONYMOUS);
			$data['tsFrom'] = $from;
			$data['tsTo'] = $to;
			$data['dozmod'] = Queries::getDozmodStats($from, $to);
			$data['machines'] = Queries::getAggregatedMachineStats($from);
			$result['days' . $day] = $data;
		}
		$result['server'] = self::getLocalHardware();
		$result['version'] = CONFIG_FOOTER;
		return $result;
	}

	private static function getLocalHardware()
	{
		$cpuInfo = file_get_contents('/proc/cpuinfo');
		$uptime = file_get_contents('/proc/uptime');
		$memInfo = file_get_contents('/proc/meminfo');
		preg_match_all('/\b(\w+):\s+(\d+)\s/s', $memInfo, $out, PREG_SET_ORDER);
		$mem = array();
		foreach ($out as $e) {
			$mem[$e[1]] = $e[2];
		}
		//
		$data = array();
		$data['cpuCount'] = preg_match_all('/\bprocessor\s+:\s+(.*)$/m', $cpuInfo, $out);
		if ($data['cpuCount'] > 0) {
			$data['cpuModel'] = $out[1][0];
		}
		if (preg_match('/^(\d+)\D/', $uptime, $out)) {
			$data['uptime'] = $out[1];
		}
		if (isset($mem['MemTotal']) && isset($mem['MemFree']) && isset($mem['SwapTotal'])) {
			$data['memTotal'] = $mem['MemTotal'];
			$data['memFree'] = ($mem['MemFree'] + $mem['Buffers'] + $mem['Cached']);
			$data['swapTotal'] = $mem['SwapTotal'];
			$data['swapUsed'] = ($mem['SwapTotal'] - $mem['SwapFree']);
		}
		return $data;
	}

}
5e3348a01e6e6e855a'>86be6a32d ^
e40f84b9d ^
e6ebab66d ^
46ae163a8 ^
cdf0c3259 ^
46ae163a8 ^





cdf0c3259 ^
46ae163a8 ^
cdf0c3259 ^

46ae163a8 ^
cdf0c3259 ^

46ae163a8 ^
cdf0c3259 ^
46ae163a8 ^

cdf0c3259 ^
a3030b8ad ^

46ae163a8 ^
cdf0c3259 ^
74ce680a3 ^

cdf0c3259 ^



e6ebab66d ^
6dbe3af94
46ae163a8 ^
e6ebab66d ^
42dea85c5 ^
0720d60cc ^
e6ebab66d ^
e6ebab66d ^


f7bac5731 ^
e6ebab66d ^








2c308875a ^
e6ebab66d ^
f7bac5731 ^
e6ebab66d ^

46ae163a8 ^
e6ebab66d ^

dd8dac191 ^

46ae163a8 ^
e6ebab66d ^
f7bac5731 ^



2c308875a ^
e6ebab66d ^
2c308875a ^
e6ebab66d ^
86be6a32d ^
e6ebab66d ^
677ec86ce ^
e6ebab66d ^
46ae163a8 ^
e6ebab66d ^
f7bac5731 ^
09af3db48 ^
f7bac5731 ^
856d856a9 ^
46ae163a8 ^

e6ebab66d ^
0720d60cc ^
42dea85c5 ^
46ae163a8 ^
ada74f3f3 ^

09af3db48 ^
e6ebab66d ^
42dea85c5 ^

e6ebab66d ^


e6ebab66d ^
6dbe3af94
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

                                                        

                                                            

                                                  






                                                                        
  
                                                       


                                                                 

   


                
                        
                      
                     
                   
                   
 


                   

                   

                     
 


                          
  
 
                        
                                  

                        
                       



                               
                                       
                                                              
 
                                      
                               
 











                                                       
                                                



                                             
                                        
                     

 
                                                     
 
                           

                                                                          
 


                                                             

                                                                            
                                                                                         


                                                                             

                                             
 
                           
 
 
                                                              
 





                                                  
 
                                                   

                                          
                                                   

                             
                                                         
                        

                                                   
                                               

                                                                             
                                                      
 

                                                                                 



                 
                               
 
                                                      
                 
                                           
                                      
              


                                                       
                                                           








                                                    
                              
 
                                                                                

                            
                                        

                              

                                                                           
                                                         
                              



                                                                                  
 
                         
                                                    
                         
                                
                        
                                                 
                 
         
 
                                         
                                                                        
 
                             

                                    
 
                                           
                                                
                        

                                                                         
                                                                  
 

                                          


                                          
                            
 
/* mcookie.c -- Generates random numbers for xauth
 * Created: Fri Feb  3 10:42:48 1995 by faith@cs.unc.edu
 * Revised: Fri Mar 19 07:48:01 1999 by faith@acm.org
 * Public Domain 1995, 1999 Rickard E. Faith (faith@acm.org)
 * This program comes with ABSOLUTELY NO WARRANTY.
 * 
 * This program gathers some random bits of data and used the MD5
 * message-digest algorithm to generate a 128-bit hexadecimal number for
 * use with xauth(1).
 *
 * NOTE: Unless /dev/random is available, this program does not actually
 * gather 128 bits of random information, so the magic cookie generated
 * will be considerably easier to guess than one might expect.
 *
 * 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
 * - added Native Language Support
 * 1999-03-21 aeb: Added some fragments of code from Colin Plumb.
 *
 */

#include "c.h"
#include "md5.h"
#include "nls.h"
#include "closestream.h"
#include "randutils.h"
#include "strutils.h"
#include "xalloc.h"
#include "all-io.h"

#include <fcntl.h>
#include <getopt.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

enum {
	BUFFERSIZE = 4096,
	RAND_BYTES = 128
};

struct mcookie_control {
	struct	UL_MD5Context ctx;
	char	**files;
	size_t	nfiles;
	uint64_t maxsz;

	unsigned int verbose:1;
};

/* The basic function to hash a file */
static uint64_t hash_file(struct mcookie_control *ctl, int fd)
{
	unsigned char buf[BUFFERSIZE];
	uint64_t wanted, count;

	wanted = ctl->maxsz ? ctl->maxsz : sizeof(buf);

	for (count = 0; count < wanted; ) {
		size_t rdsz = sizeof(buf);
		ssize_t r;

		if (wanted - count < rdsz)
			rdsz = wanted - count;

		r = read_all(fd, (char *) buf, rdsz);
		if (r < 0)
			break;
		ul_MD5Update(&ctl->ctx, buf, r);
		count += r;
	}
	/* Separate files with a null byte */
	buf[0] = '\0';
	ul_MD5Update(&ctl->ctx, buf, 1);
	return count;
}

static void __attribute__((__noreturn__)) usage(void)
{
	FILE *out = stdout;
	fputs(USAGE_HEADER, out);
	fprintf(out, _(" %s [options]\n"), program_invocation_short_name);

	fputs(USAGE_SEPARATOR, out);
	fputs(_("Generate magic cookies for xauth.\n"), out);

	fputs(USAGE_OPTIONS, out);
	fputs(_(" -f, --file <file>     use file as a cookie seed\n"), out);
	fputs(_(" -m, --max-size <num>  limit how much is read from seed files\n"), out);
	fputs(_(" -v, --verbose         explain what is being done\n"), out);

	fputs(USAGE_SEPARATOR, out);
	printf(USAGE_HELP_OPTIONS(23));
	printf(USAGE_MAN_TAIL("mcookie(1)"));

	exit(EXIT_SUCCESS);
}

static void randomness_from_files(struct mcookie_control *ctl)
{
	size_t i;

	for (i = 0; i < ctl->nfiles; i++) {
		const char *fname = ctl->files[i];
		size_t count;
		int fd;

		if (*fname == '-' && !*(fname + 1))
			fd = STDIN_FILENO;
		else
			fd = open(fname, O_RDONLY);

		if (fd < 0) {
			warn(_("cannot open %s"), fname);
		} else {
			count = hash_file(ctl, fd);
			if (ctl->verbose)
				fprintf(stderr,
					P_("Got %zu byte from %s\n",
					   "Got %zu bytes from %s\n", count),
					count, fname);

			if (fd != STDIN_FILENO && close(fd))
				err(EXIT_FAILURE, _("closing %s failed"), fname);
		}
	}
}

int main(int argc, char **argv)
{
	struct mcookie_control ctl = { .verbose = 0 };
	size_t i;
	unsigned char digest[UL_MD5LENGTH];
	unsigned char buf[RAND_BYTES];
	int c;

	static const struct option longopts[] = {
		{"file", required_argument, NULL, 'f'},
		{"max-size", required_argument, NULL, 'm'},
		{"verbose", no_argument, NULL, 'v'},
		{"version", no_argument, NULL, 'V'},
		{"help", no_argument, NULL, 'h'},
		{NULL, 0, NULL, 0}
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	close_stdout_atexit();

	while ((c = getopt_long(argc, argv, "f:m:vVh", longopts, NULL)) != -1) {
		switch (c) {
		case 'v':
			ctl.verbose = 1;
			break;
		case 'f':
			if (!ctl.files)
				ctl.files = xmalloc(sizeof(char *) * argc);
			ctl.files[ctl.nfiles++] = optarg;
			break;
		case 'm':
			ctl.maxsz = strtosize_or_err(optarg,
						     _("failed to parse length"));
			break;

		case 'V':
			print_version(EXIT_SUCCESS);
		case 'h':
			usage();
		default:
			errtryhelp(EXIT_FAILURE);
		}
	}

	if (ctl.maxsz && ctl.nfiles == 0)
		warnx(_("--max-size ignored when used without --file"));

	ul_MD5Init(&ctl.ctx);
	randomness_from_files(&ctl);
	free(ctl.files);

	random_get_bytes(&buf, RAND_BYTES);
	ul_MD5Update(&ctl.ctx, buf, RAND_BYTES);
	if (ctl.verbose)
		fprintf(stderr, P_("Got %d byte from %s\n",
				   "Got %d bytes from %s\n", RAND_BYTES),
				RAND_BYTES, random_tell_source());

	ul_MD5Final(digest, &ctl.ctx);
	for (i = 0; i < UL_MD5LENGTH; i++)
		printf("%02x", digest[i]);
	putchar('\n');

	return EXIT_SUCCESS;
}