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

« back to all changes in this revision

Viewing changes to dttools/src/auth_test.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) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
#include "auth.h"
 
9
#include "link.h"
 
10
#include "getopt.h"
 
11
#include "debug.h"
 
12
#include "domain_name_cache.h"
 
13
#include "auth_all.h"
 
14
#include "stringtools.h"
 
15
 
 
16
#include <stdio.h>
 
17
#include <stdlib.h>
 
18
#include <string.h>
 
19
#include <errno.h>
 
20
 
 
21
static void show_use( const char *cmd ) 
 
22
{
 
23
        fprintf(stderr,"Use: %s [options]\n",cmd);
 
24
        fprintf(stderr,"Where options are:\n");
 
25
        fprintf(stderr," -a <type> Allow this auth type\n");
 
26
        fprintf(stderr," -p <num>  Port number\n");
 
27
        fprintf(stderr," -r <host> Remote host\n");
 
28
        fprintf(stderr," -d <flag> Debugging\n");
 
29
        fprintf(stderr,"Where debug flags arg: ");
 
30
        debug_flags_print(stderr);
 
31
        fprintf(stderr,"\n");
 
32
}
 
33
 
 
34
 
 
35
int main( int argc, char *argv[] )
 
36
{
 
37
        struct link *link, *master;
 
38
        char *subject=0, *type=0;
 
39
        time_t stoptime;
 
40
        char line[1024];
 
41
        char c;
 
42
        int portnum=30000;
 
43
        char *hostname=0;
 
44
        int timeout=30;
 
45
 
 
46
        debug_config(argv[0]);
 
47
 
 
48
        while((c=getopt(argc,argv,"a:p:r:d:o:O:"))!=(char)-1) {
 
49
                switch(c) {
 
50
                        case 'p':
 
51
                                portnum = atoi(optarg);
 
52
                                break;
 
53
                        case 'r':
 
54
                                hostname = optarg;
 
55
                                break;
 
56
                        case 'd':
 
57
                                debug_flags_set(optarg);
 
58
                                break;
 
59
                        case 'o':
 
60
                                debug_config_file(optarg);
 
61
                                break;
 
62
                        case 'O':
 
63
                                debug_config_file_size(string_metric_parse(optarg));
 
64
                                break;
 
65
                        case 'a':
 
66
                                if(!auth_register_byname(optarg)) fatal("couldn't register %s authentication",optarg);
 
67
                                break;
 
68
                        default:
 
69
                                show_use(argv[0]);
 
70
                                exit(1);
 
71
                }
 
72
        }
 
73
 
 
74
        if(hostname) {
 
75
                char addr[LINK_ADDRESS_MAX];
 
76
 
 
77
                stoptime = time(0)+timeout;
 
78
 
 
79
                if(!domain_name_cache_lookup(hostname,addr)) fatal("unknown host name: %s",hostname);
 
80
 
 
81
                link = link_connect(addr,portnum,stoptime);
 
82
                if(!link) fatal("couldn't connect to %s:%d: %s",hostname,portnum,strerror(errno));
 
83
 
 
84
                if(auth_assert(link,&type,&subject,stoptime)) {
 
85
                        printf("server thinks I am %s %s\n",type,subject);
 
86
                        if(link_readline(link,line,sizeof(line),stoptime)) {
 
87
                                printf("got message: %s\n",line);
 
88
                        } else {
 
89
                                printf("lost connection!\n");
 
90
                        }
 
91
                } else {
 
92
                        printf("unable to authenticate.\n");
 
93
                }
 
94
 
 
95
                link_close(link);
 
96
 
 
97
        } else {
 
98
                stoptime = time(0)+timeout;
 
99
 
 
100
                master = link_serve(portnum);
 
101
                if(!master) fatal("couldn't serve port %d: %s\n",portnum,strerror(errno));
 
102
 
 
103
                while(time(0)<stoptime) {
 
104
                        link = link_accept(master,stoptime);
 
105
                        if(!link) continue;
 
106
 
 
107
                        if(auth_accept(link,&type,&subject,stoptime)) {
 
108
                                time_t t = time(0);
 
109
                                link_putfstring(link,"Hello %s:%s, it is now %s",stoptime,type,subject,ctime(&t)); /* ctime ends with newline */
 
110
                        } else {
 
111
                                printf("couldn't auth accept\n");
 
112
                        } 
 
113
                        link_close(link);
 
114
                }
 
115
        }
 
116
 
 
117
        return 0;
 
118
}
 
119
 
 
120