Revert "Remove pinentry.conf support."
[libpwmd.git] / src / misc.c
blob0bbd92b1c6b9b7e20f6c7a18d5d4c82eea61c781
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
3 2016
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of libpwmd.
8 Libpwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Libpwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Libpwmd. If not, see <http://www.gnu.org/licenses/>.
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #ifdef HAVE_LIMITS_H
26 #include <limits.h>
27 #endif
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <ctype.h>
39 #ifndef LINE_MAX
40 #define LINE_MAX 2048
41 #endif
43 #include <libpwmd.h>
44 #include "misc.h"
46 #ifdef HAVE_GETPWUID_R
47 char *
48 _expand_homedir (char *str, struct passwd *pw)
50 char *p = str;
51 char *pwbuf = NULL;
52 char *result;
53 struct passwd t;
55 if (!p)
56 return NULL;
58 if (*p != '~' || *(p + 1) != '/')
59 return pwmd_strdup (p);
61 if (!pw)
63 pwbuf = _getpwuid (&t);
64 if (!pwbuf)
65 return NULL;
67 pw = &t;
70 p += 2;
71 result = pwmd_strdup_printf ("%s/%s", pw->pw_dir, p);
72 pwmd_free (pwbuf);
73 return result;
76 char *
77 _getpwuid (struct passwd *pwd)
79 size_t size = sysconf (_SC_GETPW_R_SIZE_MAX);
80 struct passwd *result;
81 char *buf;
82 int n;
84 if (size == -1)
85 size = 16384;
87 buf = pwmd_malloc (size);
88 if (!buf)
89 return NULL;
91 n = getpwuid_r (getuid (), pwd, buf, size, &result);
92 if (n)
94 pwmd_free (buf);
95 errno = n;
96 return NULL;
99 if (!result)
101 pwmd_free (buf);
102 return NULL;
105 errno = n;
106 return buf;
108 #else
109 char *
110 _expand_homedir (char *str, struct passwd *pw)
112 char *p = str;
114 if (!p)
115 return NULL;
117 if (*p != '~' || *(p + 1) != '/')
118 return pwmd_strdup (p);
120 if (!pw)
122 pw = getpwuid (getuid ());
123 if (!pw)
124 return NULL;
127 p += 2;
128 return pwmd_strdup_printf ("%s/%s", pw->pw_dir, p);
131 char *
132 _getpwuid (struct passwd *pwd)
134 struct passwd *pw = getpwuid (getuid ());
136 if (!pw)
137 return NULL;
139 *pwd = *pw;
140 return pwmd_strdup ("");
142 #endif
145 * Borrowed from libassuan.
147 char *
148 _percent_escape (const char *atext)
150 const unsigned char *s;
151 int len;
152 char *buf, *p;
154 if (!atext)
155 return NULL;
157 len = strlen (atext) * 3 + 1;
158 buf = (char *) pwmd_malloc (len);
160 if (!buf)
161 return NULL;
163 p = buf;
165 for (s = (const unsigned char *) atext; *s; s++)
167 if (*s < ' ')
169 sprintf (p, "%%%02X", *s);
170 p += 3;
172 else
173 *p++ = *s;
176 *p = 0;
177 return buf;
180 void
181 update_pinentry_settings (pwm_t * pwm)
183 FILE *fp;
184 char buf[LINE_MAX];
185 char *p;
186 struct passwd pw;
187 char *pwbuf = _getpwuid (&pw);
189 if (!pwbuf)
190 return;
192 snprintf (buf, sizeof (buf), "%s/.pwmd/pinentry.conf", pw.pw_dir);
194 if ((fp = fopen (buf, "r")) == NULL)
196 pwmd_free (pwbuf);
197 return;
200 while ((p = fgets (buf, sizeof (buf), fp)) != NULL)
202 char name[32] = {0}, val[256] = {0};
204 if (sscanf (p, " %31[a-zA-Z] = %255s", name, val) != 2)
205 continue;
207 if (strcasecmp (name, "TTYNAME") == 0)
209 pwmd_free (pwm->pinentry_tty);
210 pwm->pinentry_tty = pwmd_strdup (val);
211 if (!pwm->pinentry_tty)
212 goto fail;
214 else if (strcasecmp (name, "TTYTYPE") == 0)
216 pwmd_free (pwm->pinentry_term);
217 pwm->pinentry_term = pwmd_strdup (val);
218 if (!pwm->pinentry_term)
219 goto fail;
221 else if (strcasecmp (name, "DISPLAY") == 0)
223 pwmd_free (pwm->pinentry_display);
224 pwm->pinentry_display = pwmd_strdup (val);
225 if (!pwm->pinentry_display)
226 goto fail;
228 else if (strcasecmp (name, "PATH") == 0)
230 pwmd_free (pwm->pinentry_path);
231 pwm->pinentry_path = _expand_homedir (val, &pw);
232 if (!pwm->pinentry_path)
233 goto fail;
235 else if (strcasecmp (name, "LC_MESSAGES") == 0)
237 pwmd_free (pwm->pinentry_lcmessages);
238 pwm->pinentry_lcmessages = pwmd_strdup (val);
239 if (!pwm->pinentry_lcmessages)
240 goto fail;
242 else if (strcasecmp (name, "LC_CTYPE") == 0)
244 pwmd_free (pwm->pinentry_lcctype);
245 pwm->pinentry_lcctype = pwmd_strdup (val);
246 if (!pwm->pinentry_lcctype)
247 goto fail;
251 fail:
252 pwmd_free (pwbuf);
253 fclose (fp);
256 /* Common hostname parsing for urls. Handles both IPv4 and IPv6 hostname and
257 * port specification.
259 gpg_error_t
260 parse_hostname_common (const char *str, char **host, int *port)
262 const char *p = str;
263 char *t = NULL;
265 /* IPv6 with optional port. */
266 if (*p == '[')
268 p++;
269 t = strchr (p, ']');
271 else
273 int n = 0;
274 const char *x;
276 /* Handle IPv6 without proper braces around the IP. */
277 for (x = p; *x; x++)
279 if (*x == ':')
280 n++;
283 if (n <= 1)
284 t = strchr (p, ':');
287 if (t)
289 size_t len = strlen (p) - strlen (t) + 1;
291 *host = pwmd_malloc (len);
292 if (!*host)
293 return gpg_error_from_errno (ENOMEM);
295 snprintf (*host, len, "%s", p);
296 t++;
298 if (*t == ':')
299 t++;
301 if (*t)
302 *port = strtol (t, NULL, 10);
304 if (*t == '-')
305 t++;
307 while (*t && isdigit (*t))
308 t++;
310 p = t;
312 else
314 *host = pwmd_strdup (str);
315 if (!*host)
316 return gpg_error_from_errno (ENOMEM);
319 return 0;
322 char *
323 bin2hex (const unsigned char *data, size_t len)
325 size_t size = len * 2 + 1;
326 char buf[size]; // C99
327 size_t n;
329 buf[0] = 0;
331 for (n = 0; n < len; n++)
333 char c[3];
335 sprintf (c, "%02X", data[n]);
336 strcat (buf, c);
339 return pwmd_strdup (buf);