2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
35 #include "pwmd-error.h"
37 #include "util-misc.h"
38 #include "util-string.h"
43 extern void log_write(const char *fmt
, ...);
45 int valid_filename(const char *filename
)
49 if (!filename
|| !*filename
)
52 if (!strcmp(filename
, "-"))
55 for (p
= filename
; *p
; p
++) {
56 if (*p
== '/' || isspace(*p
))
65 struct passwd
*pw
= getpwuid(getuid());
67 return pw
? pw
->pw_name
: NULL
;
72 struct passwd pw
, *result
;
77 return home_directory
;
79 len
= sysconf(_SC_GETPW_R_SIZE_MAX
);
87 if (!getpwuid_r(getuid(), &pw
, buf
, len
, &result
))
88 home_directory
= str_dup(result
->pw_dir
);
91 return home_directory
;
94 char *expand_homedir(char *str
)
99 return str_asprintf("%s%s", get_home_dir(), p
);
104 gpg_error_t
parse_options(char **line
, struct argv_s
*args
[], void * data
)
109 for (; p
&& *p
; p
++) {
116 if (*p
== '-' && *(p
+1) == '-') {
118 char opt
[255] = {0}, value
[255] = {0};
122 if (!*p
|| *p
== ' ') {
129 for (tp
= opt
, len
= 0; *p
; p
++, len
++) {
131 return GPG_ERR_LINE_TOO_LONG
;
133 if (*p
== ' ' || *p
== '=') {
141 for (tp
= value
, len
= 0; *p
; p
++, len
++) {
143 return GPG_ERR_LINE_TOO_LONG
;
149 else if (*p
== ' ' && !inquote
)
167 for (int i
= 0; args
[i
]; i
++) {
168 if (!strcmp(args
[i
]->opt
, opt
)) {
169 log_write1("param: name='%s' value='%s'", opt
, value
);
170 if (args
[i
]->type
== OPTION_TYPE_NOARG
&& *value
)
171 return GPG_ERR_SYNTAX
;
172 else if (args
[i
]->type
== OPTION_TYPE_ARG
&& !*value
)
173 return GPG_ERR_SYNTAX
;
175 rc
= args
[i
]->func(data
, value
);
186 return GPG_ERR_UNKNOWN_OPTION
;
201 char *bin2hex(const unsigned char *data
, size_t len
)
203 size_t size
= len
*2+1;
209 for (n
= 0; n
< len
; n
++) {
212 sprintf(c
, "%02X", data
[n
]);
219 char *plus_escape(const char *fmt
, ...)
226 if (str_vasprintf(&str
, fmt
, ap
) > 0) {
229 for (p
= str
; *p
; p
++) {
242 static char *strip_texi(const char *str
)
245 char *s
= str_dup(str
);
251 for (p
= str
; *p
; p
++) {
252 if (*p
== '@' && *(p
+1) != '@') {
253 if (!strncasecmp(p
+1, "table", 5)
254 || !strncasecmp(p
+1, "end ", 4)) {
255 while (*p
++ != '\n');
259 else if (!strncasecmp(p
+1, "example", 7)
260 || !strncasecmp(p
+1, "end ", 4)) {
261 while (*p
++ != '\n');
265 else if (!strncasecmp(p
+1, "sp ", 3)) {
268 while (*p
&& isdigit(*p
++))
272 else if (!strncasecmp(p
+1, "item", 4)) {
274 while (*p
&& *p
!= '\n')
279 else if (!strncasecmp(p
+1, "pxref", 5)) {
285 else if (!strncasecmp(p
+1, "xref", 4)) {
292 while (*p
&& *p
!= '{') {
303 while (*p
&& *p
!= '}')
312 else if (*p
== '@' && *(p
+1) == '@')
327 char *strip_texi_and_wrap(const char *str
)
329 char *tmp
= strip_texi(str
);
330 char *help
= xmalloc(strlen(tmp
+1)*2);
334 for (p
= tmp
, ph
= help
, i
= 0; *p
; p
++, i
++) {
335 if (i
== 79 || *p
== '\n') {
340 while (!(*--t
== ' '))
354 while (*p
!= '\n' && *p
== ' ')
367 void free_key(void *data
)
372 gpg_error_t
create_thread(void *(*cb
)(void *), void *data
,
373 pthread_t
*tid
, int detached
)
378 pthread_attr_init(&attr
);
381 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
383 n
= pthread_create(tid
, &attr
, cb
, data
);
384 pthread_attr_destroy(&attr
);
385 return gpg_error_from_errno(n
);
388 void cleanup_mutex_cb(void *arg
)
390 pthread_mutex_t
*m
= (pthread_mutex_t
*)arg
;
395 void cleanup_fd_cb(void *arg
)
397 int fd
= *(int *)arg
;
403 void cleanup_unlink_cb(void *arg
)
411 void cleanup_cache_mutex(void *arg
)
416 int valid_keygrip(const unsigned char *data
, size_t len
)
418 for (size_t i
= 0; i
< len
; i
++) {
426 gpg_error_t
get_checksum(const char *filename
, unsigned char **r_crc
, size_t *r_crclen
)
434 if (stat(filename
, &st
) == -1)
435 return gpg_error_from_syserror();
437 fd
= open(filename
, O_RDONLY
);
439 return gpg_error_from_syserror();
441 pthread_cleanup_push(cleanup_fd_cb
, &fd
);
442 buf
= xmalloc(st
.st_size
);
444 pthread_cleanup_push(xfree
, buf
);
446 len
= read(fd
, buf
, st
.st_size
);
447 if (len
== st
.st_size
) {
451 len
= gcry_md_get_algo_dlen(GCRY_MD_CRC32
);
454 pthread_cleanup_push(xfree
, crc
);
455 gcry_md_hash_buffer(GCRY_MD_CRC32
, crc
, buf
, st
.st_size
);
458 pthread_cleanup_pop(0);
465 rc
= GPG_ERR_TOO_SHORT
;
467 pthread_cleanup_pop(1);
472 pthread_cleanup_pop(1);