1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/**
* Utilities for the quality of service module mod_qos.
*
* See http://mod-qos.sourceforge.net/ for further
* details.
*
* Copyright (C) 2023 Pascal Buchbinder
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
static const char revision[] = "$Id: qs_apo.c 2654 2022-05-13 09:12:42Z pbuchbinder $";
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>
/* apr/apr-util */
#include <apr.h>
#include <apr_base64.h>
#include <apr_pools.h>
#include <apr_strings.h>
#include <apr_thread_proc.h>
#include <apr_file_io.h>
#include <apr_time.h>
#include "qs_util.h"
#include "qs_apo.h"
static apr_table_t *qs_args(apr_pool_t *pool, const char *line) {
char *last = apr_pstrdup(pool, line);
apr_table_t* table = apr_table_make(pool, 10);
char *val;
while((val = apr_strtok(NULL, " ", &last))) {
apr_table_addn(table, val, "");
}
return table;
}
static void qs_failedexec(const char *msg, const char *cmd, apr_status_t status) {
char buf[MAX_LINE];
apr_strerror(status, buf, sizeof(buf));
fprintf(stderr, "ERROR %s '%s': '%s'\n", msg, cmd, buf);
exit(1);
}
/**
* Reads a passphrase using the defined passphrase getter (executes
* the program and reads the passphras from stdout).
*
* @param pool To allocate memory
* @param prg Path of the program to exectue
* @return The passphrase
*/
char *qs_readpwd(apr_pool_t *pool, const char *prg) {
apr_status_t status;
apr_proc_t proc;
const char **args;
apr_table_entry_t *entry;
char *last;
char *copy = apr_pstrdup(pool, prg);
char *cmd = apr_strtok(copy, " ", &last);
apr_table_t *a = qs_args(pool, prg);
int i;
apr_procattr_t *attr;
apr_size_t len = MAX_LINE;
char *buf = apr_pcalloc(pool, len);
args = apr_pcalloc(pool, (apr_table_elts(a)->nelts + 1) * sizeof(const char *));
entry = (apr_table_entry_t *) apr_table_elts(a)->elts;
for(i = 0; i < apr_table_elts(a)->nelts; i++) {
args[i] = entry[i].key;
}
args[i] = NULL;
if(cmd == NULL) {
qs_failedexec("can't read password, invalid executable", prg, APR_EGENERAL);
}
if((status = apr_procattr_create(&attr, pool)) != APR_SUCCESS) {
qs_failedexec("while reading password from executable", prg, status);
}
if((status = apr_procattr_cmdtype_set(attr, APR_PROGRAM_PATH)) != APR_SUCCESS) {
qs_failedexec("while reading password from executable", prg, status);
}
if((status = apr_procattr_detach_set(attr, 0)) != APR_SUCCESS) {
qs_failedexec("while reading password from executable", prg, status);
}
if((status = apr_procattr_io_set(attr, APR_FULL_BLOCK, APR_FULL_BLOCK, APR_NO_PIPE)) != APR_SUCCESS) {
qs_failedexec("while reading password from executable", prg, status);
}
if((status = apr_proc_create(&proc, cmd, args, NULL, attr, pool)) != APR_SUCCESS) {
qs_failedexec("could not execute program", prg, status);
} else {
char *e;
status = apr_proc_wait(&proc, NULL, NULL, APR_WAIT);
if(status != APR_CHILD_DONE && status != APR_SUCCESS) {
qs_failedexec("while reading password from executable", prg, status);
}
status = apr_file_read(proc.out, buf, &len);
if(status != APR_SUCCESS) {
qs_failedexec("failed to read password from program", prg, status);
}
e = buf;
while(e && e[0]) {
if((e[0] == LF) || (e[0] == CR)) {
e[0] = '\0';
} else {
e++;
}
}
}
return buf;
}
|