2 Copyright (C) 2006-2018 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Pwmd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
34 #include "pwmd-error.h"
36 #include "util-misc.h"
37 #include "util-string.h"
42 void log_write (const char *fmt
, ...);
45 valid_filename (const char *filename
)
49 if (!filename
|| !*filename
)
52 if (filename
[0] == '-' && filename
[1] == 0)
55 for (p
= filename
; *p
; p
++)
57 if (*p
== '/' || isspace (*p
))
64 /* If 'user' is null then lookup entry for 'uid'. Caller must free 'buf'. */
66 get_pwd_struct (const char *user
, uid_t uid
, struct passwd
*pw
, char **buf
,
69 struct passwd
*result
= NULL
;
70 #ifdef HAVE_GETPWNAM_R
74 len
= sysconf (_SC_GETPW_R_SIZE_MAX
);
83 err
= getpwnam_r (user
, pw
, *buf
, len
, &result
);
85 err
= getpwuid_r (uid
, pw
, *buf
, len
, &result
);
93 *rc
= gpg_error_from_errno (err
);
101 result
= getpwnam (user
);
103 result
= getpwuid (uid
);
106 *rc
= gpg_error_from_syserror ();
113 get_pwd_entry(uid_t uid
, int which
)
118 struct passwd
*result
= get_pwd_struct (NULL
, uid
, &pw
, &buf
, &rc
);
127 value
= str_dup (result
->pw_dir
);
129 value
= str_dup (result
->pw_name
);
137 get_username (uid_t uid
)
139 return get_pwd_entry (uid
, 1);
146 return home_directory
;
148 home_directory
= get_pwd_entry (getuid(), 0);
149 return home_directory
;
153 expand_homedir (char *str
)
159 char *dir
= get_pwd_entry(getuid(), 0);
165 s
= str_asprintf ("%s%s", dir
, p
);
170 return str_dup (str
);
173 /* The 'more' parameter lets the calling command process remaining
174 * non-option arguments. */
176 parse_options (char **line
, struct argv_s
* args
[], void *data
, int more
)
189 if (*p
== '-' && *(p
+ 1) == '-')
192 char opt
[255] = { 0 }, value
[255] =
198 if (!*p
|| *p
== ' ')
206 for (tp
= opt
, len
= 0; *p
; p
++, len
++)
209 return GPG_ERR_LINE_TOO_LONG
;
211 if (*p
== ' ' || *p
== '=')
221 for (tp
= value
, len
= 0; *p
; p
++, len
++)
224 return GPG_ERR_LINE_TOO_LONG
;
231 else if (*p
== ' ' && !inquote
)
249 for (int i
= 0; args
[i
]; i
++)
251 if (!strcmp (args
[i
]->opt
, opt
))
253 log_write2 ("param: name='%s' value='%s'", opt
, value
);
254 if (args
[i
]->type
== OPTION_TYPE_NOARG
&& *value
)
255 return GPG_ERR_SYNTAX
;
256 else if (args
[i
]->type
== OPTION_TYPE_ARG
&& !*value
)
257 return GPG_ERR_SYNTAX
;
261 rc
= args
[i
]->func (data
, value
);
272 return GPG_ERR_UNKNOWN_OPTION
;
281 return GPG_ERR_SYNTAX
;
291 bin2hex (const unsigned char *data
, size_t len
)
293 size_t size
= len
* 2 + 1;
299 for (n
= 0; n
< len
; n
++)
303 sprintf (c
, "%02X", data
[n
]);
307 return str_dup (buf
);
311 html_escape (const char *line
, size_t len
, int space
)
313 struct string_s
*string
= string_new ("");
320 for (n
= 0; n
< len
; n
++)
325 string
= string_append (string
, "<");
328 string
= string_append (string
, ">");
333 string
= string_append (string
, "%20");
337 string
= string_append_printf (string
, "%c", line
[n
]);
343 string_free (string
, 0);
348 strip_texi_html (const char *line
)
353 struct string_s
*string
= string_new ("<html><body>");
358 /* The command usage text. */
359 p
= strchr (line
, '\n');
362 len
= strlen (line
)-strlen(p
);
363 tmp
= html_escape (line
, len
, 0);
364 string
= string_append_printf (string
, "<b>%s</b><p>", tmp
);
379 if (*p
== '@' && *(p
+ 1) != '@')
383 if (!strncasecmp (p
, "end ", 4))
386 if (!strncasecmp (p
, "table", 5))
388 string
= string_append (string
, "</p>");
390 else if (!strncasecmp (p
, "example", 7))
392 string
= string_append (string
, "</code>");
395 while (*p
++ != '\n');
399 else if (!strncasecmp (p
, "table ", 6))
401 while (*p
++ != '\n');
405 else if (!strncasecmp (p
, "example", 7))
407 string
= string_append (string
, "<code>");
408 while (*p
++ != '\n');
412 else if (!strncasecmp (p
, "sp ", 3))
414 while (*p
++ != '\n');
416 string
= string_append (string
, "<p>");
419 else if (!strncasecmp (p
, "item ", 5))
422 string
= string_append (string
, "<p><b>");
423 tmp
= strchr (p
, '\n');
424 len
= strlen (p
) - strlen (tmp
);
429 string
= string_append (string
, "<");
434 string
= string_append (string
, ">");
438 string
= string_append_printf (string
, "%c", *p
++);
441 string
= string_append (string
, "</b><br/>");
444 else if (!strncasecmp (p
, "pxref", 5)
445 || !strncasecmp (p
, "npxref", 6))
449 if (!strncasecmp (p
, "npxref", 6))
454 string
= string_append_printf (string
, "%s<a href=\"",
458 else if (!strncasecmp (p
, "xref", 4))
462 string
= string_append (string
, "See <a href=\"");
465 else if (!strncasecmp (p
, "url", 3))
469 string
= string_append (string
, "<a href=\"");
472 else if (!strncasecmp (p
, "key", 3))
477 else if (!strncasecmp (p
, "file", 4))
481 string
= string_append (string
, "<var>");
484 else if (!strncasecmp (p
, "var", 3))
488 string
= string_append (string
, "<var>");
491 else if (!strncasecmp (p
, "option", 6))
495 string
= string_append (string
, "<var>");
498 else if (!strncasecmp (p
, "code", 4))
502 string
= string_append (string
, "<b>");
505 else if (!strncasecmp (p
, "emph", 4))
509 string
= string_append (string
, "<em>");
512 else if (!strncasecmp (p
, "command", 7))
516 string
= string_append (string
, "<em>");
519 else if (!strncasecmp (p
, "cite", 4))
523 string
= string_append (string
, "<cite>");
526 else if (!strncasecmp (p
, "abbr", 4))
530 string
= string_append (string
, "<em>");
533 else if (!strncasecmp (p
, "*", 1))
535 string
= string_append (string
, "<br/>");
539 while (*p
&& *p
!= '{')
553 tmp
= strchr (p
, '}');
554 len
= strlen (p
) - strlen (tmp
);
566 tmp
= html_escape (buf
, strlen (buf
), 1);
567 string
= string_append_printf (string
, "%s\">%s</a>", tmp
, buf
);
571 string
= string_append (string
, buf
);
574 string
= string_append (string
, "</var>");
576 string
= string_append (string
, "</b>");
578 string
= string_append (string
, "</em>");
580 string
= string_append (string
, "</cite>");
583 else if (*p
== '@' && *(p
+ 1) == '@')
587 string
= string_append_printf (string
, "%c", *p
);
593 string
= string_append (string
, "</body></html>");
595 string_free (string
, 0);
600 strip_texi (const char *str
, int html
)
607 return strip_texi_html (str
);
613 for (p
= str
; *p
; p
++)
615 if (*p
== '@' && *(p
+ 1) != '@')
619 if (!strncasecmp (p
, "table", 5)
620 || !strncasecmp (p
, "end ", 4))
622 while (*p
++ != '\n');
626 else if (!strncasecmp (p
, "example", 7)
627 || !strncasecmp (p
, "end ", 4))
629 while (*p
++ != '\n');
633 else if (!strncasecmp (p
, "sp ", 3))
635 while (*p
++ != '\n');
639 else if (!strncasecmp (p
, "item", 4))
642 while (*p
&& *p
!= '\n')
648 else if (!strncasecmp (p
, "pxref", 5))
655 else if (!strncasecmp (p
, "xref", 4))
662 else if (!strncasecmp (p
, "*", 1))
669 while (*p
&& *p
!= '{')
683 while (*p
&& *p
!= '}')
693 else if (*p
== '@' && *(p
+ 1) == '@')
709 strip_texi_and_wrap (const char *str
, int html
)
711 char *tmp
= strip_texi (str
, html
);
712 char *help
= xcalloc (1, strlen (tmp
) * 2 + 1);
716 for (p
= tmp
, ph
= help
, i
= 0; *p
; p
++, i
++)
718 if (i
== 78 || *p
== '\n')
725 while (*(--t
) != ' ')
741 while (*p
!= '\n' && *p
== ' ')
757 free_key (void *data
)
763 create_thread (void *(*cb
) (void *), void *data
,
764 pthread_t
* tid
, int detached
)
769 pthread_attr_init (&attr
);
772 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
774 n
= pthread_create (tid
, &attr
, cb
, data
);
775 pthread_attr_destroy (&attr
);
776 return gpg_error_from_errno (n
);
780 release_mutex_cb (void *arg
)
782 pthread_mutex_t
*m
= (pthread_mutex_t
*) arg
;
788 close_fd_cb (void *arg
)
790 int fd
= *(int *)arg
;
797 get_checksum_memory (void *data
, size_t size
, unsigned char **r_crc
,
800 size_t len
= gcry_md_get_algo_dlen (GCRY_MD_CRC32
);
801 unsigned char *crc
= xmalloc (len
);
807 return GPG_ERR_ENOMEM
;
809 gcry_md_hash_buffer (GCRY_MD_CRC32
, crc
, data
, size
);
815 /* The advisory lock should be obtained before calling this function. */
817 get_checksum (const char *filename
, unsigned char **r_crc
, size_t * r_crclen
)
824 rc
= open_check_file (filename
, &fd
, &st
, 1);
828 pthread_cleanup_push (close_fd_cb
, &fd
);
829 buf
= xmalloc (st
.st_size
);
832 pthread_cleanup_push (xfree
, buf
);
834 size_t len
= read (fd
, buf
, st
.st_size
);
835 if (len
== st
.st_size
)
838 rc
= get_checksum_memory (buf
, st
.st_size
, r_crc
, r_crclen
);
843 rc
= GPG_ERR_TOO_SHORT
;
845 pthread_cleanup_pop (1);
850 pthread_cleanup_pop (1);
854 wchar_t *str_to_wchar (const char *str
)
861 memset (&ps
, 0, sizeof(mbstate_t));
862 len
= mbsrtowcs (NULL
, &p
, 0, &ps
);
866 wc
= xcalloc (len
+1, sizeof(wchar_t));
871 memset (&ps
, 0, sizeof(mbstate_t));
872 len
= mbsrtowcs (wc
, &p
, len
, &ps
);
883 gnupg_escape (const char *str
)
888 char *buf
= xmalloc (strlen(str
)*4+1), *b
= buf
;
894 for (p
= str
; p
&& *p
; p
++)
920 /* From Beej's Guide to Network Programming. It's a good tutorial. */
922 get_in_addr (struct sockaddr
*sa
)
924 if (sa
->sa_family
== AF_INET
)
925 return &(((struct sockaddr_in
*) sa
)->sin_addr
);
927 return &(((struct sockaddr_in6
*) sa
)->sin6_addr
);
931 open_check_file (const char *filename
, int *r_fd
, struct stat
*r_st
, int reg
)
940 if (lstat (filename
, &st
) == -1)
941 return gpg_err_code (gpg_error_from_syserror ());
943 if (!S_ISREG (st
.st_mode
))
944 return GPG_ERR_ENODEV
;
947 memcpy (r_st
, &st
, sizeof (struct stat
));
952 fd
= open (filename
, O_RDONLY
);
954 return gpg_err_code (gpg_error_from_syserror ());
956 if (r_st
&& fstat (fd
, r_st
) == -1)
958 rc
= gpg_err_code (gpg_error_from_syserror ());