~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to chirp/src/chirp_group.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2008- The University of Notre Dame
 
3
This software is distributed under the GNU General Public License.
 
4
See the file COPYING for details.
 
5
*/
 
6
 
 
7
#include "chirp_group.h"
 
8
#include "chirp_types.h"
 
9
 
 
10
#include "debug.h"
 
11
#include "stringtools.h"
 
12
 
 
13
#include <stdlib.h>
 
14
#include <unistd.h>
 
15
#include <stdio.h>
 
16
#include <string.h>
 
17
#include <errno.h>
 
18
#include <sys/stat.h>
 
19
 
 
20
extern const char *chirp_transient_path;
 
21
extern const char *chirp_group_base_url;
 
22
extern int chirp_group_cache_time;
 
23
 
 
24
/*
 
25
Search for a given subject name in a group.
 
26
Return true if the member is found, false otherwise.
 
27
Works by downloading group files from a web server,
 
28
which are then cached for a configurable time, 15 minutes by default.
 
29
*/
 
30
 
 
31
int chirp_group_lookup( const char *group, const char *subject )
 
32
{
 
33
        char url[CHIRP_PATH_MAX];
 
34
        char cachedir[CHIRP_PATH_MAX];
 
35
        char cachepath[CHIRP_PATH_MAX];
 
36
        char line[CHIRP_PATH_MAX];
 
37
        struct stat info;
 
38
 
 
39
        if(!chirp_group_base_url) return 0;
 
40
 
 
41
        int fetch_group = 1;
 
42
 
 
43
        sprintf(cachedir,"%s/.__groups",chirp_transient_path);
 
44
        sprintf(cachepath,"%s/%s",cachedir,&group[6]);
 
45
 
 
46
        if(stat(cachepath,&info)==0) {
 
47
                int age = time(0) - info.st_mtime;
 
48
                if(age<chirp_group_cache_time) {
 
49
                        fetch_group = 0;
 
50
                }
 
51
        }
 
52
 
 
53
        if(fetch_group) {
 
54
                sprintf(url,"%s%s",chirp_group_base_url,&group[6]);
 
55
                debug(D_DEBUG,"fetching group %s from %s",group,url);
 
56
                mkdir(cachedir,0777);
 
57
                sprintf(line,"wget --no-check-certificate -q %s -O %s",url,cachepath);
 
58
                if(system(line)!=0) {
 
59
                        debug(D_NOTICE,"failed to fetch group using: %s",line);
 
60
                        unlink(cachepath);
 
61
                        return 0;
 
62
                }
 
63
        }
 
64
 
 
65
        FILE *file = fopen(cachepath,"r");
 
66
        if(!file) return 0;
 
67
 
 
68
        while(fgets(line,sizeof(line),file)) {
 
69
                string_chomp(line);
 
70
 
 
71
                // If it matches exactly, return.
 
72
                if(!strcmp(line,subject)) {
 
73
                        fclose(file);
 
74
                        return 1;
 
75
                }
 
76
 
 
77
                // If the group entry does not have an auth method,
 
78
                // and the subject is unix, look for equivalence after the colon.
 
79
 
 
80
                if(!strchr(line,':') && !strncmp(subject,"unix:",5)) {
 
81
                        if(!strcmp(line,&subject[5])) {
 
82
                                fclose(file);
 
83
                                return 1;
 
84
                        }
 
85
                }
 
86
 
 
87
        }
 
88
 
 
89
        fclose(file);
 
90
        return 0;
 
91
}