annotate src/lib-auth/auth-master.c @ 9354:687ac828b964 HEAD

lib-index: modseqs weren't tracked properly within session when changes were done.
author Timo Sirainen <tss@iki.fi>
date Tue, 01 Sep 2009 13:05:03 -0400
parents b9faf4db2a9f
children 2d4d9b0cdcc1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8590
b9faf4db2a9f Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents: 8458
diff changeset
1 /* Copyright (c) 2005-2009 Dovecot authors, see the included COPYING file */
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
2
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
3 #include "lib.h"
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
4 #include "lib-signals.h"
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
5 #include "array.h"
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
6 #include "ioloop.h"
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
7 #include "network.h"
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
8 #include "istream.h"
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
9 #include "ostream.h"
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
10 #include "auth-master.h"
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
11
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
12 #include <stdlib.h>
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
13 #include <unistd.h>
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
14
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
15 #define AUTH_PROTOCOL_MAJOR 1
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
16 #define AUTH_PROTOCOL_MINOR 0
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
17
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
18 #define AUTH_REQUEST_TIMEOUT_SECS 30
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
19 #define AUTH_MASTER_IDLE_SECS 60
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
20
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
21 #define MAX_INBUF_SIZE 8192
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
22 #define MAX_OUTBUF_SIZE 1024
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
23
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
24 struct auth_master_connection {
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
25 char *auth_socket_path;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
26
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
27 int fd;
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
28 struct ioloop *ioloop;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
29 struct io *io;
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
30 struct istream *input;
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
31 struct ostream *output;
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
32 struct timeout *to;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
33
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
34 unsigned int request_counter;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
35 pool_t pool;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
36 const char *user;
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
37 struct auth_user_reply *user_reply;
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
38 int return_value;
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
39
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
40 unsigned int debug:1;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
41 unsigned int sent_handshake:1;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
42 unsigned int handshaked:1;
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
43 unsigned int aborted:1;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
44 };
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
45
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
46 static void auth_input(struct auth_master_connection *conn);
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
47
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
48 struct auth_master_connection *
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
49 auth_master_init(const char *auth_socket_path, bool debug)
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
50 {
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
51 struct auth_master_connection *conn;
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
52
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
53 conn = i_new(struct auth_master_connection, 1);
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
54 conn->auth_socket_path = i_strdup(auth_socket_path);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
55 conn->fd = -1;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
56 conn->debug = debug;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
57 return conn;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
58 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
59
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
60 static void auth_connection_close(struct auth_master_connection *conn)
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
61 {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
62 if (conn->to != NULL)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
63 timeout_remove(&conn->to);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
64 if (conn->fd != -1) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
65 if (close(conn->fd) < 0)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
66 i_error("close(%s) failed: %m", conn->auth_socket_path);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
67 conn->fd = -1;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
68 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
69
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
70 conn->sent_handshake = FALSE;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
71 conn->handshaked = FALSE;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
72 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
73
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
74 void auth_master_deinit(struct auth_master_connection **_conn)
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
75 {
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
76 struct auth_master_connection *conn = *_conn;
8369
6db401560d37 auth-master API cleanup.
Timo Sirainen <tss@iki.fi>
parents: 8368
diff changeset
77
6db401560d37 auth-master API cleanup.
Timo Sirainen <tss@iki.fi>
parents: 8368
diff changeset
78 *_conn = NULL;
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
79 auth_connection_close(conn);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
80 i_free(conn->auth_socket_path);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
81 i_free(conn);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
82 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
83
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
84 static void auth_request_lookup_abort(struct auth_master_connection *conn)
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
85 {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
86 io_loop_stop(conn->ioloop);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
87 conn->aborted = TRUE;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
88 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
89
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
90 static void auth_parse_input(struct auth_master_connection *conn,
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
91 const char *const *args)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
92 {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
93 struct auth_user_reply *reply = conn->user_reply;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
94
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
95 memset(reply, 0, sizeof(*reply));
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
96 reply->uid = (uid_t)-1;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
97 reply->gid = (gid_t)-1;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
98 p_array_init(&reply->extra_fields, conn->pool, 64);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
99
8458
ed4d6f6e4da7 deliver: Allow userdb to change the username.
Timo Sirainen <tss@iki.fi>
parents: 8373
diff changeset
100 reply->user = p_strdup(conn->pool, *args);
ed4d6f6e4da7 deliver: Allow userdb to change the username.
Timo Sirainen <tss@iki.fi>
parents: 8373
diff changeset
101 for (args++; *args != NULL; args++) {
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
102 if (conn->debug)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
103 i_info("auth input: %s", *args);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
104
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
105 if (strncmp(*args, "uid=", 4) == 0)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
106 reply->uid = strtoul(*args + 4, NULL, 10);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
107 else if (strncmp(*args, "gid=", 4) == 0)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
108 reply->gid = strtoul(*args + 4, NULL, 10);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
109 else if (strncmp(*args, "home=", 5) == 0)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
110 reply->home = p_strdup(conn->pool, *args + 5);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
111 else if (strncmp(*args, "chroot=", 7) == 0)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
112 reply->chroot = p_strdup(conn->pool, *args + 7);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
113 else {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
114 const char *field = p_strdup(conn->pool, *args);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
115 array_append(&reply->extra_fields, &field, 1);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
116 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
117 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
118 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
119
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
120 static int auth_input_handshake(struct auth_master_connection *conn)
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
121 {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
122 const char *line, *const *tmp;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
123
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
124 while ((line = i_stream_next_line(conn->input)) != NULL) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
125 tmp = t_strsplit(line, "\t");
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
126 if (strcmp(tmp[0], "VERSION") == 0 &&
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
127 tmp[1] != NULL && tmp[2] != NULL) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
128 if (strcmp(tmp[1], dec2str(AUTH_PROTOCOL_MAJOR)) != 0) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
129 i_error("userdb lookup(%s): "
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
130 "Auth protocol version mismatch "
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
131 "(%s vs %d)", conn->user, tmp[1],
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
132 AUTH_PROTOCOL_MAJOR);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
133 auth_request_lookup_abort(conn);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
134 return -1;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
135 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
136 } else if (strcmp(tmp[0], "SPID") == 0) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
137 conn->handshaked = TRUE;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
138 break;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
139 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
140 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
141 return 0;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
142 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
143
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
144 static void auth_input(struct auth_master_connection *conn)
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
145 {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
146 const char *line, *cmd, *const *args, *id, *wanted_id;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
147
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
148 switch (i_stream_read(conn->input)) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
149 case 0:
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
150 return;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
151 case -1:
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
152 /* disconnected */
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
153 i_error("userdb lookup(%s): Disconnected unexpectedly",
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
154 conn->user);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
155 auth_request_lookup_abort(conn);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
156 return;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
157 case -2:
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
158 /* buffer full */
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
159 i_error("userdb lookup(%s): BUG: Received more than %d bytes",
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
160 conn->user, MAX_INBUF_SIZE);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
161 auth_request_lookup_abort(conn);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
162 return;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
163 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
164
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
165 if (!conn->handshaked) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
166 if (auth_input_handshake(conn) < 0)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
167 return;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
168 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
169
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
170 line = i_stream_next_line(conn->input);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
171 if (line == NULL)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
172 return;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
173
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
174 args = t_strsplit(line, "\t");
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
175 cmd = *args; args++;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
176 if (*args == NULL)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
177 id = "";
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
178 else {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
179 id = *args;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
180 args++;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
181 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
182
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
183 wanted_id = dec2str(conn->request_counter);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
184 if (strcmp(id, wanted_id) == 0) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
185 io_loop_stop(conn->ioloop);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
186 if (strcmp(cmd, "USER") == 0) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
187 auth_parse_input(conn, args);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
188 conn->return_value = 1;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
189 return;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
190 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
191 if (strcmp(cmd, "NOTFOUND") == 0) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
192 conn->return_value = 0;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
193 return;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
194 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
195 if (strcmp(cmd, "FAIL") == 0) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
196 i_error("userdb lookup(%s) failed: %s",
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
197 conn->user, *args != NULL ? *args :
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
198 "Internal failure");
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
199 return;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
200 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
201 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
202
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
203 if (strcmp(cmd, "CUID") == 0) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
204 i_error("userdb lookup(%s): %s is an auth client socket. "
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
205 "It should be a master socket.",
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
206 conn->user, conn->auth_socket_path);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
207 } else {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
208 i_error("userdb lookup(%s): BUG: Unexpected input: %s",
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
209 conn->user, line);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
210 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
211 auth_request_lookup_abort(conn);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
212 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
213
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
214 static int auth_master_connect(struct auth_master_connection *conn)
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
215 {
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
216 int fd, try;
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
217
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
218 i_assert(conn->fd == -1);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
219
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
220 /* max. 1 second wait here. */
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
221 for (try = 0; try < 10; try++) {
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
222 fd = net_connect_unix(conn->auth_socket_path);
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
223 if (fd != -1 || (errno != EAGAIN && errno != ECONNREFUSED))
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
224 break;
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
225
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
226 /* busy. wait for a while. */
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
227 usleep(((rand() % 10) + 1) * 10000);
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
228 }
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
229 if (fd == -1) {
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
230 i_error("userdb lookup: connect(%s) failed: %m",
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
231 conn->auth_socket_path);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
232 return -1;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
233 }
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
234 conn->fd = fd;
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
235 return 0;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
236 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
237
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
238 static void auth_request_timeout(struct auth_master_connection *conn)
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
239 {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
240 if (!conn->handshaked)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
241 i_error("userdb lookup(%s): Connecting timed out", conn->user);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
242 else
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
243 i_error("userdb lookup(%s): Request timed out", conn->user);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
244 auth_request_lookup_abort(conn);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
245 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
246
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
247 static void auth_idle_timeout(struct auth_master_connection *conn)
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
248 {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
249 auth_connection_close(conn);
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
250 }
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
251
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
252 static void auth_master_set_io(struct auth_master_connection *conn)
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
253 {
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
254 if (conn->to != NULL)
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
255 timeout_remove(&conn->to);
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
256
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
257 conn->ioloop = io_loop_create();
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
258 conn->input = i_stream_create_fd(conn->fd, MAX_INBUF_SIZE, FALSE);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
259 conn->output = o_stream_create_fd(conn->fd, MAX_OUTBUF_SIZE, FALSE);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
260 conn->io = io_add(conn->fd, IO_READ, auth_input, conn);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
261 conn->to = timeout_add(1000*AUTH_REQUEST_TIMEOUT_SECS,
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
262 auth_request_timeout, conn);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
263 lib_signals_reset_ioloop();
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
264 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
265
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
266 static void auth_master_unset_io(struct auth_master_connection *conn,
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
267 struct ioloop *prev_ioloop)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
268 {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
269 io_loop_set_current(prev_ioloop);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
270 lib_signals_reset_ioloop();
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
271 io_loop_set_current(conn->ioloop);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
272
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
273 timeout_remove(&conn->to);
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
274 io_remove(&conn->io);
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
275 i_stream_unref(&conn->input);
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
276 o_stream_unref(&conn->output);
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
277 io_loop_destroy(&conn->ioloop);
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
278
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
279 conn->to = timeout_add(1000*AUTH_MASTER_IDLE_SECS,
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
280 auth_idle_timeout, conn);
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
281 }
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
282
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
283 static bool is_valid_string(const char *str)
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
284 {
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
285 const char *p;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
286
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
287 /* make sure we're not sending any characters that have a special
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
288 meaning. */
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
289 for (p = str; *p != '\0'; p++) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
290 if (*p == '\t' || *p == '\n' || *p == '\r')
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
291 return FALSE;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
292 }
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
293 return TRUE;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
294 }
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
295
8373
9524ec6e14fc auth-master API cleanups.
Timo Sirainen <tss@iki.fi>
parents: 8369
diff changeset
296 int auth_master_user_lookup(struct auth_master_connection *conn,
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
297 const char *user, const char *service,
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
298 pool_t pool, struct auth_user_reply *reply_r)
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
299 {
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
300 struct ioloop *prev_ioloop;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
301 const char *str;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
302
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
303 if (!is_valid_string(user) || !is_valid_string(service)) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
304 /* non-allowed characters, the user can't exist */
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
305 return 0;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
306 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
307 if (conn->fd == -1) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
308 if (auth_master_connect(conn) < 0)
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
309 return -1;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
310 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
311
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
312 prev_ioloop = current_ioloop;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
313 auth_master_set_io(conn);
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
314 conn->return_value = -1;
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
315 conn->pool = pool;
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
316 conn->user = user;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
317 conn->user_reply = reply_r;
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
318 if (++conn->request_counter == 0) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
319 /* avoid zero */
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
320 conn->request_counter++;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
321 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
322
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
323 o_stream_cork(conn->output);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
324 if (!conn->sent_handshake) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
325 str = t_strdup_printf("VERSION\t%d\t%d\n",
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
326 AUTH_PROTOCOL_MAJOR, AUTH_PROTOCOL_MINOR);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
327 o_stream_send_str(conn->output, str);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
328 conn->sent_handshake = TRUE;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
329 }
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
330
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
331 str = t_strdup_printf("USER\t%u\t%s\tservice=%s\n",
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
332 conn->request_counter, user, service);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
333 o_stream_send_str(conn->output, str);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
334 o_stream_uncork(conn->output);
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
335
8368
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
336 if (conn->output->stream_errno != 0) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
337 errno = conn->output->stream_errno;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
338 i_error("write(auth socket) failed: %m");
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
339 } else {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
340 io_loop_run(conn->ioloop);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
341 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
342
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
343 auth_master_unset_io(conn, prev_ioloop);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
344 if (conn->aborted) {
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
345 conn->aborted = FALSE;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
346 auth_connection_close(conn);
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
347 }
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
348 conn->user = NULL;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
349 conn->pool = NULL;
9babcdc6f4f7 Cleanups and fixes to auth-master API.
Timo Sirainen <tss@iki.fi>
parents: 8365
diff changeset
350 conn->user_reply = NULL;
8365
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
351 return conn->return_value;
f97099eb4dee New generic userdb lookup api `auth-master' in lib-auth.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
352 }