Test auth schemes
Showing
1 changed file
with
130 additions
and
0 deletions
examples/muauth.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2006 Free Software Foundation, Inc. | ||
3 | |||
4 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Mailutils 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 General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with GNU Mailutils; if not, write to the Free Software | ||
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
17 | MA 02110-1301 USA */ | ||
18 | |||
19 | #ifdef HAVE_CONFIG_H | ||
20 | # include <config.h> | ||
21 | #endif | ||
22 | #include <unistd.h> | ||
23 | #include <stdio.h> | ||
24 | #include <assert.h> | ||
25 | #include <ctype.h> | ||
26 | #include <string.h> | ||
27 | #include <mailutils/mailutils.h> | ||
28 | |||
29 | const char *program_version = "muauth (" PACKAGE_STRING ")"; | ||
30 | static char doc[] = | ||
31 | "muauth -- test mailutils authentication and authorization schemes"; | ||
32 | static char args_doc[] = "key"; | ||
33 | |||
34 | static const char *capa[] = { | ||
35 | "auth", | ||
36 | "license", | ||
37 | "common", | ||
38 | NULL | ||
39 | }; | ||
40 | |||
41 | static struct argp_option options[] = { | ||
42 | { "password", 'p', "STRING", 0, "user password", 0 }, | ||
43 | { "uid", 'u', NULL, 0, "test getpwuid functions", 0 }, | ||
44 | { "name", 'n', NULL, 0, "test getpwnam functions", 0 }, | ||
45 | { NULL }, | ||
46 | }; | ||
47 | |||
48 | enum mu_auth_key_type key_type = mu_auth_key_name; | ||
49 | char *password; | ||
50 | |||
51 | static error_t | ||
52 | parse_opt (int key, char *arg, struct argp_state *state) | ||
53 | { | ||
54 | switch (key) | ||
55 | { | ||
56 | case 'p': | ||
57 | password = arg; | ||
58 | break; | ||
59 | |||
60 | case 'u': | ||
61 | key_type = mu_auth_key_uid; | ||
62 | break; | ||
63 | |||
64 | case 'n': | ||
65 | key_type = mu_auth_key_name; | ||
66 | break; | ||
67 | |||
68 | default: | ||
69 | return ARGP_ERR_UNKNOWN; | ||
70 | } | ||
71 | return 0; | ||
72 | } | ||
73 | |||
74 | static struct argp argp = { | ||
75 | options, | ||
76 | parse_opt, | ||
77 | args_doc, | ||
78 | doc, | ||
79 | NULL, | ||
80 | NULL, NULL | ||
81 | }; | ||
82 | |||
83 | int | ||
84 | main (int argc, char * argv []) | ||
85 | { | ||
86 | int rc, index; | ||
87 | struct mu_auth_data *auth; | ||
88 | void *key; | ||
89 | uid_t uid; | ||
90 | |||
91 | MU_AUTH_REGISTER_ALL_MODULES (); | ||
92 | mu_argp_init (program_version, NULL); | ||
93 | mu_argp_parse (&argp, &argc, &argv, 0, capa, &index, NULL); | ||
94 | if (index == argc) | ||
95 | { | ||
96 | mu_error ("not enough arguments, try `%s --help' for more info", | ||
97 | argv[0]); | ||
98 | return 1; | ||
99 | } | ||
100 | |||
101 | if (key_type == mu_auth_key_uid) | ||
102 | { | ||
103 | uid = strtoul (argv[index], NULL, 0); | ||
104 | key = &uid; | ||
105 | } | ||
106 | else | ||
107 | key = argv[index]; | ||
108 | |||
109 | rc = mu_get_auth (&auth, key_type, key); | ||
110 | printf ("mu_get_auth => %d, %s\n", rc, mu_strerror (rc)); | ||
111 | if (rc == 0) | ||
112 | { | ||
113 | printf ("user name: %s\n", auth->name); | ||
114 | printf ("password: %s\n", auth->passwd); | ||
115 | printf ("uid: %lu\n", (unsigned long) auth->uid); | ||
116 | printf ("gid: %lu\n", (unsigned long) auth->gid); | ||
117 | printf ("gecos: %s\n", auth->gecos); | ||
118 | printf ("home: %s\n", auth->dir); | ||
119 | printf ("shell: %s\n", auth->shell); | ||
120 | printf ("mailbox: %s\n", auth->mailbox); | ||
121 | printf ("change_uid: %d\n", auth->change_uid); | ||
122 | |||
123 | rc = mu_authenticate (auth, password); | ||
124 | printf ("mu_authenticate => %d, %s\n", rc, mu_strerror (rc)); | ||
125 | mu_auth_data_free (auth); | ||
126 | } | ||
127 | return rc != 0; | ||
128 | } | ||
129 | |||
130 |
-
Please register or sign in to post a comment