2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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
, ...);
46 valid_filename (const char *filename
)
50 if (!filename
|| !*filename
)
53 if (filename
[0] == '-' && filename
[1] == 0)
56 for (p
= filename
; *p
; p
++)
58 if (*p
== '/' || isspace (*p
))
68 struct passwd
*pw
= getpwuid (getuid ());
70 return pw
? pw
->pw_name
: NULL
;
76 #ifdef HAVE_GETPWNAM_R
77 struct passwd pw
, *result
;
82 return home_directory
;
84 len
= sysconf (_SC_GETPW_R_SIZE_MAX
);
92 if (!getpwuid_r (getuid (), &pw
, buf
, len
, &result
))
93 home_directory
= str_dup (result
->pw_dir
);
97 struct passwd
*result
= getpwuid (getuid ());
100 home_directory
= str_dup (result
->pw_dir
);
103 return home_directory
;
107 expand_homedir (char *str
)
112 return str_asprintf ("%s%s", get_home_dir (), p
);
114 return str_dup (str
);
118 parse_options (char **line
, struct argv_s
* args
[], void *data
)
131 if (*p
== '-' && *(p
+ 1) == '-')
134 char opt
[255] = { 0 }, value
[255] =
140 if (!*p
|| *p
== ' ')
148 for (tp
= opt
, len
= 0; *p
; p
++, len
++)
151 return GPG_ERR_LINE_TOO_LONG
;
153 if (*p
== ' ' || *p
== '=')
163 for (tp
= value
, len
= 0; *p
; p
++, len
++)
166 return GPG_ERR_LINE_TOO_LONG
;
173 else if (*p
== ' ' && !inquote
)
191 for (int i
= 0; args
[i
]; i
++)
193 if (!strcmp (args
[i
]->opt
, opt
))
195 log_write1 ("param: name='%s' value='%s'", opt
, value
);
196 if (args
[i
]->type
== OPTION_TYPE_NOARG
&& *value
)
197 return GPG_ERR_SYNTAX
;
198 else if (args
[i
]->type
== OPTION_TYPE_ARG
&& !*value
)
199 return GPG_ERR_SYNTAX
;
201 rc
= args
[i
]->func (data
, value
);
212 return GPG_ERR_UNKNOWN_OPTION
;
228 bin2hex (const unsigned char *data
, size_t len
)
230 size_t size
= len
* 2 + 1;
236 for (n
= 0; n
< len
; n
++)
240 sprintf (c
, "%02X", data
[n
]);
244 return str_dup (buf
);
248 plus_escape (const char *fmt
, ...)
255 if (str_vasprintf (&str
, fmt
, ap
) > 0)
259 for (p
= str
; *p
; p
++)
274 strip_texi (const char *str
)
277 char *s
= str_dup (str
);
283 for (p
= str
; *p
; p
++)
285 if (*p
== '@' && *(p
+ 1) != '@')
287 if (!strncasecmp (p
+ 1, "table", 5)
288 || !strncasecmp (p
+ 1, "end ", 4))
290 while (*p
++ != '\n');
294 else if (!strncasecmp (p
+ 1, "example", 7)
295 || !strncasecmp (p
+ 1, "end ", 4))
297 while (*p
++ != '\n');
301 else if (!strncasecmp (p
+ 1, "sp ", 3))
305 while (*p
&& isdigit (*p
++))
309 else if (!strncasecmp (p
+ 1, "item", 4))
312 while (*p
&& *p
!= '\n')
317 else if (!strncasecmp (p
+ 1, "pxref", 5))
324 else if (!strncasecmp (p
+ 1, "xref", 4))
332 while (*p
&& *p
!= '{')
346 while (*p
&& *p
!= '}')
355 else if (*p
== '@' && *(p
+ 1) == '@')
371 strip_texi_and_wrap (const char *str
)
373 char *tmp
= strip_texi (str
);
374 char *help
= xmalloc (strlen (tmp
+ 1) * 2);
378 for (p
= tmp
, ph
= help
, i
= 0; *p
; p
++, i
++)
380 if (i
== 78 || *p
== '\n')
387 while (!(*--t
== ' '))
403 while (*p
!= '\n' && *p
== ' ')
417 free_key (void *data
)
423 create_thread (void *(*cb
) (void *), void *data
,
424 pthread_t
* tid
, int detached
)
429 pthread_attr_init (&attr
);
432 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
434 n
= pthread_create (tid
, &attr
, cb
, data
);
435 pthread_attr_destroy (&attr
);
436 return gpg_error_from_errno (n
);
440 cleanup_mutex_cb (void *arg
)
442 pthread_mutex_t
*m
= (pthread_mutex_t
*) arg
;
448 cleanup_fd_cb (void *arg
)
450 int fd
= *(int *) arg
;
457 cleanup_unlink_cb (void *arg
)
466 cleanup_cache_mutex (void *arg
)
472 valid_keygrip (const unsigned char *data
, size_t len
)
474 for (size_t i
= 0; i
< len
; i
++)
484 get_checksum (const char *filename
, unsigned char **r_crc
, size_t * r_crclen
)
492 if (stat (filename
, &st
) == -1)
493 return gpg_error_from_errno (errno
);
495 fd
= open (filename
, O_RDONLY
);
497 return gpg_error_from_errno (errno
);
499 pthread_cleanup_push (cleanup_fd_cb
, &fd
);
500 buf
= xmalloc (st
.st_size
);
503 pthread_cleanup_push (xfree
, buf
);
505 len
= read (fd
, buf
, st
.st_size
);
506 if (len
== st
.st_size
)
512 len
= gcry_md_get_algo_dlen (GCRY_MD_CRC32
);
516 pthread_cleanup_push (xfree
, crc
);
517 gcry_md_hash_buffer (GCRY_MD_CRC32
, crc
, buf
, st
.st_size
);
520 pthread_cleanup_pop (0);
527 rc
= GPG_ERR_TOO_SHORT
;
529 pthread_cleanup_pop (1);
534 pthread_cleanup_pop (1);