Annotation of www/porting.html, Revision 1.14
1.1 marc 1: <html>
2: <head>
3: <meta http-equiv="Content-Type"
4: content="text/html; charset=iso-8859-1">
5: <meta name="resource-type"
6: content="document">
7: <meta name="description"
8: CONTENT="How to make an OpenBSD port">
9: <meta name="keywords"
10: content="openbsd,ports">
11: <meta name="distribution"
12: content="global">
13: <meta name="copyright"
14: content="This document copyright 1997 by the OpenBSD project">
15: <title>Building an OpenBSD port</title>
16: <link rev="made" HREF="mailto:www@openbsd.org">
17: </head>
18: <body text="#000000" bgcolor="#FFFFFF" link="#23238E">
1.6 pauls 19: <img height=30 width=141 src=images/smalltitle.gif alt="[OpenBSD]" >
1.1 marc 20:
1.3 marc 21: <h2><font color=#e00000>Building an OpenBSD port</font></h2>
1.1 marc 22:
23: So you've just compiled your favorite software package on your
24: OpenBSD machine and you want to share your effort by turning
25: it into a standard port. What to do?
26: <p>
1.9 marc 27: First look at the porting information on this page. Then check
28: out the referenced documents, especially the OpenBSD porting
1.14 ! espie 29: <a href="checklist.html">checklist</a>.
1.1 marc 30: <p>
1.9 marc 31: Test, then re-test, and finally test again!
32: <p>
33: Submit the port. Create a gzipped tarball of the port directory.
34: You can then either place it on a public FTP or HTTP server, sending
35: its address to <a href=mailto:ports@openbsd.org>ports@openbsd.org</a>
36: or send the port mime encoded to the same address. Pick whichever
37: method works best for you.
1.1 marc 38: <p>
39: <h3><font color=#0000e0>Available Porting Information</font></h3>
40: <ul>
41: <li>The file <code>/usr/share/mk/bsd.port.mk</code>. This is the
42: system ports makefile included at the end of each individual
43: port makefile. Read the comments at the start of the makefile.
44: They do a good job of describing the available make options.
45: <li>The
1.13 art 46: <a href="http://www.netbsd.org/Documentation/software/packages.html">
47: NetBSD Package System</a> documentation. This document describes
48: NetBSD's version of the FreeBSD ports system and is quite helpful.
1.8 marc 49: <li>Section 19.2.5 of the
1.1 marc 50: <a href="http://www.freebsd.org/handbook/porting.html">FreeBSD
51: Handbook</a>. This is the FreeBSD porting bible.
1.9 marc 52: <li>OpenBSD porting <a href="checklist.html">checklist</a>.
1.14 ! espie 53: <li><a href="audio-port.html">Porting audio applications to OpenBSD</a>.
1.1 marc 54: <li>The OpenBSD ports mailing list,
55: <a href="mailto:ports@openbsd.org">ports@OpenBSD.ORG</a>.
56: </ul>
57: <h3><font color=#0000e0>OpenBSD Porting Policy</font></h3>
58: <ul>
59: <li>OpenBSD does NOT use /usr/local/etc/rc.d.<br>
1.7 espie 60: <code>/usr/local</code> is often shared between several machines
1.8 marc 61: thanks to NFS. For this reason, configuration files that are specific
62: to a given machine can't be stored under <code>/usr/local</code>,
63: <code>/etc</code> is the central repository for per machine
64: configuration files. Moreover, OpenBSD policy is to never update
65: files under <code>/etc</code> automatically. Ports that need some
66: specific boot setup should advise the administrator about what to do
67: instead of blindly installing files.
1.1 marc 68: <li>OpenBSD does NOT compress man pages.
69: <li>OpenBSD does NOT require <code>-lcrypt</code>.<br>
70: DES encryption is part of the standard <code>libc</code>.
1.10 espie 71: <li>OpenBSD is strongly security-oriented. You should read and understand
72: this page's <a href="#security">security section</a>.
73: <li>Be sure to add the <code>$</code><code>OpenBSD$</code> CVS tag to
74: the Makefile. If importing a port from another system be sure to
75: leave their tag in the Makefile, too. However, replace the FreeBSD
76: <code>$</code><code>Id$</code> tag with the
77: <code>$</code><code>FreeBSD$</code> tag.
78: <li>The goal is to get all ported applications to support OpenBSD. To
79: achieve this goal you <strong>must</strong> feed any OpenBSD patches
80: back to the application maintainer.
81: </ul>
82: <a name=security>
83: <h3><font color=#0000e0>Security recommendations</font></h3>
84: There are many security problems to worry about. If
1.2 marc 85: you are not absolutely sure of what you are doing please request
1.1 marc 86: help from the <a href="mailto:ports@openbsd.org">ports</a> mailing
87: list.
1.10 espie 88:
89: <ul>
90: <li>Do <emph>not</emph> use alpha or beta code when preparing a port. Use the
91: latest regular or patch release.
92:
1.1 marc 93: <li>Any software to be installed as a server should be scanned
94: for buffer overflows, especially unsafe use of
95: <code>strcat/strcpy/strcmp/sprintf</code>. In general,
96: <code>sprintf</code> should be replaced with <code>snprintf</code>.
1.10 espie 97:
98: <li>Never use filenames when you need security. There are numerous race
99: conditions where you don't have proper control. For instance, an attacker
100: who already has user privileges on your machines may replace files in
101: <code>/tmp</code> with symbolic links to more strategic files, such as
102: <code>/etc/passwd</code>. For instance, the bsd linker warns about
103: <code>mktemp</code> uses. <strong>These must be fixed</strong>.
104: This is not quite as simple as <code>s/mktemp/mkstemp/g</code>.
105:
106: <li>Just because you can read it doesn't mean you should. A well-known hole
107: of this nature was the <code>startx</code> problem. As a setuid program,
108: you could launch startx with any file as a script. If the file was not
109: a valid shell script, a syntax error message would follow, along with the
110: first line of the offending file, without any further permission check.
111: Pretty handy to grab the first line of a shadow passwd file, considering
1.12 deraadt 112: these often start with root entry. Do not open your file, and then do
113: an <code>fstat</code> on the open descriptor to check if you should have
114: been able to open it (or the attacked will play with /dev/rst0 and rewind
115: your tape) -- open it with the correct uid/gid/grouplist set.
1.10 espie 116:
117: <li>Don't use anything that forks a shell in setuid programs before dropping
118: your privileges. This includes <code>popen</code> and <code>system</code>.
119: Use <code>fork</code>, <code>pipe</code> and <code>execve</code> instead.
120:
121: <li>Pass open descriptors instead of filenames to other programs to avoid race
122: conditions. Even if a program does not accept file descriptors, you can
123: still use <code>/dev/fd/0</code>.
124:
125: <li>Access rights are attached to file descriptors. If you need setuid rights
126: to open a file, open that file, then drop your privileges. You can still
127: access the open descriptor, but you have less to worry about. This is
128: double-edged: even after dropping privileges, you should still watch out
129: for those descriptors.
130:
131: <li>Avoid root setuid as much as you can. Basically, root can do anything,
132: but root rights are very rarely needed, except maybe to create socket ports with
133: a number under 1024. It is arguably better to keep that under <code>inetd</code>
134: control and just add the relevant entries to <code>inetd.conf</code>.
135: You must know the appropriate magic for writing daemons to achieve that.
136: It could be argued that you have no business writing setuid programs if you don't
137: know how to do that.
138:
139: <li>Use setgid instead of setuid. Apart from those specific files needed by setgid
140: programs, most files are not group-writable. Hence, a security problem in a setgid
141: program won't compromise your system as much: with only group rights, the worst
142: an intruder will be able to do is hack a games score table or some such.
143: See the <code>xkobo</code> port for an instance of such a change.
144:
145: <li>Don't trust group-writable files. Even though they are audited, setgid programs
146: are not perceived as important potential security holes. Hence stuff they can tamper
147: with shouldn't be considered sensitive information.
148:
149: <li>Don't trust your environment ! This involves simple things such as your <code>PATH</code>
150: (never use <code>system</code> with an unqualified name), but also more subtle items
151: such as your locale, timezone, termcap, and so on. Be aware of transitivity: even though you're
152: taking full precautions, programs you call directly won't necessarily. <strong>Never</strong>
153: use <code>system</code> in privileged programs, build your command line, a controlled environment,
154: and call <code>execvp</code> directly. The <code>perlsec</code> man page is a good tutorial on
155: such problems.
156:
157: <li>Never used setuid shell-scripts. These are inherently insecure. Wrap them into some C code
158: that ensures a proper environment. On the other hand, OpenBSD features secure perl scripts.
159:
160: <li>Beware the dynamic loader. If you are running setuid, it will only use trusted libraries
161: that were scanned with ldconfig. Setgid is not enough.
162:
163: <li>Dynamic libraries are tricky. C++ code sets a similar problem. Basically, libraries may take
164: some action based upon your environment before your main program even gets to check its setuid
165: status. OpenBSD <code>issetugid</code> addresses this problem, from the library writer point
166: of view. Don't try to port libraries unless you understand this issue thoroughly.
167: </ul>
168: <h3><font color=#0000e0>Generic porting hints</font></h3>
169: <ul>
170: <li><code>__OpenBSD__</code> should be used sparingly, if at all.
171: Constructs that look like
172: <pre>
173: #if defined(__NetBSD__) || defined(__FreeBSD__)
174: </pre>
175: are often inappropriate. Don't add blindly <code>__OpenBSD__</code>
176: to it. Instead, try to figure out what's going on, and what actual
177: feature is needed. Manual pages are often useful, as they include
178: historic comments, stating when a particular feature was incorporated
179: into BSD. Checking the numeric value of <code>BSD</code> against known
180: releases is often the right way. See
181: <a href="http://www.netbsd.org/Documentation/netbsd/Packages.txt">the NetBSD package guide</a>
182: for more information.
183: <li>Defining <code>BSD</code> is a bad idea. Try to include <code>sys/param.h</code>.
184: This not only defines <code>BSD</code>, it also gives it a proper value.
185: The right code fragment should look like:
186: <pre>
187: #if (defined(__unix__) || defined(unix)) && !defined(USG)
188: #include <sys/param.h>
189: #endif
190: </pre>
191: <li>Test for features, not for specific OSes. In the long run, it is much
192: better to test whether <code>tcgetattr</code> works than whether you're running
193: under BSD 4.3 or later, or SystemVR4. These kind of tests just confuse the
194: issue. The way to go about it is, for instance, to test for one particular
195: system, set up a slew of <code>HAVE_TCGETATTR</code> defines, then proceed to
196: the next system. This technique separates features tests from specific OSes.
197: In a hurry, another porter can just add the whole set of <code>-DHAVE_XXX</code>
198: defines to the Makefile. One may also write or add to a configure script to check for
199: that feature and add it automatically. As an example not to follow, check nethack 3.2.2
200: source: it assumes loads of things based on the system type. Most of these assumptions
201: are obsolete and no longer reflect reality: POSIX functions are more useful than older
202: BSD versus SystemV differences, to the point that some traditional bsd functions are
203: now only supported through compatibility libraries.
204:
205: <li>Avoid include files that include other includes that... First, because
206: this is inefficient. Your project will end up including a file that includes
207: everything. Also, because it makes some problems difficult to correct. It
208: becomes harder to <em>not</em> include one particular file at a given point.
209:
210: <li>Avoid bizarre macro tricks. Undefining a macro that was defined by a
211: header file is a bad idea. Defining macros to get some specific behavior
212: from an include file is also a bad idea: compilation modes should be global.
213: If you want POSIX behavior, say so, and <code>#define POSIX_C_SOURCE</code>
214: throughout the whole project, not when you feel like it.
215:
216: <li>Don't ever write system function prototypes. All modern systems,
217: OpenBSD included, have a standard location for these prototypes. Likely
218: places include <code>unistd.h</code>, <code>fcntl.h</code> or <code>termios.h</code>.
219: The man page frequently states where the prototype lies. You might need
220: another slew of <code>HAVE_XXX</code> macros to procure the right file.
221: Don't worry about including the same file twice, include files have guards
222: that prevent all kinds of nastiness.<br>
223: If some broken system needs you to write the prototype, don't impose
224: on all other systems. Roll-your-own prototypes will break because they may
225: use types that are equivalent on your system, but are not portable to other
226: systems (<code>unsigned long</code> instead of <code>size_t</code>), or get some
227: <code>const</code> status wrong. Also, some compilers, such as OpenBSD's own gcc,
228: are able to do a better job with some very frequent functions such as
229: <code>strlen</code> if you include the right header file.
230:
231: <li>Don't use the name of a standard system function for anything else.
232: If you want to write your own function, give it its own name, and call that
233: function everywhere. If you wish to revert to the default system function,
234: you just need to comment your code out, and define your own name to the
235: system function. Don't do it the other way round. Code should look like this
236: <pre>
237: /* prototype part */
238: #ifdef USE_OWN_GCVT
239: char *foo_gcvt(double number, size_t ndigit, char *buf);
240: #else
241: /* include correct file */
242: #include <stdlib.h>
243: /* use system function */
244: #define foo_gcvt gcvt
245: #endif
246:
247: /* definition part */
248: #ifdef USE_OWN_GCVT
249: char *foo_gcvt(double number, size_t ndigit, char *buf)
250: {
251: /* proper definition */
252: }
253:
254: /* typical use */
255: s = foo_gcvt(n, 15, b);
256: </pre>
1.1 marc 257: </ul>
258: <h3><font color=#0000e0>Other Helpful Hints</font></h3>
259: <ul>
260: <li>Do not assume <code>/usr/local/bin</code> or
261: <code>/usr/X11R6/bin</code> is in the installers path. A good
262: way to verify this is to create and install your port while
263: running as <code>root</code> with only <code>/bin</code> and
264: <code>/usr/bin</code> in your path.
265: <li>Do NOT generate shared libraries for <code>${MACHINE_ARCH} ==
266: alpha</code>
267: <li>In OpenBSD <code>curses.h/libcurses/libtermlib</code> are the
268: ``new curses''. Change:<br>
269: <code>ncurses.h ==> curses.h</code><br>
270: <code>-lncurses ==> -lcurses</code><br>
1.11 millert 271: ``old (BSD) curses'' is available by defining <code>_USE_OLD_CURSES_</code>
272: before including <code>curses.h</code> (usually in a Makefile) and
273: linking with <code>-lcurses</code>.
274: <li>In OpenBSD, terminal discipline has been upgraded from the older BSD
275: <code>sgtty</code> to the newer POSIX <code>tcgetattr</code> family.
1.10 espie 276: Avoid the older style in new code. Some code may define <code>tcgetattr</code>
1.11 millert 277: to be a synonym for the older <code>sgtty</code>, but this is at best a stopgap
1.10 espie 278: measure on OpenBSD. The <code>xterm</code> source code is a very good example of
279: what not to do.
280: Try to get your system functionality right: you want a type that remembers
281: the state of your terminal (possible typedef), you want a function that
282: extracts the current state, and a function that sets the new state.
283: Functions that modify this state are more difficult, as they tend to vary
284: depending upon the system. Also, don't forget that you will have to handle
285: cases where you are not connected to the terminal, and that you need to
286: handle signals: not only termination, but also getting put in the background
287: you should leave your terminal in a sane state. Do your tests under an
288: older shell, such as sh, which does not reset the terminal in any way at
289: program's termination.
1.11 millert 290: <li>The newer termcap/terminfo and curses, as included with OpenBSD, include standard sequences
1.10 espie 291: for controlling color terminals. You should use these if possible, reverting
292: to standard ANSI color sequences on other systems. However, some terminal descriptions
293: have not been updated yet, and you may need to be able to specify these sequences manually.
294: This is the way vim5.1 handles it. This is not strictly necessary. Except for privileged
295: programs, it is generally possible to override a termcap definition through the
296: <code>TERMCAP</code> variable and get it to work properly.
297: <li>Signal semantics are tricky, and vary from one system to another. Use <code>sigaction</code>
298: to ensure a specific semantics, along with other system calls referenced in its manpage.
1.1 marc 299: </ul>
300: <hr>
1.6 pauls 301: <a href="index.html"><img height=24 width=24 src=back.gif border=0 alt=OpenBSD></a>
1.1 marc 302: <a href=mailto:www@openbsd.org>www@openbsd.org</a>
1.14 ! espie 303: <br><small>$OpenBSD: porting.html,v 1.13 1998/11/08 16:42:26 art Exp $</small>
1.1 marc 304: </body>
305: </html>