]> andersk Git - moira.git/blob - server/mr_glue.c
Switch from Imake-based build system to autoconf-based.
[moira.git] / server / mr_glue.c
1 /* $Id$
2  *
3  * Glue routines to allow the database stuff to be linked in to
4  * a program expecting a library level interface.
5  *
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>.
9  */
10
11 #include <mit-copyright.h>
12 #include "mr_server.h"
13 #include "query.h"
14
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
28 static int already_connected = 0;
29
30 #define CHECK_CONNECTED { if (!already_connected) return MR_NOT_CONNECTED; }
31
32 static client pseudo_client;
33 client *cur_client = &pseudo_client;
34 extern char *whoami;
35 time_t now;
36
37 void reapchild(void);
38 int callback(int argc, char **argv, void *arg);
39
40 int mr_connect(char *server)
41 {
42   int status;
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     {
64       com_err(whoami, errno, "Unable to establish signal handlers.");
65       exit(1);
66     }
67   return status;
68 }
69
70 int mr_disconnect(void)
71 {
72   CHECK_CONNECTED;
73   mr_close_database();
74   already_connected = 0;
75   return 0;
76 }
77
78 int mr_noop(void)
79 {
80   CHECK_CONNECTED;
81   return 0;
82 }
83
84 /*
85  * This routine is rather bogus, as it only fills in who you claim to be.
86  */
87 int mr_auth(char *prog)
88 {
89   struct passwd *pw;
90   char buf[MAX_K_NAME_SZ];
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);
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);
107   return 0;
108 }
109
110 struct hint {
111   int (*proc)(int, char **, void *);
112   char *hint;
113 };
114
115 int callback(int argc, char **argv, void *arg)
116 {
117   struct hint *hint = arg;
118   if (mr_trim_args(argc, argv) == MR_NO_MEM)
119     com_err(whoami, MR_NO_MEM, "while trimmming args");
120   return (*hint->proc)(argc, argv, hint->hint);
121 }
122
123
124 int mr_query(char *name, int argc, char **argv,
125              int (*callproc)(int, char **, void *), void *callarg)
126 {
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);
136 }
137
138 int mr_access(char *name, int argc, char **argv)
139 {
140   time(&now);
141   return mr_check_access(&pseudo_client, name, argc,
142                          mr_copy_args(argv, argc));
143 }
144
145 void reapchild(void)
146 {
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();
155         }
156       if (WTERMSIG(status) != 0 || WEXITSTATUS(status) != 0)
157         {
158           com_err(whoami, 0, "%d: child exits with signal %d status %d",
159                   pid, WTERMSIG(status), WEXITSTATUS(status));
160         }
161     }
162 }
This page took 0.054415 seconds and 5 git commands to generate.