]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
083d87de | 2 | * |
7ac48069 | 3 | * Glue routines to allow the database stuff to be linked in to |
4 | * a program expecting a library level interface. | |
083d87de | 5 | * |
7ac48069 | 6 | * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
083d87de | 9 | */ |
10 | ||
674fd273 | 11 | #include <mit-copyright.h> |
e448f08d | 12 | #include "mr_server.h" |
03c05291 | 13 | #include "query.h" |
53db4098 | 14 | |
7ac48069 | 15 | #include <sys/wait.h> |
16 | ||
17 | #include <errno.h> | |
18 | #include <pwd.h> | |
19 | #include <signal.h> | |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
22 | #include <unistd.h> | |
23 | ||
24 | extern char *krb_get_lrealm(char *, int); | |
25 | ||
26 | RCSID("$Header$"); | |
27 | ||
083d87de | 28 | static int already_connected = 0; |
29 | ||
d548a4e7 | 30 | #define CHECK_CONNECTED { if (!already_connected) return MR_NOT_CONNECTED; } |
083d87de | 31 | |
32 | static client pseudo_client; | |
ea0caf4a | 33 | client *cur_client = &pseudo_client; |
e448f08d | 34 | extern char *whoami; |
ea0caf4a | 35 | time_t now; |
7ac48069 | 36 | |
37 | void reapchild(void); | |
38 | int callback(int argc, char **argv, void *arg); | |
083d87de | 39 | |
5eaef520 | 40 | int mr_connect(char *server) |
083d87de | 41 | { |
44d12d58 | 42 | int status; |
5eaef520 | 43 | extern int query_timeout; |
44 | struct sigaction action; | |
45 | ||
46 | if (already_connected) | |
47 | return MR_ALREADY_CONNECTED; | |
48 | ||
49 | initialize_sms_error_table(); | |
50 | initialize_krb_error_table(); | |
51 | memset(&pseudo_client, 0, sizeof(pseudo_client)); | |
52 | ||
53 | query_timeout = 0; | |
54 | status = mr_open_database(); | |
55 | if (!status) | |
56 | already_connected = 1; | |
57 | ||
58 | action.sa_flags = 0; | |
59 | sigemptyset(&action.sa_mask); | |
60 | sigaddset(&action.sa_mask, SIGCHLD); | |
61 | action.sa_handler = reapchild; | |
62 | if (sigaction(SIGCHLD, &action, NULL) < 0) | |
63 | { | |
03c05291 | 64 | com_err(whoami, errno, "Unable to establish signal handlers."); |
65 | exit(1); | |
66 | } | |
5eaef520 | 67 | return status; |
083d87de | 68 | } |
69 | ||
5eaef520 | 70 | int mr_disconnect(void) |
083d87de | 71 | { |
5eaef520 | 72 | CHECK_CONNECTED; |
73 | mr_close_database(); | |
74 | already_connected = 0; | |
75 | return 0; | |
083d87de | 76 | } |
77 | ||
5eaef520 | 78 | int mr_noop(void) |
083d87de | 79 | { |
5eaef520 | 80 | CHECK_CONNECTED; |
81 | return 0; | |
083d87de | 82 | } |
5eaef520 | 83 | |
083d87de | 84 | /* |
85 | * This routine is rather bogus, as it only fills in who you claim to be. | |
86 | */ | |
5eaef520 | 87 | int mr_auth(char *prog) |
083d87de | 88 | { |
5eaef520 | 89 | struct passwd *pw; |
e688520a | 90 | char buf[MAX_K_NAME_SZ]; |
5eaef520 | 91 | |
92 | CHECK_CONNECTED; | |
93 | pw = getpwuid(getuid()); | |
94 | if (!pw) | |
95 | return KDC_PR_UNKNOWN + ERROR_TABLE_BASE_krb; | |
96 | strcpy(pseudo_client.kname.name, pw->pw_name); | |
97 | krb_get_lrealm(pseudo_client.kname.realm, 1); | |
5eaef520 | 98 | |
99 | strcpy(buf, pw->pw_name); | |
100 | strcat(buf, "@"); | |
101 | strcat(buf, pseudo_client.kname.realm); | |
102 | strcpy(pseudo_client.clname, buf); | |
103 | pseudo_client.users_id = 0; | |
104 | name_to_id(pseudo_client.kname.name, USERS_TABLE, &pseudo_client.users_id); | |
105 | pseudo_client.client_id = pseudo_client.users_id; | |
106 | strncpy(pseudo_client.entity, prog, 8); | |
5eaef520 | 107 | return 0; |
083d87de | 108 | } |
109 | ||
53db4098 | 110 | struct hint { |
7ac48069 | 111 | int (*proc)(int, char **, void *); |
5eaef520 | 112 | char *hint; |
53db4098 | 113 | }; |
114 | ||
7ac48069 | 115 | int callback(int argc, char **argv, void *arg) |
53db4098 | 116 | { |
7ac48069 | 117 | struct hint *hint = arg; |
5eaef520 | 118 | if (mr_trim_args(argc, argv) == MR_NO_MEM) |
119 | com_err(whoami, MR_NO_MEM, "while trimmming args"); | |
7ac48069 | 120 | return (*hint->proc)(argc, argv, hint->hint); |
53db4098 | 121 | } |
122 | ||
123 | ||
7ac48069 | 124 | int mr_query(char *name, int argc, char **argv, |
125 | int (*callproc)(int, char **, void *), void *callarg) | |
083d87de | 126 | { |
5eaef520 | 127 | struct hint hints; |
128 | ||
129 | time(&now); | |
130 | hints.proc = callproc; | |
131 | hints.hint = callarg; | |
132 | next_incremental(); | |
133 | return mr_process_query(&pseudo_client, name, argc, | |
134 | mr_copy_args(argv, argc), callback, | |
135 | (char *)&hints); | |
083d87de | 136 | } |
137 | ||
5eaef520 | 138 | int mr_access(char *name, int argc, char **argv) |
083d87de | 139 | { |
5eaef520 | 140 | time(&now); |
141 | return mr_check_access(&pseudo_client, name, argc, | |
142 | mr_copy_args(argv, argc)); | |
083d87de | 143 | } |
144 | ||
5eaef520 | 145 | void reapchild(void) |
cba4da55 | 146 | { |
5eaef520 | 147 | int status, pid; |
148 | ||
149 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) | |
150 | { | |
151 | if (pid == inc_pid) | |
152 | { | |
153 | inc_running = 0; | |
154 | next_incremental(); | |
65b73e9d | 155 | } |
5eaef520 | 156 | if (WTERMSIG(status) != 0 || WEXITSTATUS(status) != 0) |
157 | { | |
cba4da55 | 158 | com_err(whoami, 0, "%d: child exits with signal %d status %d", |
03c05291 | 159 | pid, WTERMSIG(status), WEXITSTATUS(status)); |
5eaef520 | 160 | } |
cba4da55 | 161 | } |
162 | } |