To provide an easy way to crete a ticket_t from
a file, one can create a wicket_t and fetch ticket_t's for different users to set on the authority. { wicket_t (&wicket, "/home/alain/.tickets"); wicket_get_ticket (wicket, &ticket, "alain", NULL); authority_set_ticket (wicket, ticket); } * include/mailutils/auth.h: Added wicket_t prototypes functions: wicket_create(), wicket_destroy, wicket_get_ticket(). New prototype ticket_set_data () ticket_get_data () ticket_set_destroy (), ticket_set_owner(); * mailbox/wicket.c: New file, implements wicket_t. * mailbox/ticket.c: New file, implements ticket_t. * mailbox/auth.c: Implements only authority_t. * mailbox/Makefile.am: Update for ticket.c and wicket.c
Showing
7 changed files
with
479 additions
and
134 deletions
1 | 2001-11-14 Alain Magloire | ||
2 | |||
3 | To provide an easy way to crete a ticket_t from | ||
4 | a file, one can create a wicket_t and fetch ticket_t's | ||
5 | for different users to set on the authority. | ||
6 | { | ||
7 | wicket_t (&wicket, "/home/alain/.tickets"); | ||
8 | wicket_get_ticket (wicket, &ticket, "alain", NULL); | ||
9 | authority_set_ticket (wicket, ticket); | ||
10 | } | ||
11 | |||
12 | * include/mailutils/auth.h: Added wicket_t prototypes | ||
13 | functions: wicket_create(), wicket_destroy, | ||
14 | wicket_get_ticket(). New prototype ticket_set_data () | ||
15 | ticket_get_data () ticket_set_destroy (), ticket_set_owner(); | ||
16 | * mailbox/wicket.c: New file, implements wicket_t. | ||
17 | * mailbox/ticket.c: New file, implements ticket_t. | ||
18 | * mailbox/auth.c: Implements only authority_t. | ||
19 | * mailbox/Makefile.am: Update for ticket.c and wicket.c | ||
20 | |||
1 | 2001-11-14 Sergey Poznyakoff | 21 | 2001-11-14 Sergey Poznyakoff |
2 | 22 | ||
3 | * comsat/action.c: Expand \n only for `echo'. | 23 | * comsat/action.c: Expand \n only for `echo'. | ... | ... |
1 | /* GNU mailutils - a suite of utilities for electronic mail | 1 | /* GNU mailutils - a suite of utilities for electronic mail |
2 | Copyright (C) 1999, 2000 Free Software Foundation, Inc. | 2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. |
3 | 3 | ||
4 | This program is free software; you can redistribute it and/or modify | 4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Library Public License as published by | 5 | it under the terms of the GNU General Library Public License as published by |
... | @@ -38,13 +38,14 @@ typedef struct _ticket *ticket_t; | ... | @@ -38,13 +38,14 @@ typedef struct _ticket *ticket_t; |
38 | 38 | ||
39 | extern int ticket_create __P ((ticket_t *, void *owner)); | 39 | extern int ticket_create __P ((ticket_t *, void *owner)); |
40 | extern void ticket_destroy __P ((ticket_t *, void *owner)); | 40 | extern void ticket_destroy __P ((ticket_t *, void *owner)); |
41 | extern void * ticket_get_owner __P ((ticket_t)); | 41 | extern int ticket_set_destroy __P ((ticket_t, void (*) |
42 | __P ((ticket_t)), void *owner)); | ||
43 | extern void *ticket_get_owner __P ((ticket_t)); | ||
42 | 44 | ||
43 | extern int ticket_set_pop __P ((ticket_t, int (*_pop) __P ((ticket_t, const char *, char **)), void *)); | 45 | extern int ticket_set_pop __P ((ticket_t, int (*_pop) __P ((ticket_t, const char *, char **)), void *)); |
44 | extern int ticket_pop __P ((ticket_t, const char *, char **)); | 46 | extern int ticket_pop __P ((ticket_t, const char *, char **)); |
45 | 47 | extern int ticket_set_data __P ((ticket_t, void *, void *owner)); | |
46 | extern int ticket_get_type __P ((ticket_t, char *, size_t, size_t *)); | 48 | extern int ticket_get_data __P ((ticket_t, void **)); |
47 | extern int ticket_set_type __P ((ticket_t, char *)); | ||
48 | 49 | ||
49 | struct _authority; | 50 | struct _authority; |
50 | typedef struct _authority *authority_t; | 51 | typedef struct _authority *authority_t; |
... | @@ -57,6 +58,18 @@ extern int authority_get_ticket __P ((authority_t, ticket_t *)); | ... | @@ -57,6 +58,18 @@ extern int authority_get_ticket __P ((authority_t, ticket_t *)); |
57 | extern int authority_authenticate __P ((authority_t)); | 58 | extern int authority_authenticate __P ((authority_t)); |
58 | extern int authority_set_authenticate __P ((authority_t, int (*_authenticate) __P ((authority_t)), void *)); | 59 | extern int authority_set_authenticate __P ((authority_t, int (*_authenticate) __P ((authority_t)), void *)); |
59 | 60 | ||
61 | struct _wicket; | ||
62 | typedef struct _wicket *wicket_t; | ||
63 | |||
64 | extern int wicket_create __P ((wicket_t *, const char *)); | ||
65 | extern int wicket_destroy __P ((wicket_t *)); | ||
66 | extern int wicket_set_filename __P ((wicket_t, const char *)); | ||
67 | extern int wicket_get_filename __P ((wicket_t, char *, size_t, size_t *)); | ||
68 | extern int wicket_set_ticket __P ((wicket_t, int (*) | ||
69 | __P ((wicket_t, const char *, | ||
70 | const char *, ticket_t *)))); | ||
71 | extern int wicket_get_ticket __P ((wicket_t, ticket_t *, const char *, const char *)); | ||
72 | |||
60 | #ifdef __cplusplus | 73 | #ifdef __cplusplus |
61 | } | 74 | } |
62 | #endif | 75 | #endif | ... | ... |
... | @@ -56,6 +56,7 @@ sendmail.c \ | ... | @@ -56,6 +56,7 @@ sendmail.c \ |
56 | smtp.c \ | 56 | smtp.c \ |
57 | stream.c \ | 57 | stream.c \ |
58 | tcp.c \ | 58 | tcp.c \ |
59 | ticket.c \ | ||
59 | url.c \ | 60 | url.c \ |
60 | url_file.c \ | 61 | url_file.c \ |
61 | url_imap.c \ | 62 | url_imap.c \ |
... | @@ -64,6 +65,7 @@ url_mh.c \ | ... | @@ -64,6 +65,7 @@ url_mh.c \ |
64 | url_path.c \ | 65 | url_path.c \ |
65 | url_pop.c \ | 66 | url_pop.c \ |
66 | url_sendmail.c \ | 67 | url_sendmail.c \ |
67 | url_smtp.c | 68 | url_smtp.c \ |
69 | wicket.c | ||
68 | 70 | ||
69 | libmailbox_la_LDFLAGS = -version-info 0:0:0 | 71 | libmailbox_la_LDFLAGS = -version-info 0:0:0 | ... | ... |
... | @@ -22,134 +22,9 @@ | ... | @@ -22,134 +22,9 @@ |
22 | #include <sys/types.h> | 22 | #include <sys/types.h> |
23 | #include <string.h> | 23 | #include <string.h> |
24 | #include <stdlib.h> | 24 | #include <stdlib.h> |
25 | #include <stdio.h> | ||
26 | #include <termios.h> | ||
27 | 25 | ||
28 | #include <mailutils/mailbox.h> | ||
29 | #include <mailutils/mutil.h> | ||
30 | #include <auth0.h> | 26 | #include <auth0.h> |
31 | 27 | ||
32 | static void | ||
33 | echo_off(struct termios *stored_settings) | ||
34 | { | ||
35 | struct termios new_settings; | ||
36 | tcgetattr (0, stored_settings); | ||
37 | new_settings = *stored_settings; | ||
38 | new_settings.c_lflag &= (~ECHO); | ||
39 | tcsetattr (0, TCSANOW, &new_settings); | ||
40 | } | ||
41 | |||
42 | static void | ||
43 | echo_on(struct termios *stored_settings) | ||
44 | { | ||
45 | tcsetattr (0, TCSANOW, stored_settings); | ||
46 | } | ||
47 | |||
48 | int | ||
49 | ticket_create (ticket_t *pticket, void *owner) | ||
50 | { | ||
51 | ticket_t ticket; | ||
52 | if (pticket == NULL) | ||
53 | return EINVAL; | ||
54 | ticket = calloc (1, sizeof (*ticket)); | ||
55 | if (ticket == NULL) | ||
56 | return ENOMEM; | ||
57 | ticket->owner = owner; | ||
58 | *pticket = ticket; | ||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | void | ||
63 | ticket_destroy (ticket_t *pticket, void *owner) | ||
64 | { | ||
65 | if (pticket && *pticket) | ||
66 | { | ||
67 | ticket_t ticket = *pticket; | ||
68 | if (ticket->owner == owner) | ||
69 | { | ||
70 | if (ticket->type) | ||
71 | free (ticket->type); | ||
72 | free (ticket); | ||
73 | } | ||
74 | *pticket = NULL; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | void * | ||
79 | ticket_get_owner (ticket_t ticket) | ||
80 | { | ||
81 | return (ticket) ? ticket->owner : NULL; | ||
82 | } | ||
83 | |||
84 | int | ||
85 | ticket_set_pop (ticket_t ticket, | ||
86 | int (*_pop) __P ((ticket_t, const char *, char **)), | ||
87 | void *owner) | ||
88 | { | ||
89 | if (ticket == NULL) | ||
90 | return EINVAL; | ||
91 | if (ticket->owner != owner) | ||
92 | return EACCES; | ||
93 | ticket->_pop = _pop; | ||
94 | return 0; | ||
95 | } | ||
96 | |||
97 | int | ||
98 | ticket_pop (ticket_t ticket, const char *challenge, char **parg) | ||
99 | { | ||
100 | if (ticket == NULL || parg == NULL) | ||
101 | return EINVAL; | ||
102 | if (ticket->_pop) | ||
103 | return ticket->_pop (ticket, challenge, parg); | ||
104 | else | ||
105 | { | ||
106 | char arg[256]; | ||
107 | struct termios stored_settings; | ||
108 | int echo = 1; | ||
109 | |||
110 | /* Being smart if we see "Passwd" and turning off echo. */ | ||
111 | if (strstr (challenge, "ass") != NULL | ||
112 | || strstr (challenge, "ASS") != NULL) | ||
113 | echo = 0; | ||
114 | printf ("%s", challenge); | ||
115 | fflush (stdout); | ||
116 | if (!echo) | ||
117 | echo_off (&stored_settings); | ||
118 | fgets (arg, sizeof (arg), stdin); | ||
119 | if (!echo) | ||
120 | { | ||
121 | echo_on (&stored_settings); | ||
122 | putchar ('\n'); | ||
123 | fflush (stdout); | ||
124 | } | ||
125 | arg [strlen (arg) - 1] = '\0'; /* nuke the trailing line. */ | ||
126 | *parg = strdup (arg); | ||
127 | } | ||
128 | return 0; | ||
129 | } | ||
130 | |||
131 | int | ||
132 | ticket_get_type (ticket_t ticket, char *type, size_t len, size_t *pwriten) | ||
133 | { | ||
134 | size_t n; | ||
135 | if (ticket == NULL || type == NULL) | ||
136 | return EINVAL; | ||
137 | n = util_cpystr (type, ticket->type, len); | ||
138 | if (pwriten) | ||
139 | *pwriten = n; | ||
140 | return 0; | ||
141 | } | ||
142 | |||
143 | int | ||
144 | ticket_set_type (ticket_t ticket, char *type) | ||
145 | { | ||
146 | if (ticket == NULL) | ||
147 | return EINVAL; | ||
148 | if (ticket->type) | ||
149 | free (ticket->type); | ||
150 | ticket->type = strdup ((type) ? type : ""); | ||
151 | return 0; | ||
152 | } | ||
153 | 28 | ||
154 | int | 29 | int |
155 | authority_create (authority_t *pauthority, ticket_t ticket, void *owner) | 30 | authority_create (authority_t *pauthority, ticket_t ticket, void *owner) |
... | @@ -213,7 +88,6 @@ authority_get_ticket (authority_t authority, ticket_t *pticket) | ... | @@ -213,7 +88,6 @@ authority_get_ticket (authority_t authority, ticket_t *pticket) |
213 | return 0; | 88 | return 0; |
214 | } | 89 | } |
215 | 90 | ||
216 | |||
217 | int | 91 | int |
218 | authority_authenticate (authority_t authority) | 92 | authority_authenticate (authority_t authority) |
219 | { | 93 | { | ... | ... |
1 | /* GNU mailutils - a suite of utilities for electronic mail | 1 | /* GNU mailutils - a suite of utilities for electronic mail |
2 | Copyright (C) 1999, 2000 Free Software Foundation, Inc. | 2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. |
3 | 3 | ||
4 | This program is free software; you can redistribute it and/or modify | 4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Library Public License as published by | 5 | it under the terms of the GNU General Library Public License as published by |
... | @@ -41,8 +41,9 @@ struct _ticket | ... | @@ -41,8 +41,9 @@ struct _ticket |
41 | { | 41 | { |
42 | void *owner; | 42 | void *owner; |
43 | char *challenge; | 43 | char *challenge; |
44 | char *type; | 44 | void *data; |
45 | int (*_pop) __P ((ticket_t, const char *challenge, char **)); | 45 | int (*_pop) __P ((ticket_t, const char *challenge, char **)); |
46 | void (*_destroy) __P ((ticket_t)); | ||
46 | }; | 47 | }; |
47 | 48 | ||
48 | struct _authority | 49 | struct _authority |
... | @@ -52,6 +53,12 @@ struct _authority | ... | @@ -52,6 +53,12 @@ struct _authority |
52 | int (*_authenticate) __P ((authority_t)); | 53 | int (*_authenticate) __P ((authority_t)); |
53 | }; | 54 | }; |
54 | 55 | ||
56 | struct _wicket | ||
57 | { | ||
58 | char *filename; | ||
59 | int (*_get_ticket) __P ((wicket_t, const char *, const char *, ticket_t *)); | ||
60 | }; | ||
61 | |||
55 | 62 | ||
56 | #ifdef __cplusplus | 63 | #ifdef __cplusplus |
57 | } | 64 | } | ... | ... |
mailbox/ticket.c
0 → 100644
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Library Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Library General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | #ifdef HAVE_CONFIG_H | ||
18 | # include <config.h> | ||
19 | #endif | ||
20 | |||
21 | #include <errno.h> | ||
22 | #include <sys/types.h> | ||
23 | #include <string.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <stdio.h> | ||
26 | #include <termios.h> | ||
27 | |||
28 | #include <mailutils/mutil.h> | ||
29 | #include <auth0.h> | ||
30 | |||
31 | static void | ||
32 | echo_off(struct termios *stored_settings) | ||
33 | { | ||
34 | struct termios new_settings; | ||
35 | tcgetattr (0, stored_settings); | ||
36 | new_settings = *stored_settings; | ||
37 | new_settings.c_lflag &= (~ECHO); | ||
38 | tcsetattr (0, TCSANOW, &new_settings); | ||
39 | } | ||
40 | |||
41 | static void | ||
42 | echo_on(struct termios *stored_settings) | ||
43 | { | ||
44 | tcsetattr (0, TCSANOW, stored_settings); | ||
45 | } | ||
46 | |||
47 | int | ||
48 | ticket_create (ticket_t *pticket, void *owner) | ||
49 | { | ||
50 | ticket_t ticket; | ||
51 | if (pticket == NULL) | ||
52 | return EINVAL; | ||
53 | ticket = calloc (1, sizeof (*ticket)); | ||
54 | if (ticket == NULL) | ||
55 | return ENOMEM; | ||
56 | ticket->owner = owner; | ||
57 | *pticket = ticket; | ||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | void | ||
62 | ticket_destroy (ticket_t *pticket, void *owner) | ||
63 | { | ||
64 | if (pticket && *pticket) | ||
65 | { | ||
66 | ticket_t ticket = *pticket; | ||
67 | if (ticket->owner == owner) | ||
68 | { | ||
69 | if (ticket->_destroy) | ||
70 | ticket->_destroy (ticket); | ||
71 | if (ticket->challenge) | ||
72 | free (ticket->challenge); | ||
73 | free (ticket); | ||
74 | } | ||
75 | *pticket = NULL; | ||
76 | } | ||
77 | } | ||
78 | |||
79 | int | ||
80 | ticket_set_destroy (ticket_t ticket, void (*_destroy) __P ((ticket_t)), | ||
81 | void *owner) | ||
82 | { | ||
83 | if (ticket == NULL) | ||
84 | return EINVAL; | ||
85 | if (ticket->owner != owner) | ||
86 | return EACCES; | ||
87 | ticket->_destroy = _destroy; | ||
88 | return 0; | ||
89 | } | ||
90 | |||
91 | void * | ||
92 | ticket_get_owner (ticket_t ticket) | ||
93 | { | ||
94 | return (ticket) ? ticket->owner : NULL; | ||
95 | } | ||
96 | |||
97 | int | ||
98 | ticket_set_pop (ticket_t ticket, | ||
99 | int (*_pop) __P ((ticket_t, const char *, char **)), | ||
100 | void *owner) | ||
101 | { | ||
102 | if (ticket == NULL) | ||
103 | return EINVAL; | ||
104 | if (ticket->owner != owner) | ||
105 | return EACCES; | ||
106 | ticket->_pop = _pop; | ||
107 | return 0; | ||
108 | } | ||
109 | |||
110 | int | ||
111 | ticket_pop (ticket_t ticket, const char *challenge, char **parg) | ||
112 | { | ||
113 | if (ticket == NULL || parg == NULL) | ||
114 | return EINVAL; | ||
115 | if (ticket->_pop) | ||
116 | return ticket->_pop (ticket, challenge, parg); | ||
117 | else | ||
118 | { | ||
119 | char arg[256]; | ||
120 | struct termios stored_settings; | ||
121 | int echo = 1; | ||
122 | |||
123 | /* Being smart if we see "Passwd" and turning off echo. */ | ||
124 | if (strstr (challenge, "ass") != NULL | ||
125 | || strstr (challenge, "ASS") != NULL) | ||
126 | echo = 0; | ||
127 | printf ("%s", challenge); | ||
128 | fflush (stdout); | ||
129 | if (!echo) | ||
130 | echo_off (&stored_settings); | ||
131 | fgets (arg, sizeof (arg), stdin); | ||
132 | if (!echo) | ||
133 | { | ||
134 | echo_on (&stored_settings); | ||
135 | putchar ('\n'); | ||
136 | fflush (stdout); | ||
137 | } | ||
138 | arg [strlen (arg) - 1] = '\0'; /* nuke the trailing line. */ | ||
139 | *parg = strdup (arg); | ||
140 | } | ||
141 | return 0; | ||
142 | } | ||
143 | |||
144 | int | ||
145 | ticket_get_data (ticket_t ticket, void **data) | ||
146 | { | ||
147 | if (ticket == NULL || data == NULL) | ||
148 | return EINVAL; | ||
149 | *data = ticket->data; | ||
150 | return 0; | ||
151 | } | ||
152 | |||
153 | int | ||
154 | ticket_set_data (ticket_t ticket, void *data, void *owner) | ||
155 | { | ||
156 | if (ticket == NULL) | ||
157 | return EINVAL; | ||
158 | if (ticket->owner != owner) | ||
159 | return EACCES; | ||
160 | ticket->data = data; | ||
161 | return 0; | ||
162 | } |
mailbox/wicket.c
0 → 100644
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Library Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Library General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | #ifdef HAVE_CONFIG_H | ||
18 | # include <config.h> | ||
19 | #endif | ||
20 | |||
21 | #include <errno.h> | ||
22 | #include <sys/types.h> | ||
23 | #include <string.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <stdio.h> | ||
26 | #include <unistd.h> | ||
27 | #include <ctype.h> | ||
28 | |||
29 | #include <auth0.h> | ||
30 | |||
31 | struct myticket_data | ||
32 | { | ||
33 | char *user; | ||
34 | char *pass; | ||
35 | }; | ||
36 | |||
37 | static char * stripwhite __P ((char *)); | ||
38 | static int myticket_create __P ((ticket_t *, const char *, const char *)); | ||
39 | static void myticket_destroy __P ((ticket_t)); | ||
40 | static int _get_ticket __P ((ticket_t *, const char *, const char *)); | ||
41 | static int myticket_pop __P ((ticket_t, const char *, char **)); | ||
42 | |||
43 | int | ||
44 | wicket_create (wicket_t *pwicket, const char *filename) | ||
45 | { | ||
46 | if (pwicket == NULL) | ||
47 | return EINVAL; | ||
48 | |||
49 | *pwicket = calloc (1, sizeof (**pwicket)); | ||
50 | if (*pwicket == NULL) | ||
51 | return ENOMEM; | ||
52 | |||
53 | if (filename) | ||
54 | (*pwicket)->filename = strdup (filename); | ||
55 | return 0; | ||
56 | } | ||
57 | |||
58 | int | ||
59 | wicket_destroy (wicket_t *pwicket) | ||
60 | { | ||
61 | if (pwicket && *pwicket) | ||
62 | { | ||
63 | wicket_t wicket = *pwicket; | ||
64 | if (wicket->filename) | ||
65 | free (wicket->filename); | ||
66 | free (wicket); | ||
67 | *pwicket = NULL; | ||
68 | } | ||
69 | } | ||
70 | |||
71 | int | ||
72 | wicket_get_filename (wicket_t wicket, char *filename, size_t len, | ||
73 | size_t *pwriten) | ||
74 | { | ||
75 | size_t n; | ||
76 | if (wicket == NULL) | ||
77 | return EINVAL; | ||
78 | n = util_cpystr (filename, wicket->filename, len); | ||
79 | if (pwriten) | ||
80 | *pwriten = n; | ||
81 | return 0; | ||
82 | } | ||
83 | |||
84 | int | ||
85 | wicket_set_filename (wicket_t wicket, const char *filename) | ||
86 | { | ||
87 | if (wicket == NULL) | ||
88 | return EINVAL; | ||
89 | |||
90 | if (wicket->filename) | ||
91 | free (wicket->filename); | ||
92 | |||
93 | wicket->filename = (filename) ? strdup (filename) : NULL; | ||
94 | return 0; | ||
95 | } | ||
96 | |||
97 | int | ||
98 | wicket_set_ticket (wicket_t wicket, int _get_ticket | ||
99 | __P ((wicket_t, const char *, const char *, ticket_t *))) | ||
100 | { | ||
101 | if (wicket == NULL) | ||
102 | return EINVAL; | ||
103 | |||
104 | wicket->_get_ticket = _get_ticket; | ||
105 | return 0; | ||
106 | } | ||
107 | |||
108 | int | ||
109 | wicket_get_ticket (wicket_t wicket, ticket_t *pticket, const char *user, | ||
110 | const char *type) | ||
111 | { | ||
112 | if (wicket == NULL || pticket == NULL || user == NULL) | ||
113 | return EINVAL; | ||
114 | |||
115 | if (wicket->filename == NULL) | ||
116 | return EINVAL; | ||
117 | |||
118 | if (wicket->_get_ticket) | ||
119 | return wicket->_get_ticket (wicket, user, type, pticket); | ||
120 | return _get_ticket (pticket, wicket->filename, user); | ||
121 | } | ||
122 | |||
123 | /* FIXME: This is a proof of concept ... write a more intelligent parser. */ | ||
124 | static int | ||
125 | _get_ticket (ticket_t *pticket, const char *filename, const char *user) | ||
126 | { | ||
127 | FILE *fp; | ||
128 | char *buf; | ||
129 | size_t buflen; | ||
130 | int status = ENOENT; | ||
131 | |||
132 | fp = fopen (filename, "r"); | ||
133 | if (fp == NULL) | ||
134 | return errno; | ||
135 | |||
136 | buflen = 128; | ||
137 | buf = malloc (buflen); | ||
138 | if (buf) | ||
139 | { | ||
140 | char *ptr = buf; | ||
141 | while (fgets (ptr, buflen, fp) != NULL) | ||
142 | { | ||
143 | size_t len = strlen (buf); | ||
144 | char *sep; | ||
145 | /* Check if a complete line. */ | ||
146 | if (len && buf[len - 1] != '\n') | ||
147 | { | ||
148 | char *tmp = realloc (buf, 2*buflen); | ||
149 | if (tmp == NULL) | ||
150 | { | ||
151 | status = ENOMEM; | ||
152 | break; | ||
153 | } | ||
154 | buf = tmp; | ||
155 | ptr = buf + len; | ||
156 | continue; | ||
157 | } | ||
158 | else | ||
159 | ptr = buf; | ||
160 | |||
161 | /* Comments. */ | ||
162 | if (*ptr == '#') | ||
163 | continue; | ||
164 | |||
165 | /* Skip leading spaces. */ | ||
166 | while (isspace (*ptr)) | ||
167 | { | ||
168 | ptr++; | ||
169 | len--; | ||
170 | } | ||
171 | |||
172 | /* user:passwd. Separator maybe ": \t" */ | ||
173 | if (len && ((sep = memchr (ptr, ':', len)) != NULL | ||
174 | || (sep = memchr (ptr, ' ', len)) != NULL | ||
175 | || (sep = memchr (ptr, '\t', len)) != NULL)) | ||
176 | { | ||
177 | *sep++ = '\0'; | ||
178 | ptr = stripwhite (ptr); | ||
179 | if (strcmp (ptr, user) == 0) | ||
180 | { | ||
181 | sep = stripwhite (sep); | ||
182 | status = myticket_create (pticket, ptr, sep); | ||
183 | break; | ||
184 | } | ||
185 | } | ||
186 | } | ||
187 | } | ||
188 | else | ||
189 | status = ENOMEM; | ||
190 | |||
191 | if (buf) | ||
192 | free (buf); | ||
193 | fclose (fp); | ||
194 | return status; | ||
195 | } | ||
196 | |||
197 | static int | ||
198 | myticket_create (ticket_t *pticket, const char *user, const char *pass) | ||
199 | { | ||
200 | struct myticket_data *mdata; | ||
201 | int status = ticket_create (pticket, NULL); | ||
202 | if (status != 0) | ||
203 | return status; | ||
204 | |||
205 | mdata = calloc (1, sizeof *mdata); | ||
206 | if (mdata == NULL) | ||
207 | ticket_destroy (pticket, NULL); | ||
208 | ticket_set_destroy (*pticket, myticket_destroy, NULL); | ||
209 | ticket_set_pop (*pticket, myticket_pop, NULL); | ||
210 | ticket_set_data (*pticket, mdata, NULL); | ||
211 | if ((mdata->user = strdup (user)) == NULL | ||
212 | || (mdata->pass = strdup (pass)) == NULL) | ||
213 | { | ||
214 | status = ENOMEM; | ||
215 | ticket_destroy (pticket, NULL); | ||
216 | } | ||
217 | return status; | ||
218 | } | ||
219 | |||
220 | static int | ||
221 | myticket_pop (ticket_t ticket, const char *challenge, char **parg) | ||
222 | { | ||
223 | struct myticket_data *mdata = NULL; | ||
224 | ticket_get_data (ticket, (void **)&mdata); | ||
225 | if (challenge && (strstr (challenge, "ass") != NULL | ||
226 | || strstr (challenge, "ASS") != NULL)) | ||
227 | *parg = strdup (mdata->pass); | ||
228 | else | ||
229 | *parg = strdup (mdata->user); | ||
230 | return 0; | ||
231 | } | ||
232 | |||
233 | static void | ||
234 | myticket_destroy (ticket_t ticket) | ||
235 | { | ||
236 | struct myticket_data *mdata = NULL; | ||
237 | ticket_get_data (ticket, (void **)&mdata); | ||
238 | if (mdata) | ||
239 | { | ||
240 | if (mdata->user) | ||
241 | free (mdata->user); | ||
242 | if (mdata->pass) | ||
243 | free (mdata->pass); | ||
244 | free (mdata); | ||
245 | } | ||
246 | } | ||
247 | |||
248 | /* Strip whitespace from the start and end of STRING. Return a pointer | ||
249 | into STRING. */ | ||
250 | static char * | ||
251 | stripwhite (char *string) | ||
252 | { | ||
253 | register char *s, *t; | ||
254 | |||
255 | for (s = string; isspace (*s); s++) | ||
256 | ; | ||
257 | |||
258 | if (*s == 0) | ||
259 | return (s); | ||
260 | |||
261 | t = s + strlen (s) - 1; | ||
262 | while (t > s && isspace (*t)) | ||
263 | t--; | ||
264 | *++t = '\0'; | ||
265 | |||
266 | return s; | ||
267 | } |
-
Please register or sign in to post a comment