commit
51da5915b0
@ -0,0 +1,28 @@
|
|||||||
|
MIT/X Consortium License
|
||||||
|
|
||||||
|
© 2006-2014 Anselm R Garbe <anselm@garbe.us>
|
||||||
|
© 2010-2012 Connor Lane Smith <cls@lubutu.com>
|
||||||
|
© 2009 Gottox <gottox@s01.de>
|
||||||
|
© 2009 Markus Schnalke <meillo@marmaro.de>
|
||||||
|
© 2009 Evan Gates <evan.gates@gmail.com>
|
||||||
|
© 2006-2008 Sander van Dijk <a dot h dot vandijk at gmail dot com>
|
||||||
|
© 2006-2007 Michał Janeczek <janeczek at gmail dot com>
|
||||||
|
© 2014-2015 Hiltjo Posthuma <hiltjo@codemadness.org>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
copy of this software and associated documentation files (the "Software"),
|
||||||
|
to deal in the Software without restriction, including without limitation
|
||||||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,35 @@
|
|||||||
|
# spine - dmenu-like stupid pin entry
|
||||||
|
# See LICENSE file for copyright and license details.
|
||||||
|
|
||||||
|
include config.mk
|
||||||
|
|
||||||
|
SRC = spine.c drw.c util.c
|
||||||
|
OBJ = ${SRC:.c=.o}
|
||||||
|
|
||||||
|
all: options spine
|
||||||
|
|
||||||
|
options:
|
||||||
|
@echo spine build options:
|
||||||
|
@echo "CFLAGS = ${CFLAGS}"
|
||||||
|
@echo "LDFLAGS = ${LDFLAGS}"
|
||||||
|
@echo "CC = ${CC}"
|
||||||
|
|
||||||
|
.c.o:
|
||||||
|
@echo CC $<
|
||||||
|
@${CC} -c ${CFLAGS} $<
|
||||||
|
|
||||||
|
config.h:
|
||||||
|
@echo creating $@ from config.def.h
|
||||||
|
@cp config.def.h $@
|
||||||
|
|
||||||
|
${OBJ}: config.h config.mk drw.h
|
||||||
|
|
||||||
|
spine: spine.o drw.o util.o
|
||||||
|
@echo CC -o $@
|
||||||
|
@${CC} -o $@ spine.o drw.o util.o pinentry/pinentry.o pinentry/util.o pinentry/password-cache.o pinentry/argparse.o pinentry/secmem.o ${LDFLAGS} -lassuan -lgpgme -lgpg-error
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@echo cleaning
|
||||||
|
@rm -f spine ${OBJ}
|
||||||
|
|
||||||
|
.PHONY: all options clean
|
@ -0,0 +1,5 @@
|
|||||||
|
spine - stupid pinentry replacement
|
||||||
|
|
||||||
|
I didn't like how long it takes to load pinentry-gtk/qt and pinentry-curses/tty is not always the option, so I duct-taped pinentry and dmenu together.
|
||||||
|
|
||||||
|
Right now it's a real mess, so don't expect much, but I w-will work on it some more I promise.
|
@ -0,0 +1,3 @@
|
|||||||
|
* replace pinentry's code with our own talker to gpg-agent
|
||||||
|
* look over code and see what I broke while copypasting code from dmenu
|
||||||
|
* get licenses in order
|
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Copy me if you can.
|
||||||
|
* by 20h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __ARG_H__
|
||||||
|
#define __ARG_H__
|
||||||
|
|
||||||
|
extern char *argv0;
|
||||||
|
|
||||||
|
#define USED(x) ((void)(x))
|
||||||
|
|
||||||
|
#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
|
||||||
|
argv[0] && argv[0][1]\
|
||||||
|
&& argv[0][0] == '-';\
|
||||||
|
argc--, argv++) {\
|
||||||
|
char _argc;\
|
||||||
|
char **_argv;\
|
||||||
|
if (argv[0][1] == '-' && argv[0][2] == '\0') {\
|
||||||
|
argv++;\
|
||||||
|
argc--;\
|
||||||
|
break;\
|
||||||
|
}\
|
||||||
|
for (argv[0]++, _argv = argv; argv[0][0];\
|
||||||
|
argv[0]++) {\
|
||||||
|
if (_argv != argv)\
|
||||||
|
break;\
|
||||||
|
_argc = argv[0][0];\
|
||||||
|
switch (_argc)
|
||||||
|
|
||||||
|
#define ARGEND }\
|
||||||
|
USED(_argc);\
|
||||||
|
}\
|
||||||
|
USED(argv);\
|
||||||
|
USED(argc);
|
||||||
|
|
||||||
|
#define EARGF(x) ((argv[1] == NULL)? ((x), abort(), (char *)0) :\
|
||||||
|
(argc--, argv++, argv[0]))
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -0,0 +1,12 @@
|
|||||||
|
/* See LICENSE file for copyright and license details. */
|
||||||
|
static Bool topbar = True;
|
||||||
|
static const char *fonts[]={
|
||||||
|
"monospace:size=8"
|
||||||
|
};
|
||||||
|
static char *secchar = "*";
|
||||||
|
static char *description = NULL;
|
||||||
|
static char *prompt = "PIN:";
|
||||||
|
static const char *normbgcolor = "#000000";
|
||||||
|
static const char *normfgcolor = "#ffffff";
|
||||||
|
static const char *selbgcolor = "#ff0000";
|
||||||
|
static const char *selfgcolor = "#ffffff";
|
@ -0,0 +1,31 @@
|
|||||||
|
# spine version
|
||||||
|
VERSION = 0.1
|
||||||
|
|
||||||
|
# paths
|
||||||
|
PREFIX = /usr/local
|
||||||
|
MANPREFIX = ${PREFIX}/share/man
|
||||||
|
|
||||||
|
X11INC = /usr/X11R6/include
|
||||||
|
X11LIB = /usr/X11R6/lib
|
||||||
|
|
||||||
|
# Xinerama, comment if you don't want it
|
||||||
|
XINERAMALIBS = -lXinerama
|
||||||
|
XINERAMAFLAGS = -DXINERAMA
|
||||||
|
|
||||||
|
# freetype
|
||||||
|
FREETYPELIBS = -lfontconfig -lXft
|
||||||
|
FREETYPEINC = /usr/include/freetype2
|
||||||
|
# OpenBSD (uncomment)
|
||||||
|
#FREETYPEINC = ${X11INC}/freetype2
|
||||||
|
|
||||||
|
# includes and libs
|
||||||
|
INCS = -I${X11INC} -I${FREETYPEINC}
|
||||||
|
LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS}
|
||||||
|
|
||||||
|
# flags
|
||||||
|
CPPFLAGS = -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
|
||||||
|
CFLAGS = -ansi -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
|
||||||
|
LDFLAGS = -s ${LIBS}
|
||||||
|
|
||||||
|
# compiler and linker
|
||||||
|
CC = cc
|
@ -0,0 +1,413 @@
|
|||||||
|
/* See LICENSE file for copyright and license details. */
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <X11/Xlib.h>
|
||||||
|
#include <X11/Xft/Xft.h>
|
||||||
|
|
||||||
|
#include "drw.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#define UTF_INVALID 0xFFFD
|
||||||
|
#define UTF_SIZ 4
|
||||||
|
|
||||||
|
static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
|
||||||
|
static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
|
||||||
|
static const long utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
|
||||||
|
static const long utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
|
||||||
|
|
||||||
|
static long
|
||||||
|
utf8decodebyte(const char c, size_t *i) {
|
||||||
|
for(*i = 0; *i < (UTF_SIZ + 1); ++(*i))
|
||||||
|
if(((unsigned char)c & utfmask[*i]) == utfbyte[*i])
|
||||||
|
return (unsigned char)c & ~utfmask[*i];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
utf8validate(long *u, size_t i) {
|
||||||
|
if(!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
|
||||||
|
*u = UTF_INVALID;
|
||||||
|
for(i = 1; *u > utfmax[i]; ++i)
|
||||||
|
;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
utf8decode(const char *c, long *u, size_t clen) {
|
||||||
|
size_t i, j, len, type;
|
||||||
|
long udecoded;
|
||||||
|
|
||||||
|
*u = UTF_INVALID;
|
||||||
|
if(!clen)
|
||||||
|
return 0;
|
||||||
|
udecoded = utf8decodebyte(c[0], &len);
|
||||||
|
if(!BETWEEN(len, 1, UTF_SIZ))
|
||||||
|
return 1;
|
||||||
|
for(i = 1, j = 1; i < clen && j < len; ++i, ++j) {
|
||||||
|
udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
|
||||||
|
if(type != 0)
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
if(j < len)
|
||||||
|
return 0;
|
||||||
|
*u = udecoded;
|
||||||
|
utf8validate(u, len);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
Drw *
|
||||||
|
drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) {
|
||||||
|
Drw *drw = (Drw *)calloc(1, sizeof(Drw));
|
||||||
|
if(!drw)
|
||||||
|
return NULL;
|
||||||
|
drw->dpy = dpy;
|
||||||
|
drw->screen = screen;
|
||||||
|
drw->root = root;
|
||||||
|
drw->w = w;
|
||||||
|
drw->h = h;
|
||||||
|
drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
|
||||||
|
drw->gc = XCreateGC(dpy, root, 0, NULL);
|
||||||
|
drw->fontcount = 0;
|
||||||
|
XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
|
||||||
|
return drw;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
drw_resize(Drw *drw, unsigned int w, unsigned int h) {
|
||||||
|
if(!drw)
|
||||||
|
return;
|
||||||
|
drw->w = w;
|
||||||
|
drw->h = h;
|
||||||
|
if(drw->drawable != 0)
|
||||||
|
XFreePixmap(drw->dpy, drw->drawable);
|
||||||
|
drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
drw_free(Drw *drw) {
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < drw->fontcount; i++) {
|
||||||
|
drw_font_free(drw->fonts[i]);
|
||||||
|
}
|
||||||
|
XFreePixmap(drw->dpy, drw->drawable);
|
||||||
|
XFreeGC(drw->dpy, drw->gc);
|
||||||
|
free(drw);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* This function is an implementation detail. Library users should use
|
||||||
|
* drw_font_create instead.
|
||||||
|
*/
|
||||||
|
static Fnt *
|
||||||
|
drw_font_xcreate(Drw *drw, const char *fontname, FcPattern *fontpattern) {
|
||||||
|
Fnt *font;
|
||||||
|
|
||||||
|
if (!(fontname || fontpattern))
|
||||||
|
die("No font specified.\n");
|
||||||
|
|
||||||
|
if (!(font = (Fnt *)calloc(1, sizeof(Fnt))))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (fontname) {
|
||||||
|
/* Using the pattern found at font->xfont->pattern does not yield same
|
||||||
|
* the same substitution results as using the pattern returned by
|
||||||
|
* FcNameParse; using the latter results in the desired fallback
|
||||||
|
* behaviour whereas the former just results in
|
||||||
|
* missing-character-rectangles being drawn, at least with some fonts.
|
||||||
|
*/
|
||||||
|
if (!(font->xfont = XftFontOpenName(drw->dpy, drw->screen, fontname)) ||
|
||||||
|
!(font->pattern = FcNameParse((FcChar8 *) fontname))) {
|
||||||
|
if (font->xfont) {
|
||||||
|
XftFontClose(drw->dpy, font->xfont);
|
||||||
|
font->xfont = NULL;
|
||||||
|
}
|
||||||
|
fprintf(stderr, "error, cannot load font: '%s'\n", fontname);
|
||||||
|
}
|
||||||
|
} else if (fontpattern) {
|
||||||
|
if (!(font->xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
|
||||||
|
fprintf(stderr, "error, cannot load font pattern.\n");
|
||||||
|
} else {
|
||||||
|
font->pattern = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!font->xfont) {
|
||||||
|
free(font);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
font->ascent = font->xfont->ascent;
|
||||||
|
font->descent = font->xfont->descent;
|
||||||
|
font->h = font->ascent + font->descent;
|
||||||
|
font->dpy = drw->dpy;
|
||||||
|
return font;
|
||||||
|
}
|
||||||
|
|
||||||
|
Fnt*
|
||||||
|
drw_font_create(Drw *drw, const char *fontname) {
|
||||||
|
return drw_font_xcreate(drw, fontname, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount) {
|
||||||
|
size_t i;
|
||||||
|
Fnt *font;
|
||||||
|
|
||||||
|
for (i = 0; i < fontcount; i++) {
|
||||||
|
if (drw->fontcount >= DRW_FONT_CACHE_SIZE) {
|
||||||
|
die("Font cache exhausted.\n");
|
||||||
|
} else if ((font = drw_font_xcreate(drw, fonts[i], NULL))) {
|
||||||
|
drw->fonts[drw->fontcount++] = font;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
drw_font_free(Fnt *font) {
|
||||||
|
if(!font)
|
||||||
|
return;
|
||||||
|
if(font->pattern)
|
||||||
|
FcPatternDestroy(font->pattern);
|
||||||
|
XftFontClose(font->dpy, font->xfont);
|
||||||
|
free(font);
|
||||||
|
}
|
||||||
|
|
||||||
|
Clr *
|
||||||
|
drw_clr_create(Drw *drw, const char *clrname) {
|
||||||
|
Clr *clr;
|
||||||
|
Colormap cmap;
|
||||||
|
Visual *vis;
|
||||||
|
|
||||||
|
if(!drw)
|
||||||
|
return NULL;
|
||||||
|
clr = (Clr *)calloc(1, sizeof(Clr));
|
||||||
|
if(!clr)
|
||||||
|
return NULL;
|
||||||
|
cmap = DefaultColormap(drw->dpy, drw->screen);
|
||||||
|
vis = DefaultVisual(drw->dpy, drw->screen);
|
||||||
|
if(!XftColorAllocName(drw->dpy, vis, cmap, clrname, &clr->rgb))
|
||||||
|
die("error, cannot allocate color '%s'\n", clrname);
|
||||||
|
clr->pix = clr->rgb.pixel;
|
||||||
|
return clr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
drw_clr_free(Clr *clr) {
|
||||||
|
free(clr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
drw_setscheme(Drw *drw, ClrScheme *scheme) {
|
||||||
|
if(!drw)
|
||||||
|
return;
|
||||||
|
drw->scheme = scheme;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert) {
|
||||||
|
if(!drw || !drw->scheme)
|
||||||
|
return;
|
||||||
|
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->bg->pix : drw->scheme->fg->pix);
|
||||||
|
if(filled)
|
||||||
|
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w + 1, h + 1);
|
||||||
|
else if(empty)
|
||||||
|
XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert) {
|
||||||
|
char buf[1024];
|
||||||
|
int tx, ty, th;
|
||||||
|
Extnts tex;
|
||||||
|
Colormap cmap;
|
||||||
|
Visual *vis;
|
||||||
|
XftDraw *d;
|
||||||
|
Fnt *curfont, *nextfont;
|
||||||
|
size_t i, len;
|
||||||
|
int utf8strlen, utf8charlen, render;
|
||||||
|
long utf8codepoint = 0;
|
||||||
|
const char *utf8str;
|
||||||
|
FcCharSet *fccharset;
|
||||||
|
FcPattern *fcpattern;
|
||||||
|
FcPattern *match;
|
||||||
|
XftResult result;
|
||||||
|
int charexists = 0;
|
||||||
|
|
||||||
|
if (!(render = x || y || w || h)) {
|
||||||
|
w = ~w;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!drw || !drw->scheme) {
|
||||||
|
return 0;
|
||||||
|
} else if (render) {
|
||||||
|
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme->fg->pix : drw->scheme->bg->pix);
|
||||||
|
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!text || !drw->fontcount) {
|
||||||
|
return 0;
|
||||||
|
} else if (render) {
|
||||||
|
cmap = DefaultColormap(drw->dpy, drw->screen);
|
||||||
|
vis = DefaultVisual(drw->dpy, drw->screen);
|
||||||
|
d = XftDrawCreate(drw->dpy, drw->drawable, vis, cmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
curfont = drw->fonts[0];
|
||||||
|
while (1) {
|
||||||
|
utf8strlen = 0;
|
||||||
|
utf8str = text;
|
||||||
|
nextfont = NULL;
|
||||||
|
while (*text) {
|
||||||
|
utf8charlen = utf8decode(text, &utf8codepoint, UTF_SIZ);
|
||||||
|
for (i = 0; i < drw->fontcount; i++) {
|
||||||
|
charexists = charexists || XftCharExists(drw->dpy, drw->fonts[i]->xfont, utf8codepoint);
|
||||||
|
if (charexists) {
|
||||||
|
if (drw->fonts[i] == curfont) {
|
||||||
|
utf8strlen += utf8charlen;
|
||||||
|
text += utf8charlen;
|
||||||
|
} else {
|
||||||
|
nextfont = drw->fonts[i];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!charexists || (nextfont && nextfont != curfont)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
charexists = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (utf8strlen) {
|
||||||
|
drw_font_getexts(curfont, utf8str, utf8strlen, &tex);
|
||||||
|
/* shorten text if necessary */
|
||||||
|
for(len = MIN(utf8strlen, (sizeof buf) - 1); len && (tex.w > w - drw->fonts[0]->h || w < drw->fonts[0]->h); len--)
|
||||||
|
drw_font_getexts(curfont, utf8str, len, &tex);
|
||||||
|
|
||||||
|
if (len) {
|
||||||
|
memcpy(buf, utf8str, len);
|
||||||
|
buf[len] = '\0';
|
||||||
|
if(len < utf8strlen)
|
||||||
|
for(i = len; i && i > len - 3; buf[--i] = '.');
|
||||||
|
|
||||||
|
if (render) {
|
||||||
|
th = curfont->ascent + curfont->descent;
|
||||||
|
ty = y + (h / 2) - (th / 2) + curfont->ascent;
|
||||||
|
tx = x + (h / 2);
|
||||||
|
XftDrawStringUtf8(d, invert ? &drw->scheme->bg->rgb : &drw->scheme->fg->rgb, curfont->xfont, tx, ty, (XftChar8 *)buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
x += tex.w;
|
||||||
|
w -= tex.w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!*text) {
|
||||||
|
break;
|
||||||
|
} else if (nextfont) {
|
||||||
|
charexists = 0;
|
||||||
|
curfont = nextfont;
|
||||||
|
} else {
|
||||||
|
/* Regardless of whether or not a fallback font is found, the
|
||||||
|
* character must be drawn.
|
||||||
|
*/
|
||||||
|
charexists = 1;
|
||||||
|
|
||||||
|
if (drw->fontcount >= DRW_FONT_CACHE_SIZE) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fccharset = FcCharSetCreate();
|
||||||
|
FcCharSetAddChar(fccharset, utf8codepoint);
|
||||||
|
|
||||||
|
if (!drw->fonts[0]->pattern) {
|
||||||
|
/* Refer to the comment in drw_font_xcreate for more
|
||||||
|
* information.
|
||||||
|
*/
|
||||||
|
die("The first font in the cache must be loaded from a font string.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
fcpattern = FcPatternDuplicate(drw->fonts[0]->pattern);
|
||||||
|
FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
|
||||||
|
FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
|
||||||
|
|
||||||
|
FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
|
||||||
|
FcDefaultSubstitute(fcpattern);
|
||||||
|
match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
|
||||||
|
|
||||||
|
FcCharSetDestroy(fccharset);
|
||||||
|
FcPatternDestroy(fcpattern);
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
curfont = drw_font_xcreate(drw, NULL, match);
|
||||||
|
if (curfont && XftCharExists(drw->dpy, curfont->xfont, utf8codepoint)) {
|
||||||
|
drw->fonts[drw->fontcount++] = curfont;
|
||||||
|
} else {
|
||||||
|
if (curfont) {
|
||||||
|
drw_font_free(curfont);
|
||||||
|
}
|
||||||
|
curfont = drw->fonts[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (render) {
|
||||||
|
XftDrawDestroy(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
|
||||||
|
if(!drw)
|
||||||
|
return;
|
||||||
|
XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
|
||||||
|
XSync(drw->dpy, False);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *tex) {
|
||||||
|
XGlyphInfo ext;
|
||||||
|
|
||||||
|
if(!font || !text)
|
||||||
|
return;
|
||||||
|
XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
|
||||||
|
tex->h = font->h;
|
||||||
|
tex->w = ext.xOff;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int
|
||||||
|
drw_font_getexts_width(Fnt *font, const char *text, unsigned int len) {
|
||||||
|
Extnts tex;
|
||||||
|
|
||||||
|
if(!font)
|
||||||
|
return -1;
|
||||||
|
drw_font_getexts(font, text, len, &tex);
|
||||||
|
return tex.w;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cur *
|
||||||
|
drw_cur_create(Drw *drw, int shape) {
|
||||||
|
Cur *cur;
|
||||||
|
|
||||||
|
if(!drw)
|
||||||
|
return NULL;
|
||||||
|
cur = (Cur *)calloc(1, sizeof(Cur));
|
||||||
|
if (!cur)
|
||||||
|
return NULL;
|
||||||
|
cur->cursor = XCreateFontCursor(drw->dpy, shape);
|
||||||
|
return cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
drw_cur_free(Drw *drw, Cur *cursor) {
|
||||||
|
if(!drw || !cursor)
|
||||||
|
return;
|
||||||
|
XFreeCursor(drw->dpy, cursor->cursor);
|
||||||
|
free(cursor);
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
/* See LICENSE file for copyright and license details. */
|
||||||
|
#define DRW_FONT_CACHE_SIZE 32
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
unsigned long pix;
|
||||||
|
XftColor rgb;
|
||||||
|
} Clr;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Cursor cursor;
|
||||||
|
} Cur;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Display *dpy;
|
||||||
|
int ascent;
|
||||||
|
int descent;
|
||||||
|
unsigned int h;
|
||||||
|
XftFont *xfont;
|
||||||
|
FcPattern *pattern;
|
||||||
|
} Fnt;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Clr *fg;
|
||||||
|
Clr *bg;
|
||||||
|
Clr *border;
|
||||||
|
} ClrScheme;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
unsigned int w, h;
|
||||||
|
Display *dpy;
|
||||||
|
int screen;
|
||||||
|
Window root;
|
||||||
|
Drawable drawable;
|
||||||
|
GC gc;
|
||||||
|
ClrScheme *scheme;
|
||||||
|
size_t fontcount;
|
||||||
|
Fnt *fonts[DRW_FONT_CACHE_SIZE];
|
||||||
|
} Drw;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
unsigned int w;
|
||||||
|
unsigned int h;
|
||||||
|
} Extnts;
|
||||||
|
|
||||||
|
/* Drawable abstraction */
|
||||||
|
Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h);
|
||||||
|
void drw_resize(Drw *drw, unsigned int w, unsigned int h);
|
||||||
|
void drw_free(Drw *drw);
|
||||||
|
|
||||||
|
/* Fnt abstraction */
|
||||||
|
Fnt *drw_font_create(Drw *drw, const char *fontname);
|
||||||
|
void drw_load_fonts(Drw* drw, const char *fonts[], size_t fontcount);
|
||||||
|
void drw_font_free(Fnt *font);
|
||||||
|
void drw_font_getexts(Fnt *font, const char *text, unsigned int len, Extnts *extnts);
|
||||||
|
unsigned int drw_font_getexts_width(Fnt *font, const char *text, unsigned int len);
|
||||||
|
|
||||||
|
/* Colour abstraction */
|
||||||
|
Clr *drw_clr_create(Drw *drw, const char *clrname);
|
||||||
|
void drw_clr_free(Clr *clr);
|
||||||
|
|
||||||
|
/* Cursor abstraction */
|
||||||
|
Cur *drw_cur_create(Drw *drw, int shape);
|
||||||
|
void drw_cur_free(Drw *drw, Cur *cursor);
|
||||||
|
|
||||||
|
/* Drawing context manipulation */
|
||||||
|
void drw_setfont(Drw *drw, Fnt *font);
|
||||||
|
void drw_setscheme(Drw *drw, ClrScheme *scheme);
|
||||||
|
|
||||||
|
/* Drawing functions */
|
||||||
|
void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert);
|
||||||
|
int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, const char *text, int invert);
|
||||||
|
|
||||||
|
/* Map functions */
|
||||||
|
void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h);
|
@ -0,0 +1,11 @@
|
|||||||
|
Program: Pinentry
|
||||||
|
Bug reports: <gpa-dev@gnupg.org>
|
||||||
|
Security related bug reports: <security@gnupg.org>
|
||||||
|
License: GPLv2+
|
||||||
|
|
||||||
|
Robert Bihlmeyer <robbe@orcus.priv.at>
|
||||||
|
Werner Koch, g10 Code GmbH <wk@gnupg.org>
|
||||||
|
Steffen Hansen, Klarälvdalens Datakonsult AB <steffen@klaralvdalens-datakonsult.se>
|
||||||
|
Marcus Brinkmann, g10 Code GmbH <marcus@g10code.com>
|
||||||
|
Timo Schulz, g10 Code GmbH
|
||||||
|
Neal Walfied, g10 Code GmbH <neal@gnu.org>
|
@ -0,0 +1,20 @@
|
|||||||
|
include ../config.mk
|
||||||
|
|
||||||
|
SRC = pinentry.c argparse.c password-cache.c
|
||||||
|
OBJ = ${SRC:.c=.o}
|
||||||
|
|
||||||
|
all: pinentry
|
||||||
|
|
||||||
|
.c.o:
|
||||||
|
@echo CC $<
|
||||||
|
@${CC} -c ${CFLAGS} $<
|
||||||
|
|
||||||
|
${OBJ}: pinentry.h argparse.h password-cache.h memory.h
|
||||||
|
|
||||||
|
pinentry: pinentry.o argparse.o password-cache.o secmem.o
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@echo cleaning
|
||||||
|
@rm -f ${OBJ}
|
||||||
|
|
||||||
|
.PHONY: all clean pinentry
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,203 @@
|
|||||||
|
/* argparse.h - Argument parser for option handling.
|
||||||
|
* Copyright (C) 1998,1999,2000,2001,2006 Free Software Foundation, Inc.
|
||||||
|
*
|
||||||
|
* This file is part of JNLIB, which is a subsystem of GnuPG.
|
||||||
|
*
|
||||||
|
* JNLIB is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of either
|
||||||
|
*
|
||||||
|
* - the GNU Lesser General Public License as published by the Free
|
||||||
|
* Software Foundation; either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* or
|
||||||
|
*
|
||||||
|
* - the GNU General Public License as published by the Free
|
||||||
|
* Software Foundation; either version 2 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* or both in parallel, as here.
|
||||||
|
*
|
||||||
|
* JNLIB is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copies of the GNU General Public License
|
||||||
|
* and the GNU Lesser General Public License along with this program;
|
||||||
|
* if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIBJNLIB_ARGPARSE_H
|
||||||
|
#define LIBJNLIB_ARGPARSE_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int *argc; /* Pointer to ARGC (value subject to change). */
|
||||||
|
char ***argv; /* Pointer to ARGV (value subject to change). */
|
||||||
|
unsigned int flags; /* Global flags. May be set prior to calling the
|
||||||
|
parser. The parser may change the value. */
|
||||||
|
int err; /* Print error description for last option.
|
||||||
|
Either 0, ARGPARSE_PRINT_WARNING or
|
||||||
|
ARGPARSE_PRINT_ERROR. */
|
||||||
|
|
||||||
|
int r_opt; /* Returns option code. */
|
||||||
|
int r_type; /* Returns type of option value. */
|
||||||
|
union {
|
||||||
|
int ret_int;
|
||||||
|
long ret_long;
|
||||||
|
unsigned long ret_ulong;
|
||||||
|
char *ret_str;
|
||||||
|
} r; /* Return values */
|
||||||
|
|
||||||
|
struct {
|
||||||
|
int idx;
|
||||||
|
int inarg;
|
||||||
|
int stopped;
|
||||||
|
const char *last;
|
||||||
|
void *aliases;
|
||||||
|
const void *cur_alias;
|
||||||
|
void *iio_list;
|
||||||
|
} internal; /* Private - do not change. */
|
||||||
|
} ARGPARSE_ARGS;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int short_opt;
|
||||||
|
const char *long_opt;
|
||||||
|
unsigned int flags;
|
||||||
|
const char *description; /* Optional option description. */
|
||||||
|
} ARGPARSE_OPTS;
|
||||||
|
|
||||||
|
|
||||||
|
/* Global flags (ARGPARSE_ARGS). */
|
||||||
|
#define ARGPARSE_FLAG_KEEP 1 /* Do not remove options form argv. */
|
||||||
|
#define ARGPARSE_FLAG_ALL 2 /* Do not stop at last option but return
|
||||||
|
remaining args with R_OPT set to -1. */
|
||||||
|
#define ARGPARSE_FLAG_MIXED 4 /* Assume options and args are mixed. */
|
||||||
|
#define ARGPARSE_FLAG_NOSTOP 8 /* Do not stop processing at "--". */
|
||||||
|
#define ARGPARSE_FLAG_ARG0 16 /* Do not skip the first arg. */
|
||||||
|
#define ARGPARSE_FLAG_ONEDASH 32 /* Allow long options with one dash. */
|
||||||
|
#define ARGPARSE_FLAG_NOVERSION 64 /* No output for "--version". */
|
||||||
|
|
||||||
|
#define ARGPARSE_FLAG_STOP_SEEN 256 /* Set to true if a "--" has been seen. */
|
||||||
|
|
||||||
|
/* Flags for each option (ARGPARSE_OPTS). The type code may be
|
||||||
|
ORed with the OPT flags. */
|
||||||
|
#define ARGPARSE_TYPE_NONE 0 /* Does not take an argument. */
|
||||||
|
#define ARGPARSE_TYPE_INT 1 /* Takes an int argument. */
|
||||||
|
#define ARGPARSE_TYPE_STRING 2 /* Takes a string argument. */
|
||||||
|
#define ARGPARSE_TYPE_LONG 3 /* Takes a long argument. */
|
||||||
|
#define ARGPARSE_TYPE_ULONG 4 /* Takes an unsigned long argument. */
|
||||||
|
#define ARGPARSE_OPT_OPTIONAL (1<<3) /* Argument is optional. */
|
||||||
|
#define ARGPARSE_OPT_PREFIX (1<<4) /* Allow 0x etc. prefixed values. */
|
||||||
|
#define ARGPARSE_OPT_IGNORE (1<<6) /* Ignore command or option. */
|
||||||
|
#define ARGPARSE_OPT_COMMAND (1<<7) /* The argument is a command. */
|
||||||
|
|
||||||
|
#define ARGPARSE_TYPE_MASK 7 /* Mask for the type values (internal). */
|
||||||
|
|
||||||
|
/* A set of macros to make option definitions easier to read. */
|
||||||
|
#define ARGPARSE_x(s,l,t,f,d) \
|
||||||
|
{ (s), (l), ARGPARSE_TYPE_ ## t | (f), (d) }
|
||||||
|
|
||||||
|
#define ARGPARSE_s(s,l,t,d) \
|
||||||
|
{ (s), (l), ARGPARSE_TYPE_ ## t, (d) }
|
||||||
|
#define ARGPARSE_s_n(s,l,d) \
|
||||||
|
{ (s), (l), ARGPARSE_TYPE_NONE, (d) }
|
||||||
|
#define ARGPARSE_s_i(s,l,d) \
|
||||||
|
{ (s), (l), ARGPARSE_TYPE_INT, (d) }
|
||||||
|
#define ARGPARSE_s_s(s,l,d) \
|
||||||
|
{ (s), (l), ARGPARSE_TYPE_STRING, (d) }
|
||||||
|
#define ARGPARSE_s_l(s,l,d) \
|
||||||
|
{ (s), (l), ARGPARSE_TYPE_LONG, (d) }
|
||||||
|
#define ARGPARSE_s_u(s,l,d) \
|
||||||
|
{ (s), (l), ARGPARSE_TYPE_ULONG, (d) }
|
||||||
|
|
||||||
|
#define ARGPARSE_o(s,l,t,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_ ## t | ARGPARSE_OPT_OPTIONAL), (d) }
|
||||||
|
#define ARGPARSE_o_n(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_OPTIONAL), (d) }
|
||||||
|
#define ARGPARSE_o_i(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_INT | ARGPARSE_OPT_OPTIONAL), (d) }
|
||||||
|
#define ARGPARSE_o_s(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_STRING | ARGPARSE_OPT_OPTIONAL), (d) }
|
||||||
|
#define ARGPARSE_o_l(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_LONG | ARGPARSE_OPT_OPTIONAL), (d) }
|
||||||
|
#define ARGPARSE_o_u(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_ULONG | ARGPARSE_OPT_OPTIONAL), (d) }
|
||||||
|
|
||||||
|
#define ARGPARSE_p(s,l,t,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_ ## t | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
#define ARGPARSE_p_n(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
#define ARGPARSE_p_i(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_INT | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
#define ARGPARSE_p_s(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_STRING | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
#define ARGPARSE_p_l(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_LONG | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
#define ARGPARSE_p_u(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_ULONG | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
|
||||||
|
#define ARGPARSE_op(s,l,t,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_ ## t \
|
||||||
|
| ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
#define ARGPARSE_op_n(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_NONE \
|
||||||
|
| ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
#define ARGPARSE_op_i(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_INT \
|
||||||
|
| ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
#define ARGPARSE_op_s(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_STRING \
|
||||||
|
| ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
#define ARGPARSE_op_l(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_LONG \
|
||||||
|
| ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
#define ARGPARSE_op_u(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_ULONG \
|
||||||
|
| ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
|
||||||
|
|
||||||
|
#define ARGPARSE_c(s,l,d) \
|
||||||
|
{ (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_COMMAND), (d) }
|
||||||
|
|
||||||
|
#define ARGPARSE_ignore(s,l) \
|
||||||
|
{ (s), (l), (ARGPARSE_OPT_IGNORE), "@" }
|
||||||
|
|
||||||
|
#define ARGPARSE_group(s,d) \
|
||||||
|
{ (s), NULL, 0, (d) }
|
||||||
|
|
||||||
|
#define ARGPARSE_end() { 0, NULL, 0, NULL }
|
||||||
|
|
||||||
|
|
||||||
|
/* Other constants. */
|
||||||
|
#define ARGPARSE_PRINT_WARNING 1
|
||||||
|
#define ARGPARSE_PRINT_ERROR 2
|
||||||
|
|
||||||
|
|
||||||
|
/* Error values. */
|
||||||
|
#define ARGPARSE_IS_ARG (-1)
|
||||||
|
#define ARGPARSE_INVALID_OPTION (-2)
|
||||||
|
#define ARGPARSE_MISSING_ARG (-3)
|
||||||
|
#define ARGPARSE_KEYWORD_TOO_LONG (-4)
|
||||||
|
#define ARGPARSE_READ_ERROR (-5)
|
||||||
|
#define ARGPARSE_UNEXPECTED_ARG (-6)
|
||||||
|
#define ARGPARSE_INVALID_COMMAND (-7)
|
||||||
|
#define ARGPARSE_AMBIGUOUS_OPTION (-8)
|
||||||
|
#define ARGPARSE_AMBIGUOUS_COMMAND (-9)
|
||||||
|
#define ARGPARSE_INVALID_ALIAS (-10)
|
||||||
|
#define ARGPARSE_OUT_OF_CORE (-11)
|
||||||
|
#define ARGPARSE_INVALID_ARG (-12)
|
||||||
|
|
||||||
|
|
||||||
|
int arg_parse (ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
|
||||||
|
int optfile_parse (FILE *fp, const char *filename, unsigned *lineno,
|
||||||
|
ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
|
||||||
|
void usage (int level);
|
||||||
|
const char *strusage (int level);
|
||||||
|
void set_strusage (const char *(*f)( int ));
|
||||||
|
void argparse_register_outfnc (int (*fnc)(int, const char *));
|
||||||
|
|
||||||
|
#endif /*LIBJNLIB_ARGPARSE_H*/
|
Binary file not shown.
@ -0,0 +1,55 @@
|
|||||||
|
/* Quintuple Agent secure memory allocation
|
||||||
|
* Copyright (C) 1998,1999 Free Software Foundation, Inc.
|
||||||
|
* Copyright (C) 1999,2000 Robert Bihlmeyer <robbe@orcus.priv.at>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MEMORY_H
|
||||||
|
#define _MEMORY_H
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#if 0
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* values for flags, hardcoded in secmem.c */
|
||||||
|
#define SECMEM_WARN 0
|
||||||
|
#define SECMEM_DONT_WARN 1
|
||||||
|
#define SECMEM_SUSPEND_WARN 2
|
||||||
|
|
||||||
|
void secmem_init( size_t npool );
|
||||||
|
void secmem_term( void );
|
||||||
|
void *secmem_malloc( size_t size );
|
||||||
|
void *secmem_realloc( void *a, size_t newsize );
|
||||||
|
void secmem_free( void *a );
|
||||||
|
int m_is_secure( const void *p );
|
||||||
|
void secmem_dump_stats(void);
|
||||||
|
void secmem_set_flags( unsigned flags );
|
||||||
|
unsigned secmem_get_flags(void);
|
||||||
|
size_t secmem_get_max_size (void);
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif /* _MEMORY_H */
|
@ -0,0 +1,163 @@
|
|||||||
|
/* password-cache.c - Password cache support.
|
||||||
|
Copyright (C) 2015 g10 Code GmbH
|
||||||
|
|
||||||
|
This file is part of PINENTRY.
|
||||||
|
|
||||||
|
PINENTRY is free software; you can redistribute it and/or modify it
|
||||||
|
under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
PINENTRY is distributed in the hope that it will be useful, but
|
||||||
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef HAVE_CONFIG_H
|
||||||
|
# include <config.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef HAVE_LIBSECRET
|
||||||
|
# include <libsecret/secret.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "password-cache.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
#ifdef HAVE_LIBSECRET
|
||||||
|
static const SecretSchema *
|
||||||
|
gpg_schema (void)
|
||||||
|
{
|
||||||
|
static const SecretSchema the_schema = {
|
||||||
|
"org.gnupg.Passphrase", SECRET_SCHEMA_NONE,
|
||||||
|
{
|
||||||
|
{ "stored-by", SECRET_SCHEMA_ATTRIBUTE_STRING },
|
||||||
|
{ "keygrip", SECRET_SCHEMA_ATTRIBUTE_STRING },
|
||||||
|
{ "NULL", 0 },
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return &the_schema;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *
|
||||||
|
keygrip_to_label (const char *keygrip)
|
||||||
|
{
|
||||||
|
char const prefix[] = "GnuPG: ";
|
||||||
|
char *label;
|
||||||
|
|
||||||
|
label = malloc (sizeof (prefix) + strlen (keygrip));
|
||||||
|
if (label)
|
||||||
|
{
|
||||||
|
memcpy (label, prefix, sizeof (prefix) - 1);
|
||||||
|
strcpy (&label[sizeof (prefix) - 1], keygrip);
|
||||||
|
}
|
||||||
|
return label;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void
|
||||||
|
password_cache_save (const char *keygrip, const char *password)
|
||||||
|
{
|
||||||
|
#ifdef HAVE_LIBSECRET
|
||||||
|
char *label;
|
||||||
|
GError *error = NULL;
|
||||||
|
|
||||||
|
if (! *keygrip)
|
||||||
|
return;
|
||||||
|
|
||||||
|
label = keygrip_to_label (keygrip);
|
||||||
|
if (! label)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (! secret_password_store_sync (gpg_schema (),
|
||||||
|
SECRET_COLLECTION_DEFAULT,
|
||||||
|
label, password, NULL, &error,
|
||||||
|
"stored-by", "GnuPG Pinentry",
|
||||||
|
"keygrip", keygrip, NULL))
|
||||||
|
{
|
||||||
|
printf("Failed to cache password for key %s with secret service: %s\n",
|
||||||
|
keygrip, error->message);
|
||||||
|
|
||||||
|
g_error_free (error);
|
||||||
|
}
|
||||||
|
|
||||||
|
free (label);
|
||||||
|
#else
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
password_cache_lookup (const char *keygrip)
|
||||||
|
{
|
||||||
|
#ifdef HAVE_LIBSECRET
|
||||||
|
GError *error = NULL;
|
||||||
|
char *password;
|
||||||
|
char *password2;
|
||||||
|
|
||||||
|
if (! *keygrip)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
password = secret_password_lookup_nonpageable_sync
|
||||||
|
(gpg_schema (), NULL, &error,
|
||||||
|
"keygrip", keygrip, NULL);
|
||||||
|
|
||||||
|
if (error != NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to lookup password for key %s with secret service: %s\n",
|
||||||
|
keygrip, error->message);
|
||||||
|
g_error_free (error);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (! password)
|
||||||
|
/* The password for this key is not cached. Just return NULL. */
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
/* The password needs to be returned in secmem allocated memory. */
|
||||||
|
password2 = secmem_malloc (strlen (password) + 1);
|
||||||
|
if (password2)
|
||||||
|
strcpy(password2, password);
|
||||||
|
else
|
||||||
|
printf("secmem_malloc failed: can't copy password!\n");
|
||||||
|
|
||||||
|
secret_password_free (password);
|
||||||
|
|
||||||
|
return password2;
|
||||||
|
#else
|
||||||
|
return NULL;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Try and remove the cached password for key grip. Returns -1 on
|
||||||
|
error, 0 if the key is not found and 1 if the password was
|
||||||
|
removed. */
|
||||||
|
int
|
||||||
|
password_cache_clear (const char *keygrip)
|
||||||
|
{
|
||||||
|
#ifdef HAVE_LIBSECRET
|
||||||
|
GError *error = NULL;
|
||||||
|
int removed = secret_password_clear_sync (gpg_schema (), NULL, &error,
|
||||||
|
"keygrip", keygrip, NULL);
|
||||||
|
if (error != NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to clear password for key %s with secret service: %s\n",
|
||||||
|
keygrip, error->message);
|
||||||
|
g_debug("%s", error->message);
|
||||||
|
g_error_free (error);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (removed)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
#else
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
/* password-cache.h - Password cache support interfaces.
|
||||||
|
Copyright (C) 2015 g10 Code GmbH
|
||||||
|
|
||||||
|
This file is part of PINENTRY.
|
||||||
|
|
||||||
|
PINENTRY is free software; you can redistribute it and/or modify it
|
||||||
|
under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
PINENTRY is distributed in the hope that it will be useful, but
|
||||||
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PASSWORD_CACHE_H
|
||||||
|
#define PASSWORD_CACHE_H
|
||||||
|
|
||||||
|
void password_cache_save (const char *key_grip, const char *password);
|
||||||
|
|
||||||
|
char *password_cache_lookup (const char *key_grip);
|
||||||
|
|
||||||
|
int password_cache_clear (const char *keygrip);
|
||||||
|
|
||||||
|
#endif
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@ -0,0 +1,91 @@
|
|||||||
|
/* STL allocator for secmem
|
||||||
|
* Copyright (C) 2008 Marc Mutz <marc@kdab.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __SECMEM_SECMEMPP_H__
|
||||||
|
#define __SECMEM_SECMEMPP_H__
|
||||||
|
|
||||||
|
#include "secmem/memory.h"
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace secmem {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class alloc {
|
||||||
|
public:
|
||||||
|
// type definitions:
|
||||||
|
typedef size_t size_type;
|
||||||
|
typedef ptrdiff_t difference_type;
|
||||||
|
typedef T* pointer;
|
||||||
|
typedef const T* const_pointer;
|
||||||
|
typedef T& reference;
|
||||||
|
typedef const T& const_reference;
|
||||||
|
typedef T value_type;
|
||||||
|
|
||||||
|
// rebind
|
||||||
|
template <typename U>
|
||||||
|
struct rebind {
|
||||||
|
typedef alloc<U> other;
|
||||||
|
};
|
||||||
|
|
||||||
|
// address
|
||||||
|
pointer address( reference value ) const {
|
||||||
|
return &value;
|
||||||
|
}
|
||||||
|
const_pointer address( const_reference value ) const {
|
||||||
|
return &value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// (trivial) ctors and dtors
|
||||||
|
alloc() {}
|
||||||
|
alloc( const alloc & ) {}
|
||||||
|
template <typename U> alloc( const alloc<U> & ) {}
|
||||||
|
// copy ctor is ok
|
||||||
|
~alloc() {}
|
||||||
|
|
||||||
|
// de/allocation
|
||||||
|
size_type max_size() const {
|
||||||
|
return secmem_get_max_size();
|
||||||
|
}
|
||||||
|
|
||||||
|
pointer allocate( size_type n, void * =0 ) {
|
||||||
|
return static_cast<pointer>( secmem_malloc( n * sizeof(T) ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate( pointer p, size_type ) {
|
||||||
|
secmem_free( p );
|
||||||
|
}
|
||||||
|
|
||||||
|
// de/construct
|
||||||
|
void construct( pointer p, const T & value ) {
|
||||||
|
void * loc = p;
|
||||||
|
new (loc)T(value);
|
||||||
|
}
|
||||||
|
void destruct( pointer p ) {
|
||||||
|
p->~T();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// equality comparison
|
||||||
|
template <typename T1,typename T2>
|
||||||
|
bool operator==( const alloc<T1> &, const alloc<T2> & ) { return true; }
|
||||||
|
template <typename T1, typename T2>
|
||||||
|
bool operator!=( const alloc<T1> &, const alloc<T2> & ) { return false; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* __SECMEM_SECMEMPP_H__ */
|
@ -0,0 +1,3 @@
|
|||||||
|
/* This file exists because "util.h" is such a generic name that it is
|
||||||
|
likely to clash with other such files. */
|
||||||
|
#include "util.h"
|
@ -0,0 +1,460 @@
|
|||||||
|
/* secmem.c - memory allocation from a secure heap
|
||||||
|
* Copyright (C) 1998, 1999, 2003 Free Software Foundation, Inc.
|
||||||
|
* Copyright (C) 2015 g10 Code GmbH
|
||||||
|
*
|
||||||
|
* This file is part of GnuPG.
|
||||||
|
*
|
||||||
|
* GnuPG is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* GnuPG is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#if defined(HAVE_MLOCK) || defined(HAVE_MMAP)
|
||||||
|
# include <sys/mman.h>
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include <fcntl.h>
|
||||||
|
# ifdef USE_CAPABILITIES
|
||||||
|
# include <sys/capability.h>
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
#ifdef ORIGINAL_GPG_VERSION
|
||||||
|
#include "types.h"
|
||||||
|
#include "util.h"
|
||||||
|
#else /* ORIGINAL_GPG_VERSION */
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
int a;
|
||||||
|
short b;
|
||||||
|
char c[1];
|
||||||
|
long d;
|
||||||
|
#ifdef HAVE_U64_TYPEDEF
|
||||||
|
u64 e;
|
||||||
|
#endif
|
||||||
|
float f;
|
||||||
|
double g;
|
||||||
|
} PROPERLY_ALIGNED_TYPE;
|
||||||
|
|
||||||
|
#define log_error log_info
|
||||||
|
#define log_bug log_fatal
|
||||||
|
|
||||||
|
void
|
||||||
|
log_info(char *template, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
|
||||||
|
va_start(args, template);
|
||||||
|
vfprintf(stderr, template, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
log_fatal(char *template, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
|
||||||
|
va_start(args, template);
|
||||||
|
vfprintf(stderr, template, args);
|
||||||
|
va_end(args);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* ORIGINAL_GPG_VERSION */
|
||||||
|
|
||||||
|
#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
|
||||||
|
# define MAP_ANONYMOUS MAP_ANON
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define DEFAULT_POOLSIZE 16384
|
||||||
|
|
||||||
|
typedef struct memblock_struct MEMBLOCK;
|
||||||
|
struct memblock_struct {
|
||||||
|
unsigned size;
|
||||||
|
union {
|
||||||
|
MEMBLOCK *next;
|
||||||
|
PROPERLY_ALIGNED_TYPE aligned;
|
||||||
|
} u;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void *pool;
|
||||||
|
static volatile int pool_okay; /* may be checked in an atexit function */
|
||||||
|
static int pool_is_mmapped;
|
||||||
|
static size_t poolsize; /* allocated length */
|
||||||
|
static size_t poollen; /* used length */
|
||||||
|
static MEMBLOCK *unused_blocks;
|
||||||
|
static unsigned max_alloced;
|
||||||
|
static unsigned cur_alloced;
|
||||||
|
static unsigned max_blocks;
|
||||||
|
static unsigned cur_blocks;
|
||||||
|
static int disable_secmem;
|
||||||
|
static int show_warning;
|
||||||
|
static int no_warning;
|
||||||
|
static int suspend_warning;
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
print_warn(void)
|
||||||
|
{
|
||||||
|
if( !no_warning )
|
||||||
|
log_info("Warning: using insecure memory!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
lock_pool( void *p, size_t n )
|
||||||
|
{
|
||||||
|
#if defined(USE_CAPABILITIES) && defined(HAVE_MLOCK)
|
||||||
|
int err;
|
||||||
|
|
||||||
|
cap_set_proc( cap_from_text("cap_ipc_lock+ep") );
|
||||||
|
err = mlock( p, n );
|
||||||
|
if( err && errno )
|
||||||
|
err = errno;
|
||||||
|
cap_set_proc( cap_from_text("cap_ipc_lock+p") );
|
||||||
|
|
||||||
|
if( err ) {
|
||||||
|
if( errno != EPERM
|
||||||
|
#ifdef EAGAIN /* OpenBSD returns this */
|
||||||
|
&& errno != EAGAIN
|
||||||
|
#endif
|
||||||
|
)
|
||||||
|
log_error("can't lock memory: %s\n", strerror(err));
|
||||||
|
show_warning = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(HAVE_MLOCK)
|
||||||
|
uid_t uid;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
uid = getuid();
|
||||||
|
|
||||||
|
#ifdef HAVE_BROKEN_MLOCK
|
||||||
|
if( uid ) {
|
||||||
|
errno = EPERM;
|
||||||
|
err = errno;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
err = mlock( p, n );
|
||||||
|
if( err && errno )
|
||||||
|
err = errno;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
err = mlock( p, n );
|
||||||
|
if( err && errno )
|
||||||
|
err = errno;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if( uid && !geteuid() ) {
|
||||||
|
if( setuid( uid ) || getuid() != geteuid() )
|
||||||
|
log_fatal("failed to reset uid: %s\n", strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
if( err ) {
|
||||||
|
if( errno != EPERM
|
||||||
|
#ifdef EAGAIN /* OpenBSD returns this */
|
||||||
|
&& errno != EAGAIN
|
||||||
|
#endif
|
||||||
|
)
|
||||||
|
log_error("can't lock memory: %s\n", strerror(err));
|
||||||
|
show_warning = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
log_info("Please note that you don't have secure memory on this system\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
init_pool( size_t n)
|
||||||
|
{
|
||||||
|
size_t pgsize;
|
||||||
|
|
||||||
|
poolsize = n;
|
||||||
|
|
||||||
|
if( disable_secmem )
|
||||||
|
log_bug("secure memory is disabled");
|
||||||
|
|
||||||
|
#ifdef HAVE_GETPAGESIZE
|
||||||
|
pgsize = getpagesize();
|
||||||
|
#else
|
||||||
|
pgsize = 4096;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if HAVE_MMAP
|
||||||
|
poolsize = (poolsize + pgsize -1 ) & ~(pgsize-1);
|
||||||
|
# ifdef MAP_ANONYMOUS
|
||||||
|
pool = mmap( 0, poolsize, PROT_READ|PROT_WRITE,
|
||||||
|
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
|
||||||
|
# else /* map /dev/zero instead */
|
||||||
|
{ int fd;
|
||||||
|
|
||||||
|
fd = open("/dev/zero", O_RDWR);
|
||||||
|
if( fd == -1 ) {
|
||||||
|
log_error("can't open /dev/zero: %s\n", strerror(errno) );
|
||||||
|
pool = (void*)-1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
pool = mmap( 0, poolsize, PROT_READ|PROT_WRITE,
|
||||||
|
MAP_PRIVATE, fd, 0);
|
||||||
|
close (fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
if( pool == (void*)-1 )
|
||||||
|
log_info("can't mmap pool of %u bytes: %s - using malloc\n",
|
||||||
|
(unsigned)poolsize, strerror(errno));
|
||||||
|
else {
|
||||||
|
pool_is_mmapped = 1;
|
||||||
|
pool_okay = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
if( !pool_okay ) {
|
||||||
|
pool = malloc( poolsize );
|
||||||
|
if( !pool )
|
||||||
|
log_fatal("can't allocate memory pool of %u bytes\n",
|
||||||
|
(unsigned)poolsize);
|
||||||
|
else
|
||||||
|
pool_okay = 1;
|
||||||
|
}
|
||||||
|
lock_pool( pool, poolsize );
|
||||||
|
poollen = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* concatenate unused blocks */
|
||||||
|
static void
|
||||||
|
compress_pool(void)
|
||||||
|
{
|
||||||
|
/* fixme: we really should do this */
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
secmem_set_flags( unsigned flags )
|
||||||
|
{
|
||||||
|
int was_susp = suspend_warning;
|
||||||
|
|
||||||
|
no_warning = flags & 1;
|
||||||
|
suspend_warning = flags & 2;
|
||||||
|
|
||||||
|
/* and now issue the warning if it is not longer suspended */
|
||||||
|
if( was_susp && !suspend_warning && show_warning ) {
|
||||||
|
show_warning = 0;
|
||||||
|
print_warn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned
|
||||||
|
secmem_get_flags(void)
|
||||||
|
{
|
||||||
|
unsigned flags;
|
||||||
|
|
||||||
|
flags = no_warning ? 1:0;
|
||||||
|
flags |= suspend_warning ? 2:0;
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
secmem_init( size_t n )
|
||||||
|
{
|
||||||
|
if( !n ) {
|
||||||
|
#ifdef USE_CAPABILITIES
|
||||||
|
/* drop all capabilities */
|
||||||
|
cap_set_proc( cap_from_text("all-eip") );
|
||||||
|
|
||||||
|
#elif !defined(HAVE_DOSISH_SYSTEM)
|
||||||
|
uid_t uid;
|
||||||
|
|
||||||
|
disable_secmem=1;
|
||||||
|
uid = getuid();
|
||||||
|
if( uid != geteuid() ) {
|
||||||
|
if( setuid( uid ) || getuid() != geteuid() )
|
||||||
|
log_fatal("failed to drop setuid\n" );
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if( n < DEFAULT_POOLSIZE )
|
||||||
|
n = DEFAULT_POOLSIZE;
|
||||||
|
if( !pool_okay )
|
||||||
|
init_pool(n);
|
||||||
|
else
|
||||||
|
log_error("Oops, secure memory pool already initialized\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *
|
||||||
|
secmem_malloc( size_t size )
|
||||||
|
{
|
||||||
|
MEMBLOCK *mb, *mb2;
|
||||||
|
int compressed=0;
|
||||||
|
|
||||||
|
if( !pool_okay ) {
|
||||||
|
log_info(
|
||||||
|
"operation is not possible without initialized secure memory\n");
|
||||||
|
log_info("(you may have used the wrong program for this task)\n");
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
if( show_warning && !suspend_warning ) {
|
||||||
|
show_warning = 0;
|
||||||
|
print_warn();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* blocks are always a multiple of 32 */
|
||||||
|
size += sizeof(MEMBLOCK);
|
||||||
|
size = ((size + 31) / 32) * 32;
|
||||||
|
|
||||||
|
retry:
|
||||||
|
/* try to get it from the used blocks */
|
||||||
|
for(mb = unused_blocks,mb2=NULL; mb; mb2=mb, mb = mb->u.next )
|
||||||
|
if( mb->size >= size ) {
|
||||||
|
if( mb2 )
|
||||||
|
mb2->u.next = mb->u.next;
|
||||||
|
else
|
||||||
|
unused_blocks = mb->u.next;
|
||||||
|
goto leave;
|
||||||
|
}
|
||||||
|
/* allocate a new block */
|
||||||
|
if( (poollen + size <= poolsize) ) {
|
||||||
|
mb = (void*)((char*)pool + poollen);
|
||||||
|
poollen += size;
|
||||||
|
mb->size = size;
|
||||||
|
}
|
||||||
|
else if( !compressed ) {
|
||||||
|
compressed=1;
|
||||||
|
compress_pool();
|
||||||
|
goto retry;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
leave:
|
||||||
|
cur_alloced += mb->size;
|
||||||
|
cur_blocks++;
|
||||||
|
if( cur_alloced > max_alloced )
|
||||||
|
max_alloced = cur_alloced;
|
||||||
|
if( cur_blocks > max_blocks )
|
||||||
|
max_blocks = cur_blocks;
|
||||||
|
|
||||||
|
memset (&mb->u.aligned.c, 0,
|
||||||
|
size - (size_t) &((struct memblock_struct *) 0)->u.aligned.c);
|
||||||
|
|
||||||
|
return &mb->u.aligned.c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *
|
||||||
|
secmem_realloc( void *p, size_t newsize )
|
||||||
|
{
|
||||||
|
MEMBLOCK *mb;
|
||||||
|
size_t size;
|
||||||
|
void *a;
|
||||||
|
|
||||||
|
if (! p)
|
||||||
|
return secmem_malloc(newsize);
|
||||||
|
|
||||||
|
mb = (MEMBLOCK*)((char*)p - ((size_t) &((MEMBLOCK*)0)->u.aligned.c));
|
||||||
|
size = mb->size;
|
||||||
|
if( newsize < size )
|
||||||
|
return p; /* it is easier not to shrink the memory */
|
||||||
|
a = secmem_malloc( newsize );
|
||||||
|
memcpy(a, p, size);
|
||||||
|
memset((char*)a+size, 0, newsize-size);
|
||||||
|
secmem_free(p);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
secmem_free( void *a )
|
||||||
|
{
|
||||||
|
MEMBLOCK *mb;
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
if( !a )
|
||||||
|
return;
|
||||||
|
|
||||||
|
mb = (MEMBLOCK*)((char*)a - ((size_t) &((MEMBLOCK*)0)->u.aligned.c));
|
||||||
|
size = mb->size;
|
||||||
|
/* This does not make much sense: probably this memory is held in the
|
||||||
|
* cache. We do it anyway: */
|
||||||
|
wipememory2(mb, 0xff, size );
|
||||||
|
wipememory2(mb, 0xaa, size );
|
||||||
|
wipememory2(mb, 0x55, size );
|
||||||
|
wipememory2(mb, 0x00, size );
|
||||||
|
mb->size = size;
|
||||||
|
mb->u.next = unused_blocks;
|
||||||
|
unused_blocks = mb;
|
||||||
|
cur_blocks--;
|
||||||
|
cur_alloced -= size;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
m_is_secure( const void *p )
|
||||||
|
{
|
||||||
|
return p >= pool && p < (void*)((char*)pool+poolsize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
secmem_term()
|
||||||
|
{
|
||||||
|
if( !pool_okay )
|
||||||
|
return;
|
||||||
|
|
||||||
|
wipememory2( pool, 0xff, poolsize);
|
||||||
|
wipememory2( pool, 0xaa, poolsize);
|
||||||
|
wipememory2( pool, 0x55, poolsize);
|
||||||
|
wipememory2( pool, 0x00, poolsize);
|
||||||
|
#if HAVE_MMAP
|
||||||
|
if( pool_is_mmapped )
|
||||||
|
munmap( pool, poolsize );
|
||||||
|
#endif
|
||||||
|
pool = NULL;
|
||||||
|
pool_okay = 0;
|
||||||
|
poolsize=0;
|
||||||
|
poollen=0;
|
||||||
|
unused_blocks=NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
secmem_dump_stats()
|
||||||
|
{
|
||||||
|
if( disable_secmem )
|
||||||
|
return;
|
||||||
|
fprintf(stderr,
|
||||||
|
"secmem usage: %u/%u bytes in %u/%u blocks of pool %lu/%lu\n",
|
||||||
|
cur_alloced, max_alloced, cur_blocks, max_blocks,
|
||||||
|
(ulong)poollen, (ulong)poolsize );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
size_t
|
||||||
|
secmem_get_max_size (void)
|
||||||
|
{
|
||||||
|
return poolsize;
|
||||||
|
}
|
Binary file not shown.
@ -0,0 +1,150 @@
|
|||||||
|
/* Quintuple Agent
|
||||||
|
* Copyright (C) 1999 Robert Bihlmeyer <robbe@orcus.priv.at>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef HAVE_CONFIG_H
|
||||||
|
#include <config.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define _GNU_SOURCE 1
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#ifndef HAVE_W32CE_SYSTEM
|
||||||
|
# include <errno.h>
|
||||||
|
#endif
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
|
static int uid_set = 0;
|
||||||
|
static uid_t real_uid, file_uid;
|
||||||
|
#endif /*!HAVE_DOSISH_SYSTEM*/
|
||||||
|
|
||||||
|
/* Write DATA of size BYTES to FD, until all is written or an error
|
||||||
|
occurs. */
|
||||||
|
ssize_t
|
||||||
|
xwrite(int fd, const void *data, size_t bytes)
|
||||||
|
{
|
||||||
|
char *ptr;
|
||||||
|
size_t todo;
|
||||||
|
ssize_t written = 0;
|
||||||
|
|
||||||
|
for (ptr = (char *)data, todo = bytes; todo; ptr += written, todo -= written)
|
||||||
|
{
|
||||||
|
do
|
||||||
|
written = write (fd, ptr, todo);
|
||||||
|
while (
|
||||||
|
#ifdef HAVE_W32CE_SYSTEM
|
||||||
|
0
|
||||||
|
#else
|
||||||
|
written == -1 && errno == EINTR
|
||||||
|
#endif
|
||||||
|
);
|
||||||
|
if (written < 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return written;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
extern int debug;
|
||||||
|
|
||||||
|
int
|
||||||
|
debugmsg(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list va;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
va_start(va, fmt);
|
||||||
|
fprintf(stderr, "\e[4m");
|
||||||
|
ret = vfprintf(stderr, fmt, va);
|
||||||
|
fprintf(stderr, "\e[24m");
|
||||||
|
va_end(va);
|
||||||
|
return ret;
|
||||||
|
} else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* initialize uid variables */
|
||||||
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
|
static void
|
||||||
|
init_uids(void)
|
||||||
|
{
|
||||||
|
real_uid = getuid();
|
||||||
|
file_uid = geteuid();
|
||||||
|
uid_set = 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if 0 /* Not used. */
|
||||||
|
/* lower privileges to the real user's */
|
||||||
|
void
|
||||||
|
lower_privs()
|
||||||
|
{
|
||||||
|
if (!uid_set)
|
||||||
|
init_uids();
|
||||||
|
if (real_uid != file_uid) {
|
||||||
|
#ifdef HAVE_SETEUID
|
||||||
|
if (seteuid(real_uid) < 0) {
|
||||||
|
perror("lowering privileges failed");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
fprintf(stderr, _("Warning: running q-agent setuid on this system is dangerous\n"));
|
||||||
|
#endif /* HAVE_SETEUID */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif /* if 0 */
|
||||||
|
|
||||||
|
#if 0 /* Not used. */
|
||||||
|
/* raise privileges to the effective user's */
|
||||||
|
void
|
||||||
|
raise_privs()
|
||||||
|
{
|
||||||
|
assert(real_uid >= 0); /* lower_privs() must be called before this */
|
||||||
|
#ifdef HAVE_SETEUID
|
||||||
|
if (real_uid != file_uid && seteuid(file_uid) < 0) {
|
||||||
|
perror("Warning: raising privileges failed");
|
||||||
|
}
|
||||||
|
#endif /* HAVE_SETEUID */
|
||||||
|
}
|
||||||
|
#endif /* if 0 */
|
||||||
|
|
||||||
|
/* drop all additional privileges */
|
||||||
|
void
|
||||||
|
drop_privs()
|
||||||
|
{
|
||||||
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
|
if (!uid_set)
|
||||||
|
init_uids();
|
||||||
|
if (real_uid != file_uid) {
|
||||||
|
if (setuid(real_uid) < 0) {
|
||||||
|
perror("dropping privileges failed");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
file_uid = real_uid;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
/* Quintuple Agent utilities
|
||||||
|
* Copyright (C) 1999 Robert Bihlmeyer <robbe@orcus.priv.at>
|
||||||
|
* Copyright (C) 2003 g10 Code GmbH
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _UTIL_H
|
||||||
|
#define _UTIL_H
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#ifndef HAVE_BYTE_TYPEDEF
|
||||||
|
# undef byte
|
||||||
|
# ifdef __riscos__
|
||||||
|
/* Norcroft treats char == unsigned char but char* != unsigned char* */
|
||||||
|
typedef char byte;
|
||||||
|
# else
|
||||||
|
typedef unsigned char byte;
|
||||||
|
# endif
|
||||||
|
# define HAVE_BYTE_TYPEDEF
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_ULONG_TYPEDEF
|
||||||
|
# undef ulong
|
||||||
|
typedef unsigned long ulong;
|
||||||
|
# define HAVE_ULONG_TYPEDEF
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
ssize_t xwrite(int, const void *, size_t); /* write until finished */
|
||||||
|
int debugmsg(const char *, ...); /* output a debug message if debugging==on */
|
||||||
|
void drop_privs(void); /* finally drop privileges */
|
||||||
|
|
||||||
|
|
||||||
|
/* To avoid that a compiler optimizes certain memset calls away, these
|
||||||
|
macros may be used instead. */
|
||||||
|
#define wipememory2(_ptr,_set,_len) do { \
|
||||||
|
volatile char *_vptr=(volatile char *)(_ptr); \
|
||||||
|
size_t _vlen=(_len); \
|
||||||
|
while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \
|
||||||
|
} while(0)
|
||||||
|
#define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
|
||||||
|
#define wipe(_ptr,_len) wipememory2(_ptr,0,_len)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
|
||||||
|
*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
|
||||||
|
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
Binary file not shown.
@ -0,0 +1,339 @@
|
|||||||
|
#include <signal.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <locale.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <X11/Xlib.h>
|
||||||
|
#include <X11/Xatom.h>
|
||||||
|
#include <X11/Xutil.h>
|
||||||
|
#include <X11/Xft/Xft.h>
|
||||||
|
|
||||||
|
#include "drw.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#include "pinentry/pinentry.h"
|
||||||
|
#include "pinentry/memory.h"
|
||||||
|
|
||||||
|
/* macros */
|
||||||
|
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
|
||||||
|
* MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
|
||||||
|
#define LENGTH(X) (sizeof X / sizeof X[0])
|
||||||
|
#define TEXTNW(X,N) (drw_font_getexts_width(drw->fonts[0], (X), (N)))
|
||||||
|
#define TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
|
||||||
|
|
||||||
|
const char *str_OK = "OK\n";
|
||||||
|
const char *str_ERRUNPARS = "ERR1337 dunno what to do with it\n";
|
||||||
|
const char *str_ERRNOTIMP = "ERR4100 not implemented yet\n";
|
||||||
|
|
||||||
|
/* enums */
|
||||||
|
enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
|
||||||
|
|
||||||
|
static char text[2048] = "";
|
||||||
|
static int bh, mw, mh;
|
||||||
|
static int inputw, promptw;
|
||||||
|
static size_t cursor = 0;
|
||||||
|
static Atom clip, utf8;
|
||||||
|
static Window win;
|
||||||
|
static XIC xic;
|
||||||
|
static int mon = -1;
|
||||||
|
|
||||||
|
static ClrScheme scheme[SchemeLast];
|
||||||
|
static Display *dpy;
|
||||||
|
static int screen;
|
||||||
|
static Window root;
|
||||||
|
static Drw *drw;
|
||||||
|
static int sw, sh;
|
||||||
|
|
||||||
|
static int timed_out;
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
grabkeyboard(void) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/* try to grab keyboard, we may have to wait for another process to ungrab */
|
||||||
|
for(i = 0; i < 1000; i++) {
|
||||||
|
if(XGrabKeyboard(dpy, DefaultRootWindow(dpy), True,
|
||||||
|
GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
|
||||||
|
return;
|
||||||
|
usleep(1000);
|
||||||
|
}
|
||||||
|
die("cannot grab keyboard\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t
|
||||||
|
nextrune(int cursor, int inc) {
|
||||||
|
ssize_t n;
|
||||||
|
|
||||||
|
/* return location of next utf8 rune in the given direction (+1 or -1) */
|
||||||
|
for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
insert(const char *str, ssize_t n) {
|
||||||
|
if(strlen(text) + n > sizeof text - 1)
|
||||||
|
return;
|
||||||
|
/* move existing text out of the way, insert new text, and update cursor */
|
||||||
|
memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
|
||||||
|
if(n > 0)
|
||||||
|
memcpy(&text[cursor], str, n);
|
||||||
|
cursor += n;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
drawwin(void){
|
||||||
|
int curpos;
|
||||||
|
int x=0, y=0, h=bh, w;
|
||||||
|
drw_setscheme(drw, &scheme[SchemeNorm]);
|
||||||
|
drw_rect(drw, 0,0,mw,mh,True,1,1);
|
||||||
|
|
||||||
|
if (description && *description) {
|
||||||
|
drw_setscheme(drw, &scheme[SchemeSel]);
|
||||||
|
drw_text(drw, 0,0,mw,bh,description,0);
|
||||||
|
y+=bh;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prompt && *prompt) {
|
||||||
|
drw_setscheme(drw, &scheme[SchemeSel]);
|
||||||
|
drw_text(drw, x, y, promptw, bh, prompt, 0);
|
||||||
|
x += promptw;
|
||||||
|
}
|
||||||
|
|
||||||
|
w = inputw;
|
||||||
|
drw_setscheme(drw, &scheme[SchemeNorm]);
|
||||||
|
|
||||||
|
char *sectext = malloc (sizeof (char) * 2048);
|
||||||
|
sectext[0] = '\0';
|
||||||
|
int i;
|
||||||
|
int seccursor=0;
|
||||||
|
for (i=0; text[i]!='\0'; i=nextrune(i, +1)){
|
||||||
|
strcat(sectext, secchar);
|
||||||
|
if (i<cursor){
|
||||||
|
ssize_t n;
|
||||||
|
for(n = seccursor + 1; n + 1 >= 0 && (sectext[n] & 0xc0) == 0x80; n ++);
|
||||||
|
seccursor = n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drw_text(drw, x, y, mw, bh, sectext, 0);
|
||||||
|
if((curpos = TEXTNW(sectext, seccursor) + bh/2 - 2) < w) {
|
||||||
|
drw_rect(drw, x + curpos + 2, y + 2, 1 , bh-4 , True, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
drw_map(drw, win, 0, 0, mw, mh);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
setup(void){
|
||||||
|
int x,y;
|
||||||
|
XSetWindowAttributes swa;
|
||||||
|
XIM xim;
|
||||||
|
scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
|
||||||
|
scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
|
||||||
|
scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
|
||||||
|
scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
|
||||||
|
clip = XInternAtom(dpy, "CLIPBOARD", False);
|
||||||
|
utf8 = XInternAtom(dpy, "UTF8_STRING", False);
|
||||||
|
bh = drw->fonts[0]->h + 2;
|
||||||
|
mh = (description && *description)? bh * 2 : bh;
|
||||||
|
x = 0;
|
||||||
|
y = topbar ? 0 : sh - mh;
|
||||||
|
mw = sw;
|
||||||
|
promptw = (prompt && *prompt) ? TEXTW(prompt) : 0;
|
||||||
|
inputw = mw-promptw;
|
||||||
|
swa.override_redirect = True;
|
||||||
|
swa.background_pixel = scheme[SchemeNorm].bg->pix;
|
||||||
|
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
|
||||||
|
win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
|
||||||
|
DefaultDepth(dpy, screen), CopyFromParent,
|
||||||
|
DefaultVisual(dpy, screen),
|
||||||
|
CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
|
||||||
|
|
||||||
|
xim = XOpenIM(dpy, NULL, NULL, NULL);
|
||||||
|
xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
|
||||||
|
XNClientWindow, win, XNFocusWindow, win, NULL);
|
||||||
|
|
||||||
|
XMapRaised(dpy, win);
|
||||||
|
drw_resize(drw, mw, mh);
|
||||||
|
|
||||||
|
drawwin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
cleanup(void) {
|
||||||
|
XUngrabKey(dpy, AnyKey, AnyModifier, root);
|
||||||
|
drw_clr_free(scheme[SchemeNorm].bg);
|
||||||
|
drw_clr_free(scheme[SchemeNorm].fg);
|
||||||
|
drw_clr_free(scheme[SchemeSel].fg);
|
||||||
|
drw_clr_free(scheme[SchemeSel].bg);
|
||||||
|
drw_free(drw);
|
||||||
|
XSync(dpy, False);
|
||||||
|
XCloseDisplay(dpy);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
keypress(XKeyEvent *ev) {
|
||||||
|
char buf[32];
|
||||||
|
int len;
|
||||||
|
KeySym ksym = NoSymbol;
|
||||||
|
Status status;
|
||||||
|
len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
|
||||||
|
if (status == XBufferOverflow)
|
||||||
|
return 0;
|
||||||
|
switch(ksym){
|
||||||
|
default:
|
||||||
|
if (!iscntrl(*buf))
|
||||||
|
insert(buf, len);
|
||||||
|
break;
|
||||||
|
case XK_Delete:
|
||||||
|
if(text[cursor] == '\0')
|
||||||
|
return 0;
|
||||||
|
cursor = nextrune(cursor, +1);
|
||||||
|
/* fallthrough */
|
||||||
|
case XK_BackSpace:
|
||||||
|
if(cursor == 0)
|
||||||
|
return 0;
|
||||||
|
insert(NULL, nextrune(cursor, -1) - cursor);
|
||||||
|
break;
|
||||||
|
case XK_Escape:
|
||||||
|
cleanup();
|
||||||
|
exit(1);
|
||||||
|
break;
|
||||||
|
case XK_Left:
|
||||||
|
if(cursor > 0) {
|
||||||
|
cursor = nextrune(cursor, -1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XK_Right:
|
||||||
|
if(text[cursor]!='\0') {
|
||||||
|
cursor = nextrune(cursor, +1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XK_Return:
|
||||||
|
case XK_KP_Enter:
|
||||||
|
return 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
drawwin();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
paste(void) {
|
||||||
|
char *p, *q;
|
||||||
|
int di;
|
||||||
|
unsigned long dl;
|
||||||
|
Atom da;
|
||||||
|
|
||||||
|
/* we have been given the current selection, now insert it into input */
|
||||||
|
XGetWindowProperty(dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
|
||||||
|
utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
|
||||||
|
insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p));
|
||||||
|
XFree(p);
|
||||||
|
drawwin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
run(void) {
|
||||||
|
XEvent ev;
|
||||||
|
while(!XNextEvent(dpy, &ev)) {
|
||||||
|
if(XFilterEvent(&ev, win))
|
||||||
|
continue; /*what is this I don't even*/
|
||||||
|
switch(ev.type) {
|
||||||
|
case Expose:
|
||||||
|
if(ev.xexpose.count == 0)
|
||||||
|
drw_map(drw, win, 0, 0, mw, mh);
|
||||||
|
break;
|
||||||
|
case KeyPress:
|
||||||
|
if (keypress(&ev.xkey)) return;
|
||||||
|
break;
|
||||||
|
case SelectionNotify:
|
||||||
|
if(ev.xselection.property == utf8)
|
||||||
|
paste();
|
||||||
|
break;
|
||||||
|
case VisibilityNotify:
|
||||||
|
if(ev.xvisibility.state != VisibilityUnobscured)
|
||||||
|
XRaiseWindow(dpy, win);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
promptwin(pinentry_t pinentry) {
|
||||||
|
if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
|
||||||
|
fputs("warning: no locale support\n", stderr);
|
||||||
|
if(!(dpy = XOpenDisplay(pinentry->display))) /*NULL was here*/
|
||||||
|
die("dmenu: cannot open display\n");
|
||||||
|
screen = DefaultScreen(dpy);
|
||||||
|
root = RootWindow(dpy, screen);
|
||||||
|
sw = DisplayWidth(dpy, screen);
|
||||||
|
sh = DisplayHeight(dpy, screen);
|
||||||
|
drw = drw_create(dpy, screen, root, sw, sh);
|
||||||
|
drw_load_fonts(drw, fonts, LENGTH(fonts));
|
||||||
|
if(!drw->fontcount)
|
||||||
|
die("No fonts could be loaded.\n");
|
||||||
|
drw_setscheme(drw, &scheme[SchemeNorm]);
|
||||||
|
grabkeyboard();
|
||||||
|
setup();
|
||||||
|
run();
|
||||||
|
cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
catchsig(int sig)
|
||||||
|
{
|
||||||
|
if (sig == SIGALRM)
|
||||||
|
timed_out = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
password (pinentry_t pinentry) {
|
||||||
|
promptwin(pinentry);
|
||||||
|
char *buf = secmem_malloc(strlen(text));
|
||||||
|
strcpy(buf, text);
|
||||||
|
pinentry_setbuffer_use (pinentry, buf, 0);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
confirm(pinentry_t pinentry) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
spinecmdhandler (pinentry_t pinentry) {
|
||||||
|
if (pinentry->timeout){
|
||||||
|
struct sigaction sa;
|
||||||
|
|
||||||
|
memset(&sa, 0, sizeof(sa));
|
||||||
|
sa.sa_handler = catchsig;
|
||||||
|
sigaction(SIGALRM, &sa, NULL);
|
||||||
|
alarm(pinentry->timeout);
|
||||||
|
}
|
||||||
|
if (pinentry->pin)
|
||||||
|
return password(pinentry);
|
||||||
|
else
|
||||||
|
return confirm(pinentry);
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pinentry_cmd_handler_t pinentry_cmd_handler = spinecmdhandler;
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[]){
|
||||||
|
pinentry_init("spine");
|
||||||
|
pinentry_parse_opts(argc, argv);
|
||||||
|
if (pinentry_loop())
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
/* See LICENSE file for copyright and license details. */
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
die(const char *errstr, ...) {
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start(ap, errstr);
|
||||||
|
vfprintf(stderr, errstr, ap);
|
||||||
|
va_end(ap);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
|||||||
|
/* See LICENSE file for copyright and license details. */
|
||||||
|
|
||||||
|
#define MAX(A, B) ((A) > (B) ? (A) : (B))
|
||||||
|
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
||||||
|
#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
|
||||||
|
|
||||||
|
void die(const char *errstr, ...);
|
Loading…
Reference in new issue