[go: up one dir, main page]

File: chirp_group.c

package info (click to toggle)
cctools 9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,624 kB
  • sloc: ansic: 192,539; python: 20,827; cpp: 20,199; sh: 11,719; perl: 4,106; xml: 3,688; makefile: 1,224
file content (99 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download | duplicates (2)
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
/*
Copyright (C) 2008- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/

#include "chirp_group.h"
#include "chirp_types.h"

#include "debug.h"
#include "stringtools.h"

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>

#ifndef PATH_MAX // Hurd
#define PATH_MAX 1024
#endif

extern char chirp_transient_path[PATH_MAX];

char chirp_group_base_url[PATH_MAX];
int  chirp_group_cache_time = 900;

/*
Search for a given subject name in a group.
Return true if the member is found, false otherwise.
Works by downloading group files from a web server,
which are then cached for a configurable time, 15 minutes by default.
*/

int chirp_group_lookup(const char *group, const char *subject)
{
	char url[CHIRP_PATH_MAX];
	char cachedir[CHIRP_PATH_MAX];
	char cachepath[CHIRP_PATH_MAX];
	char line[CHIRP_PATH_MAX];
	struct stat info;

	if(chirp_group_base_url[0] == '\0')
		return 0;

	int fetch_group = 1;

	string_nformat(cachedir,  sizeof(cachedir),  "%s/.__groups", chirp_transient_path);
	string_nformat(cachepath, sizeof(cachepath), "%s/%s", cachedir, &group[6]);

	if(stat(cachepath, &info) == 0) {
		int age = time(0) - info.st_mtime;
		if(age < chirp_group_cache_time) {
			fetch_group = 0;
		}
	}

	if(fetch_group) {
		string_nformat(url, sizeof(url), "%s/%s", chirp_group_base_url, &group[6]);
		debug(D_DEBUG, "fetching group %s from %s", group, url);
		mkdir(cachedir, 0777);
		string_nformat(line, sizeof(line), "curl --silent --insecure --output '%s' %s", cachepath, url);
		if(system(line) != 0) {
			debug(D_NOTICE, "failed to fetch group using: %s", line);
			unlink(cachepath);
			return 0;
		}
	}

	FILE *file = fopen(cachepath, "r");
	if(!file)
		return 0;

	while(fgets(line, sizeof(line), file)) {
		string_chomp(line);

		// If it matches exactly, return.
		if(!strcmp(line, subject)) {
			fclose(file);
			return 1;
		}
		// If the group entry does not have an auth method,
		// and the subject is unix, look for equivalence after the colon.

		if(!strchr(line, ':') && !strncmp(subject, "unix:", 5)) {
			if(!strcmp(line, &subject[5])) {
				fclose(file);
				return 1;
			}
		}

	}

	fclose(file);
	return 0;
}

/* vim: set noexpandtab tabstop=4: */